The conditional definition of the "np" variable didn't make any sense. It was
[mmh] / zotnet / mts / client.c
1
2 /*
3  * client.c -- connect to a server
4  *
5  * $Id$
6  */
7
8 #include <h/mh.h>
9 #include <mts.h>
10 #include <errno.h>
11 #include <sys/socket.h>
12 #include <netinet/in.h>
13 #include <netdb.h>
14
15 #ifdef HAVE_ARPA_INET_H
16 # include <arpa/inet.h>
17 #endif
18
19 #ifdef HESIOD
20 # include <hesiod.h>
21 #endif
22
23 #ifdef KPOP
24 # include <krb.h>
25 # include <ctype.h>
26 #endif  /* KPOP */
27
28 #define TRUE         1
29 #define FALSE        0
30
31 #define OOPS1      (-2)
32 #define OOPS2      (-3)
33
34 #define MAXARGS   1000
35 #define MAXNETS      5
36 #define MAXHOSTS    25
37
38 struct addrent {
39     int a_addrtype;             /* assumes AF_INET for inet_netof () */
40     union {
41         int un_net;
42         char un_addr[14];
43     } un;
44 };
45
46 #define a_net  un.un_net
47 #define a_addr un.un_addr
48
49 static struct addrent *n1, *n2;
50 static struct addrent nets[MAXNETS];
51 static struct addrent *h1, *h2;
52 static struct addrent hosts[MAXHOSTS];
53
54 #ifdef KPOP
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();
62 #endif /* KPOP */
63
64 #if !defined(h_addr)
65 # define h_addr h_addr_list[0]
66 #endif
67
68 #define inaddr_copy(hp,sin) \
69     memcpy(&((sin)->sin_addr), (hp)->h_addr, (hp)->h_length)
70
71 /*
72  * static prototypes
73  */
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);
78
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 *);
84
85
86 int
87 client (char *args, char *protocol, char *service, int rproto,
88                 char *response, int len_response)
89 {
90     int sd;
91     register char **ap;
92     char *arguments[MAXARGS];
93     register struct hostent *hp;
94     register struct servent *sp;
95 #ifndef HAVE_GETHOSTBYNAME
96     register struct netent *np;
97 #endif
98
99 #ifdef KPOP
100     char *cp;
101
102     kservice = service;
103     if (cp = strchr (service, '/')) {   /* "pop/kpop" */
104         *cp++ = '\0';           /* kservice = "pop" */
105         service = cp;           /* service  = "kpop" */
106     } else {
107         kservice = NULL;        /* not using KERBEROS */
108     }
109 #endif  /* KPOP */
110     
111
112     if ((sp = getservbyname (service, protocol)) == NULL) {
113 #ifdef HESIOD
114         if ((sp = hes_getservbyname (service, protocol)) == NULL) {
115             snprintf (response, len_response, "%s/%s: unknown service", protocol, service);
116             return NOTOK;
117         }
118 #else
119         snprintf (response, len_response, "%s/%s: unknown service", protocol, service);
120         return NOTOK;
121 #endif
122     }
123
124     ap = arguments;
125     if (args != NULL && *args != 0) {
126         ap = client_copyip (client_brkstring (client_getcpy (args), " ", "\n"),
127                 ap, MAXARGS);
128     } else {
129         if (servers != NULL && *servers != 0)
130             ap = client_copyip (client_brkstring (client_getcpy (servers), " ", "\n"),
131                 ap, MAXARGS);
132     }
133     if (ap == arguments) {
134         *ap++ = client_getcpy ("localhost");
135         *ap = NULL;
136     }
137
138     n1 = nets;
139     n2 = nets + sizeof(nets) / sizeof(nets[0]);
140
141     h1 = hosts;
142     h2 = hosts + sizeof(hosts) / sizeof(hosts[0]);
143
144     for (ap = arguments; *ap; ap++) {
145         if (**ap == '\01') {
146 /*
147  * the assumption here is that if the system doesn't have a
148  * gethostbyname() function, it must not use DNS. So we need to look
149  * into the /etc/hosts using gethostent(). There probablly aren't any
150  * systems still like this, but you never know. On every system I have
151  * access to, this section is ignored.
152  */
153 #ifndef HAVE_GETHOSTBYNAME
154             if ((np = getnetbyname (*ap + 1))) {
155 #ifdef HAVE_SETHOSTENT
156                 sethostent (1);
157 #endif /* HAVE_SETHOSTENT */
158                 while ((hp = gethostent()))
159                     if (np->n_addrtype == hp->h_addrtype
160                             && inet (hp, np->n_net)) {
161                         switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
162                             case NOTOK: 
163                                 continue;
164                             case OOPS1: 
165                                 break;
166                             case OOPS2: 
167                                 return NOTOK;
168
169                             default: 
170                                 return sd;
171                         }
172                         break;
173                     }
174             }
175 #endif /* don't HAVE_GETHOSTBYNAME */
176             continue;
177         }
178
179         if ((hp = gethostbystring (*ap))) {
180             switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
181                 case NOTOK: 
182                 case OOPS1: 
183                     break;
184                 case OOPS2: 
185                     return NOTOK;
186
187                 default: 
188                     return sd;
189             }
190             continue;
191         }
192     }
193
194     strncpy (response, "no servers available", len_response);
195     return NOTOK;
196 }
197
198
199 static int
200 rcaux (struct servent *sp, struct hostent *hp, int rproto,
201                 char *response, int len_response)
202 {
203     int sd;
204     struct in_addr in;
205     register struct addrent *ap;
206     struct sockaddr_in in_socket;
207     register struct sockaddr_in *isock = &in_socket;
208
209 #ifdef KPOP
210     int rem;
211 #endif  /* KPOP */
212
213     for (ap = nets; ap < n1; ap++)
214         if (ap->a_addrtype == hp->h_addrtype && inet (hp, ap->a_net))
215             return NOTOK;
216
217     for (ap = hosts; ap < h1; ap++)
218         if (ap->a_addrtype == hp->h_addrtype
219                 && memcmp(ap->a_addr, hp->h_addr, hp->h_length) == 0)
220             return NOTOK;
221
222     if ((sd = getport (rproto, hp->h_addrtype, response, len_response)) == NOTOK)
223         return OOPS2;
224
225     memset (isock, 0, sizeof(*isock));
226     isock->sin_family = hp->h_addrtype;
227     inaddr_copy (hp, isock);
228     isock->sin_port = sp->s_port;
229
230     if (connect (sd, (struct sockaddr *) isock, sizeof(*isock)) == NOTOK)
231         switch (errno) {
232             case ENETDOWN: 
233             case ENETUNREACH: 
234                 close (sd);
235                 if (n1 < n2) {
236                     n1->a_addrtype = hp->h_addrtype;
237                     memcpy(&in, hp->h_addr, sizeof(in));
238                     n1->a_net = inet_netof (in);
239                     n1++;
240                 }
241                 return OOPS1;
242
243             case ETIMEDOUT: 
244             case ECONNREFUSED: 
245             default: 
246                 close (sd);
247                 if (h1 < h2) {
248                     h1->a_addrtype = hp->h_addrtype;
249                     memcpy(h1->a_addr, hp->h_addr, hp->h_length);
250                     h1++;
251                 }
252                 return NOTOK;
253         }
254
255 #ifdef KPOP
256     if (kservice) {     /* "pop" */
257         char *instance;
258
259         if ((instance = strdup (hp->h_name)) == NULL) {
260             close (sd);
261             strncpy (response, "Out of memory.", len_response);
262             return OOPS2;
263         }
264         ticket = (KTEXT) malloc (sizeof(KTEXT_ST));
265         rem = krb_sendauth (0L, sd, ticket, kservice, instance,
266                            (char *) krb_realmofhost (instance),
267                            (unsigned long) 0, &msg_data, &cred, schedule,
268                            (struct sockaddr_in *) NULL,
269                            (struct sockaddr_in *) NULL,
270                            "KPOPV0.1");
271         free (instance);
272         if (rem != KSUCCESS) {
273             close (sd);
274             strncpy (response, "Post office refused connection: ", len_response);
275             strncat (response, krb_err_txt[rem], len_response - strlen(response));
276             return OOPS2;
277         }
278     }
279 #endif /* KPOP */
280
281     return sd;
282 }
283
284
285 static int
286 getport (int rproto, int addrtype, char *response, int len_response)
287 {
288     int sd, port;
289     struct sockaddr_in in_socket, *isock;
290
291     isock = &in_socket;
292     if (rproto && addrtype != AF_INET) {
293         snprintf (response, len_response, "reserved ports not supported for af=%d", addrtype);
294         errno = ENOPROTOOPT;
295         return NOTOK;
296     }
297
298     if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
299         char *s;
300
301         if ((s = strerror (errno)))
302             snprintf (response, len_response, "unable to create socket: %s", s);
303         else
304             snprintf (response, len_response, "unable to create socket: unknown error");
305         return NOTOK;
306     }
307 #ifdef KPOP
308     if (kservice)       /* "pop" */
309         return(sd);
310 #endif  /* KPOP */
311     if (!rproto)
312         return sd;
313
314     memset(isock, 0, sizeof(*isock));
315     isock->sin_family = addrtype;
316     for (port = IPPORT_RESERVED - 1;;) {
317         isock->sin_port = htons ((unsigned short) port);
318         if (bind (sd, (struct sockaddr *) isock, sizeof(*isock)) != NOTOK)
319             return sd;
320
321         switch (errno) {
322             char *s;
323
324             case EADDRINUSE: 
325             case EADDRNOTAVAIL: 
326                 if (--port <= IPPORT_RESERVED / 2) {
327                     strncpy (response, "ports available", len_response);
328                     return NOTOK;
329                 }
330                 break;
331
332             default: 
333                 if ((s = strerror (errno)))
334                     snprintf (response, len_response, "unable to bind socket: %s", s);
335                 else
336                     snprintf (response, len_response, "unable to bind socket: unknown error");
337                 return NOTOK;
338         }
339     }
340 }
341
342
343 static int
344 inet (struct hostent *hp, int net)
345 {
346     struct in_addr in;
347
348     memcpy(&in, hp->h_addr, sizeof(in));
349     return (inet_netof (in) == net);
350 }
351
352
353 /*
354  * taken from ISODE's compat/internet.c
355  */
356
357 static char *empty = NULL;
358
359 #ifdef h_addr
360 static char *addrs[2] = { NULL };
361 #endif
362
363 struct hostent *
364 gethostbystring (char *s)
365 {
366     register struct hostent *h;
367     static struct hostent hs;
368 #ifdef DG
369     static struct in_addr iaddr;
370 #else
371     static unsigned long iaddr;
372 #endif
373
374     iaddr = inet_addr (s);
375 #ifdef DG
376     if (iaddr.s_addr == NOTOK && strcmp (s, "255.255.255.255"))
377 #else
378     if (((int) iaddr == NOTOK) && strcmp (s, "255.255.255.255"))
379 #endif
380         return gethostbyname (s);
381
382     h = &hs;
383     h->h_name = s;
384     h->h_aliases = &empty;
385     h->h_addrtype = AF_INET;
386     h->h_length = sizeof(iaddr);
387 #ifdef h_addr
388     h->h_addr_list = addrs;
389     memset(addrs, 0, sizeof(addrs));
390 #endif
391     h->h_addr = (char *) &iaddr;
392
393     return h;
394 }
395
396
397 /*
398  * static copies of three nmh subroutines
399  */
400
401 static char *broken[MAXARGS + 1];
402
403 static char **
404 client_brkstring (char *strg, char *brksep, char *brkterm)
405 {
406     register int bi;
407     register char c, *sp;
408
409     sp = strg;
410
411     for (bi = 0; bi < MAXARGS; bi++) {
412         while (client_brkany (c = *sp, brksep))
413             *sp++ = 0;
414         if (!c || client_brkany (c, brkterm)) {
415             *sp = 0;
416             broken[bi] = 0;
417             return broken;
418         }
419
420         broken[bi] = sp;
421         while ((c = *++sp) && !client_brkany (c, brksep) && !client_brkany (c, brkterm))
422             continue;
423     }
424     broken[MAXARGS] = 0;
425
426     return broken;
427 }
428
429
430 /*
431  * returns 1 if chr in strg, 0 otherwise
432  */
433 static int
434 client_brkany (char chr, char *strg)
435 {
436     register char *sp;
437  
438     if (strg)
439         for (sp = strg; *sp; sp++)
440             if (chr == *sp)
441                 return 1;
442     return 0;
443 }
444
445
446 /*
447  * copy a string array and return pointer to end
448  */
449 static char **
450 client_copyip (char **p, char **q, int len_q)
451 {
452     while (*p && --len_q > 0)
453         *q++ = *p++;
454
455     *q = NULL;
456
457     return q;
458 }
459
460
461 static char *
462 client_getcpy (char *str)
463 {
464     char *cp;
465     size_t len;
466
467     len = strlen(str) + 1;
468     if (!(cp = malloc(len)))
469         return NULL;
470
471     memcpy (cp, str, len);
472     return cp;
473 }
474