annotate lib-src/gnuserv.c @ 4940:9113c5044de8

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