]> andersk Git - moira.git/blob - update/update_server.c
Solaris changes
[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 <fcntl.h>
22 #include <sys/ioctl.h>
23 #ifdef SOLARIS
24 #include <termios.h>
25 #endif
26 #include "update.h"
27
28 extern int auth_001(), inst_001();
29 extern int xfer_002(), xfer_003(), exec_002();
30
31 extern int sync_proc(), quit();
32 extern char *config_lookup();
33
34 extern void gdb_debug();
35 extern int abort(), errno;
36 #ifndef sun
37 extern int exit();
38 #endif
39 extern STRING instructions;
40
41 CONNECTION conn;
42 int code;
43 char *whoami;
44
45 int have_authorization = 0;
46 int have_file = 0;
47 int have_instructions = 0;
48 int done = 0;
49 int uid = 0;
50
51 #define send_int(n) \
52      (_send_int=(n),send_object(conn,(char *)&_send_int,INTEGER_T))
53 int _send_int;
54
55 struct _dt {
56      char *str;
57      int (*proc)();
58 } dispatch_table[] = {
59      { "INST_001", inst_001 },
60      { "AUTH_001", auth_001 },
61      { "XFER_002", xfer_002 },
62      { "XFER_003", xfer_003 },
63      { "EXEC_002", exec_002 },
64      { "quit", quit },
65      { (char *)NULL, abort }
66 };
67
68 /* general scratch space -- useful for building error messages et al... */
69 char buf[BUFSIZ];
70
71
72 main(argc, argv)
73      int argc;
74      char **argv;
75 {
76      STRING str;
77      struct _dt *d;
78      char *p;
79      int n;
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      }
96
97 #ifndef DEBUG
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);
108 #endif
109
110      umask(0022);
111      initialize_sms_error_table();
112      initialize_krb_error_table();
113      mr_update_initialize();
114
115      /* wait for connection */
116      gdb_init();
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
147      if (!conn) {
148          com_err(whoami, errno, "can't get connection");
149          exit(1);
150      }
151      if (connection_status(conn) == CON_STOPPED) {
152          com_err(whoami, connection_errno(conn), ": can't get connection");
153          exit(1);
154      }
155
156      mr_log_info("got connection");
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) {
162               com_err(whoami, connection_errno(conn), "receiving command");
163               sever_connection(conn);
164               exit(1);
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));
181           mr_log_error(buf);
182           code = send_int(MR_UNKNOWN_PROC);
183           if (code) {
184               com_err(whoami, connection_errno(conn), "sending UNKNOWN_PROC");
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 }
197
198
199 initialize()
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:
219  *      closes connection from MR
220  */
221 int
222 quit(str)
223      char *str;
224 {
225 #ifdef lint
226      str = (char *)NULL;
227 #endif /* lint */
228      (void) send_ok();
229      sever_connection(conn);
230      mr_log_info("Closing connection.");
231      return(exit(0));
232 }
233
234
235 /*
236  * lose(msg)
237  *
238  * put <msg> to log as error, break connection, and exit
239  */
240
241 lose(msg)
242     char *msg;
243 {
244     com_err(whoami, code, msg);
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
256 report_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
272 reject_call(c)
273     int c;
274 {
275     code = c;
276     report_error("call rejected");
277 }
This page took 0.056271 seconds and 5 git commands to generate.