From 603cfe429f45783a47ad124913d853c6d06245c5 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Wed, 17 Jun 2009 19:31:06 +0000 Subject: [PATCH] * Use unix domain sockets because we're going to need to run this locally anyway, in order to obtain the ipgm secret. --- ctdlsh/src/main.c | 16 ++++++++--- ctdlsh/src/sockets.c | 67 ++++++++++---------------------------------- 2 files changed, 27 insertions(+), 56 deletions(-) diff --git a/ctdlsh/src/main.c b/ctdlsh/src/main.c index bbc14eb12..876b8e0f4 100644 --- a/ctdlsh/src/main.c +++ b/ctdlsh/src/main.c @@ -14,18 +14,21 @@ int main(int argc, char **argv) char *cmd = NULL; char *prompt = "> "; int server_socket = 0; + char buf[1024]; + + printf("\nCitadel administration shell v" PACKAGE_VERSION "\n"); + printf("(c) 2009 citadel.org GPLv3\n"); printf("Attaching to server...\r"); fflush(stdout); - server_socket = sock_connect("localhost", "504", "tcp"); + server_socket = uds_connectsock("/root/ctdl/trunk/citadel/citadel.socket"); if (server_socket < 0) { exit(1); } printf(" \r"); - printf("\nCitadel administration shell v" PACKAGE_VERSION "\n"); - printf("(c) 2009 citadel.org GPLv3\n"); - printf("Type a command. Or don't. We don't care.\n\n"); + sock_getln(server_socket, buf, sizeof buf); + printf("%s\n", buf); while (cmd = readline(prompt)) { @@ -36,6 +39,11 @@ int main(int argc, char **argv) printf("\nHaha, you said: '%s'\n\n", cmd); free(cmd); } + printf("\r"); + sock_puts(server_socket, "QUIT"); + sock_getln(server_socket, buf, sizeof buf); + printf("%s\n", buf); + close(server_socket); exit(0); } diff --git a/ctdlsh/src/sockets.c b/ctdlsh/src/sockets.c index 00f0d7e35..f949fe0eb 100644 --- a/ctdlsh/src/sockets.c +++ b/ctdlsh/src/sockets.c @@ -10,9 +10,7 @@ #include #include #include -#include -#include -#include +#include #include #include #include @@ -22,68 +20,31 @@ #define INADDR_NONE 0xffffffff #endif -int sock_connect(char *host, char *service, char *protocol) +int uds_connectsock(char *sockpath) { - struct hostent *phe; - struct servent *pse; - struct protoent *ppe; - struct sockaddr_in sin; - struct sockaddr_in egress_sin; - int s, type; - - if (host == NULL) return(-1); - if (service == NULL) return(-1); - if (protocol == NULL) return(-1); - - memset(&sin, 0, sizeof(sin)); - sin.sin_family = AF_INET; - - pse = getservbyname(service, protocol); - if (pse) { - sin.sin_port = pse->s_port; - } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) { - fprintf(stderr, "Can't get %s service entry: %s\n", - service, strerror(errno)); - return(-1); - } - phe = gethostbyname(host); - if (phe) { - memcpy(&sin.sin_addr, phe->h_addr, phe->h_length); - } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) { - fprintf(stderr, "Can't get %s host entry: %s\n", - host, strerror(errno)); - return(-1); - } - if ((ppe = getprotobyname(protocol)) == 0) { - fprintf(stderr, "Can't get %s protocol entry: %s\n", - protocol, strerror(errno)); - return(-1); - } - if (!strcmp(protocol, "udp")) { - type = SOCK_DGRAM; - } else { - type = SOCK_STREAM; - } + struct sockaddr_un addr; + int s; + + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, sockpath, sizeof addr.sun_path); - s = socket(PF_INET, type, ppe->p_proto); + s = socket(AF_UNIX, SOCK_STREAM, 0); if (s < 0) { - fprintf(stderr, "Can't create socket: %s\n", strerror(errno)); + fprintf(stderr, "Can't create socket[%s]: %s\n", sockpath, strerror(errno)); return(-1); } - /* Now try to connect to the remote host. */ - if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) { - fprintf(stderr, "Can't connect to %s:%s: %s\n", - host, service, strerror(errno)); + if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) { + fprintf(stderr, "Can't connect [%s]: %s\n", sockpath, strerror(errno)); close(s); return(-1); } - return (s); + return s; } - /* * sock_read_to() - input binary data from socket, with a settable timeout. * Returns the number of bytes read, or -1 for error. @@ -204,3 +165,5 @@ int sock_puts(int sock, char *buf) if (j<0) return(j); return(i+j); } + + -- 2.30.2