1 // Command-line utility to transmit a server command.
3 // Copyright (c) 1987-2022 by the citadel.org team
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.
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.
16 #include <sys/types.h>
25 #include <sys/socket.h>
27 #include "../server/citadel.h"
28 #include "../server/citadel_dirs.h"
29 #include <libcitadel.h>
33 int uds_connectsock(char *sockpath) {
35 struct sockaddr_un addr;
37 memset(&addr, 0, sizeof(addr));
38 addr.sun_family = AF_UNIX;
39 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
41 s = socket(AF_UNIX, SOCK_STREAM, 0);
43 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
47 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
48 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
57 // input binary data from socket
58 void serv_read(char *buf, int bytes) {
63 rlen = read(serv_sock, &buf[len], bytes - len);
72 // send binary to server
73 void serv_write(char *buf, int nbytes) {
74 int bytes_written = 0;
76 while (bytes_written < nbytes) {
77 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
81 bytes_written = bytes_written + retval;
86 // input string from socket - implemented in terms of serv_read()
87 void serv_gets(char *buf) {
90 // Read one character at a time.
92 serv_read(&buf[i], 1);
93 if (buf[i] == '\n' || i == (SIZ-1))
97 // If we got a long line, discard characters until the newline.
99 while (buf[i] != '\n') {
100 serv_read(&buf[i], 1);
104 // Strip all trailing nonprintables (crlf)
109 // send line to server - implemented in terms of serv_write()
110 void serv_puts(char *buf) {
111 serv_write(buf, strlen(buf));
116 // Main loop. Do things and have fun.
117 int main(int argc, char **argv) {
122 char ctdldir[PATH_MAX]=CTDLDIR;
124 // Parse command line
125 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
128 strncpy(ctdldir, optarg, sizeof ctdldir);
131 watchdog = atoi(optarg);
134 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
139 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server with data directory %s\n",
145 if (chdir(ctdldir) != 0) {
146 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
151 serv_sock = uds_connectsock(file_citadel_admin_socket);
153 fprintf(stderr, "%s\n", buf);
156 for (a=optind; a<argc; ++a) {
160 strcat(buf, argv[a]);
163 fprintf(stderr, "%s\n", buf);
166 fprintf(stderr, "%s\n", buf);
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;
180 if ((xfermode == '1') || (xfermode == '8')) { // receive text
181 while(serv_gets(buf), strcmp(buf, "000")) {
186 if (xfermode == '6') { // receive binary
187 size_t len = atoi(&buf[4]);
188 size_t bytes_remaining = len;
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;
200 alarm(0); // cancel the watchdog timer
202 fprintf(stderr, "sendcommand: processing ended.\n");
203 if (xfermode == '5') {