]> andersk Git - moira.git/blob - update/update_server.c
New database and column names for Moira2.
[moira.git] / update / update_server.c
1 /*
2  *      $Source$
3  *      $Header$
4  */
5 /*  (c) Copyright 1988 by the Massachusetts Institute of Technology. */
6 /*  For copying and distribution information, please see the file */
7 /*  <mit-copyright.h>. */
8
9 #ifndef lint
10 static char *rcsid_dispatch_c = "$Header$";
11 #endif  lint
12
13 #include <mit-copyright.h>
14 #include <stdio.h>
15 #include <gdb.h>
16 #include <errno.h>
17 #include <strings.h>
18 #include <pwd.h>
19 #include <moira.h>
20 #include <sys/file.h>
21 #include <sys/ioctl.h>
22 #include "update.h"
23
24 extern int auth_001(), inst_001();
25 extern int xfer_002(), exec_002();
26
27 extern int sync_proc(), quit();
28 extern char *config_lookup();
29
30 extern void gdb_debug();
31 extern int exit(), abort(), errno;
32 extern STRING instructions;
33
34 CONNECTION conn;
35 int code;
36 char *whoami;
37
38 int have_authorization = 0;
39 int have_file = 0;
40 int have_instructions = 0;
41 int done = 0;
42 int uid = 0;
43
44 #define send_int(n) \
45      (_send_int=(n),send_object(conn,(char *)&_send_int,INTEGER_T))
46 int _send_int;
47
48 struct _dt {
49      char *str;
50      int (*proc)();
51 } dispatch_table[] = {
52      { "INST_001", inst_001 },
53      { "AUTH_001", auth_001 },
54      { "XFER_002", xfer_002 },
55      { "EXEC_002", exec_002 },
56      { "quit", quit },
57      { (char *)NULL, abort }
58 };
59
60 /* general scratch space -- useful for building error messages et al... */
61 char buf[BUFSIZ];
62
63
64 main(argc, argv)
65      int argc;
66      char **argv;
67 {
68      STRING str;
69      struct _dt *d;
70      char *p;
71      int n;
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      }
88
89 #ifndef DEBUG
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);
100 #endif
101
102      umask(0022);
103      initialize_sms_error_table();
104      initialize_krb_error_table();
105      mr_update_initialize();
106
107      /* wait for connection */
108      gdb_init();
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
139      if (!conn) {
140          com_err(whoami, errno, "can't get connection");
141          exit(1);
142      }
143      if (connection_status(conn) == CON_STOPPED) {
144          com_err(whoami, connection_errno(conn), ": can't get connection");
145          exit(1);
146      }
147
148      mr_log_info("got connection");
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) {
154               com_err(whoami, connection_errno(conn), "receiving command");
155               sever_connection(conn);
156               exit(1);
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));
173           mr_log_error(buf);
174           code = send_int(MR_UNKNOWN_PROC);
175           if (code) {
176               com_err(whoami, connection_errno(conn), "sending UNKNOWN_PROC");
177           }
178      ok:
179           string_free(&str);
180      }
181 }
182
183 int
184 send_ok()
185 {
186      static int zero = 0;
187      return((code = send_object(conn, (char *)&zero, INTEGER_T)));
188 }
189
190
191 initialize()
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:
211  *      closes connection from MR
212  */
213 int
214 quit(str)
215      char *str;
216 {
217 #ifdef lint
218      str = (char *)NULL;
219 #endif /* lint */
220      (void) send_ok();
221      sever_connection(conn);
222      mr_log_info("Closing connection.");
223      exit(0);
224 }
225
226
227 /*
228  * lose(msg)
229  *
230  * put <msg> to log as error, break connection, and exit
231  */
232
233 lose(msg)
234     char *msg;
235 {
236     com_err(whoami, code, msg);
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
248 report_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
264 reject_call(c)
265     int c;
266 {
267     code = c;
268     report_error("call rejected");
269 }
This page took 0.054455 seconds and 5 git commands to generate.