2 * (c) 2009 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
12 #include <readline/readline.h>
15 #define CTDLDIR "/root/ctdl/trunk/citadel"
17 int discover_ipgm_secret(char *dirname) {
19 struct partial_config ccc;
20 char configfile[1024];
22 sprintf(configfile, "%s/citadel.config", dirname);
23 fd = open(configfile, O_RDONLY);
25 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
29 if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
30 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
34 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
37 return(ccc.c_ipgm_secret);
41 void do_main_loop(int server_socket) {
45 char server_reply[1024];
50 /* Do an INFO command and learn the hostname for the prompt */
51 sock_puts(server_socket, "INFO");
52 sock_getln(server_socket, buf, sizeof buf);
55 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
57 sprintf(prompt, "\n%s> ", buf);
63 /* Here we go ... main command loop */
64 while (cmd = readline(prompt)) {
66 if ((cmd) && (*cmd)) {
69 sock_puts(server_socket, cmd);
70 sock_getln(server_socket, server_reply, sizeof server_reply);
71 printf("%s\n", server_reply);
73 if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
77 if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
78 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
89 int main(int argc, char **argv)
91 int server_socket = 0;
93 int ipgm_secret = (-1);
95 char *ctdldir = CTDLDIR;
97 printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
98 printf("(c) 2009 citadel.org GPLv3\n");
101 while ((c = getopt (argc, argv, "h:")) != -1) {
108 fprintf(stderr, "Option -%c requires an argument\n", optopt);
111 fprintf(stderr, "Unknown option '-%c'\n", optopt);
112 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
118 ipgm_secret = discover_ipgm_secret(ctdldir);
119 if (ipgm_secret < 0) {
123 printf("Attaching to server...\r");
125 sprintf(buf, "%s/citadel.socket", ctdldir);
126 server_socket = uds_connectsock(buf);
127 if (server_socket < 0) {
132 sock_getln(server_socket, buf, sizeof buf);
135 sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
136 sock_getln(server_socket, buf, sizeof buf);
140 do_main_loop(server_socket);
143 sock_puts(server_socket, "QUIT");
144 sock_getln(server_socket, buf, sizeof buf);
146 close(server_socket);