]> andersk Git - moira.git/blame - update/update_server.c
No debugging printf.
[moira.git] / update / update_server.c
CommitLineData
7ac48069 1/* $Id$
2 *
3 * Copyright 1988-1998 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
5 * <mit-copyright.h>.
de56407f 6 */
de56407f 7
546bc43b 8#include <mit-copyright.h>
7ac48069 9#include <moira.h>
10#include "update_server.h"
11
12#include <sys/stat.h>
85330553 13#include <sys/utsname.h>
cf0ec460 14#include <sys/wait.h>
7ac48069 15
85330553 16#include <netinet/in.h>
ea0caf4a 17#include <arpa/inet.h>
85330553 18
19#include <errno.h>
7ac48069 20#include <pwd.h>
cf0ec460 21#include <signal.h>
de56407f 22#include <stdio.h>
7da203a3 23#include <stdlib.h>
698271c7 24#include <string.h>
7ac48069 25#include <unistd.h>
9d3d51ad 26#include <syslog.h>
de56407f 27
7ac48069 28#include <des.h>
7ac48069 29#include "update.h"
de56407f 30
7ac48069 31RCSID("$Header$");
de56407f 32
85330553 33char *whoami, *hostname;
de56407f 34
45c91bf7 35int have_authorization = 0;
85330553 36des_cblock session;
529a5b0d 37int uid = 0;
45c91bf7 38
cf0ec460 39void child_handler(int signal);
9d3d51ad 40static void syslog_com_err_proc(const char *progname, long code,
41 const char *fmt, va_list args);
cf0ec460 42
de56407f 43struct _dt {
5eaef520 44 char *str;
85330553 45 void (*proc)(int, char *);
de56407f 46} dispatch_table[] = {
5eaef520 47 { "AUTH_002", auth_002 },
991417e4 48 { "AUTH_003", auth_003 },
5eaef520 49 { "XFER_002", xfer_002 },
50 { "XFER_003", xfer_003 },
51 { "EXEC_002", exec_002 },
52 { "quit", quit },
85330553 53 { NULL, (void (*)(int, char *))abort }
de56407f 54};
55
5eaef520 56int main(int argc, char **argv)
de56407f 57{
85330553 58 char *str, *p;
59 size_t len;
5eaef520 60 struct _dt *d;
85330553 61 struct utsname name;
62 int s, conn;
cf0ec460 63 struct sigaction sa;
5eaef520 64
65 whoami = strrchr(argv[0], '/');
66 if (whoami)
67 whoami++;
68 else
69 whoami = argv[0];
70
71 /* interpret arguments here */
72 if (argc != 1)
73 {
74 fprintf(stderr, "Usage: %s\n", whoami);
75 exit(1);
76 }
77
78 if (!config_lookup("nofork"))
79 {
80 if (fork())
81 exit(0);
82 setsid();
83 }
5eaef520 84
85330553 85 uname(&name);
86 hostname = name.nodename;
5eaef520 87
85330553 88 umask(0022);
89 mr_init();
5eaef520 90
cf0ec460 91 sigemptyset(&sa.sa_mask);
92 sa.sa_flags = SA_RESTART;
93 sa.sa_handler = child_handler;
94 sigaction(SIGCHLD, &sa, NULL);
95
5eaef520 96 /* If the config file contains a line "user username", the
97 * daemon will run with that user's UID.
98 */
99 if ((p = config_lookup("user")))
100 {
101 struct passwd *pw;
102 pw = getpwnam(p);
103 if (!pw)
104 {
105 com_err(whoami, errno, "Unable to find user %s\n", p);
de56407f 106 exit(1);
5eaef520 107 }
108 uid = pw->pw_uid;
109 }
de56407f 110
85330553 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")
113 */
114 if (!(p = config_lookup("port")))
115 p = SERVICE_NAME;
116
117 s = mr_listen(p);
118 if (s == -1)
119 {
120 com_err(whoami, errno, "creating listening socket");
121 exit(1);
122 }
123
9d3d51ad 124 set_com_err_hook(syslog_com_err_proc);
125 openlog(whoami, LOG_PID, LOG_DAEMON);
126
85330553 127 /* now loop waiting for connections */
128 while (1)
129 {
130 struct sockaddr_in client;
131 long len;
132 char *buf;
133
134 conn = mr_accept(s, &client);
135 if (conn == -1)
136 {
137 com_err(whoami, errno, "accepting on listening socket");
138 exit(1);
139 }
140 else if (conn == 0)
141 continue;
142
143 if (config_lookup("nofork") || (fork() <= 0))
144 break;
bcb7d888 145
146 close(conn);
85330553 147 }
148
5eaef520 149 /* If the config file contains a line "chroot /dir/name", the
150 * daemon will run chrooted to that directory.
151 */
152 if ((p = config_lookup("chroot")))
153 {
154 if (chroot(p) < 0)
155 {
156 com_err(whoami, errno, "unable to chroot to %s", p);
157 exit(1);
158 }
159 }
45c91bf7 160
85330553 161 com_err(whoami, 0, "got connection");
5eaef520 162
5eaef520 163 while (1)
164 {
85330553 165 char *cp, *str;
166 size_t len;
167 int code;
168
169 code = recv_string(conn, &str, &len);
5eaef520 170 if (code)
171 {
85330553 172 com_err(whoami, code, "receiving command");
173 close(conn);
5eaef520 174 exit(1);
175 }
85330553 176
177 cp = strchr(str, ' ');
5eaef520 178 if (cp)
179 *cp = '\0';
180 for (d = dispatch_table; d->str; d++)
181 {
85330553 182 if (!strcmp(d->str, str))
5eaef520 183 {
184 if (cp)
185 *cp = ' ';
85330553 186 (d->proc)(conn, str);
5eaef520 187 goto ok;
188 }
189 }
85330553 190 com_err(whoami, 0, "unknown request received: %s", str);
191 code = send_int(conn, MR_UNKNOWN_PROC);
5eaef520 192 if (code)
85330553 193 com_err(whoami, code, "sending UNKNOWN_PROC");
5eaef520 194 ok:
85330553 195 free(str);
5eaef520 196 }
de56407f 197}
198
85330553 199int send_ok(int conn)
45c91bf7 200{
85330553 201 return send_int(conn, 0);
45c91bf7 202}
203
45c91bf7 204/*
205 * quit request:
206 *
207 * syntax:
208 * >>> quit
209 * <<< (int)0
210 * any arguments are ignored
211 *
212 * function:
2ad0a777 213 * closes connection from MR
45c91bf7 214 */
85330553 215
216void quit(int conn, char *str)
45c91bf7 217{
85330553 218 send_ok(conn);
219 close(conn);
220 com_err(whoami, 0, "Closing connection.");
5eaef520 221 exit(0);
45c91bf7 222}
223
85330553 224void fail(int conn, int err, char *msg)
45c91bf7 225{
85330553 226 com_err(whoami, err, msg);
227 close(conn);
5eaef520 228 exit(1);
45c91bf7 229}
cf0ec460 230
231void child_handler(int signal)
232{
233 int status;
234
235 while (waitpid(-1, &status, WNOHANG) > 0)
236 ;
237}
9d3d51ad 238
239static void syslog_com_err_proc(const char *progname, long code,
240 const char *fmt, va_list args)
241{
995c29c0 242 char buf[BUFSIZ + 1];
9d3d51ad 243
995c29c0 244 buf[BUFSIZ] = '\0';
9d3d51ad 245
995c29c0 246 vsnprintf(buf, BUFSIZ, fmt, args);
908337d2 247 syslog(LOG_NOTICE, "%s: %s %s", progname ? progname : "",
248 code ? error_message(code) : "", buf);
9d3d51ad 249}
This page took 0.155032 seconds and 5 git commands to generate.