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>
33 char *whoami, *hostname;
35 int have_authorization = 0;
39 void child_handler(int signal);
40 static void syslog_com_err_proc(const char *progname, long code,
41 const char *fmt, va_list args);
45 void (*proc)(int, char *);
46 } dispatch_table[] = {
47 { "AUTH_002", auth_002 },
48 { "AUTH_003", auth_003 },
49 { "XFER_002", xfer_002 },
50 { "XFER_003", xfer_003 },
51 { "EXEC_002", exec_002 },
53 { NULL, (void (*)(int, char *))abort }
56 int main(int argc, char **argv)
65 whoami = strrchr(argv[0], '/');
71 /* interpret arguments here */
74 fprintf(stderr, "Usage: %s\n", whoami);
78 if (!config_lookup("nofork"))
86 hostname = name.nodename;
91 sigemptyset(&sa.sa_mask);
92 sa.sa_flags = SA_RESTART;
93 sa.sa_handler = child_handler;
94 sigaction(SIGCHLD, &sa, NULL);
96 /* If the config file contains a line "user username", the
97 * daemon will run with that user's UID.
99 if ((p = config_lookup("user")))
105 com_err(whoami, errno, "Unable to find user %s\n", p);
111 /* If the config file contains a line "port portname", the daemon
112 * will listen on the named port rather than SERVICE_NAME ("moira_update")
114 if (!(p = config_lookup("port")))
120 com_err(whoami, errno, "creating listening socket");
124 set_com_err_hook(syslog_com_err_proc);
125 openlog(whoami, LOG_PID, LOG_DAEMON);
127 /* now loop waiting for connections */
130 struct sockaddr_in client;
134 conn = mr_accept(s, &client);
137 com_err(whoami, errno, "accepting on listening socket");
143 if (config_lookup("nofork") || (fork() <= 0))
149 /* If the config file contains a line "chroot /dir/name", the
150 * daemon will run chrooted to that directory.
152 if ((p = config_lookup("chroot")))
156 com_err(whoami, errno, "unable to chroot to %s", p);
161 com_err(whoami, 0, "got connection");
169 code = recv_string(conn, &str, &len);
172 com_err(whoami, code, "receiving command");
177 cp = strchr(str, ' ');
180 for (d = dispatch_table; d->str; d++)
182 if (!strcmp(d->str, str))
186 (d->proc)(conn, str);
190 com_err(whoami, 0, "unknown request received: %s", str);
191 code = send_int(conn, MR_UNKNOWN_PROC);
193 com_err(whoami, code, "sending UNKNOWN_PROC");
199 int send_ok(int conn)
201 return send_int(conn, 0);
210 * any arguments are ignored
213 * closes connection from MR
216 void quit(int conn, char *str)
220 com_err(whoami, 0, "Closing connection.");
224 void fail(int conn, int err, char *msg)
226 com_err(whoami, err, msg);
231 void child_handler(int signal)
235 while (waitpid(-1, &status, WNOHANG) > 0)
239 static void syslog_com_err_proc(const char *progname, long code,
240 const char *fmt, va_list args)
242 char buf[BUFSIZ + 1];
246 vsnprintf(buf, BUFSIZ, fmt, args);
247 syslog(LOG_NOTICE, "%s: %s %s", progname ? progname : "",
248 code ? error_message(code) : "", buf);