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