head    1.8;
access;
symbols;
locks; strict;
comment @ * @;


1.8
date    95.06.10.04.05.31;      author p-pomes; state Exp;
branches;
next    1.7;

1.7
date    94.03.12.04.27.19;      author paul;    state Exp;
branches;
next    1.6;

1.6
date    94.03.11.23.14.20;      author paul;    state Exp;
branches;
next    1.5;

1.5
date    93.12.16.21.46.45;      author paul;    state Exp;
branches;
next    1.4;

1.4
date    93.11.24.22.33.04;      author paul;    state Exp;
branches;
next    1.3;

1.3
date    93.11.24.18.05.56;      author paul;    state Exp;
branches;
next    1.2;

1.2
date    93.07.22.13.49.22;      author paul;    state Exp;
branches;
next    1.1;

1.1
date    93.06.14.20.37.07;      author paul;    state Exp;
branches;
next    ;


desc
@@


1.8
log
@Changed default service from ns to csnet-ns (RFC-1700).
@
text
@/*
* Copyright (c) 1985 Corporation for Research and Educational Networking
* Copyright (c) 1988 University of Illinois Board of Trustees, Steven
*              Dorner, and Paul Pomes
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
*    notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
*    notice, this list of conditions and the following disclaimer in the
*    documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
*    must display the following acknowledgement:
*      This product includes software developed by the Corporation for
*      Research and Educational Networking (CREN), the University of
*      Illinois at Urbana, and their contributors.
* 4. Neither the name of CREN, the University nor the names of its
*    contributors may be used to endorse or promote products derived from
*    this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE TRUSTEES AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.  IN NO EVENT SHALL THE TRUSTEES OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/

#ifndef lint
static char  RcsId[] = "@@(#)$Id: OpenQi.c,v 1.7 1994/03/12 04:27:19 paul Exp p-pomes $";
#endif

/*
**  OpenQi -- Connect to the Qi server
**
**      Open socket connections to the Qi server.
**
**      Parameters:
**              Host - Name of server to contact
**              To - pointer to stream descriptor
**              From - pointer to stream descriptor
**
**      Returns:
**               0 if successful
**              -1 if any error
**
**      Side Effects:
**              Initializes global QiFields by calling GetFields()
**              Reports error via syslog
*/

#include "qiapi.h"
#include <netdb.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifndef NSSERVICE
# define        NSSERVICE       "csnet-ns"
#endif

#ifdef NO_MEMMOVE
# define        memmove(a,b,c)  bcopy(b,a,c)
#endif

