428
|
1 /* -*-C-*-
|
|
2 Server code for handling requests from clients and forwarding them
|
613
|
3 on to the XEmacs process.
|
428
|
4
|
613
|
5 This file is part of XEmacs.
|
428
|
6
|
|
7 Copying is permitted under those conditions described by the GNU
|
|
8 General Public License.
|
|
9
|
|
10 Copyright (C) 1989 Free Software Foundation, Inc.
|
|
11
|
|
12 Author: Andy Norman (ange@hplb.hpl.hp.com), based on 'etc/server.c'
|
|
13 from the 18.52 GNU Emacs distribution.
|
|
14
|
|
15 Please mail bugs and suggestions to the author at the above address.
|
|
16 */
|
|
17
|
442
|
18 /* HISTORY
|
|
19 * 11-Nov-1990 bristor@simba
|
428
|
20 * Added EOT stuff.
|
|
21 */
|
|
22
|
|
23 /*
|
|
24 * This file incorporates new features added by Bob Weiner <weiner@mot.com>,
|
|
25 * Darrell Kindred <dkindred@cmu.edu> and Arup Mukherjee <arup@cmu.edu>.
|
|
26 * Please see the note at the end of the README file for details.
|
|
27 *
|
|
28 * (If gnuserv came bundled with your emacs, the README file is probably
|
|
29 * ../etc/gnuserv.README relative to the directory containing this file)
|
|
30 */
|
|
31
|
456
|
32 #include "gnuserv.h"
|
428
|
33
|
456
|
34 char gnuserv_version[] = "gnuserv version" GNUSERV_VERSION;
|
|
35
|
428
|
36
|
|
37 #ifdef USE_LITOUT
|
|
38 #ifdef linux
|
|
39 #include <bsd/sgtty.h>
|
|
40 #else
|
|
41 #include <sgtty.h>
|
|
42 #endif
|
|
43 #endif
|
|
44
|
|
45 #ifdef AIX
|
|
46 #include <sys/select.h>
|
|
47 #endif
|
|
48
|
|
49 #include <stdlib.h>
|
|
50 #include <stdio.h>
|
|
51 #include <sys/types.h>
|
|
52 #include <sys/stat.h>
|
|
53
|
|
54 #ifdef HAVE_UNISTD_H
|
|
55 #include <unistd.h>
|
|
56 #endif /* HAVE_UNISTD_H */
|
|
57
|
|
58 #ifdef HAVE_STRING_H
|
|
59 #include <string.h>
|
|
60 #endif /* HAVE_STRING_H */
|
|
61
|
|
62 #if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && \
|
|
63 !defined(INTERNET_DOMAIN_SOCKETS)
|
|
64 main ()
|
|
65 {
|
|
66 fprintf (stderr,"Sorry, the Emacs server is only supported on systems that have\n");
|
|
67 fprintf (stderr,"Unix Domain sockets, Internet Domain sockets or System V IPC\n");
|
|
68 exit (1);
|
|
69 } /* main */
|
|
70 #else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
|
|
71
|
|
72 #ifdef SYSV_IPC
|
|
73
|
|
74 int ipc_qid = 0; /* ipc message queue id */
|
|
75 pid_t ipc_wpid = 0; /* watchdog task pid */
|
|
76
|
|
77
|
|
78 /*
|
|
79 ipc_exit -- clean up the queue id and queue, then kill the watchdog task
|
|
80 if it exists. exit with the given status.
|
|
81 */
|
|
82 void
|
|
83 ipc_exit (int stat)
|
|
84 {
|
|
85 msgctl (ipc_qid,IPC_RMID,0);
|
442
|
86
|
428
|
87 if (ipc_wpid != 0)
|
|
88 kill (ipc_wpid, SIGKILL);
|
|
89
|
|
90 exit (stat);
|
|
91 } /* ipc_exit */
|
|
92
|
|
93
|
|
94 /*
|
|
95 ipc_handle_signal -- catch the signal given and clean up.
|
|
96 */
|
|
97 void
|
|
98 ipc_handle_signal(int sig)
|
|
99 {
|
|
100 ipc_exit (0);
|
|
101 } /* ipc_handle_signal */
|
|
102
|
|
103
|
442
|
104 /*
|
428
|
105 ipc_spawn_watchdog -- spawn a watchdog task to clean up the message queue should the
|
|
106 server process die.
|
|
107 */
|
|
108 void
|
|
109 ipc_spawn_watchdog (void)
|
|
110 {
|
|
111 if ((ipc_wpid = fork ()) == 0)
|
|
112 { /* child process */
|
|
113 pid_t ppid = getppid (); /* parent's process id */
|
|
114
|
|
115 setpgrp(); /* gnu kills process group on exit */
|
|
116
|
|
117 while (1)
|
|
118 {
|
|
119 if (kill (ppid, 0) < 0) /* ppid is no longer valid, parent
|
|
120 may have died */
|
|
121 {
|
|
122 ipc_exit (0);
|
|
123 } /* if */
|
|
124
|
|
125 sleep(10); /* have another go later */
|
|
126 } /* while */
|
|
127 } /* if */
|
|
128
|
|
129 } /* ipc_spawn_watchdog */
|
|
130
|
|
131
|
|
132 /*
|
|
133 ipc_init -- initialize server, setting the global msqid that can be listened on.
|
|
134 */
|
|
135 void
|
|
136 ipc_init (struct msgbuf **msgpp)
|
|
137 {
|
|
138 key_t key; /* messge key */
|
|
139 char buf[GSERV_BUFSZ]; /* pathname for key */
|
|
140
|
|
141 sprintf (buf,"%s/gsrv%d",tmpdir,(int)geteuid ());
|
|
142 creat (buf,0600);
|
|
143 key = ftok (buf,1);
|
|
144
|
|
145 if ((ipc_qid = msgget (key,0600|IPC_CREAT)) == -1)
|
|
146 {
|
|
147 perror (progname);
|
|
148 fprintf (stderr, "%s: unable to create msg queue\n", progname);
|
|
149 ipc_exit (1);
|
|
150 } /* if */
|
|
151
|
|
152 ipc_spawn_watchdog ();
|
|
153
|
|
154 signal (SIGTERM,ipc_handle_signal);
|
|
155 signal (SIGINT,ipc_handle_signal);
|
|
156
|
|
157 if ((*msgpp = (struct msgbuf *)
|
|
158 malloc (sizeof **msgpp + GSERV_BUFSZ)) == NULL)
|
|
159 {
|
|
160 fprintf (stderr,
|
|
161 "%s: unable to allocate space for message buffer\n", progname);
|
|
162 ipc_exit(1);
|
|
163 } /* if */
|
|
164 } /* ipc_init */
|
|
165
|
|
166
|
|
167 /*
|
|
168 handle_ipc_request -- accept a request from a client, pass the request on
|
613
|
169 to the XEmacs process, then wait for its reply and
|
428
|
170 pass that on to the client.
|
|
171 */
|
|
172 void
|
|
173 handle_ipc_request (struct msgbuf *msgp)
|
|
174 {
|
|
175 struct msqid_ds msg_st; /* message status */
|
|
176 char buf[GSERV_BUFSZ];
|
|
177 int len; /* length of message / read */
|
|
178 int s, result_len; /* tag fields on the response from emacs */
|
|
179 int offset = 0;
|
|
180 int total = 1; /* # bytes that will actually be sent off */
|
|
181
|
|
182 if ((len = msgrcv (ipc_qid, msgp, GSERV_BUFSZ - 1, 1, 0)) < 0)
|
|
183 {
|
|
184 perror (progname);
|
|
185 fprintf (stderr, "%s: unable to receive\n", progname);
|
|
186 ipc_exit (1);
|
|
187 } /* if */
|
|
188
|
|
189 msgctl (ipc_qid, IPC_STAT, &msg_st);
|
|
190 strncpy (buf, msgp->mtext, len);
|
|
191 buf[len] = '\0'; /* terminate */
|
442
|
192
|
428
|
193 printf ("%d %s", ipc_qid, buf);
|
|
194 fflush (stdout);
|
|
195
|
|
196 /* now for the response from gnu */
|
|
197 msgp->mtext[0] = '\0';
|
|
198
|
|
199 #if 0
|
|
200 if ((len = read(0,buf,GSERV_BUFSZ-1)) < 0)
|
|
201 {
|
|
202 perror (progname);
|
|
203 fprintf (stderr, "%s: unable to read\n", progname);
|
|
204 ipc_exit (1);
|
|
205 } /* if */
|
|
206
|
|
207 sscanf (buf, "%d:%[^\n]\n", &junk, msgp->mtext);
|
|
208 #else
|
|
209
|
|
210 /* read in "n/m:" (n=client fd, m=message length) */
|
|
211
|
442
|
212 while (offset < (GSERV_BUFSZ-1) &&
|
428
|
213 ((len = read (0, buf + offset, 1)) > 0) &&
|
|
214 buf[offset] != ':')
|
|
215 {
|
|
216 offset += len;
|
|
217 }
|
|
218
|
|
219 if (len < 0)
|
|
220 {
|
|
221 perror (progname);
|
|
222 fprintf (stderr, "%s: unable to read\n", progname);
|
|
223 exit(1);
|
|
224 }
|
|
225
|
|
226 /* parse the response from emacs, getting client fd & result length */
|
|
227 buf[offset] = '\0';
|
|
228 sscanf (buf, "%d/%d", &s, &result_len);
|
|
229
|
|
230 while (result_len > 0)
|
|
231 {
|
|
232 if ((len = read(0, buf, min2 (result_len, GSERV_BUFSZ - 1))) < 0)
|
|
233 {
|
|
234 perror (progname);
|
|
235 fprintf (stderr, "%s: unable to read\n", progname);
|
|
236 exit (1);
|
|
237 }
|
|
238
|
442
|
239 /* Send this string off, but only if we have enough space */
|
428
|
240
|
|
241 if (GSERV_BUFSZ > total)
|
|
242 {
|
|
243 if (total + len <= GSERV_BUFSZ)
|
|
244 buf[len] = 0;
|
|
245 else
|
|
246 buf[GSERV_BUFSZ - total] = 0;
|
|
247
|
|
248 send_string(s,buf);
|
|
249 total += strlen(buf);
|
|
250 }
|
|
251
|
|
252 result_len -= len;
|
|
253 }
|
|
254
|
|
255 /* eat the newline */
|
|
256 while ((len = read (0,buf,1)) == 0)
|
|
257 ;
|
|
258 if (len < 0)
|
|
259 {
|
|
260 perror(progname);
|
|
261 fprintf (stderr,"%s: unable to read\n", progname);
|
|
262 exit (1);
|
|
263 }
|
|
264 if (buf[0] != '\n')
|
|
265 {
|
|
266 fprintf (stderr,"%s: garbage after result [%c]\n", progname, buf[0]);
|
|
267 exit (1);
|
|
268 }
|
|
269 #endif
|
|
270
|
|
271 /* Send a response back to the client. */
|
|
272
|
|
273 msgp->mtype = msg_st.msg_lspid;
|
|
274 if (msgsnd (ipc_qid,msgp,strlen(msgp->mtext)+1,0) < 0)
|
|
275 perror ("msgsend(gnuserv)");
|
|
276
|
|
277 } /* handle_ipc_request */
|
|
278 #endif /* SYSV_IPC */
|
|
279
|
|
280
|
|
281 #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS)
|
|
282 /*
|
|
283 echo_request -- read request from a given socket descriptor, and send the information
|
|
284 to stdout (the gnu process).
|
|
285 */
|
|
286 static void
|
|
287 echo_request (int s)
|
|
288 {
|
|
289 char buf[GSERV_BUFSZ];
|
|
290 int len;
|
|
291
|
|
292 printf("%d ",s);
|
442
|
293
|
428
|
294 /* read until we get a newline or no characters */
|
|
295 while ((len = recv(s,buf,GSERV_BUFSZ-1,0)) > 0) {
|
|
296 buf[len] = '\0';
|
|
297 printf("%s",buf);
|
|
298
|
|
299 if (buf[len-1] == EOT_CHR) {
|
|
300 fflush(stdout);
|
|
301 break; /* end of message */
|
|
302 }
|
|
303
|
|
304 } /* while */
|
|
305
|
|
306 if (len < 0) {
|
|
307 perror(progname);
|
|
308 fprintf(stderr,"%s: unable to recv\n",progname);
|
|
309 exit(1);
|
|
310 } /* if */
|
442
|
311
|
428
|
312 } /* echo_request */
|
|
313
|
|
314
|
|
315 /*
|
|
316 handle_response -- accept a response from stdin (the gnu process) and pass the
|
|
317 information on to the relevant client.
|
|
318 */
|
|
319 static void
|
|
320 handle_response (void)
|
|
321 {
|
|
322 char buf[GSERV_BUFSZ+1];
|
|
323 int offset=0;
|
|
324 int s;
|
|
325 int len = 0;
|
|
326 int result_len;
|
|
327
|
|
328 /* read in "n/m:" (n=client fd, m=message length) */
|
442
|
329 while (offset < GSERV_BUFSZ &&
|
428
|
330 ((len = read(0,buf+offset,1)) > 0) &&
|
|
331 buf[offset] != ':') {
|
|
332 offset += len;
|
|
333 }
|
|
334
|
|
335 if (len < 0) {
|
|
336 perror(progname);
|
|
337 fprintf(stderr,"%s: unable to read\n",progname);
|
|
338 exit(1);
|
|
339 }
|
442
|
340
|
428
|
341 /* parse the response from emacs, getting client fd & result length */
|
|
342 buf[offset] = '\0';
|
|
343 sscanf(buf,"%d/%d", &s, &result_len);
|
|
344
|
|
345 while (result_len > 0) {
|
|
346 if ((len = read(0,buf,min2(result_len,GSERV_BUFSZ))) < 0) {
|
|
347 perror(progname);
|
|
348 fprintf(stderr,"%s: unable to read\n",progname);
|
|
349 exit(1);
|
|
350 }
|
|
351 buf[len] = '\0';
|
|
352 send_string(s,buf);
|
|
353 result_len -= len;
|
|
354 }
|
|
355
|
|
356 /* eat the newline */
|
|
357 while ((len = read(0,buf,1)) == 0)
|
|
358 ;
|
|
359 if (len < 0)
|
|
360 {
|
|
361 perror(progname);
|
|
362 fprintf(stderr,"%s: unable to read\n",progname);
|
|
363 exit(1);
|
|
364 }
|
|
365 if (buf[0] != '\n')
|
|
366 {
|
|
367 fprintf(stderr,"%s: garbage after result\n",progname);
|
|
368 exit(1);
|
|
369 }
|
|
370 /* send the newline */
|
|
371 buf[1] = '\0';
|
|
372 send_string(s,buf);
|
442
|
373 close(s);
|
428
|
374
|
|
375 } /* handle_response */
|
|
376 #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */
|
|
377
|
|
378
|
|
379 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
380 struct entry {
|
458
|
381 unsigned long host_addr;
|
428
|
382 struct entry *next;
|
|
383 };
|
|
384
|
|
385 struct entry *permitted_hosts[TABLE_SIZE];
|
|
386
|
|
387 #ifdef AUTH_MAGIC_COOKIE
|
|
388 # include <X11/X.h>
|
|
389 # include <X11/Xauth.h>
|
|
390
|
|
391 static Xauth *server_xauth = NULL;
|
442
|
392 #endif
|
428
|
393
|
442
|
394 static int
|
428
|
395 timed_read (int fd, char *buf, int max, int timeout, int one_line)
|
|
396 {
|
|
397 fd_set rmask;
|
|
398 struct timeval tv; /* = {timeout, 0}; */
|
|
399 char c = 0;
|
|
400 int nbytes = 0;
|
|
401 int r;
|
442
|
402
|
428
|
403 tv.tv_sec = timeout;
|
|
404 tv.tv_usec = 0;
|
|
405
|
|
406 FD_ZERO(&rmask);
|
|
407 FD_SET(fd, &rmask);
|
442
|
408
|
428
|
409 do
|
|
410 {
|
|
411 r = select(fd + 1, &rmask, NULL, NULL, &tv);
|
|
412
|
|
413 if (r > 0)
|
|
414 {
|
|
415 if (read (fd, &c, 1) == 1 )
|
|
416 {
|
|
417 *buf++ = c;
|
|
418 ++nbytes;
|
|
419 }
|
|
420 else
|
|
421 {
|
|
422 printf ("read error on socket\004\n");
|
|
423 return -1;
|
|
424 }
|
|
425 }
|
|
426 else if (r == 0)
|
|
427 {
|
|
428 printf ("read timed out\004\n");
|
|
429 return -1;
|
|
430 }
|
|
431 else
|
|
432 {
|
|
433 printf ("error in select\004\n");
|
|
434 return -1;
|
|
435 }
|
|
436 } while ((nbytes < max) && !(one_line && (c == '\n')));
|
|
437
|
|
438 --buf;
|
|
439 if (one_line && *buf == '\n')
|
|
440 {
|
|
441 *buf = 0;
|
|
442 }
|
|
443
|
|
444 return nbytes;
|
|
445 }
|
442
|
446
|
|
447
|
428
|
448
|
|
449 /*
|
|
450 permitted -- return whether a given host is allowed to connect to the server.
|
|
451 */
|
|
452 static int
|
458
|
453 permitted (unsigned long host_addr, int fd)
|
428
|
454 {
|
|
455 int key;
|
|
456 struct entry *entry;
|
|
457
|
442
|
458 char auth_protocol[128];
|
428
|
459 char buf[1024];
|
|
460 int auth_data_len;
|
|
461
|
|
462 if (fd > 0)
|
|
463 {
|
|
464 /* we are checking permission on a real connection */
|
|
465
|
|
466 /* Read auth protocol name */
|
442
|
467
|
428
|
468 if (timed_read(fd, auth_protocol, AUTH_NAMESZ, AUTH_TIMEOUT, 1) <= 0)
|
|
469 return FALSE;
|
|
470
|
|
471 if (strcmp (auth_protocol, DEFAUTH_NAME) &&
|
|
472 strcmp (auth_protocol, MCOOKIE_NAME))
|
|
473 {
|
442
|
474 printf ("authentication protocol (%s) from client is invalid...\n",
|
428
|
475 auth_protocol);
|
|
476 printf ("... Was the client an old version of gnuclient/gnudoit?\004\n");
|
442
|
477
|
428
|
478 return FALSE;
|
|
479 }
|
|
480
|
|
481 if (!strcmp(auth_protocol, MCOOKIE_NAME))
|
|
482 {
|
|
483
|
|
484 /*
|
|
485 * doing magic cookie auth
|
|
486 */
|
|
487
|
647
|
488 if (timed_read (fd, buf, 10, AUTH_TIMEOUT, 1) <= 0)
|
428
|
489 return FALSE;
|
|
490
|
647
|
491 auth_data_len = atoi (buf);
|
428
|
492
|
647
|
493 if (auth_data_len <= 0 || auth_data_len > (int) sizeof (buf))
|
456
|
494 {
|
|
495 return FALSE;
|
|
496 }
|
|
497
|
647
|
498 if (timed_read (fd, buf, auth_data_len, AUTH_TIMEOUT, 0) !=
|
|
499 auth_data_len)
|
428
|
500 return FALSE;
|
442
|
501
|
428
|
502 #ifdef AUTH_MAGIC_COOKIE
|
456
|
503 if (server_xauth && server_xauth->data)
|
462
|
504 {
|
456
|
505 /* Do a compare without comprising info about
|
|
506 the size of the cookie */
|
460
|
507 int auth_data_pos;
|
|
508 int auth_mismatches =
|
456
|
509 ( auth_data_len ^
|
|
510 server_xauth->data_length );
|
|
511
|
647
|
512 for(auth_data_pos = 0; auth_data_pos < auth_data_len;
|
|
513 ++auth_data_pos)
|
456
|
514 auth_mismatches |=
|
|
515 ( buf[auth_data_pos] ^
|
647
|
516 server_xauth->data[auth_data_pos %
|
|
517 server_xauth->data_length]);
|
456
|
518
|
|
519 if (auth_mismatches == 0)
|
428
|
520 return TRUE;
|
456
|
521
|
|
522 for(;rand() % 1000;);
|
462
|
523 }
|
456
|
524
|
442
|
525 #else
|
428
|
526 printf ("client tried Xauth, but server is not compiled with Xauth\n");
|
|
527 #endif
|
442
|
528
|
428
|
529 /*
|
|
530 * auth failed, but allow this to fall through to the GNU_SECURE
|
|
531 * protocol....
|
|
532 */
|
|
533
|
|
534 printf ("Xauth authentication failed, trying GNU_SECURE auth...\004\n");
|
|
535
|
|
536 }
|
442
|
537
|
428
|
538 /* Other auth protocols go here, and should execute only if the
|
|
539 * auth_protocol name matches.
|
|
540 */
|
|
541
|
|
542 }
|
|
543
|
|
544
|
|
545 /* Now, try the old GNU_SECURE stuff... */
|
442
|
546
|
428
|
547 /* First find the hash key */
|
|
548 key = HASH(host_addr) % TABLE_SIZE;
|
442
|
549
|
428
|
550 /* Now check the chain for that hash key */
|
|
551 for(entry=permitted_hosts[key]; entry != NULL; entry=entry->next)
|
442
|
552 if (host_addr == entry->host_addr)
|
428
|
553 return(TRUE);
|
442
|
554
|
428
|
555 return(FALSE);
|
|
556
|
|
557 } /* permitted */
|
|
558
|
|
559
|
442
|
560 /*
|
428
|
561 add_host -- add the given host to the list of permitted hosts, provided it isn't
|
|
562 already there.
|
442
|
563 */
|
428
|
564 static void
|
458
|
565 add_host (unsigned long host_addr)
|
428
|
566 {
|
|
567 int key;
|
|
568 struct entry *new_entry;
|
442
|
569
|
428
|
570 if (!permitted(host_addr, -1))
|
|
571 {
|
|
572 if ((new_entry = (struct entry *) malloc(sizeof(struct entry))) == NULL) {
|
|
573 fprintf(stderr,"%s: unable to malloc space for permitted host entry\n",
|
|
574 progname);
|
|
575 exit(1);
|
|
576 } /* if */
|
|
577
|
|
578 new_entry->host_addr = host_addr;
|
|
579 key = HASH(host_addr) % TABLE_SIZE;
|
|
580 new_entry->next = permitted_hosts[key];
|
|
581 permitted_hosts[key] = new_entry;
|
|
582 } /* if */
|
|
583
|
|
584 } /* add_host */
|
|
585
|
|
586
|
|
587 /*
|
|
588 setup_table -- initialize the table of hosts allowed to contact the server,
|
|
589 by reading from the file specified by the GNU_SECURE
|
|
590 environment variable
|
|
591 Put in the local machine, and, if a security file is specifed,
|
|
592 add each host that is named in the file.
|
|
593 Return the number of hosts added.
|
|
594 */
|
|
595 static int
|
|
596 setup_table (void)
|
|
597 {
|
|
598 FILE *host_file;
|
|
599 char *file_name;
|
|
600 char hostname[HOSTNAMSZ];
|
458
|
601 unsigned int host_addr;
|
428
|
602 int i, hosts=0;
|
442
|
603
|
428
|
604 /* Make sure every entry is null */
|
|
605 for (i=0; i<TABLE_SIZE; i++)
|
|
606 permitted_hosts[i] = NULL;
|
|
607
|
|
608 gethostname(hostname,HOSTNAMSZ);
|
|
609
|
647
|
610 if ((host_addr = internet_addr (hostname)) == (unsigned int) -1)
|
428
|
611 {
|
442
|
612 fprintf(stderr,"%s: unable to find %s in /etc/hosts or from YP",
|
428
|
613 progname,hostname);
|
|
614 exit(1);
|
|
615 } /* if */
|
|
616
|
|
617 #ifdef AUTH_MAGIC_COOKIE
|
442
|
618
|
|
619 server_xauth = XauGetAuthByAddr (FamilyInternet,
|
428
|
620 sizeof(host_addr), (char *)&host_addr,
|
442
|
621 strlen(MCOOKIE_SCREEN), MCOOKIE_SCREEN,
|
428
|
622 strlen(MCOOKIE_X_NAME), MCOOKIE_X_NAME);
|
|
623 hosts++;
|
|
624
|
|
625 #endif /* AUTH_MAGIC_COOKIE */
|
442
|
626
|
428
|
627
|
|
628 #if 0 /* Don't even want to allow access from the local host by default */
|
|
629 add_host(host_addr); /* add local host */
|
442
|
630 #endif
|
428
|
631
|
|
632 if (((file_name = getenv("GNU_SECURE")) != NULL && /* security file */
|
|
633 (host_file = fopen(file_name,"r")) != NULL)) /* opened ok */
|
|
634 {
|
|
635 while ((fscanf(host_file,"%s",hostname) != EOF)) /* find a host */
|
647
|
636 if ((host_addr = internet_addr(hostname)) != (unsigned int) -1)
|
|
637 /* get its addr */
|
428
|
638 {
|
647
|
639 add_host(host_addr); /* add the addr */
|
428
|
640 hosts++;
|
|
641 }
|
|
642 fclose(host_file);
|
|
643 } /* if */
|
|
644
|
|
645 return hosts;
|
|
646 } /* setup_table */
|
|
647
|
|
648
|
|
649 /*
|
|
650 internet_init -- initialize server, returning an internet socket that can
|
|
651 be listened on.
|
|
652 */
|
|
653 static int
|
|
654 internet_init (void)
|
|
655 {
|
|
656 int ls; /* socket descriptor */
|
|
657 struct servent *sp; /* pointer to service information */
|
|
658 struct sockaddr_in server; /* for local socket address */
|
|
659 char *ptr; /* ptr to return from getenv */
|
|
660
|
442
|
661 if (setup_table() == 0)
|
428
|
662 return -1;
|
|
663
|
|
664 /* clear out address structure */
|
442
|
665 memset (&server, '\0', sizeof (server));
|
|
666
|
428
|
667 /* Set up address structure for the listen socket. */
|
|
668 server.sin_family = AF_INET;
|
|
669 server.sin_addr.s_addr = INADDR_ANY;
|
|
670
|
|
671 /* Find the information for the gnu server
|
|
672 * in order to get the needed port number.
|
|
673 */
|
|
674 if ((ptr=getenv("GNU_PORT")) != NULL)
|
|
675 server.sin_port = htons(atoi(ptr));
|
|
676 else if ((sp = getservbyname ("gnuserv", "tcp")) == NULL)
|
|
677 server.sin_port = htons(DEFAULT_PORT+getuid());
|
|
678 else
|
|
679 server.sin_port = sp->s_port;
|
442
|
680
|
428
|
681 /* Create the listen socket. */
|
|
682 if ((ls = socket (AF_INET,SOCK_STREAM, 0)) == -1)
|
|
683 {
|
|
684 perror(progname);
|
|
685 fprintf(stderr,"%s: unable to create socket\n",progname);
|
|
686 exit(1);
|
|
687 } /* if */
|
442
|
688
|
428
|
689 /* Bind the listen address to the socket. */
|
|
690 if (bind(ls,(struct sockaddr *) &server,sizeof(struct sockaddr_in)) == -1)
|
|
691 {
|
|
692 perror(progname);
|
|
693 fprintf(stderr,"%s: unable to bind socket\n",progname);
|
|
694 exit(1);
|
|
695 } /* if */
|
|
696
|
|
697 /* Initiate the listen on the socket so remote users
|
442
|
698 * can connect.
|
428
|
699 */
|
|
700 if (listen(ls,20) == -1)
|
|
701 {
|
|
702 perror(progname);
|
|
703 fprintf(stderr,"%s: unable to listen\n",progname);
|
|
704 exit(1);
|
|
705 } /* if */
|
|
706
|
|
707 return(ls);
|
|
708
|
|
709 } /* internet_init */
|
|
710
|
|
711
|
|
712 /*
|
|
713 handle_internet_request -- accept a request from a client and send the information
|
|
714 to stdout (the gnu process).
|
|
715 */
|
|
716 static void
|
|
717 handle_internet_request (int ls)
|
|
718 {
|
|
719 int s;
|
442
|
720 socklen_t addrlen = sizeof (struct sockaddr_in);
|
428
|
721 struct sockaddr_in peer; /* for peer socket address */
|
|
722
|
442
|
723 memset (&peer, '\0', sizeof (peer));
|
428
|
724
|
440
|
725 if ((s = accept(ls,(struct sockaddr *)&peer, &addrlen)) == -1)
|
428
|
726 {
|
|
727 perror(progname);
|
|
728 fprintf(stderr,"%s: unable to accept\n",progname);
|
|
729 exit(1);
|
|
730 } /* if */
|
442
|
731
|
428
|
732 /* Check that access is allowed - if not return crud to the client */
|
|
733 if (!permitted(peer.sin_addr.s_addr, s))
|
|
734 {
|
|
735 send_string(s,"gnudoit: Connection refused\ngnudoit: unable to connect to remote");
|
|
736 close(s);
|
|
737
|
|
738 printf("Refused connection from %s\004\n", inet_ntoa(peer.sin_addr));
|
|
739 return;
|
|
740 } /* if */
|
|
741
|
|
742 echo_request(s);
|
442
|
743
|
428
|
744 } /* handle_internet_request */
|
|
745 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
746
|
|
747
|
|
748 #ifdef UNIX_DOMAIN_SOCKETS
|
|
749 /*
|
|
750 unix_init -- initialize server, returning an unix-domain socket that can
|
|
751 be listened on.
|
|
752 */
|
|
753 static int
|
|
754 unix_init (void)
|
|
755 {
|
|
756 int ls; /* socket descriptor */
|
|
757 struct sockaddr_un server; /* unix socket address */
|
442
|
758 socklen_t bindlen;
|
428
|
759
|
|
760 if ((ls = socket(AF_UNIX,SOCK_STREAM, 0)) < 0)
|
|
761 {
|
|
762 perror(progname);
|
|
763 fprintf(stderr,"%s: unable to create socket\n",progname);
|
|
764 exit(1);
|
|
765 } /* if */
|
|
766
|
|
767 /* Set up address structure for the listen socket. */
|
|
768 #ifdef HIDE_UNIX_SOCKET
|
|
769 sprintf(server.sun_path,"%s/gsrvdir%d",tmpdir,(int)geteuid());
|
|
770 if (mkdir(server.sun_path, 0700) < 0)
|
|
771 {
|
|
772 /* assume it already exists, and try to set perms */
|
|
773 if (chmod(server.sun_path, 0700) < 0)
|
|
774 {
|
|
775 perror(progname);
|
|
776 fprintf(stderr,"%s: can't set permissions on %s\n",
|
|
777 progname, server.sun_path);
|
|
778 exit(1);
|
|
779 }
|
|
780 }
|
|
781 strcat(server.sun_path,"/gsrv");
|
|
782 unlink(server.sun_path); /* remove old file if it exists */
|
|
783 #else /* HIDE_UNIX_SOCKET */
|
|
784 sprintf(server.sun_path,"%s/gsrv%d",tmpdir,(int)geteuid());
|
|
785 unlink(server.sun_path); /* remove old file if it exists */
|
|
786 #endif /* HIDE_UNIX_SOCKET */
|
|
787
|
|
788 server.sun_family = AF_UNIX;
|
|
789 #ifdef HAVE_SOCKADDR_SUN_LEN
|
|
790 /* See W. R. Stevens "Advanced Programming in the Unix Environment"
|
|
791 p. 502 */
|
|
792 bindlen = (sizeof (server.sun_len) + sizeof (server.sun_family)
|
|
793 + strlen (server.sun_path) + 1);
|
|
794 server.sun_len = bindlen;
|
|
795 #else
|
|
796 bindlen = strlen (server.sun_path) + sizeof (server.sun_family);
|
|
797 #endif
|
442
|
798
|
428
|
799 if (bind(ls,(struct sockaddr *)&server,bindlen) < 0)
|
|
800 {
|
|
801 perror(progname);
|
|
802 fprintf(stderr,"%s: unable to bind socket\n",progname);
|
|
803 exit(1);
|
|
804 } /* if */
|
|
805
|
|
806 chmod(server.sun_path,0700); /* only this user can send commands */
|
|
807
|
|
808 if (listen(ls,20) < 0) {
|
|
809 perror(progname);
|
|
810 fprintf(stderr,"%s: unable to listen\n",progname);
|
|
811 exit(1);
|
|
812 } /* if */
|
|
813
|
|
814 /* #### there are also better ways of dealing with this when
|
|
815 sigvec() is present. */
|
|
816 #if defined (HAVE_SIGPROCMASK)
|
442
|
817 {
|
428
|
818 sigset_t _mask;
|
|
819 sigemptyset (&_mask);
|
|
820 sigaddset (&_mask, SIGPIPE);
|
|
821 sigprocmask (SIG_BLOCK, &_mask, NULL);
|
|
822 }
|
|
823 #else
|
|
824 signal(SIGPIPE,SIG_IGN); /* in case user kills client */
|
|
825 #endif
|
|
826
|
|
827 return(ls);
|
|
828
|
|
829 } /* unix_init */
|
|
830
|
|
831
|
|
832 /*
|
|
833 handle_unix_request -- accept a request from a client and send the information
|
|
834 to stdout (the gnu process).
|
|
835 */
|
|
836 static void
|
|
837 handle_unix_request (int ls)
|
|
838 {
|
|
839 int s;
|
442
|
840 socklen_t len = sizeof (struct sockaddr_un);
|
428
|
841 struct sockaddr_un server; /* for unix socket address */
|
|
842
|
|
843 server.sun_family = AF_UNIX;
|
|
844
|
440
|
845 if ((s = accept(ls,(struct sockaddr *)&server, &len)) < 0)
|
428
|
846 {
|
|
847 perror(progname);
|
|
848 fprintf(stderr,"%s: unable to accept\n",progname);
|
|
849 } /* if */
|
|
850
|
|
851 echo_request(s);
|
442
|
852
|
428
|
853 } /* handle_unix_request */
|
|
854 #endif /* UNIX_DOMAIN_SOCKETS */
|
|
855
|
|
856
|
|
857 int
|
|
858 main (int argc, char *argv[])
|
|
859 {
|
|
860 int chan; /* temporary channel number */
|
|
861 #ifdef SYSV_IPC
|
|
862 struct msgbuf *msgp; /* message buffer */
|
|
863 #else
|
|
864 int ils = -1; /* internet domain listen socket */
|
|
865 int uls = -1; /* unix domain listen socket */
|
|
866 #endif /* SYSV_IPC */
|
|
867
|
|
868 progname = argv[0];
|
|
869
|
|
870 for(chan=3; chan < _NFILE; close(chan++)) /* close unwanted channels */
|
|
871 ;
|
|
872
|
|
873 #ifdef USE_TMPDIR
|
|
874 tmpdir = getenv("TMPDIR");
|
|
875 #endif
|
|
876 if (!tmpdir)
|
|
877 tmpdir = "/tmp";
|
|
878 #ifdef USE_LITOUT
|
|
879 {
|
|
880 /* this is to allow ^D to pass to emacs */
|
|
881 int d = LLITOUT;
|
|
882 (void) ioctl(fileno(stdout), TIOCLBIS, &d);
|
|
883 }
|
|
884 #endif
|
|
885
|
|
886 #ifdef SYSV_IPC
|
|
887 ipc_init(&msgp); /* get a msqid to listen on, and a message buffer */
|
|
888 #endif /* SYSV_IPC */
|
|
889
|
|
890 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
891 ils = internet_init(); /* get an internet domain socket to listen on */
|
|
892 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
893
|
|
894 #ifdef UNIX_DOMAIN_SOCKETS
|
|
895 uls = unix_init(); /* get a unix domain socket to listen on */
|
|
896 #endif /* UNIX_DOMAIN_SOCKETS */
|
|
897
|
|
898 while (1) {
|
|
899 #ifdef SYSV_IPC
|
|
900 handle_ipc_request(msgp);
|
|
901 #else /* NOT SYSV_IPC */
|
|
902 fd_set rmask;
|
|
903 FD_ZERO(&rmask);
|
|
904 FD_SET(fileno(stdin), &rmask);
|
|
905 if (uls >= 0)
|
|
906 FD_SET(uls, &rmask);
|
|
907 if (ils >= 0)
|
|
908 FD_SET(ils, &rmask);
|
442
|
909
|
|
910 if (select(max2(fileno(stdin),max2(uls,ils)) + 1, &rmask,
|
428
|
911 (fd_set *)NULL, (fd_set *)NULL, (struct timeval *)NULL) < 0)
|
|
912 {
|
|
913 perror(progname);
|
|
914 fprintf(stderr,"%s: unable to select\n",progname);
|
442
|
915 return 1;
|
428
|
916 } /* if */
|
|
917
|
|
918 #ifdef UNIX_DOMAIN_SOCKETS
|
|
919 if (uls > 0 && FD_ISSET(uls, &rmask))
|
|
920 handle_unix_request(uls);
|
|
921 #endif
|
|
922
|
|
923 #ifdef INTERNET_DOMAIN_SOCKETS
|
|
924 if (ils > 0 && FD_ISSET(ils, &rmask))
|
|
925 handle_internet_request(ils);
|
|
926 #endif /* INTERNET_DOMAIN_SOCKETS */
|
|
927
|
|
928 if (FD_ISSET(fileno(stdin), &rmask)) /* from stdin (gnu process) */
|
|
929 handle_response();
|
|
930 #endif /* NOT SYSV_IPC */
|
442
|
931 } /* while (1) */
|
428
|
932 } /* main */
|
|
933
|
|
934 #endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */
|