1 // Command-line utility to transmit a server command.
3 // Copyright (c) 1987-2023 by the citadel.org team
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.
11 #include <sys/types.h>
20 #include <sys/socket.h>
22 #include "../server/citadel_defs.h"
23 #include "../server/server.h"
24 #include "../server/citadel_dirs.h"
25 #include <libcitadel.h>
29 int uds_connectsock(char *sockpath) {
31 struct sockaddr_un addr;
33 memset(&addr, 0, sizeof(addr));
34 addr.sun_family = AF_UNIX;
35 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
37 s = socket(AF_UNIX, SOCK_STREAM, 0);
39 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
43 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
44 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
53 // input binary data from socket
54 void serv_read(char *buf, int bytes) {
59 rlen = read(serv_sock, &buf[len], bytes - len);
68 // send binary to server
69 void serv_write(char *buf, int nbytes) {
70 int bytes_written = 0;
72 while (bytes_written < nbytes) {
73 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
77 bytes_written = bytes_written + retval;
82 // input string from socket - implemented in terms of serv_read()
83 void serv_gets(char *buf) {
86 // Read one character at a time.
88 serv_read(&buf[i], 1);
89 if (buf[i] == '\n' || i == (SIZ-1))
93 // If we got a long line, discard characters until the newline.
95 while (buf[i] != '\n') {
96 serv_read(&buf[i], 1);
100 // Strip all trailing nonprintables (crlf)
105 // send line to server - implemented in terms of serv_write()
106 void serv_puts(char *buf) {
107 serv_write(buf, strlen(buf));
112 // Main loop. Do things and have fun.
113 int main(int argc, char **argv) {
118 char ctdldir[PATH_MAX]=CTDLDIR;
120 // Parse command line
121 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
124 strncpy(ctdldir, optarg, sizeof ctdldir);
127 watchdog = atoi(optarg);
130 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
135 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server with data directory %s\n",
141 if (chdir(ctdldir) != 0) {
142 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
147 serv_sock = uds_connectsock(file_citadel_admin_socket);
149 fprintf(stderr, "%s\n", buf);
152 for (a=optind; a<argc; ++a) {
156 strcat(buf, argv[a]);
159 fprintf(stderr, "%s\n", buf);
162 fprintf(stderr, "%s\n", buf);
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;
176 if ((xfermode == '1') || (xfermode == '8')) { // receive text
177 while(serv_gets(buf), strcmp(buf, "000")) {
182 if (xfermode == '6') { // receive binary
183 size_t len = atoi(&buf[4]);
184 size_t bytes_remaining = len;
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;
196 alarm(0); // cancel the watchdog timer
198 fprintf(stderr, "sendcommand: processing ended.\n");
199 if (xfermode == '5') {