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