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