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>
35 char *whoami, *hostname;
37 int have_authorization = 0;
43 void child_handler(int signal);
44 static void syslog_com_err_proc(const char *progname, long code,
45 const char *fmt, va_list args);
49 void (*proc)(int, char *);
50 } dispatch_table[] = {
51 { "AUTH_002", auth_002 },
52 { "AUTH_003", auth_003 },
53 { "XFER_002", xfer_002 },
54 { "XFER_003", xfer_003 },
55 { "EXEC_002", exec_002 },
57 { NULL, (void (*)(int, char *))abort }
60 int main(int argc, char **argv)
69 whoami = strrchr(argv[0], '/');
75 /* interpret arguments here */
78 fprintf(stderr, "Usage: %s\n", whoami);
82 if (!config_lookup("nofork"))
90 hostname = name.nodename;
95 sigemptyset(&sa.sa_mask);
96 sa.sa_flags = SA_RESTART;
97 sa.sa_handler = child_handler;
98 sigaction(SIGCHLD, &sa, NULL);
100 /* If the config file contains a line "user username", the
101 * daemon will run with that user's UID.
103 if ((p = config_lookup("user")))
109 com_err(whoami, errno, "Unable to find user %s\n", p);
115 /* If the config file contains a line "port portname", the daemon
116 * will listen on the named port rather than SERVICE_NAME ("moira_update")
118 if (!(p = config_lookup("port")))
124 com_err(whoami, errno, "creating listening socket");
128 set_com_err_hook(syslog_com_err_proc);
129 openlog(whoami, LOG_PID, LOG_DAEMON);
131 /* now loop waiting for connections */
134 struct sockaddr_in client;
138 conn = mr_accept(s, &client);
141 com_err(whoami, errno, "accepting on listening socket");
147 if (config_lookup("nofork") || (fork() <= 0))
153 /* If the config file contains a line "chroot /dir/name", the
154 * daemon will run chrooted to that directory.
156 if ((p = config_lookup("chroot")))
160 com_err(whoami, errno, "unable to chroot to %s", p);
165 com_err(whoami, 0, "got connection");
173 code = recv_string(conn, &str, &len);
176 com_err(whoami, code, "receiving command");
181 cp = strchr(str, ' ');
184 for (d = dispatch_table; d->str; d++)
186 if (!strcmp(d->str, str))
190 (d->proc)(conn, str);
194 com_err(whoami, 0, "unknown request received: %s", str);
195 code = send_int(conn, MR_UNKNOWN_PROC);
197 com_err(whoami, code, "sending UNKNOWN_PROC");
203 int send_ok(int conn)
205 return send_int(conn, 0);
214 * any arguments are ignored
217 * closes connection from MR
220 void quit(int conn, char *str)
224 com_err(whoami, 0, "Closing connection.");
228 void fail(int conn, int err, char *msg)
230 com_err(whoami, err, msg);
235 void child_handler(int signal)
239 while (waitpid(-1, &status, WNOHANG) > 0)
243 static void syslog_com_err_proc(const char *progname, long code,
244 const char *fmt, va_list args)
246 char buf[BUFSIZ + 1];
250 vsnprintf(buf, BUFSIZ, fmt, args);
251 syslog(LOG_NOTICE, "%s: %s %s", progname ? progname : "",
252 code ? error_message(code) : "", buf);