e97965f19b329dda6c0107549467381db21f4096
[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 #include "include/citadel_dirs.h"
30
31
32
33 int serv_sock = (-1);
34
35
36 int uds_connectsock(char *sockpath)
37 {
38         int s;
39         struct sockaddr_un addr;
40
41         memset(&addr, 0, sizeof(addr));
42         addr.sun_family = AF_UNIX;
43         strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
44
45         s = socket(AF_UNIX, SOCK_STREAM, 0);
46         if (s < 0) {
47                 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
48                 exit(3);
49         }
50
51         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
52                 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
53                 close(s);
54                 exit(3);
55         }
56
57         return s;
58 }
59
60
61 /*
62  * input binary data from socket
63  */
64 void serv_read(char *buf, int bytes)
65 {
66         int len, rlen;
67
68         len = 0;
69         while (len < bytes) {
70                 rlen = read(serv_sock, &buf[len], bytes - len);
71                 if (rlen < 1) {
72                         return;
73                 }
74                 len = len + rlen;
75         }
76 }
77
78
79 /*
80  * send binary to server
81  */
82 void serv_write(char *buf, int nbytes)
83 {
84         int bytes_written = 0;
85         int retval;
86         while (bytes_written < nbytes) {
87                 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
88                 if (retval < 1) {
89                         return;
90                 }
91                 bytes_written = bytes_written + retval;
92         }
93 }
94
95
96
97 /*
98  * input string from socket - implemented in terms of serv_read()
99  */
100 void serv_gets(char *buf)
101 {
102         int i;
103
104         /* Read one character at a time.
105          */
106         for (i = 0;; i++) {
107                 serv_read(&buf[i], 1);
108                 if (buf[i] == '\n' || i == (SIZ-1))
109                         break;
110         }
111
112         /* If we got a long line, discard characters until the newline.
113          */
114         if (i == (SIZ-1)) {
115                 while (buf[i] != '\n') {
116                         serv_read(&buf[i], 1);
117                 }
118         }
119
120         /* Strip all trailing nonprintables (crlf)
121          */
122         buf[i] = 0;
123 }
124
125
126 /*
127  * send line to server - implemented in terms of serv_write()
128  */
129 void serv_puts(char *buf)
130 {
131         serv_write(buf, strlen(buf));
132         serv_write("\n", 1);
133 }
134
135
136
137
138 /*
139  * Main loop.  Do things and have fun.
140  */
141 int main(int argc, char **argv)
142 {
143         int a;
144         int watchdog = 60;
145         char buf[SIZ];
146         int xfermode = 0;
147         int relh=0;
148         int home=0;
149         char relhome[PATH_MAX]="";
150         char ctdldir[PATH_MAX]=CTDLDIR;
151
152         /* Parse command line */
153         while ((a = getopt(argc, argv, "h:w:")) != EOF) {
154                 switch (a) {
155                 case 'h':
156                         relh=optarg[0]!='/';
157                         if (!relh) {
158                                 strncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
159                         } else {
160                                 strncpy(relhome, optarg, sizeof relhome);
161                         }
162                         home = 1;
163                         break;
164                 case 'w':
165                         watchdog = atoi(optarg);
166                         break;
167                 default:
168                         fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
169                         return(1);
170                 }
171         }
172
173         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
174
175         fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server at %s\n",
176                 (int) getpid(),
177                 file_citadel_admin_socket
178         );
179         fflush(stderr);
180
181         alarm(watchdog);
182         serv_sock = uds_connectsock(file_citadel_admin_socket);
183
184         serv_gets(buf);
185         fprintf(stderr, "%s\n", buf);
186
187         strcpy(buf, "");
188         for (a=optind; a<argc; ++a) {
189                 if (a != optind) {
190                         strcat(buf, " ");
191                 }
192                 strcat(buf, argv[a]);
193         }
194
195         fprintf(stderr, "%s\n", buf);
196         serv_puts(buf);
197         serv_gets(buf);
198         fprintf(stderr, "%s\n", buf);
199
200         xfermode = buf[0];
201
202         if ((xfermode == '4') || (xfermode == '8')) {           /* send text */
203                 while (fgets(buf, sizeof buf, stdin)) {
204                         buf[strlen(buf)-1] = 0;
205                         serv_puts(buf);
206                         alarm(watchdog);                        /* reset the watchdog timer */
207                 }
208                 serv_puts("000");
209         }
210
211         if ((xfermode == '1') || (xfermode == '8')) {           /* receive text */
212                 while (serv_gets(buf), strcmp(buf, "000")) {
213                         printf("%s\n", buf);
214                         alarm(watchdog);                        /* reset the watchdog timer */
215                 }
216         }
217         
218         if (xfermode == '6') {                                  /* receive binary */
219                 size_t len = atoi(&buf[4]);
220                 size_t bytes_remaining = len;
221
222                 while (bytes_remaining > 0) {
223                         size_t this_block = bytes_remaining;
224                         if (this_block > SIZ) this_block = SIZ;
225                         serv_read(buf, this_block);
226                         fwrite(buf, this_block, 1, stdout);
227                         bytes_remaining -= this_block;
228                 }
229         }
230
231         close(serv_sock);
232         alarm(0);                                               /* cancel the watchdog timer */
233         fprintf(stderr, "sendcommand: processing ended.\n");
234         if (xfermode == '5') {
235                 return(1);
236         }
237         return(0);
238 }
239
240
241
242
243
244
245
246
247
248
249
250
251
252