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