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