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