sendcommand now uses the admin socket, eliminating any need to touch the config file.
[citadel.git] / citadel / utils / sendcommand.c
1 /*
2  * Command-line utility to transmit a server command.
3  *
4  * Copyright (c) 1987-2012 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/wait.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <signal.h>
24 #include <errno.h>
25 #include <limits.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include "citadel.h"
29
30
31
32 int serv_sock = (-1);
33
34
35 int uds_connectsock(char *sockpath)
36 {
37         int s;
38         struct sockaddr_un addr;
39
40         memset(&addr, 0, sizeof(addr));
41         addr.sun_family = AF_UNIX;
42         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
43
44         s = socket(AF_UNIX, SOCK_STREAM, 0);
45         if (s < 0) {
46                 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
47                 exit(3);
48         }
49
50         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
51                 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
52                 close(s);
53                 exit(3);
54         }
55
56         return s;
57 }
58
59
60 /*
61  * input binary data from socket
62  */
63 void serv_read(char *buf, int bytes)
64 {
65         int len, rlen;
66
67         len = 0;
68         while (len < bytes) {
69                 rlen = read(serv_sock, &buf[len], bytes - len);
70                 if (rlen < 1) {
71                         return;
72                 }
73                 len = len + rlen;
74         }
75 }
76
77
78 /*
79  * send binary to server
80  */
81 void serv_write(char *buf, int nbytes)
82 {
83         int bytes_written = 0;
84         int retval;
85         while (bytes_written < nbytes) {
86                 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
87                 if (retval < 1) {
88                         return;
89                 }
90                 bytes_written = bytes_written + retval;
91         }
92 }
93
94
95
96 /*
97  * input string from socket - implemented in terms of serv_read()
98  */
99 void serv_gets(char *buf)
100 {
101         int i;
102
103         /* Read one character at a time.
104          */
105         for (i = 0;; i++) {
106                 serv_read(&buf[i], 1);
107                 if (buf[i] == '\n' || i == (SIZ-1))
108                         break;
109         }
110
111         /* If we got a long line, discard characters until the newline.
112          */
113         if (i == (SIZ-1)) {
114                 while (buf[i] != '\n') {
115                         serv_read(&buf[i], 1);
116                 }
117         }
118
119         /* Strip all trailing nonprintables (crlf)
120          */
121         buf[i] = 0;
122 }
123
124
125 /*
126  * send line to server - implemented in terms of serv_write()
127  */
128 void serv_puts(char *buf)
129 {
130         serv_write(buf, strlen(buf));
131         serv_write("\n", 1);
132 }
133
134
135
136
137 /*
138  * Main loop.  Do things and have fun.
139  */
140 int main(int argc, char **argv)
141 {
142         int a;
143         int watchdog = 60;
144         char *ctdl_home_directory = CTDLDIR;
145         char buf[SIZ];
146         int xfermode = 0;
147
148         /* Parse command line */
149         while ((a = getopt(argc, argv, "h:w:")) != EOF) {
150                 switch (a) {
151                 case 'h':
152                         ctdl_home_directory = strdup(optarg);
153                         break;
154                 case 'w':
155                         watchdog = atoi(optarg);
156                 default:
157                         fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
158                         return(1);
159                 }
160         }
161
162         fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server in %s\n",
163                 (int) getpid(),
164                 ctdl_home_directory
165         );
166         fflush(stderr);
167
168         alarm(watchdog);
169         snprintf(buf, sizeof buf, "%s/citadel-admin.socket", ctdl_home_directory);
170         serv_sock = uds_connectsock(buf);
171
172         serv_gets(buf);
173         fprintf(stderr, "%s\n", buf);
174
175         strcpy(buf, "");
176         for (a=optind; a<argc; ++a) {
177                 if (a != optind) {
178                         strcat(buf, " ");
179                 }
180                 strcat(buf, argv[a]);
181         }
182
183         fprintf(stderr, "%s\n", buf);
184         serv_puts(buf);
185         serv_gets(buf);
186         fprintf(stderr, "%s\n", buf);
187
188         xfermode = buf[0];
189
190         if ((xfermode == '4') || (xfermode == '8')) {           /* send text */
191                 while (fgets(buf, sizeof buf, stdin)) {
192                         buf[strlen(buf)-1] = 0;
193                         serv_puts(buf);
194                         alarm(watchdog);                        /* reset the watchdog timer */
195                 }
196                 serv_puts("000");
197         }
198
199         if ((xfermode == '1') || (xfermode == '8')) {           /* receive text */
200                 while (serv_gets(buf), strcmp(buf, "000")) {
201                         printf("%s\n", buf);
202                         alarm(watchdog);                        /* reset the watchdog timer */
203                 }
204         }
205         
206         if (xfermode == '6') {                                  /* receive binary */
207                 size_t len = atoi(&buf[4]);
208                 size_t bytes_remaining = len;
209
210                 while (bytes_remaining > 0) {
211                         size_t this_block = bytes_remaining;
212                         if (this_block > SIZ) this_block = SIZ;
213                         serv_read(buf, this_block);
214                         fwrite(buf, this_block, 1, stdout);
215                         bytes_remaining -= this_block;
216                 }
217         }
218
219         close(serv_sock);
220         alarm(0);                                               /* cancel the watchdog timer */
221         fprintf(stderr, "sendcommand: processing ended.\n");
222         if (xfermode == '5') {
223                 return(1);
224         }
225         return(0);
226 }
227
228
229
230
231
232
233
234
235
236
237
238
239
240