The encoding and decoding tables for Base64 are
[citadel.git] / citadel / sendcommand.c
1 /*
2  * $Id$
3  *
4  * Command-line utility to transmit a server command.
5  *
6  */
7
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <ctype.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <signal.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "citadel_ipc.h"
35 #include "server.h"
36 #include "config.h"
37
38 #define LOCKFILE "/tmp/LCK.sendcommand"
39
40 static CtdlIPC *ipc = NULL;
41
42 /*
43  * make sure only one copy of sendcommand runs at a time, using lock files
44  */
45 int set_lockfile(void)
46 {
47         FILE *lfp;
48         int onppid;
49
50         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
51                 fscanf(lfp, "%d", &onppid);
52                 fclose(lfp);
53                 if (!kill(onppid, 0) || errno == EPERM)
54                         return 1;
55         }
56         lfp = fopen(LOCKFILE, "w");
57         fprintf(lfp, "%ld\n", (long) getpid());
58         fclose(lfp);
59         return (0);
60 }
61
62 void remove_lockfile(void)
63 {
64         unlink(LOCKFILE);
65 }
66
67 /*
68  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
69  * cleanup() .  If for some reason sendcommand hangs waiting for the server
70  * to clean up, the alarm clock goes off and the program exits anyway.
71  * The cleanup() routine makes a check to ensure it's not reentering, in
72  * case the ipc module looped it somehow.
73  */
74 void nq_cleanup(int e)
75 {
76         remove_lockfile();
77         exit(e);
78 }
79
80 /*
81  * send binary to server
82  */
83 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
84 {
85         unsigned int bytes_written = 0;
86         int retval;
87 /*
88 #if defined(HAVE_OPENSSL)
89         if (ipc->ssl) {
90                 serv_write_ssl(ipc, buf, nbytes);
91                 return;
92         }
93 #endif
94 */
95         while (bytes_written < nbytes) {
96                 retval = write(ipc->sock, &buf[bytes_written],
97                                nbytes - bytes_written);
98                 if (retval < 1) {
99                         connection_died(ipc, 0);
100                         return;
101                 }
102                 bytes_written += retval;
103         }
104 }
105
106
107 void cleanup(int e)
108 {
109         static int nested = 0;
110
111         alarm(30);
112         signal(SIGALRM, nq_cleanup);
113         serv_write(ipc, "\n", 1);
114         if (nested++ < 1)
115                 CtdlIPCQuit(ipc);
116         nq_cleanup(e);
117 }
118
119 /*
120  * This is implemented as a function rather than as a macro because the
121  * client-side IPC modules expect logoff() to be defined.  They call logoff()
122  * when a problem connecting or staying connected to the server occurs.
123  */
124 void logoff(int e)
125 {
126         cleanup(e);
127 }
128
129 /*
130  * Connect sendcommand to the Citadel server running on this computer.
131  */
132 void np_attach_to_server(char *host, char *port)
133 {
134         char buf[SIZ];
135         char hostbuf[256], portbuf[256];
136         char *args[] =
137         {"sendcommand", NULL};
138         int r;
139
140         fprintf(stderr, "Attaching to server...\n");
141         strcpy(hostbuf, host);
142         strcpy(portbuf, port);
143         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
144         if (!ipc) {
145                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
146                 exit(3);
147         }
148         CtdlIPC_chat_recv(ipc, buf);
149         fprintf(stderr, "%s\n", &buf[4]);
150         snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
151         r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
152         fprintf(stderr, "%s\n", buf);
153         if (r / 100 != 2) {
154                 cleanup(2);
155         }
156 }
157
158
159 void sendcommand_die(void) {
160         exit(0);
161 }
162
163
164 /*
165  * main
166  */
167 int main(int argc, char **argv)
168 {
169         int a;
170         char cmd[SIZ];
171         char buf[SIZ];
172
173         int relh=0;
174         int home=0;
175         char relhome[PATH_MAX]="";
176         char ctdldir[PATH_MAX]=CTDLDIR;
177         fd_set read_fd;
178         struct timeval tv;
179         int ret, err;
180         int server_shutting_down = 0;
181
182         strcpy(ctdl_home_directory, DEFAULT_PORT);
183
184         strcpy(cmd, "");
185         /*
186          * Change directories if specified
187          */
188         for (a = 1; a < argc; ++a) {
189                 if (!strncmp(argv[a], "-h", 2)) {
190                         relh=argv[a][2]!='/';
191                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
192                                                                    sizeof ctdl_home_directory);
193                         else
194                                 safestrncpy(relhome, &argv[a][2],
195                                                         sizeof relhome);
196                         home_specified = 1;
197                         home=1;
198                 } else {
199                         if (!IsEmptyStr(cmd))
200                                 strcat(cmd, " ");
201                         strcat(cmd, argv[a]);
202                 }
203         }
204
205         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
206         get_config();
207
208         signal(SIGINT, cleanup);
209         signal(SIGQUIT, cleanup);
210         signal(SIGHUP, cleanup);
211         signal(SIGTERM, cleanup);
212
213         fprintf(stderr, "sendcommand: started (pid=%d) "
214                         "running in %s\n",
215                         (int) getpid(),
216                         ctdl_home_directory);
217         fflush(stderr);
218
219         alarm(5);
220         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
221         
222         np_attach_to_server(UDS, ctdl_home_directory);
223         fflush(stderr);
224         setIPCDeathHook(sendcommand_die);
225
226         fprintf(stderr, "%s\n", cmd);
227         CtdlIPC_chat_send(ipc, cmd);
228         CtdlIPC_chat_recv(ipc, buf);
229         fprintf(stderr, "%s\n", buf);
230
231         tv.tv_sec = 0;
232         tv.tv_usec = 1000;
233
234         if (!strncasecmp(&buf[1], "31", 2)) {
235                 server_shutting_down = 1;
236         }
237
238         if (buf[0] == '1') {
239                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
240                         printf("%s\n", buf);
241                         alarm(5); /* Kick the watchdog timer */
242                 }
243         } else if (buf[0] == '4') {
244                 do {
245                         if (fgets(buf, sizeof buf, stdin) == NULL)
246                                 strcpy(buf, "000");
247                         if (!IsEmptyStr(buf))
248                                 if (buf[strlen(buf) - 1] == '\n')
249                                         buf[strlen(buf) - 1] = 0;
250                         if (!IsEmptyStr(buf))
251                                 if (buf[strlen(buf) - 1] == '\r')
252                                         buf[strlen(buf) - 1] = 0;
253                         if (strcmp(buf, "000"))
254                                 CtdlIPC_chat_send(ipc, buf);
255                         
256                         FD_ZERO(&read_fd);
257                         FD_SET(ipc->sock, &read_fd);
258                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
259                         err = errno;
260                         if (err!=0)
261                                 printf("select failed: %d", err);
262
263                         if (ret == -1) {
264                                 if (!(errno == EINTR || errno == EAGAIN))
265                                         printf("select failed: %d", err);
266                                 return 1;
267                         }
268
269                         if (ret != 0) {
270                                 size_t n;
271                                 char rbuf[SIZ];
272
273                                 rbuf[0] = '\0';
274                                 n = read(ipc->sock, rbuf, SIZ);
275                                 if (n>0) {
276                                         rbuf[n]='\0';
277                                         fprintf (stderr, rbuf);
278                                         fflush (stdout);
279                                 }
280                         }
281                         alarm(5); /* Kick the watchdog timer */
282                 } while (strcmp(buf, "000"));
283                 CtdlIPC_chat_send(ipc, "\n");
284                 CtdlIPC_chat_send(ipc, "000");
285         }
286         alarm(0);       /* Shutdown the watchdog timer */
287         fprintf(stderr, "sendcommand: processing ended.\n");
288
289         /* Clean up and log off ... unless the server indicated that the command
290          * we sent is shutting it down, in which case we want to just cut the
291          * connection and exit.
292          */
293         if (server_shutting_down) {
294                 nq_cleanup(0);
295         }
296         else {
297                 cleanup(0);
298         }
299         return 0;
300 }