--- /dev/null
+/*
+ * This is a small subset of 'struct config' ... don't worry about the rest; we
+ * only need to snarf the c_ipgm_secret.
+ */
+struct partial_config {
+ char c_nodename[16]; /* Unqualified "short" nodename */
+ char c_fqdn[64]; /* Fully Qualified Domain Name */
+ char c_humannode[21]; /* Long name of system */
+ char c_phonenum[16]; /* Dialup number of system */
+ uid_t c_ctdluid; /* UID under which we run Citadel */
+ char c_creataide; /* room creator = room aide flag */
+ int c_sleeping; /* watchdog timer setting */
+ char c_initax; /* initial access level */
+ char c_regiscall; /* call number to register on */
+ char c_twitdetect; /* twit detect flag */
+ char c_twitroom[128]; /* twit detect msg move to room */
+ char c_moreprompt[80]; /* paginator prompt */
+ char c_restrict; /* restrict Internet mail flag */
+ long c_niu_1; /* (not in use) */
+ char c_site_location[32]; /* physical location of server */
+ char c_sysadm[26]; /* name of system administrator */
+ char c_niu_2[15]; /* (not in use) */
+ int c_setup_level; /* what rev level we've setup to */
+ int c_maxsessions; /* maximum concurrent sessions */
+ char c_ip_addr[20]; /* IP address to listen on */
+ int c_port_number; /* Cit listener port (usually 504) */
+ int c_ipgm_secret; /* Internal program authentication */
+};
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
#include <readline/readline.h>
+#include "ctdlsh.h"
+
+#define CTDLDIR "/root/ctdl/trunk/citadel"
+
+
+int discover_ipgm_secret(void) {
+ int fd;
+ struct partial_config ccc;
+
+ fd = open(CTDLDIR "/citadel.config", O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "citadel.config: %s\n", strerror(errno));
+ return(-1);
+ }
+
+ if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
+ fprintf(stderr, "citadel.config: %s\n", strerror(errno));
+ return(-1);
+ }
+ if (close(fd) != 0) {
+ fprintf(stderr, "citadel.config: %s\n", strerror(errno));
+ return(-1);
+ }
+ return(ccc.c_ipgm_secret);
+}
+
+
+void do_main_loop(int server_socket) {
+ char *cmd = NULL;
+ char prompt[1024];
+ char buf[1024];
+ char server_reply[1024];
+ int i;
+
+ strcpy(prompt, "> ");
+
+ /* Do an INFO command and learn the hostname for the prompt */
+ sock_puts(server_socket, "INFO");
+ sock_getln(server_socket, buf, sizeof buf);
+ if (buf[0] == '1') {
+ i = 0;
+ while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
+ if (i == 1) {
+ sprintf(prompt, "\n%s> ", buf);
+ }
+ ++i;
+ }
+ }
+
+ /* Here we go ... main command loop */
+ while (cmd = readline(prompt)) {
+
+ if ((cmd) && (*cmd)) {
+ add_history(cmd);
+
+ sock_puts(server_socket, cmd);
+ sock_getln(server_socket, server_reply, sizeof server_reply);
+ printf("%s\n", server_reply);
+
+ if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
+ // FIXME
+ }
+
+ if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
+ while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
+ printf("%s\n", buf);
+ }
+ }
+
+ }
+
+ free(cmd);
+ }
+}
int main(int argc, char **argv)
{
- char *cmd = NULL;
- char *prompt = "> ";
int server_socket = 0;
char buf[1024];
+ int ipgm_secret = (-1);
printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
printf("(c) 2009 citadel.org GPLv3\n");
+ ipgm_secret = discover_ipgm_secret();
+ if (ipgm_secret < 0) {
+ exit(1);
+ }
+
printf("Attaching to server...\r");
fflush(stdout);
- server_socket = uds_connectsock("/root/ctdl/trunk/citadel/citadel.socket");
+ server_socket = uds_connectsock(CTDLDIR "/citadel.socket");
if (server_socket < 0) {
exit(1);
}
sock_getln(server_socket, buf, sizeof buf);
printf("%s\n", buf);
- while (cmd = readline(prompt)) {
-
- if ((cmd) && (*cmd)) {
- add_history(cmd);
- }
+ sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
+ sock_getln(server_socket, buf, sizeof buf);
+ printf("%s\n", buf);
- printf("\nHaha, you said: '%s'\n\n", cmd);
- free(cmd);
+ if (buf[0] == '2') {
+ do_main_loop(server_socket);
}
- printf("\r");
sock_puts(server_socket, "QUIT");
sock_getln(server_socket, buf, sizeof buf);