]>
Commit | Line | Data |
---|---|---|
de56407f | 1 | /* |
2 | * $Source$ | |
3 | * $Header$ | |
4 | */ | |
546bc43b | 5 | /* (c) Copyright 1988 by the Massachusetts Institute of Technology. */ |
6 | /* For copying and distribution information, please see the file */ | |
7 | /* <mit-copyright.h>. */ | |
de56407f | 8 | |
9 | #ifndef lint | |
10 | static char *rcsid_dispatch_c = "$Header$"; | |
7da203a3 | 11 | #endif |
de56407f | 12 | |
546bc43b | 13 | #include <mit-copyright.h> |
de56407f | 14 | #include <stdio.h> |
7da203a3 | 15 | #include <stdlib.h> |
45c91bf7 | 16 | #include <gdb.h> |
de56407f | 17 | #include <errno.h> |
698271c7 | 18 | #include <string.h> |
529a5b0d | 19 | #include <pwd.h> |
2ad0a777 | 20 | #include <moira.h> |
45c91bf7 | 21 | #include <sys/file.h> |
4691347d | 22 | #include <fcntl.h> |
45c91bf7 | 23 | #include <sys/ioctl.h> |
698271c7 | 24 | #ifdef POSIX |
4691347d | 25 | #include <termios.h> |
26 | #endif | |
de56407f | 27 | #include "update.h" |
5df6ee25 | 28 | #include "des.h" |
de56407f | 29 | |
5df6ee25 | 30 | extern int auth_002(), xfer_002(), xfer_003(), exec_002(); |
de56407f | 31 | |
32 | extern int sync_proc(), quit(); | |
529a5b0d | 33 | extern char *config_lookup(); |
de56407f | 34 | |
35 | extern void gdb_debug(); | |
7da203a3 | 36 | extern int errno; |
de56407f | 37 | |
38 | CONNECTION conn; | |
7da203a3 | 39 | int code, log_priority; |
de56407f | 40 | char *whoami; |
41 | ||
45c91bf7 | 42 | int have_authorization = 0; |
5df6ee25 | 43 | C_Block session; |
45c91bf7 | 44 | int have_file = 0; |
45c91bf7 | 45 | int done = 0; |
529a5b0d | 46 | int uid = 0; |
45c91bf7 | 47 | |
de56407f | 48 | #define send_int(n) \ |
49 | (_send_int=(n),send_object(conn,(char *)&_send_int,INTEGER_T)) | |
50 | int _send_int; | |
51 | ||
52 | struct _dt { | |
53 | char *str; | |
54 | int (*proc)(); | |
55 | } dispatch_table[] = { | |
5df6ee25 | 56 | { "AUTH_002", auth_002 }, |
de56407f | 57 | { "XFER_002", xfer_002 }, |
b0a35414 | 58 | { "XFER_003", xfer_003 }, |
de56407f | 59 | { "EXEC_002", exec_002 }, |
60 | { "quit", quit }, | |
7da203a3 | 61 | { (char *)NULL, (int (*)())abort } |
de56407f | 62 | }; |
63 | ||
529a5b0d | 64 | /* general scratch space -- useful for building error messages et al... */ |
de56407f | 65 | char buf[BUFSIZ]; |
529a5b0d | 66 | |
de56407f | 67 | |
68 | main(argc, argv) | |
69 | int argc; | |
70 | char **argv; | |
71 | { | |
72 | STRING str; | |
73 | struct _dt *d; | |
529a5b0d | 74 | char *p; |
45c91bf7 | 75 | int n; |
de56407f | 76 | |
77 | #ifdef DEBUG | |
78 | gdb_debug(GDB_NOFORK); | |
79 | #endif /* DEBUG */ | |
80 | ||
698271c7 | 81 | whoami = strrchr(argv[0], '/'); |
de56407f | 82 | if (whoami) |
83 | whoami++; | |
84 | else | |
85 | whoami = argv[0]; | |
86 | ||
87 | /* interpret arguments here */ | |
88 | if (argc != 1) { | |
89 | fprintf(stderr, "Usage: %s\n", whoami); | |
90 | exit(1); | |
91 | } | |
de56407f | 92 | |
45c91bf7 | 93 | #ifndef DEBUG |
529a5b0d | 94 | if (!config_lookup("nofork")) { |
95 | if (fork()) | |
96 | exit(0); | |
698271c7 | 97 | #ifdef POSIX |
98 | setsid(); | |
99 | #else | |
529a5b0d | 100 | n = open("/dev/tty", O_RDWR|FNDELAY); |
101 | if (n > 0) { | |
102 | (void) ioctl(n, TIOCNOTTY, (char *)NULL); | |
103 | (void) close(n); | |
104 | } | |
698271c7 | 105 | #endif |
529a5b0d | 106 | } else |
107 | gdb_debug(GDB_NOFORK); | |
45c91bf7 | 108 | #endif |
109 | ||
110 | umask(0022); | |
0dac1a73 | 111 | initialize_sms_error_table(); |
112 | initialize_krb_error_table(); | |
2ad0a777 | 113 | mr_update_initialize(); |
de56407f | 114 | |
115 | /* wait for connection */ | |
116 | gdb_init(); | |
529a5b0d | 117 | /* If the config file contains a line "port portname", the daemon |
118 | * will listen on the named port rather than SERVICE_NAME "sms_update" | |
119 | */ | |
120 | if ((p = config_lookup("port")) == NULL) | |
121 | p = SERVICE_NAME; | |
122 | conn = create_forking_server(p, 0); | |
123 | ||
124 | /* If the config file contains a line "user username", the | |
125 | * daemon will run with that user's UID. | |
126 | */ | |
127 | if (p = config_lookup("user")) { | |
128 | struct passwd *pw; | |
129 | pw = getpwnam(p); | |
130 | if (pw == 0) { | |
131 | com_err(whoami, errno, "Unable to find user %s\n", p); | |
132 | exit(1); | |
133 | } | |
134 | uid = pw->pw_uid; | |
135 | } | |
136 | ||
137 | /* If the config file contains a line "chroot /dir/name", the | |
138 | * daemon will run chrooted to that directory. | |
139 | */ | |
140 | if (p = config_lookup("chroot")) { | |
141 | if (chroot(p) < 0) { | |
142 | com_err(whoami, errno, "unable to chroot to %s", p); | |
143 | exit(1); | |
144 | } | |
145 | } | |
146 | ||
de56407f | 147 | if (!conn) { |
529a5b0d | 148 | com_err(whoami, errno, "can't get connection"); |
149 | exit(1); | |
de56407f | 150 | } |
151 | if (connection_status(conn) == CON_STOPPED) { | |
152 | com_err(whoami, connection_errno(conn), ": can't get connection"); | |
153 | exit(1); | |
154 | } | |
155 | ||
2ad0a777 | 156 | mr_log_info("got connection"); |
de56407f | 157 | /* got a connection; loop forever */ |
158 | while (1) { | |
159 | register char *cp; | |
160 | code = receive_object(conn, (char *)&str, STRING_T); | |
161 | if (code) { | |
529a5b0d | 162 | com_err(whoami, connection_errno(conn), "receiving command"); |
163 | sever_connection(conn); | |
164 | exit(1); | |
de56407f | 165 | } |
698271c7 | 166 | cp = strchr(STRING_DATA(str), ' '); |
de56407f | 167 | if (cp) |
168 | *cp = '\0'; | |
169 | for (d = dispatch_table; d->str; d++) { | |
170 | if (!strcmp(d->str, STRING_DATA(str))) { | |
171 | if (cp) | |
172 | *cp = ' '; | |
173 | #ifdef DEBUG | |
174 | printf("got request: %s\n", STRING_DATA(str)); | |
175 | #endif /* DEBUG */ | |
176 | (void)(d->proc)(STRING_DATA(str)); | |
177 | goto ok; | |
178 | } | |
179 | } | |
180 | sprintf(buf, "unknown request received: %s\n", STRING_DATA(str)); | |
2ad0a777 | 181 | mr_log_error(buf); |
182 | code = send_int(MR_UNKNOWN_PROC); | |
de56407f | 183 | if (code) { |
529a5b0d | 184 | com_err(whoami, connection_errno(conn), "sending UNKNOWN_PROC"); |
de56407f | 185 | } |
186 | ok: | |
187 | string_free(&str); | |
188 | } | |
189 | } | |
190 | ||
191 | int | |
192 | send_ok() | |
193 | { | |
194 | static int zero = 0; | |
195 | return((code = send_object(conn, (char *)&zero, INTEGER_T))); | |
196 | } | |
45c91bf7 | 197 | |
198 | ||
199 | initialize() | |
200 | { | |
201 | /* keep have_authorization around */ | |
202 | have_file = 0; | |
45c91bf7 | 203 | done = 0; |
45c91bf7 | 204 | } |
205 | ||
206 | ||
207 | /* | |
208 | * quit request: | |
209 | * | |
210 | * syntax: | |
211 | * >>> quit | |
212 | * <<< (int)0 | |
213 | * any arguments are ignored | |
214 | * | |
215 | * function: | |
2ad0a777 | 216 | * closes connection from MR |
45c91bf7 | 217 | */ |
218 | int | |
219 | quit(str) | |
220 | char *str; | |
221 | { | |
222 | #ifdef lint | |
223 | str = (char *)NULL; | |
224 | #endif /* lint */ | |
225 | (void) send_ok(); | |
226 | sever_connection(conn); | |
2ad0a777 | 227 | mr_log_info("Closing connection."); |
7da203a3 | 228 | exit(0); |
45c91bf7 | 229 | } |
230 | ||
231 | ||
232 | /* | |
233 | * lose(msg) | |
234 | * | |
235 | * put <msg> to log as error, break connection, and exit | |
236 | */ | |
237 | ||
238 | lose(msg) | |
239 | char *msg; | |
240 | { | |
529a5b0d | 241 | com_err(whoami, code, msg); |
45c91bf7 | 242 | if (conn) |
243 | sever_connection(conn); | |
244 | exit(1); | |
245 | } | |
246 | ||
247 | /* | |
248 | * report_error(msg) | |
249 | * | |
250 | * send back (external) <code>; if error, punt big with <lose(msg)> | |
251 | */ | |
252 | ||
253 | report_error(msg) | |
254 | char *msg; | |
255 | { | |
256 | code = send_object(conn, (char *)&code, INTEGER_T); | |
257 | if (code) { | |
258 | code = connection_errno(conn); | |
259 | lose(msg); | |
260 | } | |
261 | } | |
262 | ||
263 | /* | |
264 | * reject_call(c) | |
265 | * | |
266 | * set (external) <code> to <c> and call <report_error> | |
267 | */ | |
268 | ||
269 | reject_call(c) | |
270 | int c; | |
271 | { | |
272 | code = c; | |
273 | report_error("call rejected"); | |
274 | } |