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