]> andersk Git - moira.git/blob - update/update_server.c
Switch from Imake-based build system to autoconf-based.
[moira.git] / update / update_server.c
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>.
6  */
7
8 #include <mit-copyright.h>
9 #include <moira.h>
10 #include "update_server.h"
11
12 #include <sys/stat.h>
13 #include <sys/utsname.h>
14
15 #include <netinet/in.h>
16 #include <arpa/inet.h>
17
18 #include <errno.h>
19 #include <pwd.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <des.h>
26 #include "update.h"
27
28 RCSID("$Header$");
29
30 char *whoami, *hostname;
31
32 int have_authorization = 0;
33 des_cblock session;
34 int uid = 0;
35
36 struct _dt {
37   char *str;
38   void (*proc)(int, char *);
39 } dispatch_table[] = {
40   { "AUTH_002", auth_002 },
41   { "XFER_002", xfer_002 },
42   { "XFER_003", xfer_003 },
43   { "EXEC_002", exec_002 },
44   { "quit", quit },
45   { NULL, (void (*)(int, char *))abort }
46 };
47
48 int main(int argc, char **argv)
49 {
50   char *str, *p;
51   size_t len;
52   struct _dt *d;
53   struct utsname name;
54   int s, conn;
55
56   whoami = strrchr(argv[0], '/');
57   if (whoami)
58     whoami++;
59   else
60     whoami = argv[0];
61
62   /* interpret arguments here */
63   if (argc != 1)
64     {
65       fprintf(stderr, "Usage:  %s\n", whoami);
66       exit(1);
67     }
68
69   if (!config_lookup("nofork"))
70     {
71       if (fork())
72         exit(0);
73       setsid();
74     }
75
76   uname(&name);
77   hostname = name.nodename;
78
79   umask(0022);
80   mr_init();
81
82   /* If the config file contains a line "user username", the
83    * daemon will run with that user's UID.
84    */
85   if ((p = config_lookup("user")))
86     {
87       struct passwd *pw;
88       pw = getpwnam(p);
89       if (!pw)
90         {
91           com_err(whoami, errno, "Unable to find user %s\n", p);
92           exit(1);
93         }
94       uid = pw->pw_uid;
95     }
96
97   /* If the config file contains a line "port portname", the daemon
98    * will listen on the named port rather than SERVICE_NAME ("moira_update")
99    */
100   if (!(p = config_lookup("port")))
101     p = SERVICE_NAME;
102
103   s = mr_listen(p);
104   if (s == -1)
105     {
106       com_err(whoami, errno, "creating listening socket");
107       exit(1);
108     }
109
110   /* now loop waiting for connections */
111   while (1)
112     {
113       struct sockaddr_in client;
114       long len;
115       char *buf;
116
117       conn = mr_accept(s, &client);
118       if (conn == -1)
119         {
120           com_err(whoami, errno, "accepting on listening socket");
121           exit(1);
122         }
123       else if (conn == 0)
124         continue;
125
126       if (config_lookup("nofork") || (fork() <= 0))
127         break;
128     }
129
130   /* If the config file contains a line "chroot /dir/name", the
131    * daemon will run chrooted to that directory.
132    */
133   if ((p = config_lookup("chroot")))
134     {
135       if (chroot(p) < 0)
136         {
137           com_err(whoami, errno, "unable to chroot to %s", p);
138           exit(1);
139         }
140     }
141
142   com_err(whoami, 0, "got connection");
143
144   while (1)
145     {
146       char *cp, *str;
147       size_t len;
148       int code;
149
150       code = recv_string(conn, &str, &len);
151       if (code)
152         {
153           com_err(whoami, code, "receiving command");
154           close(conn);
155           exit(1);
156         }
157
158       cp = strchr(str, ' ');
159       if (cp)
160         *cp = '\0';
161       for (d = dispatch_table; d->str; d++)
162         {
163           if (!strcmp(d->str, str))
164             {
165               if (cp)
166                 *cp = ' ';
167               (d->proc)(conn, str);
168               goto ok;
169             }
170         }
171       com_err(whoami, 0, "unknown request received: %s", str);
172       code = send_int(conn, MR_UNKNOWN_PROC);
173       if (code)
174         com_err(whoami, code, "sending UNKNOWN_PROC");
175     ok:
176       free(str);
177     }
178 }
179
180 int send_ok(int conn)
181 {
182   return send_int(conn, 0);
183 }
184
185 /*
186  * quit request:
187  *
188  * syntax:
189  * >>> quit
190  * <<< (int)0
191  * any arguments are ignored
192  *
193  * function:
194  *      closes connection from MR
195  */
196
197 void quit(int conn, char *str)
198 {
199   send_ok(conn);
200   close(conn);
201   com_err(whoami, 0, "Closing connection.");
202   exit(0);
203 }
204
205 void fail(int conn, int err, char *msg)
206 {
207   com_err(whoami, err, msg);
208   close(conn);
209   exit(1);
210 }
This page took 0.068284 seconds and 5 git commands to generate.