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