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