annotate lib-src/gnuserv.c @ 653:8779cda24ee7

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