b6c9102483ede828940b41a872fb5d0bfafed810
[citadel.git] / citadel / utils / sendcommand.c
1 /*
2  * Command-line utility to transmit a server command.
3  *
4  * Copyright (c) 1987-2021 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 <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <string.h>
21 #include <fcntl.h>
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <signal.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include "citadel.h"
30 #include "include/citadel_dirs.h"
31 #include <libcitadel.h>
32
33 int serv_sock = (-1);
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  * input string from socket - implemented in terms of serv_read()
97  */
98 void serv_gets(char *buf)
99 {
100         int i;
101
102         /* Read one character at a time.
103          */
104         for (i = 0;; i++) {
105                 serv_read(&buf[i], 1);
106                 if (buf[i] == '\n' || i == (SIZ-1))
107                         break;
108         }
109
110         /* If we got a long line, discard characters until the newline.
111          */
112         if (i == (SIZ-1)) {
113                 while (buf[i] != '\n') {
114                         serv_read(&buf[i], 1);
115                 }
116         }
117
118         /* Strip all trailing nonprintables (crlf)
119          */
120         buf[i] = 0;
121 }
122
123
124 /*
125  * send line to server - implemented in terms of serv_write()
126  */
127 void serv_puts(char *buf)
128 {
129         serv_write(buf, strlen(buf));
130         serv_write("\n", 1);
131 }
132
133
134 /*
135  * Main loop.  Do things and have fun.
136  */
137 int main(int argc, char **argv)
138 {
139         int a;
140         int watchdog = 60;
141         char buf[SIZ];
142         int xfermode = 0;
143         int relh=0;
144         int home=0;
145         char relhome[PATH_MAX]="";
146         char ctdldir[PATH_MAX]=CTDLDIR;
147
148         StartLibCitadel(SIZ);
149
150         /* Parse command line */
151         while ((a = getopt(argc, argv, "h:w:")) != EOF) {
152                 switch (a) {
153                 case 'h':
154                         relh=optarg[0]!='/';
155                         if (!relh) {
156                                 strncpy(ctdl_home_directory, optarg, sizeof ctdl_home_directory);
157                         } else {
158                                 strncpy(relhome, optarg, sizeof relhome);
159                         }
160                         home = 1;
161                         break;
162                 case 'w':
163                         watchdog = atoi(optarg);
164                         break;
165                 default:
166                         fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
167                         return(1);
168                 }
169         }
170
171         fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server at %s\n",
172                 (int) getpid(),
173                 file_citadel_admin_socket
174         );
175         fflush(stderr);
176
177         alarm(watchdog);
178         serv_sock = uds_connectsock(file_citadel_admin_socket);
179         serv_gets(buf);
180         fprintf(stderr, "%s\n", buf);
181
182         strcpy(buf, "");
183         for (a=optind; a<argc; ++a) {
184                 if (a != optind) {
185                         strcat(buf, " ");
186                 }
187                 strcat(buf, argv[a]);
188         }
189
190         fprintf(stderr, "%s\n", buf);
191         serv_puts(buf);
192         serv_gets(buf);
193         fprintf(stderr, "%s\n", buf);
194
195         xfermode = buf[0];
196
197         if ((xfermode == '4') || (xfermode == '8')) {           /* send text */
198                 while (fgets(buf, sizeof buf, stdin) > 0) {
199                         if (buf[strlen(buf)-1] == '\n') {
200                                 buf[strlen(buf)-1] = 0;
201                         }
202                         serv_puts(buf);
203                 }
204                 serv_puts("000");
205         }
206
207         if ((xfermode == '1') || (xfermode == '8')) {           /* receive text */
208                 while(serv_gets(buf), strcmp(buf, "000")) {
209                         printf("%s\n", buf);
210                 }
211         }
212         
213         if (xfermode == '6') {                                  /* receive binary */
214                 size_t len = atoi(&buf[4]);
215                 size_t bytes_remaining = len;
216
217                 while (bytes_remaining > 0) {
218                         size_t this_block = bytes_remaining;
219                         if (this_block > SIZ) this_block = SIZ;
220                         serv_read(buf, this_block);
221                         fwrite(buf, this_block, 1, stdout);
222                         bytes_remaining -= this_block;
223                 }
224         }
225
226         close(serv_sock);
227         alarm(0);                                               /* cancel the watchdog timer */
228         fprintf(stderr, "sendcommand: processing ended.\n");
229         if (xfermode == '5') {
230                 return(1);
231         }
232         return(0);
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246
247