3 * client.c -- connect to a server
11 #include <sys/socket.h>
12 #include <netinet/in.h>
15 #ifdef HAVE_ARPA_INET_H
16 # include <arpa/inet.h>
39 int a_addrtype; /* assumes AF_INET for inet_netof () */
46 #define a_net un.un_net
47 #define a_addr un.un_addr
49 static struct addrent *n1, *n2;
50 static struct addrent nets[MAXNETS];
51 static struct addrent *h1, *h2;
52 static struct addrent hosts[MAXHOSTS];
55 static CREDENTIALS cred;
56 static MSG_DAT msg_data;
57 static KTEXT ticket = (KTEXT) NULL;
58 static Key_schedule schedule;
59 static char *kservice; /* "pop" if using kpop */
60 char krb_realm[REALM_SZ];
61 char *PrincipalHostname();
65 # define h_addr h_addr_list[0]
68 #define inaddr_copy(hp,sin) \
69 memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
74 static int rcaux (struct servent *, struct hostent *, int, char *, int);
75 static int getport (int, int, char *, int);
76 static int inet (struct hostent *, int);
77 struct hostent *gethostbystring (char *s);
79 /* client's own static version of several nmh subroutines */
80 static char **client_brkstring (char *, char *, char *);
81 static int client_brkany (char, char *);
82 static char **client_copyip (char **, char **, int);
83 static char *client_getcpy (char *);
87 client (char *args, char *protocol, char *service, int rproto,
88 char *response, int len_response)
92 char *arguments[MAXARGS];
93 register struct hostent *hp;
94 register struct servent *sp;
95 /* we assume netent and getnetbyaddr come with gethostbyname */
96 #ifdef HAVE_GETHOSTBYNAME
97 register struct netent *np;
104 if (cp = strchr (service, '/')) { /* "pop/kpop" */
105 *cp++ = '\0'; /* kservice = "pop" */
106 service = cp; /* service = "kpop" */
108 kservice = NULL; /* not using KERBEROS */
113 if ((sp = getservbyname (service, protocol)) == NULL) {
115 if ((sp = hes_getservbyname (service, protocol)) == NULL) {
116 snprintf (response, len_response, "%s/%s: unknown service", protocol, service);
120 snprintf (response, len_response, "%s/%s: unknown service", protocol, service);
126 if (args != NULL && *args != 0) {
127 ap = client_copyip (client_brkstring (client_getcpy (args), " ", "\n"),
130 if (servers != NULL && *servers != 0)
131 ap = client_copyip (client_brkstring (client_getcpy (servers), " ", "\n"),
134 if (ap == arguments) {
135 *ap++ = client_getcpy ("localhost");
140 n2 = nets + sizeof(nets) / sizeof(nets[0]);
143 h2 = hosts + sizeof(hosts) / sizeof(hosts[0]);
145 for (ap = arguments; *ap; ap++) {
148 * the assumption here is that if the system doesn't have a
149 * gethostbyname() function, it must not use DNS. So we need to look
150 * into the /etc/hosts using gethostent(). There probablly aren't any
151 * systems still like this, but you never know. On every system I have
152 * access to, this section is ignored.
154 #ifndef HAVE_GETHOSTBYNAME
155 if ((np = getnetbyname (*ap + 1))) {
156 #ifdef HAVE_SETHOSTENT
158 #endif /* HAVE_SETHOSTENT */
159 while ((hp = gethostent()))
160 if (np->n_addrtype == hp->h_addrtype
161 && inet (hp, np->n_net)) {
162 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
176 #endif /* don't HAVE_GETHOSTBYNAME */
180 if ((hp = gethostbystring (*ap))) {
181 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
195 strncpy (response, "no servers available", len_response);
201 rcaux (struct servent *sp, struct hostent *hp, int rproto,
202 char *response, int len_response)
206 register struct addrent *ap;
207 struct sockaddr_in in_socket;
208 register struct sockaddr_in *isock = &in_socket;
214 for (ap = nets; ap < n1; ap++)
215 if (ap->a_addrtype == hp->h_addrtype && inet (hp, ap->a_net))
218 for (ap = hosts; ap < h1; ap++)
219 if (ap->a_addrtype == hp->h_addrtype
220 && memcmp(ap->a_addr, hp->h_addr, hp->h_length) == 0)
223 if ((sd = getport (rproto, hp->h_addrtype, response, len_response)) == NOTOK)
226 memset (isock, 0, sizeof(*isock));
227 isock->sin_family = hp->h_addrtype;
228 inaddr_copy (hp, isock);
229 isock->sin_port = sp->s_port;
231 if (connect (sd, (struct sockaddr *) isock, sizeof(*isock)) == NOTOK)
237 n1->a_addrtype = hp->h_addrtype;
238 memcpy(&in, hp->h_addr, sizeof(in));
239 n1->a_net = inet_netof (in);
249 h1->a_addrtype = hp->h_addrtype;
250 memcpy(h1->a_addr, hp->h_addr, hp->h_length);
257 if (kservice) { /* "pop" */
260 if ((instance = strdup (hp->h_name)) == NULL) {
262 strncpy (response, "Out of memory.", len_response);
265 ticket = (KTEXT) malloc (sizeof(KTEXT_ST));
266 rem = krb_sendauth (0L, sd, ticket, kservice, instance,
267 (char *) krb_realmofhost (instance),
268 (unsigned long) 0, &msg_data, &cred, schedule,
269 (struct sockaddr_in *) NULL,
270 (struct sockaddr_in *) NULL,
273 if (rem != KSUCCESS) {
275 strncpy (response, "Post office refused connection: ", len_response);
276 strncat (response, krb_err_txt[rem], len_response - strlen(response));
287 getport (int rproto, int addrtype, char *response, int len_response)
290 struct sockaddr_in in_socket, *isock;
293 if (rproto && addrtype != AF_INET) {
294 snprintf (response, len_response, "reserved ports not supported for af=%d", addrtype);
299 if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
302 if ((s = strerror (errno)))
303 snprintf (response, len_response, "unable to create socket: %s", s);
305 snprintf (response, len_response, "unable to create socket: unknown error");
309 if (kservice) /* "pop" */
315 memset(isock, 0, sizeof(*isock));
316 isock->sin_family = addrtype;
317 for (port = IPPORT_RESERVED - 1;;) {
318 isock->sin_port = htons ((unsigned short) port);
319 if (bind (sd, (struct sockaddr *) isock, sizeof(*isock)) != NOTOK)
327 if (--port <= IPPORT_RESERVED / 2) {
328 strncpy (response, "ports available", len_response);
334 if ((s = strerror (errno)))
335 snprintf (response, len_response, "unable to bind socket: %s", s);
337 snprintf (response, len_response, "unable to bind socket: unknown error");
345 inet (struct hostent *hp, int net)
349 memcpy(&in, hp->h_addr, sizeof(in));
350 return (inet_netof (in) == net);
355 * taken from ISODE's compat/internet.c
358 static char *empty = NULL;
361 static char *addrs[2] = { NULL };
365 gethostbystring (char *s)
367 register struct hostent *h;
368 static struct hostent hs;
370 static struct in_addr iaddr;
372 static unsigned long iaddr;
375 iaddr = inet_addr (s);
377 if (iaddr.s_addr == NOTOK && strcmp (s, "255.255.255.255"))
379 if (((int) iaddr == NOTOK) && strcmp (s, "255.255.255.255"))
381 return gethostbyname (s);
385 h->h_aliases = ∅
386 h->h_addrtype = AF_INET;
387 h->h_length = sizeof(iaddr);
389 h->h_addr_list = addrs;
390 memset(addrs, 0, sizeof(addrs));
392 h->h_addr = (char *) &iaddr;
399 * static copies of three nmh subroutines
402 static char *broken[MAXARGS + 1];
405 client_brkstring (char *strg, char *brksep, char *brkterm)
408 register char c, *sp;
412 for (bi = 0; bi < MAXARGS; bi++) {
413 while (client_brkany (c = *sp, brksep))
415 if (!c || client_brkany (c, brkterm)) {
422 while ((c = *++sp) && !client_brkany (c, brksep) && !client_brkany (c, brkterm))
432 * returns 1 if chr in strg, 0 otherwise
435 client_brkany (char chr, char *strg)
440 for (sp = strg; *sp; sp++)
448 * copy a string array and return pointer to end
451 client_copyip (char **p, char **q, int len_q)
453 while (*p && --len_q > 0)
463 client_getcpy (char *str)
468 len = strlen(str) + 1;
469 if (!(cp = malloc(len)))
472 memcpy (cp, str, len);