2 * (c) 2009-2011 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
11 int cmd_quit(int sock, char *cmdbuf) {
17 * Commands understood by ctdlsh
21 ctdlsh_cmdfunc_t *func;
25 COMMAND commands[] = {
26 { "?", cmd_help, "Display this message" },
27 { "help", cmd_help, "Display this message" },
28 { "quit", cmd_quit, "Quit using ctdlsh" },
29 { "exit", cmd_quit, "Quit using ctdlsh" },
30 { "date", cmd_datetime, "Print the server's date and time" },
31 { "time", cmd_datetime, "Print the server's date and time" },
32 { "passwd", cmd_passwd, "Set or change an account password" },
37 int cmd_help(int sock, char *cmdbuf) {
40 for (i=0; commands[i].func != NULL; ++i) {
41 printf("%-10s %s\n", commands[i].name, commands[i].doc);
50 int discover_ipgm_secret(char *dirname) {
52 struct partial_config ccc;
53 char configfile[1024];
55 sprintf(configfile, "%s/citadel.config", dirname);
56 fd = open(configfile, O_RDONLY);
58 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
62 if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
63 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
67 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
70 return(ccc.c_ipgm_secret);
74 /* Auto-completer function */
75 char *command_generator(const char *text, int state) {
76 static int list_index;
85 while (name = commands[list_index].name) {
88 if (!strncmp(name, text, len)) {
97 /* Auto-completer function */
98 char **ctdlsh_completion(const char *text, int start, int end) {
99 char **matches = (char **) NULL;
102 matches = rl_completion_matches(text, command_generator);
105 rl_bind_key('\t', rl_abort);
113 void do_main_loop(int server_socket) {
117 char server_reply[1024];
121 strcpy(prompt, "> ");
123 /* Do an INFO command and learn the hostname for the prompt */
124 sock_puts(server_socket, "INFO");
125 sock_getln(server_socket, buf, sizeof buf);
128 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
130 sprintf(prompt, "\n%s> ", buf);
136 /* Tell libreadline how we will help with auto-completion of commands */
137 rl_attempted_completion_function = ctdlsh_completion;
139 /* Here we go ... main command loop */
140 while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
142 if ((cmd) && (*cmd)) {
145 for (i=0; commands[i].func != NULL; ++i) {
146 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
147 ret = (*commands[i].func) (server_socket, cmd);
157 int main(int argc, char **argv)
159 int server_socket = 0;
161 int ipgm_secret = (-1);
163 char *ctdldir = CTDLDIR;
165 printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
166 printf("(c) 2009-2011 citadel.org GPLv3\n");
169 while ((c = getopt (argc, argv, "h:")) != -1) {
176 fprintf(stderr, "Option -%c requires an argument\n", optopt);
179 fprintf(stderr, "Unknown option '-%c'\n", optopt);
180 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
186 ipgm_secret = discover_ipgm_secret(ctdldir);
187 if (ipgm_secret < 0) {
191 printf("Trying %s...\n", ctdldir);
192 sprintf(buf, "%s/citadel.socket", ctdldir);
193 server_socket = uds_connectsock(buf);
194 if (server_socket < 0) {
198 sock_getln(server_socket, buf, sizeof buf);
201 sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
202 sock_getln(server_socket, buf, sizeof buf);
206 do_main_loop(server_socket);
209 sock_puts(server_socket, "QUIT");
210 sock_getln(server_socket, buf, sizeof buf);
212 close(server_socket);