3 * Copyright 1988-1998 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
8 #include <mit-copyright.h>
10 #include "update_server.h"
13 #include <sys/utsname.h>
16 #include <netinet/in.h>
17 #include <arpa/inet.h>
32 char *whoami, *hostname;
34 int have_authorization = 0;
38 void child_handler(int signal);
42 void (*proc)(int, char *);
43 } dispatch_table[] = {
44 { "AUTH_002", auth_002 },
45 { "XFER_002", xfer_002 },
46 { "XFER_003", xfer_003 },
47 { "EXEC_002", exec_002 },
49 { NULL, (void (*)(int, char *))abort }
52 int main(int argc, char **argv)
61 whoami = strrchr(argv[0], '/');
67 /* interpret arguments here */
70 fprintf(stderr, "Usage: %s\n", whoami);
74 if (!config_lookup("nofork"))
82 hostname = name.nodename;
87 sigemptyset(&sa.sa_mask);
88 sa.sa_flags = SA_RESTART;
89 sa.sa_handler = child_handler;
90 sigaction(SIGCHLD, &sa, NULL);
92 /* If the config file contains a line "user username", the
93 * daemon will run with that user's UID.
95 if ((p = config_lookup("user")))
101 com_err(whoami, errno, "Unable to find user %s\n", p);
107 /* If the config file contains a line "port portname", the daemon
108 * will listen on the named port rather than SERVICE_NAME ("moira_update")
110 if (!(p = config_lookup("port")))
116 com_err(whoami, errno, "creating listening socket");
120 /* now loop waiting for connections */
123 struct sockaddr_in client;
127 conn = mr_accept(s, &client);
130 com_err(whoami, errno, "accepting on listening socket");
136 if (config_lookup("nofork") || (fork() <= 0))
142 /* If the config file contains a line "chroot /dir/name", the
143 * daemon will run chrooted to that directory.
145 if ((p = config_lookup("chroot")))
149 com_err(whoami, errno, "unable to chroot to %s", p);
154 com_err(whoami, 0, "got connection");
162 code = recv_string(conn, &str, &len);
165 com_err(whoami, code, "receiving command");
170 cp = strchr(str, ' ');
173 for (d = dispatch_table; d->str; d++)
175 if (!strcmp(d->str, str))
179 (d->proc)(conn, str);
183 com_err(whoami, 0, "unknown request received: %s", str);
184 code = send_int(conn, MR_UNKNOWN_PROC);
186 com_err(whoami, code, "sending UNKNOWN_PROC");
192 int send_ok(int conn)
194 return send_int(conn, 0);
203 * any arguments are ignored
206 * closes connection from MR
209 void quit(int conn, char *str)
213 com_err(whoami, 0, "Closing connection.");
217 void fail(int conn, int err, char *msg)
219 com_err(whoami, err, msg);
224 void child_handler(int signal)
228 while (waitpid(-1, &status, WNOHANG) > 0)