]> diplodocus.org Git - nmh/blob - zotnet/mts/client.c
Added notes about the configure change.
[nmh] / 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(BIND) && !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 BIND
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 #ifndef BIND
147 if ((np = getnetbyname (*ap + 1))) {
148 sethostent (1);
149 while ((hp = gethostent()))
150 if (np->n_addrtype == hp->h_addrtype
151 && inet (hp, np->n_net)) {
152 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
153 case NOTOK:
154 continue;
155 case OOPS1:
156 break;
157 case OOPS2:
158 return NOTOK;
159
160 default:
161 return sd;
162 }
163 break;
164 }
165 }
166 #endif
167 continue;
168 }
169
170 if ((hp = gethostbystring (*ap))) {
171 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
172 case NOTOK:
173 case OOPS1:
174 break;
175 case OOPS2:
176 return NOTOK;
177
178 default:
179 return sd;
180 }
181 continue;
182 }
183 }
184
185 strncpy (response, "no servers available", len_response);
186 return NOTOK;
187 }
188
189
190 static int
191 rcaux (struct servent *sp, struct hostent *hp, int rproto,
192 char *response, int len_response)
193 {
194 int sd;
195 struct in_addr in;
196 register struct addrent *ap;
197 struct sockaddr_in in_socket;
198 register struct sockaddr_in *isock = &in_socket;
199
200 #ifdef KPOP
201 int rem;
202 #endif /* KPOP */
203
204 for (ap = nets; ap < n1; ap++)
205 if (ap->a_addrtype == hp->h_addrtype && inet (hp, ap->a_net))
206 return NOTOK;
207
208 for (ap = hosts; ap < h1; ap++)
209 if (ap->a_addrtype == hp->h_addrtype
210 && memcmp(ap->a_addr, hp->h_addr, hp->h_length) == 0)
211 return NOTOK;
212
213 if ((sd = getport (rproto, hp->h_addrtype, response, len_response)) == NOTOK)
214 return OOPS2;
215
216 memset (isock, 0, sizeof(*isock));
217 isock->sin_family = hp->h_addrtype;
218 inaddr_copy (hp, isock);
219 isock->sin_port = sp->s_port;
220
221 if (connect (sd, (struct sockaddr *) isock, sizeof(*isock)) == NOTOK)
222 switch (errno) {
223 case ENETDOWN:
224 case ENETUNREACH:
225 close (sd);
226 if (n1 < n2) {
227 n1->a_addrtype = hp->h_addrtype;
228 memcpy(&in, hp->h_addr, sizeof(in));
229 n1->a_net = inet_netof (in);
230 n1++;
231 }
232 return OOPS1;
233
234 case ETIMEDOUT:
235 case ECONNREFUSED:
236 default:
237 close (sd);
238 if (h1 < h2) {
239 h1->a_addrtype = hp->h_addrtype;
240 memcpy(h1->a_addr, hp->h_addr, hp->h_length);
241 h1++;
242 }
243 return NOTOK;
244 }
245
246 #ifdef KPOP
247 if (kservice) { /* "pop" */
248 char *instance;
249
250 if ((instance = strdup (hp->h_name)) == NULL) {
251 close (sd);
252 strncpy (response, "Out of memory.", len_response);
253 return OOPS2;
254 }
255 ticket = (KTEXT) malloc (sizeof(KTEXT_ST));
256 rem = krb_sendauth (0L, sd, ticket, kservice, instance,
257 (char *) krb_realmofhost (instance),
258 (unsigned long) 0, &msg_data, &cred, schedule,
259 (struct sockaddr_in *) NULL,
260 (struct sockaddr_in *) NULL,
261 "KPOPV0.1");
262 free (instance);
263 if (rem != KSUCCESS) {
264 close (sd);
265 strncpy (response, "Post office refused connection: ", len_response);
266 strncat (response, krb_err_txt[rem], len_response - strlen(response));
267 return OOPS2;
268 }
269 }
270 #endif /* KPOP */
271
272 return sd;
273 }
274
275
276 static int
277 getport (int rproto, int addrtype, char *response, int len_response)
278 {
279 int sd, port;
280 struct sockaddr_in in_socket, *isock;
281
282 isock = &in_socket;
283 if (rproto && addrtype != AF_INET) {
284 snprintf (response, len_response, "reserved ports not supported for af=%d", addrtype);
285 errno = ENOPROTOOPT;
286 return NOTOK;
287 }
288
289 if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
290 char *s;
291
292 if ((s = strerror (errno)))
293 snprintf (response, len_response, "unable to create socket: %s", s);
294 else
295 snprintf (response, len_response, "unable to create socket: unknown error");
296 return NOTOK;
297 }
298 #ifdef KPOP
299 if (kservice) /* "pop" */
300 return(sd);
301 #endif /* KPOP */
302 if (!rproto)
303 return sd;
304
305 memset(isock, 0, sizeof(*isock));
306 isock->sin_family = addrtype;
307 for (port = IPPORT_RESERVED - 1;;) {
308 isock->sin_port = htons ((unsigned short) port);
309 if (bind (sd, (struct sockaddr *) isock, sizeof(*isock)) != NOTOK)
310 return sd;
311
312 switch (errno) {
313 char *s;
314
315 case EADDRINUSE:
316 case EADDRNOTAVAIL:
317 if (--port <= IPPORT_RESERVED / 2) {
318 strncpy (response, "ports available", len_response);
319 return NOTOK;
320 }
321 break;
322
323 default:
324 if ((s = strerror (errno)))
325 snprintf (response, len_response, "unable to bind socket: %s", s);
326 else
327 snprintf (response, len_response, "unable to bind socket: unknown error");
328 return NOTOK;
329 }
330 }
331 }
332
333
334 static int
335 inet (struct hostent *hp, int net)
336 {
337 struct in_addr in;
338
339 memcpy(&in, hp->h_addr, sizeof(in));
340 return (inet_netof (in) == net);
341 }
342
343
344 /*
345 * taken from ISODE's compat/internet.c
346 */
347
348 static char *empty = NULL;
349
350 #ifdef h_addr
351 static char *addrs[2] = { NULL };
352 #endif
353
354 struct hostent *
355 gethostbystring (char *s)
356 {
357 register struct hostent *h;
358 static struct hostent hs;
359 #ifdef DG
360 static struct in_addr iaddr;
361 #else
362 static unsigned long iaddr;
363 #endif
364
365 iaddr = inet_addr (s);
366 #ifdef DG
367 if (iaddr.s_addr == NOTOK && strcmp (s, "255.255.255.255"))
368 #else
369 if (((int) iaddr == NOTOK) && strcmp (s, "255.255.255.255"))
370 #endif
371 return gethostbyname (s);
372
373 h = &hs;
374 h->h_name = s;
375 h->h_aliases = &empty;
376 h->h_addrtype = AF_INET;
377 h->h_length = sizeof(iaddr);
378 #ifdef h_addr
379 h->h_addr_list = addrs;
380 memset(addrs, 0, sizeof(addrs));
381 #endif
382 h->h_addr = (char *) &iaddr;
383
384 return h;
385 }
386
387
388 /*
389 * static copies of three nmh subroutines
390 */
391
392 static char *broken[MAXARGS + 1];
393
394 static char **
395 client_brkstring (char *strg, char *brksep, char *brkterm)
396 {
397 register int bi;
398 register char c, *sp;
399
400 sp = strg;
401
402 for (bi = 0; bi < MAXARGS; bi++) {
403 while (client_brkany (c = *sp, brksep))
404 *sp++ = 0;
405 if (!c || client_brkany (c, brkterm)) {
406 *sp = 0;
407 broken[bi] = 0;
408 return broken;
409 }
410
411 broken[bi] = sp;
412 while ((c = *++sp) && !client_brkany (c, brksep) && !client_brkany (c, brkterm))
413 continue;
414 }
415 broken[MAXARGS] = 0;
416
417 return broken;
418 }
419
420
421 /*
422 * returns 1 if chr in strg, 0 otherwise
423 */
424 static int
425 client_brkany (char chr, char *strg)
426 {
427 register char *sp;
428
429 if (strg)
430 for (sp = strg; *sp; sp++)
431 if (chr == *sp)
432 return 1;
433 return 0;
434 }
435
436
437 /*
438 * copy a string array and return pointer to end
439 */
440 static char **
441 client_copyip (char **p, char **q, int len_q)
442 {
443 while (*p && --len_q > 0)
444 *q++ = *p++;
445
446 *q = NULL;
447
448 return q;
449 }
450
451
452 static char *
453 client_getcpy (char *str)
454 {
455 char *cp;
456 size_t len;
457
458 len = strlen(str) + 1;
459 if (!(cp = malloc(len)))
460 return NULL;
461
462 memcpy (cp, str, len);
463 return cp;
464 }
465