]> andersk Git - moira.git/blame - update/update_server.c
We allow for .root instances to be counted as system users;
[moira.git] / update / update_server.c
CommitLineData
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
10static char *rcsid_dispatch_c = "$Header$";
11#endif lint
12
546bc43b 13#include <mit-copyright.h>
de56407f 14#include <stdio.h>
45c91bf7 15#include <gdb.h>
de56407f 16#include <errno.h>
17#include <strings.h>
529a5b0d 18#include <pwd.h>
2ad0a777 19#include <moira.h>
45c91bf7 20#include <sys/file.h>
4691347d 21#include <fcntl.h>
45c91bf7 22#include <sys/ioctl.h>
4691347d 23#ifdef SOLARIS
24#include <termios.h>
25#endif
de56407f 26#include "update.h"
de56407f 27
45c91bf7 28extern int auth_001(), inst_001();
b0a35414 29extern int xfer_002(), xfer_003(), exec_002();
de56407f 30
31extern int sync_proc(), quit();
529a5b0d 32extern char *config_lookup();
de56407f 33
34extern void gdb_debug();
4691347d 35extern int abort(), errno;
36#ifndef sun
37extern int exit();
38#endif
45c91bf7 39extern STRING instructions;
de56407f 40
41CONNECTION conn;
42int code;
43char *whoami;
44
45c91bf7 45int have_authorization = 0;
46int have_file = 0;
47int have_instructions = 0;
48int done = 0;
529a5b0d 49int uid = 0;
45c91bf7 50
de56407f 51#define send_int(n) \
52 (_send_int=(n),send_object(conn,(char *)&_send_int,INTEGER_T))
53int _send_int;
54
55struct _dt {
56 char *str;
57 int (*proc)();
58} dispatch_table[] = {
59 { "INST_001", inst_001 },
60 { "AUTH_001", auth_001 },
de56407f 61 { "XFER_002", xfer_002 },
b0a35414 62 { "XFER_003", xfer_003 },
de56407f 63 { "EXEC_002", exec_002 },
64 { "quit", quit },
65 { (char *)NULL, abort }
66};
67
529a5b0d 68/* general scratch space -- useful for building error messages et al... */
de56407f 69char buf[BUFSIZ];
529a5b0d 70
de56407f 71
72main(argc, argv)
73 int argc;
74 char **argv;
75{
76 STRING str;
77 struct _dt *d;
529a5b0d 78 char *p;
45c91bf7 79 int n;
de56407f 80
81#ifdef DEBUG
82 gdb_debug(GDB_NOFORK);
83#endif /* DEBUG */
84
85 whoami = rindex(argv[0], '/');
86 if (whoami)
87 whoami++;
88 else
89 whoami = argv[0];
90
91 /* interpret arguments here */
92 if (argc != 1) {
93 fprintf(stderr, "Usage: %s\n", whoami);
94 exit(1);
95 }
de56407f 96
45c91bf7 97#ifndef DEBUG
529a5b0d 98 if (!config_lookup("nofork")) {
99 if (fork())
100 exit(0);
101 n = open("/dev/tty", O_RDWR|FNDELAY);
102 if (n > 0) {
103 (void) ioctl(n, TIOCNOTTY, (char *)NULL);
104 (void) close(n);
105 }
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 }
166 cp = index(STRING_DATA(str), ' ');
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
191int
192send_ok()
193{
194 static int zero = 0;
195 return((code = send_object(conn, (char *)&zero, INTEGER_T)));
196}
45c91bf7 197
198
199initialize()
200{
201 /* keep have_authorization around */
202 have_file = 0;
203 have_instructions = 0;
204 done = 0;
205 if (STRING_DATA(instructions) != (char *)NULL)
206 string_free(&instructions);
207}
208
209
210/*
211 * quit request:
212 *
213 * syntax:
214 * >>> quit
215 * <<< (int)0
216 * any arguments are ignored
217 *
218 * function:
2ad0a777 219 * closes connection from MR
45c91bf7 220 */
221int
222quit(str)
223 char *str;
224{
225#ifdef lint
226 str = (char *)NULL;
227#endif /* lint */
228 (void) send_ok();
229 sever_connection(conn);
2ad0a777 230 mr_log_info("Closing connection.");
b0a35414 231 return(exit(0));
45c91bf7 232}
233
234
235/*
236 * lose(msg)
237 *
238 * put <msg> to log as error, break connection, and exit
239 */
240
241lose(msg)
242 char *msg;
243{
529a5b0d 244 com_err(whoami, code, msg);
45c91bf7 245 if (conn)
246 sever_connection(conn);
247 exit(1);
248}
249
250/*
251 * report_error(msg)
252 *
253 * send back (external) <code>; if error, punt big with <lose(msg)>
254 */
255
256report_error(msg)
257 char *msg;
258{
259 code = send_object(conn, (char *)&code, INTEGER_T);
260 if (code) {
261 code = connection_errno(conn);
262 lose(msg);
263 }
264}
265
266/*
267 * reject_call(c)
268 *
269 * set (external) <code> to <c> and call <report_error>
270 */
271
272reject_call(c)
273 int c;
274{
275 code = c;
276 report_error("call rejected");
277}
This page took 0.198043 seconds and 5 git commands to generate.