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. 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.h"
23 #include "../server/citadel_dirs.h"
24 #include <libcitadel.h>
28 int uds_connectsock(char *sockpath) {
30 struct sockaddr_un addr;
32 memset(&addr, 0, sizeof(addr));
33 addr.sun_family = AF_UNIX;
34 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
36 s = socket(AF_UNIX, SOCK_STREAM, 0);
38 fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno));
42 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
43 fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno));
52 // input binary data from socket
53 void serv_read(char *buf, int bytes) {
58 rlen = read(serv_sock, &buf[len], bytes - len);
67 // send binary to server
68 void serv_write(char *buf, int nbytes) {
69 int bytes_written = 0;
71 while (bytes_written < nbytes) {
72 retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written);
76 bytes_written = bytes_written + retval;
81 // input string from socket - implemented in terms of serv_read()
82 void serv_gets(char *buf) {
85 // Read one character at a time.
87 serv_read(&buf[i], 1);
88 if (buf[i] == '\n' || i == (SIZ-1))
92 // If we got a long line, discard characters until the newline.
94 while (buf[i] != '\n') {
95 serv_read(&buf[i], 1);
99 // Strip all trailing nonprintables (crlf)
104 // send line to server - implemented in terms of serv_write()
105 void serv_puts(char *buf) {
106 serv_write(buf, strlen(buf));
111 // Main loop. Do things and have fun.
112 int main(int argc, char **argv) {
117 char ctdldir[PATH_MAX]=CTDLDIR;
119 // Parse command line
120 while ((a = getopt(argc, argv, "h:w:")) != EOF) {
123 strncpy(ctdldir, optarg, sizeof ctdldir);
126 watchdog = atoi(optarg);
129 fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n");
134 fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server with data directory %s\n",
140 if (chdir(ctdldir) != 0) {
141 fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
146 serv_sock = uds_connectsock(file_citadel_admin_socket);
148 fprintf(stderr, "%s\n", buf);
151 for (a=optind; a<argc; ++a) {
155 strcat(buf, argv[a]);
158 fprintf(stderr, "%s\n", buf);
161 fprintf(stderr, "%s\n", buf);
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;
175 if ((xfermode == '1') || (xfermode == '8')) { // receive text
176 while(serv_gets(buf), strcmp(buf, "000")) {
181 if (xfermode == '6') { // receive binary
182 size_t len = atoi(&buf[4]);
183 size_t bytes_remaining = len;
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;
195 alarm(0); // cancel the watchdog timer
197 fprintf(stderr, "sendcommand: processing ended.\n");
198 if (xfermode == '5') {