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