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