]> diplodocus.org Git - nmh/blob - zotnet/mts/client.c
Just reworded the bit about '%s' being safe not to quote (it's only safe not to
[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 #ifndef HAVE_GETHOSTBYNAME
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 /*
147 * the assumption here is that if the system doesn't have a
148 * gethostbyname() function, it must not use DNS. So we need to look
149 * into the /etc/hosts using gethostent(). There probablly aren't any
150 * systems still like this, but you never know. On every system I have
151 * access to, this section is ignored.
152 */
153 #ifndef HAVE_GETHOSTBYNAME
154 if ((np = getnetbyname (*ap + 1))) {
155 #ifdef HAVE_SETHOSTENT
156 sethostent (1);
157 #endif /* HAVE_SETHOSTENT */
158 while ((hp = gethostent()))
159 if (np->n_addrtype == hp->h_addrtype
160 && inet (hp, np->n_net)) {
161 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
162 case NOTOK:
163 continue;
164 case OOPS1:
165 break;
166 case OOPS2:
167 return NOTOK;
168
169 default:
170 return sd;
171 }
172 break;
173 }
174 }
175 #endif /* don't HAVE_GETHOSTBYNAME */
176 continue;
177 }
178
179 if ((hp = gethostbystring (*ap))) {
180 switch (sd = rcaux (sp, hp, rproto, response, len_response)) {
181 case NOTOK:
182 case OOPS1:
183 break;
184 case OOPS2:
185 return NOTOK;
186
187 default:
188 return sd;
189 }
190 continue;
191 }
192 }
193
194 strncpy (response, "no servers available", len_response);
195 return NOTOK;
196 }
197
198
199 static int
200 rcaux (struct servent *sp, struct hostent *hp, int rproto,
201 char *response, int len_response)
202 {
203 int sd;
204 struct in_addr in;
205 register struct addrent *ap;
206 struct sockaddr_in in_socket;
207 register struct sockaddr_in *isock = &in_socket;
208
209 #ifdef KPOP
210 int rem;
211 struct hostent *hp2;
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 (( hp2 = gethostbyaddr( hp->h_addr, hp->h_length, hp->h_addrtype ))
261 == NULL ) {
262 return NOTOK;
263 }
264 if ((instance = strdup (hp2->h_name)) == NULL) {
265 close (sd);
266 strncpy (response, "Out of memory.", len_response);
267 return OOPS2;
268 }
269 ticket = (KTEXT) malloc (sizeof(KTEXT_ST));
270 rem = krb_sendauth (0L, sd, ticket, kservice, instance,
271 (char *) krb_realmofhost (instance),
272 (unsigned long) 0, &msg_data, &cred, schedule,
273 (struct sockaddr_in *) NULL,
274 (struct sockaddr_in *) NULL,
275 "KPOPV0.1");
276 free (instance);
277 if (rem != KSUCCESS) {
278 close (sd);
279 strncpy (response, "Post office refused connection: ", len_response);
280 strncat (response, krb_err_txt[rem], len_response - strlen(response));
281 return OOPS2;
282 }
283 }
284 #endif /* KPOP */
285
286 return sd;
287 }
288
289
290 static int
291 getport (int rproto, int addrtype, char *response, int len_response)
292 {
293 int sd, port;
294 struct sockaddr_in in_socket, *isock;
295
296 isock = &in_socket;
297 if (rproto && addrtype != AF_INET) {
298 snprintf (response, len_response, "reserved ports not supported for af=%d", addrtype);
299 errno = ENOPROTOOPT;
300 return NOTOK;
301 }
302
303 if ((sd = socket (AF_INET, SOCK_STREAM, 0)) == NOTOK) {
304 char *s;
305
306 if ((s = strerror (errno)))
307 snprintf (response, len_response, "unable to create socket: %s", s);
308 else
309 snprintf (response, len_response, "unable to create socket: unknown error");
310 return NOTOK;
311 }
312 #ifdef KPOP
313 if (kservice) /* "pop" */
314 return(sd);
315 #endif /* KPOP */
316 if (!rproto)
317 return sd;
318
319 memset(isock, 0, sizeof(*isock));
320 isock->sin_family = addrtype;
321 for (port = IPPORT_RESERVED - 1;;) {
322 isock->sin_port = htons ((unsigned short) port);
323 if (bind (sd, (struct sockaddr *) isock, sizeof(*isock)) != NOTOK)
324 return sd;
325
326 switch (errno) {
327 char *s;
328
329 case EADDRINUSE:
330 case EADDRNOTAVAIL:
331 if (--port <= IPPORT_RESERVED / 2) {
332 strncpy (response, "ports available", len_response);
333 return NOTOK;
334 }
335 break;
336
337 default:
338 if ((s = strerror (errno)))
339 snprintf (response, len_response, "unable to bind socket: %s", s);
340 else
341 snprintf (response, len_response, "unable to bind socket: unknown error");
342 return NOTOK;
343 }
344 }
345 }
346
347
348 static int
349 inet (struct hostent *hp, int net)
350 {
351 struct in_addr in;
352
353 memcpy(&in, hp->h_addr, sizeof(in));
354 return (inet_netof (in) == net);
355 }
356
357
358 /*
359 * taken from ISODE's compat/internet.c
360 */
361
362 static char *empty = NULL;
363
364 #ifdef h_addr
365 static char *addrs[2] = { NULL };
366 #endif
367
368 struct hostent *
369 gethostbystring (char *s)
370 {
371 register struct hostent *h;
372 static struct hostent hs;
373 #ifdef DG
374 static struct in_addr iaddr;
375 #else
376 static unsigned long iaddr;
377 #endif
378
379 iaddr = inet_addr (s);
380 #ifdef DG
381 if (iaddr.s_addr == NOTOK && strcmp (s, "255.255.255.255"))
382 #else
383 if (((int) iaddr == NOTOK) && strcmp (s, "255.255.255.255"))
384 #endif
385 return gethostbyname (s);
386
387 h = &hs;
388 h->h_name = s;
389 h->h_aliases = &empty;
390 h->h_addrtype = AF_INET;
391 h->h_length = sizeof(iaddr);
392 #ifdef h_addr
393 h->h_addr_list = addrs;
394 memset(addrs, 0, sizeof(addrs));
395 #endif
396 h->h_addr = (char *) &iaddr;
397
398 return h;
399 }
400
401
402 /*
403 * static copies of three nmh subroutines
404 */
405
406 static char *broken[MAXARGS + 1];
407
408 static char **
409 client_brkstring (char *strg, char *brksep, char *brkterm)
410 {
411 register int bi;
412 register char c, *sp;
413
414 sp = strg;
415
416 for (bi = 0; bi < MAXARGS; bi++) {
417 while (client_brkany (c = *sp, brksep))
418 *sp++ = 0;
419 if (!c || client_brkany (c, brkterm)) {
420 *sp = 0;
421 broken[bi] = 0;
422 return broken;
423 }
424
425 broken[bi] = sp;
426 while ((c = *++sp) && !client_brkany (c, brksep) && !client_brkany (c, brkterm))
427 continue;
428 }
429 broken[MAXARGS] = 0;
430
431 return broken;
432 }
433
434
435 /*
436 * returns 1 if chr in strg, 0 otherwise
437 */
438 static int
439 client_brkany (char chr, char *strg)
440 {
441 register char *sp;
442
443 if (strg)
444 for (sp = strg; *sp; sp++)
445 if (chr == *sp)
446 return 1;
447 return 0;
448 }
449
450
451 /*
452 * copy a string array and return pointer to end
453 */
454 static char **
455 client_copyip (char **p, char **q, int len_q)
456 {
457 while (*p && --len_q > 0)
458 *q++ = *p++;
459
460 *q = NULL;
461
462 return q;
463 }
464
465
466 static char *
467 client_getcpy (char *str)
468 {
469 char *cp;
470 size_t len;
471
472 len = strlen(str) + 1;
473 if (!(cp = malloc(len)))
474 return NULL;
475
476 memcpy (cp, str, len);
477 return cp;
478 }
479