int
OpenQi (Host, To, From)
char    *Host;
FILE    **To, **From;
{
   int sock;                           /* our socket */
   struct sockaddr_in Qi;              /* the address of the nameserver */
   struct servent *Ns;                 /* nameserver service entry */
   struct hostent *hp;                 /* host entry for nameserver */

   if (QiDebug)
       fprintf(stderr, "contacting qi on %s\r\n", Host);

   /* Locate the proper port */
   if (Ns = getservbyname (NSSERVICE, "tcp"))
       Qi.sin_port = Ns->s_port;
   else {
       if (QiDebug)
           fprintf(stderr, "OpenQi: service \"%s\" unknown - using 105\r\n",
             NSSERVICE);
       syslog(LOG_ERR, "OpenQi: service \"%s\" unknown - using 105",
           NSSERVICE);
       Qi.sin_port = htons((unsigned short) 105);
   }
   Qi.sin_family = AF_INET;

   /* Get a socket for the Qi connection */
   if (geteuid() == 0) {       /* if running as root, get a reserved port */
     int localport = IPPORT_RESERVED - 1;
     if ((sock = rresvport(&localport)) < 0) {
       syslog(LOG_ERR, "OpenQI: rresvport(): %m");
       return(-1);
     }
   } else if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
       if (QiDebug)
           fprintf(stderr, "OpenQi: socket(): %s\r\n", sys_errlist[errno]);
       syslog(LOG_ERR, "OpenQi: socket(): %m");
       return(-1);
   }

   /* Locate the proper host */
   if (hp = gethostbyname (Host))
       memmove ((char *) &Qi.sin_addr.s_addr, hp->h_addr, 4);
   else {
       if (QiDebug)
           fprintf(stderr, "OpenQi: gethostbyname(%s): %s\r\n",
             Host, sys_errlist[errno]);
       syslog(LOG_ERR, "OpenQi: gethostbyname(%s): %m", Host);
       return(-1);
   }

   /* Connect to the nameserver */
   if (connect (sock, (struct sockaddr *) &Qi, sizeof (Qi)) < 0) {
       if (QiDebug)
           fprintf(stderr, "OpenQi: connect(%s): %s\r\n",
             Host, sys_errlist[errno]);
       syslog(LOG_ERR, "OpenQi: connect(%s): %m", Host);
       (void) close(sock);
       return (-1);
   }

   /* Create stream descriptors */
   if ((*To = fdopen(sock, "w")) == (FILE *) NULL) {
       syslog(LOG_ERR, "OpenQi: fdopen(s, \"w\"): %m");
       return(-1);
   }
   if ((*From = fdopen(sock, "r")) == (FILE *) NULL) {
       syslog(LOG_ERR, "OpenQi: fdopen(s, \"r\"): %m");
       return(-1);
   }

   /* Initialize QiFields global */
   if (QiFields == QIF_NULL && (QiFields = GetFields(*To, *From)) == QIF_NULL)
       return(-1);

   /* Connection ok */
   return (0);
}
@


1.7
log
@Changed NSSERVICE back to "ns" from "csnet-ns" as I can't get the
latter to decode authenticators properly when used with Kerberos.
@
text
@d38 1
a38 1
static char  RcsId[] = "@@(#)$Id: OpenQi.c,v 1.6 1994/03/11 23:14:20 paul Exp paul $";
d68 1
a68 1
# define        NSSERVICE       "ns"
@


1.6
log
@New copyright statement.
@
text
@d38 1
a38 1
static char  RcsId[] = "@@(#)$Id$";
d68 1
a68 1
# define        NSSERVICE       "csnet-ns"
@


1.5
log
@Changed QISERVICE to NSSERVICE for consistency.
@
text
@d2 40
d68 1
a68 1
# define        NSSERVICE       "ns"
a72 4
#endif

#ifndef lint
static char RcsId[] = "@@(#)$Id: OpenQi.c,v 1.4 1993/11/24 22:33:04 paul Exp paul $";
@


1.4
log
@Use a reserved port if running as root.  From Alan Crosswell
<alan@@curta.cc.columbia.edu>.
@
text
@d27 2
a28 2
#ifndef QISERVICE
# define        QISERVICE       "ns"
d36 1
a36 1
static char RcsId[] = "@@(#)$Id: OpenQi.c,v 1.3 1993/11/24 18:05:56 paul Exp paul $";
d53 1
a53 1
   if (Ns = getservbyname (QISERVICE, "tcp"))
d58 1
a58 1
             QISERVICE);
d60 1
a60 1
           QISERVICE);
@


1.3
log
@*** empty log message ***
@
text
@d36 1
a36 1
static char RcsId[] = "@@(#)$Id: OpenQi.c,v 1.2 1993/07/22 13:49:22 paul Exp paul $";
d66 7
a72 1
   if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
@


1.2
log
@*** empty log message ***
@
text
@d31 4
d36 1
a36 1
static char RcsId[] = "@@(#)$Id: OpenQi.c,v 1.1 1993/06/14 20:37:07 paul Exp paul $";
@


1.1
log
@Initial revision
@
text
@d32 1
a32 1
static char RcsId[] = "@@(#)$Id$";
d71 1
a71 1
       bcopy (hp->h_addr, (char *) &Qi.sin_addr.s_addr, 4);
@