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