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