X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Futils%2Fsendcommand.c;fp=citadel%2Futils%2Fsendcommand.c;h=bd87ff3b48cf3be2f5c251cfb3afd5ddb0a17e3e;hb=950b5e7c91bc0146fc239dc90f3eb3d949f1f06a;hp=0000000000000000000000000000000000000000;hpb=f6fcf350671e3661f8f22696eb35133014ab6a14;p=citadel.git diff --git a/citadel/utils/sendcommand.c b/citadel/utils/sendcommand.c new file mode 100644 index 000000000..bd87ff3b4 --- /dev/null +++ b/citadel/utils/sendcommand.c @@ -0,0 +1,222 @@ +/* + * Command-line utility to transmit a server command. + * + * Copyright (c) 1987-2021 by the citadel.org team + * + * This program is open source software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "citadel.h" +#include "citadel_dirs.h" +#include + +int serv_sock = (-1); + +int uds_connectsock(char *sockpath) { + int s; + struct sockaddr_un addr; + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sockpath, sizeof addr.sun_path); + + s = socket(AF_UNIX, SOCK_STREAM, 0); + if (s < 0) { + fprintf(stderr, "sendcommand: Can't create socket: %s\n", strerror(errno)); + exit(3); + } + + if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + fprintf(stderr, "sendcommand: can't connect: %s\n", strerror(errno)); + close(s); + exit(3); + } + + return s; +} + + +/* + * input binary data from socket + */ +void serv_read(char *buf, int bytes) { + int len, rlen; + + len = 0; + while (len < bytes) { + rlen = read(serv_sock, &buf[len], bytes - len); + if (rlen < 1) { + return; + } + len = len + rlen; + } +} + + +/* + * send binary to server + */ +void serv_write(char *buf, int nbytes) { + int bytes_written = 0; + int retval; + while (bytes_written < nbytes) { + retval = write(serv_sock, &buf[bytes_written], nbytes - bytes_written); + if (retval < 1) { + return; + } + bytes_written = bytes_written + retval; + } +} + + +/* + * input string from socket - implemented in terms of serv_read() + */ +void serv_gets(char *buf) { + int i; + + /* Read one character at a time. + */ + for (i = 0;; i++) { + serv_read(&buf[i], 1); + if (buf[i] == '\n' || i == (SIZ-1)) + break; + } + + /* If we got a long line, discard characters until the newline. + */ + if (i == (SIZ-1)) { + while (buf[i] != '\n') { + serv_read(&buf[i], 1); + } + } + + /* Strip all trailing nonprintables (crlf) + */ + buf[i] = 0; +} + + +/* + * send line to server - implemented in terms of serv_write() + */ +void serv_puts(char *buf) { + serv_write(buf, strlen(buf)); + serv_write("\n", 1); +} + + +/* + * Main loop. Do things and have fun. + */ +int main(int argc, char **argv) { + int a; + int watchdog = 60; + char buf[SIZ]; + int xfermode = 0; + char ctdldir[PATH_MAX]=CTDLDIR; + + /* Parse command line */ + while ((a = getopt(argc, argv, "h:w:")) != EOF) { + switch (a) { + case 'h': + strncpy(ctdldir, optarg, sizeof ctdldir); + break; + case 'w': + watchdog = atoi(optarg); + break; + default: + fprintf(stderr, "sendcommand: usage: sendcommand [-h server_dir] [-w watchdog_timeout]\n"); + return(1); + } + } + + fprintf(stderr, "sendcommand: started (pid=%d) connecting to Citadel server with data directory %s\n", + (int) getpid(), + ctdldir + ); + fflush(stderr); + + if (chdir(ctdldir) != 0) { + fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno)); + exit(errno); + } + + alarm(watchdog); + serv_sock = uds_connectsock(file_citadel_admin_socket); + serv_gets(buf); + fprintf(stderr, "%s\n", buf); + + strcpy(buf, ""); + for (a=optind; a 0) { + if (buf[strlen(buf)-1] == '\n') { + buf[strlen(buf)-1] = 0; + } + serv_puts(buf); + } + serv_puts("000"); + } + + if ((xfermode == '1') || (xfermode == '8')) { /* receive text */ + while(serv_gets(buf), strcmp(buf, "000")) { + printf("%s\n", buf); + } + } + + if (xfermode == '6') { /* receive binary */ + size_t len = atoi(&buf[4]); + size_t bytes_remaining = len; + + while (bytes_remaining > 0) { + size_t this_block = bytes_remaining; + if (this_block > SIZ) this_block = SIZ; + serv_read(buf, this_block); + fwrite(buf, this_block, 1, stdout); + bytes_remaining -= this_block; + } + } + + close(serv_sock); + alarm(0); /* cancel the watchdog timer */ + + fprintf(stderr, "sendcommand: processing ended.\n"); + if (xfermode == '5') { + return(1); + } + return(0); +}