1 // Copyright (c) 1987-2022 by the citadel.org team
3 // This program is open source software. Use, duplication, and/or
4 // disclosure are subject to the GNU General Purpose License version 3.
6 // This program is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU General Public License for more details.
11 #include "textclient.h"
14 static SSL_CTX *ssl_ctx;
17 #endif /* HAVE_OPENSSL */
20 #define INADDR_NONE 0xffffffff
23 static void (*status_hook)(char *s) = NULL;
24 char ctdl_autoetc_dir[PATH_MAX] = "";
25 char file_citadel_rc[PATH_MAX] = "";
26 char ctdl_run_dir[PATH_MAX] = "";
27 char ctdl_etc_dir[PATH_MAX] = "";
28 char ctdl_home_directory[PATH_MAX] = "";
29 char file_citadel_socket[PATH_MAX] = "";
51 void CtdlIPC_lock(CtdlIPC * ipc) {
52 if (ipc->network_status_cb)
53 ipc->network_status_cb(1);
57 void CtdlIPC_unlock(CtdlIPC * ipc) {
58 if (ipc->network_status_cb)
59 ipc->network_status_cb(0);
63 char *libcitadelclient_version_string(void) {
64 return "libcitadelclient(unnumbered)";
68 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
69 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
70 (home&!relh)?ctdl_home_directory:basedir, \
71 ((basedir!=ctdldir)&(home&!relh))?basedir:"/", \
72 ((basedir!=ctdldir)&(home&!relh))?"/":"", \
74 (relhome[0]!='\0')?"/":"",\
76 (dirbuffer[0]!='\0')?"/":"");
78 #define DBG_PRINT(A) if (dbg==1) fprintf (stderr,"%s : %s \n", #A, A)
81 void calc_dirs_n_files(int relh, int home, const char *relhome, char *ctdldir, int dbg) {
82 const char *basedir = "";
83 char dirbuffer[PATH_MAX] = "";
85 StripSlashes(ctdldir, 1);
92 COMPUTE_DIRECTORY(ctdl_run_dir);
93 StripSlashes(ctdl_run_dir, 1);
96 #ifndef HAVE_AUTO_ETC_DIR
99 basedir = AUTO_ETC_DIR;
101 COMPUTE_DIRECTORY(ctdl_autoetc_dir);
102 StripSlashes(ctdl_autoetc_dir, 1);
110 COMPUTE_DIRECTORY(ctdl_etc_dir);
111 StripSlashes(ctdl_etc_dir, 1);
115 snprintf(file_citadel_rc, sizeof file_citadel_rc, "%scitadel.rc", ctdl_etc_dir);
116 StripSlashes(file_citadel_rc, 0);
118 snprintf(file_citadel_socket, sizeof file_citadel_socket, "%scitadel.socket", ctdl_run_dir);
119 StripSlashes(file_citadel_socket, 0);
121 DBG_PRINT(ctdl_run_dir);
122 DBG_PRINT(file_citadel_socket);
123 DBG_PRINT(ctdl_etc_dir);
124 DBG_PRINT(file_citadel_rc);
127 void setCryptoStatusHook(void (*hook)(char *s)) {
131 void CtdlIPC_SetNetworkStatusCallback(CtdlIPC * ipc, void (*hook)(int state)) {
132 ipc->network_status_cb = hook;
136 char instant_msgs = 0;
139 static void serv_read(CtdlIPC * ipc, char *buf, unsigned int bytes);
140 static void serv_write(CtdlIPC * ipc, const char *buf, unsigned int nbytes);
142 static void serv_read_ssl(CtdlIPC * ipc, char *buf, unsigned int bytes);
143 static void serv_write_ssl(CtdlIPC * ipc, const char *buf, unsigned int nbytes);
144 static void endtls(SSL * ssl);
145 #endif /* HAVE_OPENSSL */
146 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf);
147 static void CtdlIPC_putline(CtdlIPC * ipc, const char *buf);
151 const char *svn_revision(void);
154 * Does nothing. The server should always return 200.
156 int CtdlIPCNoop(CtdlIPC * ipc) {
159 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
164 * Does nothing interesting. The server should always return 200
165 * along with your string.
167 int CtdlIPCEcho(CtdlIPC * ipc, const char *arg, char *cret) {
176 aaa = (char *) malloc((size_t) (strlen(arg) + 6));
180 sprintf(aaa, "ECHO %s", arg);
181 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
188 * Asks the server to close the connecction.
189 * Should always return 200.
191 int CtdlIPCQuit(CtdlIPC * ipc) {
192 int ret = 221; /* Default to successful quit */
196 if (ipc->sock > -1) {
197 CtdlIPC_putline(ipc, "QUIT");
198 CtdlIPC_getline(ipc, aaa);
203 SSL_shutdown(ipc->ssl);
207 shutdown(ipc->sock, 2); /* Close connection; we're dead */
215 * Asks the server to log out. Should always return 200, even if no user
216 * was logged in. The user will not be logged in after this!
218 int CtdlIPCLogout(CtdlIPC * ipc) {
223 CtdlIPC_putline(ipc, "LOUT");
224 CtdlIPC_getline(ipc, aaa);
232 * First stage of authentication - pass the username. Returns 300 if the
233 * username is able to log in, with the username correctly spelled in cret.
234 * Returns various 500 error codes if the user doesn't exist, etc.
236 int CtdlIPCTryLogin(CtdlIPC * ipc, const char *username, char *cret) {
245 aaa = (char *) malloc((size_t) (strlen(username) + 6));
249 sprintf(aaa, "USER %s", username);
250 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
257 * Second stage of authentication - provide password. The server returns
258 * 200 and several arguments in cret relating to the user's account.
260 int CtdlIPCTryPassword(CtdlIPC * ipc, const char *passwd, char *cret) {
269 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
273 sprintf(aaa, "PASS %s", passwd);
274 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
281 * Create a new user. This returns 200 plus the same arguments as TryPassword
282 * if selfservice is nonzero, unless there was a problem creating the account.
283 * If selfservice is zero, creates a new user but does not log out the existing
284 * user - intended for use by system administrators to create accounts on
285 * behalf of other users.
287 int CtdlIPCCreateUser(CtdlIPC * ipc, const char *username, int selfservice, char *cret) {
296 aaa = (char *) malloc((size_t) (strlen(username) + 6));
300 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
301 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
308 * Changes the user's password. Returns 200 if changed, errors otherwise.
310 int CtdlIPCChangePassword(CtdlIPC * ipc, const char *passwd, char *cret) {
319 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
323 sprintf(aaa, "SETP %s", passwd);
324 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
332 /* Caller must free the march list */
334 /* Room types are defined in enum RoomList; keep these in sync! */
336 /* floor is -1 for all, or floornum */
337 int CtdlIPCKnownRooms(CtdlIPC * ipc, enum RoomList which, int floor, struct march **listing, char *cret) {
339 struct march *march = NULL;
340 static char *proto[] = { "LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
348 return -2; /* Free the listing first */
351 /* if (which < 0 || which > 4) return -2; */
353 return -2; /* Can't validate upper bound, sorry */
355 sprintf(aaa, "%s %d", proto[which], floor);
356 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
357 if (ret / 100 == 1) {
360 while (bbb && strlen(bbb)) {
363 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
365 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
366 mptr = (struct march *) malloc(sizeof(struct march));
369 extract_token(mptr->march_name, aaa, 0, '|', sizeof mptr->march_name);
370 mptr->march_flags = (unsigned int) extract_int(aaa, 1);
371 mptr->march_floor = (char) extract_int(aaa, 2);
372 mptr->march_order = (char) extract_int(aaa, 3);
373 mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
374 mptr->march_access = (char) extract_int(aaa, 5);
381 while (mptr2->next != NULL)
397 /* Caller must free the struct ctdluser; caller may pass an existing one */
398 int CtdlIPCGetConfig(CtdlIPC * ipc, struct ctdluser **uret, char *cret) {
406 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
410 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
411 if (ret / 100 == 2) {
412 uret[0]->flags = extract_int(cret, 2);
419 int CtdlIPCSetConfig(CtdlIPC * ipc, struct ctdluser *uret, char *cret) {
427 sprintf(aaa, "SETU 80|24|%d", uret->flags);
428 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
433 int CtdlIPCRenameUser(CtdlIPC * ipc, char *oldname, char *newname, char *cret) {
444 snprintf(cmd, sizeof cmd, "RENU %s|%s", oldname, newname);
445 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
451 int CtdlIPCGotoRoom(CtdlIPC * ipc, const char *room, const char *passwd, struct ctdlipcroom **rret, char *cret) {
460 *rret = (struct ctdlipcroom *) calloc(1, sizeof(struct ctdlipcroom));
465 aaa = (char *) malloc(strlen(room) + strlen(passwd) + 7);
470 sprintf(aaa, "GOTO %s|%s", room, passwd);
473 aaa = (char *) malloc(strlen(room) + 6);
478 sprintf(aaa, "GOTO %s", room);
480 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
481 if (ret / 100 == 2) {
482 extract_token(rret[0]->RRname, cret, 0, '|', sizeof rret[0]->RRname);
483 rret[0]->RRunread = extract_long(cret, 1);
484 rret[0]->RRtotal = extract_long(cret, 2);
485 rret[0]->RRinfoupdated = extract_int(cret, 3);
486 rret[0]->RRflags = extract_int(cret, 4);
487 rret[0]->RRhighest = extract_long(cret, 5);
488 rret[0]->RRlastread = extract_long(cret, 6);
489 rret[0]->RRismailbox = extract_int(cret, 7);
490 rret[0]->RRaide = extract_int(cret, 8);
491 // position 9 is no longer used
492 rret[0]->RRfloor = extract_int(cret, 10);
493 rret[0]->RRcurrentview = extract_int(cret, 11);
494 rret[0]->RRdefaultview = extract_int(cret, 12);
495 // position 13 is a trash folder flag ... irrelevant in this client
496 rret[0]->RRflags2 = extract_int(cret, 14);
509 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
511 /* whicharg is number of messages, applies to last, first, gt, lt */
512 int CtdlIPCGetMessages(CtdlIPC * ipc, enum MessageList which, int whicharg, const char *mtemplate, unsigned long **mret, char *cret) {
514 unsigned long count = 0;
515 static char *proto[] = { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
526 if (which < 0 || which > 6)
530 sprintf(aaa, "MSGS %s||%d", proto[which], (mtemplate) ? 1 : 0);
532 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg, (mtemplate) ? 1 : 0);
534 count = strlen(mtemplate);
535 ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbb_len, cret);
539 *mret = (unsigned long *) calloc(1, sizeof(unsigned long));
542 while (bbb && strlen(bbb)) {
543 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
544 remove_token(bbb, 0, '\n');
545 *mret = (unsigned long *) realloc(*mret, (size_t) ((count + 2) * sizeof(unsigned long)));
547 (*mret)[count++] = atol(aaa);
561 int CtdlIPCGetSingleMessage(CtdlIPC * ipc, long msgnum, int headers, int as_mime, struct ctdlipcmessage **mret, char *cret) {
566 int multipart_hunting = 0;
567 char multipart_prefix[128];
575 *mret = (struct ctdlipcmessage *) calloc(1, sizeof(struct ctdlipcmessage));
581 strcpy(encoding, "");
582 strcpy(mret[0]->content_type, "");
583 mret[0]->is_local = 0;
584 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
585 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
586 if (ret / 100 == 1) {
588 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
589 while (strlen(bbb) > 4 && bbb[4] == '=') {
590 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
591 remove_token(bbb, 0, '\n');
593 if (!strncasecmp(aaa, "nhdr=yes", 8))
595 else if (!strncasecmp(aaa, "from=", 5))
596 strncpy(mret[0]->author, &aaa[5], SIZ);
597 else if (!strncasecmp(aaa, "type=", 5))
598 mret[0]->type = atoi(&aaa[5]);
599 else if (!strncasecmp(aaa, "msgn=", 5))
600 strncpy(mret[0]->msgid, &aaa[5], SIZ);
601 else if (!strncasecmp(aaa, "subj=", 5))
602 strncpy(mret[0]->subject, &aaa[5], SIZ);
603 else if (!strncasecmp(aaa, "rfca=", 5))
604 strncpy(mret[0]->email, &aaa[5], SIZ);
605 else if (!strncasecmp(aaa, "room=", 5))
606 strncpy(mret[0]->room, &aaa[5], SIZ);
607 else if (!strncasecmp(aaa, "rcpt=", 5))
608 strncpy(mret[0]->recipient, &aaa[5], SIZ);
609 else if (!strncasecmp(aaa, "wefw=", 5))
610 strncpy(mret[0]->references, &aaa[5], SIZ);
611 else if (!strncasecmp(aaa, "time=", 5))
612 mret[0]->time = atol(&aaa[5]);
613 else if (!strncasecmp(aaa, "locl", 4))
614 mret[0]->is_local = 1;
616 /* Multipart/alternative prefix & suffix strings help
617 * us to determine which part we want to download.
619 else if (!strncasecmp(aaa, "pref=", 5)) {
620 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
621 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
625 else if (!strncasecmp(aaa, "suff=", 5)) {
626 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
627 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
632 else if (!strncasecmp(aaa, "part=", 5)) {
633 struct parts *ptr, *chain;
635 ptr = (struct parts *) calloc(1, sizeof(struct parts));
638 /* Fill the buffers for the caller */
639 extract_token(ptr->name, &aaa[5], 0, '|', sizeof ptr->name);
640 extract_token(ptr->filename, &aaa[5], 1, '|', sizeof ptr->filename);
641 extract_token(ptr->number, &aaa[5], 2, '|', sizeof ptr->number);
642 extract_token(ptr->disposition, &aaa[5], 3, '|', sizeof ptr->disposition);
643 extract_token(ptr->mimetype, &aaa[5], 4, '|', sizeof ptr->mimetype);
644 ptr->length = extract_long(&aaa[5], 5);
645 if (!mret[0]->attachments)
646 mret[0]->attachments = ptr;
648 chain = mret[0]->attachments;
654 /* Now handle multipart/alternative */
655 if (multipart_hunting > 0) {
656 if ((!strcasecmp(ptr->mimetype, "text/plain"))
657 || (!strcasecmp(ptr->mimetype, "text/html"))) {
658 strcpy(mret[0]->mime_chosen, ptr->number);
665 /* Eliminate "text\n" */
666 remove_token(bbb, 0, '\n');
668 /* If doing a MIME thing, pull out the extra headers */
671 if (!strncasecmp(bbb, "Content-type:", 13)) {
672 extract_token(mret[0]->content_type, bbb, 0, '\n', sizeof mret[0]->content_type);
673 strcpy(mret[0]->content_type, &mret[0]->content_type[13]);
674 string_trim(mret[0]->content_type);
676 /* strip out ";charset=" portion. FIXME do something with
677 * the charset (like... convert it) instead of just throwing
680 if (strstr(mret[0]->content_type, ";") != NULL) {
681 strcpy(strstr(mret[0]->content_type, ";"), "");
685 if (!strncasecmp(bbb, "X-Citadel-MSG4-Partnum:", 23)) {
686 extract_token(mret[0]->mime_chosen, bbb, 0, '\n', sizeof mret[0]->mime_chosen);
687 strcpy(mret[0]->mime_chosen, &mret[0]->mime_chosen[23]);
688 string_trim(mret[0]->mime_chosen);
690 if (!strncasecmp(bbb, "Content-transfer-encoding:", 26)) {
691 extract_token(encoding, bbb, 0, '\n', sizeof encoding);
692 strcpy(encoding, &encoding[26]);
693 string_trim(encoding);
695 remove_token(bbb, 0, '\n');
696 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
697 remove_token(bbb, 0, '\n');
704 if ((!strcasecmp(encoding, "base64")) || (!strcasecmp(encoding, "quoted-printable"))) {
706 int bytes_decoded = 0;
707 ccc = malloc(strlen(bbb) + 32768);
708 if (!strcasecmp(encoding, "base64")) {
709 bytes_decoded = CtdlDecodeBase64(ccc, bbb, strlen(bbb));
711 else if (!strcasecmp(encoding, "quoted-printable")) {
712 bytes_decoded = CtdlDecodeQuotedPrintable(ccc, bbb, strlen(bbb));
714 ccc[bytes_decoded] = 0;
719 /* FIXME: Strip trailing whitespace */
720 bbb = (char *) realloc(bbb, (size_t) (strlen(bbb) + 1));
724 bbb = (char *) realloc(bbb, 1);
734 int CtdlIPCWhoKnowsRoom(CtdlIPC * ipc, char **listing, char *cret) {
745 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
751 int CtdlIPCServerInfo(CtdlIPC * ipc, char *cret) {
754 char *listing = NULL;
760 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
761 if (ret / 100 == 1) {
764 while (*listing && strlen(listing)) {
765 extract_token(buf, listing, 0, '\n', sizeof buf);
766 remove_token(listing, 0, '\n');
769 ipc->ServInfo.pid = atoi(buf);
772 strcpy(ipc->ServInfo.nodename, buf);
775 strcpy(ipc->ServInfo.humannode, buf);
778 strcpy(ipc->ServInfo.fqdn, buf);
781 strcpy(ipc->ServInfo.software, buf);
784 ipc->ServInfo.rev_level = atoi(buf);
787 strcpy(ipc->ServInfo.site_location, buf);
790 strcpy(ipc->ServInfo.sysadm, buf);
793 strcpy(ipc->ServInfo.moreprompt, buf);
796 ipc->ServInfo.ok_floors = atoi(buf);
799 ipc->ServInfo.supports_ldap = atoi(buf);
802 ipc->ServInfo.newuser_disabled = atoi(buf);
805 strcpy(ipc->ServInfo.default_cal_zone, buf);
808 ipc->ServInfo.load_avg = atof(buf);
811 ipc->ServInfo.worker_avg = atof(buf);
814 ipc->ServInfo.thread_count = atoi(buf);
817 ipc->ServInfo.has_sieve = atoi(buf);
820 ipc->ServInfo.fulltext_enabled = atoi(buf);
823 strcpy(ipc->ServInfo.svn_revision, buf);
826 ipc->ServInfo.guest_logins = atoi(buf);
839 int CtdlIPCReadDirectory(CtdlIPC * ipc, char **listing, char *cret) {
850 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
856 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
858 int CtdlIPCSetLastRead(CtdlIPC * ipc, long msgnum, char *cret) {
866 sprintf(aaa, "SLRP %ld", msgnum);
869 sprintf(aaa, "SLRP HIGHEST");
871 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
877 int CtdlIPCInviteUserToRoom(CtdlIPC * ipc, const char *username, char *cret) {
886 aaa = (char *) malloc(strlen(username) + 6);
890 sprintf(aaa, "INVT %s", username);
891 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
898 int CtdlIPCKickoutUserFromRoom(CtdlIPC * ipc, const char *username, char *cret) {
907 aaa = (char *) malloc(strlen(username) + 6);
909 sprintf(aaa, "KICK %s", username);
910 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
917 int CtdlIPCGetRoomAttributes(CtdlIPC * ipc, struct ctdlroom **qret, char *cret) {
925 *qret = (struct ctdlroom *) calloc(1, sizeof(struct ctdlroom));
929 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
930 if (ret / 100 == 2) {
931 extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
932 extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
933 extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
934 qret[0]->QRflags = extract_int(cret, 3);
935 qret[0]->QRfloor = extract_int(cret, 4);
936 qret[0]->QRorder = extract_int(cret, 5);
937 qret[0]->QRdefaultview = extract_int(cret, 6);
938 qret[0]->QRflags2 = extract_int(cret, 7);
945 int CtdlIPCSetRoomAttributes(CtdlIPC * ipc, int forget, // if nonzero, kick all users out of the room
946 struct ctdlroom *qret, char *cret) {
955 cmd = (char *) malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) + strlen(qret->QRdirname) + 64);
959 sprintf(cmd, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
960 qret->QRname, qret->QRpasswd, qret->QRdirname,
961 qret->QRflags, forget, qret->QRfloor, qret->QRorder, qret->QRdefaultview, qret->QRflags2);
962 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
969 int CtdlIPCGetRoomAide(CtdlIPC * ipc, char *cret) {
973 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
978 int CtdlIPCSetRoomAide(CtdlIPC * ipc, const char *username, char *cret) {
987 aaa = (char *) malloc(strlen(username) + 6);
991 sprintf(aaa, "SETA %s", username);
992 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
999 int CtdlIPCPostMessage(CtdlIPC * ipc, int flag, int *subject_required, struct ctdlipcmessage *mr, char *cret) {
1009 if (mr->references) {
1010 for (ptr = mr->references; *ptr != 0; ++ptr) {
1016 snprintf(cmd, sizeof cmd,
1017 "ENT0 %d|%s|%d|%d|%s|%s||||||%s|", flag, mr->recipient,
1018 mr->anonymous, mr->type, mr->subject, mr->author, mr->references);
1019 ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL, NULL, cret);
1020 if ((flag == 0) && (subject_required != NULL)) {
1021 /* Is the server strongly recommending that the user enter a message subject? */
1022 if ((cret[3] != '\0') && (cret[4] != '\0')) {
1023 *subject_required = extract_int(&cret[4], 1);
1033 int CtdlIPCRoomInfo(CtdlIPC * ipc, char **iret, char *cret) {
1043 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
1048 int CtdlIPCDeleteMessage(CtdlIPC * ipc, long msgnum, char *cret) {
1056 sprintf(aaa, "DELE %ld", msgnum);
1057 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1062 int CtdlIPCMoveMessage(CtdlIPC * ipc, int copy, long msgnum, const char *destroom, char *cret) {
1073 aaa = (char *) malloc(strlen(destroom) + 28);
1077 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
1078 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1085 int CtdlIPCDeleteRoom(CtdlIPC * ipc, int for_real, char *cret) {
1091 sprintf(aaa, "KILL %d", for_real);
1092 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1097 int CtdlIPCCreateRoom(CtdlIPC * ipc, int for_real, const char *roomname, int type, const char *password, int floor, char *cret) {
1107 aaa = (char *) malloc(strlen(roomname) + strlen(password) + 40);
1110 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type, password, floor);
1113 aaa = (char *) malloc(strlen(roomname) + 40);
1116 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type, floor);
1118 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1125 int CtdlIPCForgetRoom(CtdlIPC * ipc, char *cret) {
1129 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
1134 int CtdlIPCSystemMessage(CtdlIPC * ipc, const char *message, char **mret, char *cret) {
1148 aaa = (char *) malloc(strlen(message) + 6);
1152 sprintf(aaa, "MESG %s", message);
1153 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
1160 int CtdlIPCNextUnvalidatedUser(CtdlIPC * ipc, char *cret) {
1164 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
1169 int CtdlIPCGetUserRegistration(CtdlIPC * ipc, const char *username, char **rret, char *cret) {
1182 aaa = (char *) malloc(strlen(username) + 6);
1184 aaa = (char *) malloc(12);
1189 sprintf(aaa, "GREG %s", username);
1191 sprintf(aaa, "GREG _SELF_");
1192 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
1199 int CtdlIPCValidateUser(CtdlIPC * ipc, const char *username, int axlevel, char *cret) {
1207 if (axlevel < AxDeleted || axlevel > AxAideU)
1210 aaa = (char *) malloc(strlen(username) + 17);
1214 sprintf(aaa, "VALI %s|%d", username, axlevel);
1215 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1222 int CtdlIPCSetRoomInfo(CtdlIPC * ipc, int for_real, const char *info, char *cret) {
1230 sprintf(aaa, "EINF %d", for_real);
1231 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1236 int CtdlIPCUserListing(CtdlIPC * ipc, char *searchstring, char **listing, char *cret) {
1250 cmd = malloc(strlen(searchstring) + 10);
1251 sprintf(cmd, "LIST %s", searchstring);
1253 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, listing, &bytes, cret);
1260 int CtdlIPCSetRegistration(CtdlIPC * ipc, const char *info, char *cret) {
1266 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info), NULL, NULL, cret);
1271 int CtdlIPCMiscCheck(CtdlIPC * ipc, struct ctdlipcmisc *chek, char *cret) {
1279 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1280 if (ret / 100 == 2) {
1281 chek->newmail = extract_long(cret, 0);
1282 chek->needregis = extract_int(cret, 1);
1283 chek->needvalid = extract_int(cret, 2);
1290 int CtdlIPCDeleteFile(CtdlIPC * ipc, const char *filename, char *cret) {
1299 aaa = (char *) malloc(strlen(filename) + 6);
1303 sprintf(aaa, "DELF %s", filename);
1304 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1311 int CtdlIPCMoveFile(CtdlIPC * ipc, const char *filename, const char *destroom, char *cret) {
1322 aaa = (char *) malloc(strlen(filename) + strlen(destroom) + 7);
1326 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1327 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1334 int CtdlIPCOnlineUsers(CtdlIPC * ipc, char **listing, time_t * stamp, char *cret) {
1345 *stamp = CtdlIPCServerTime(ipc, cret);
1347 *stamp = time(NULL);
1348 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1354 int CtdlIPCFileDownload(CtdlIPC * ipc, const char *filename, void **buf, size_t resume, void (*progress_gauge_callback)
1355 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1370 if (ipc->downloading)
1373 aaa = (char *) malloc(strlen(filename) + 6);
1377 sprintf(aaa, "OPEN %s", filename);
1378 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1380 if (ret / 100 == 2) {
1381 ipc->downloading = 1;
1382 bytes = extract_long(cret, 0);
1383 last_mod = extract_int(cret, 1);
1384 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1385 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret);
1386 ret = CtdlIPCEndDownload(ipc, cret);
1388 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1395 int CtdlIPCAttachmentDownload(CtdlIPC * ipc, long msgnum, const char *part, void **buf, void (*progress_gauge_callback)
1396 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1414 if (ipc->downloading)
1417 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1418 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1419 if (ret / 100 == 2) {
1420 ipc->downloading = 1;
1421 bytes = extract_long(cret, 0);
1422 last_mod = extract_int(cret, 1);
1423 extract_token(filename, cret, 2, '|', sizeof filename);
1424 extract_token(mimetype, cret, 3, '|', sizeof mimetype);
1425 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1426 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1427 ret = CtdlIPCEndDownload(ipc, cret);
1429 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1436 int CtdlIPCImageDownload(CtdlIPC * ipc, const char *filename, void **buf, void (*progress_gauge_callback)
1437 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1452 if (ipc->downloading)
1455 aaa = (char *) malloc(strlen(filename) + 6);
1459 sprintf(aaa, "OIMG %s", filename);
1460 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1462 if (ret / 100 == 2) {
1463 ipc->downloading = 1;
1464 bytes = extract_long(cret, 0);
1465 last_mod = extract_int(cret, 1);
1466 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1468 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1469 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1470 ret = CtdlIPCEndDownload(ipc, cret);
1472 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1479 int CtdlIPCFileUpload(CtdlIPC * ipc, const char *save_as, const char *comment, const char *path, void (*progress_gauge_callback)
1480 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1484 char MimeTestBuf[64];
1485 const char *MimeType;
1501 uploadFP = fopen(path, "r");
1505 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1510 MimeType = GuessMimeType(&MimeTestBuf[0], len);
1511 aaa = (char *) malloc(strlen(save_as) + strlen(MimeType) + strlen(comment) + 7);
1515 sprintf(aaa, "UOPN %s|%s|%s", save_as, MimeType, comment);
1516 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1518 if (ret / 100 == 2) {
1520 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1521 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1529 int CtdlIPCImageUpload(CtdlIPC * ipc, int for_real, const char *path, const char *save_as, void (*progress_gauge_callback)
1530 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1534 char MimeTestBuf[64];
1535 const char *MimeType;
1542 if (!path && for_real)
1544 if (!*path && for_real)
1549 aaa = (char *) malloc(strlen(save_as) + 17);
1553 uploadFP = fopen(path, "r");
1557 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1561 MimeType = GuessMimeType(&MimeTestBuf[0], 64);
1563 sprintf(aaa, "UIMG %d|%s|%s", for_real, MimeType, save_as);
1564 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1566 if (ret / 100 == 2 && for_real) {
1568 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1569 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1577 int CtdlIPCQueryUsername(CtdlIPC * ipc, const char *username, char *cret) {
1586 aaa = (char *) malloc(strlen(username) + 6);
1590 sprintf(aaa, "QUSR %s", username);
1591 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1598 int CtdlIPCFloorListing(CtdlIPC * ipc, char **listing, char *cret) {
1608 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1613 int CtdlIPCCreateFloor(CtdlIPC * ipc, int for_real, const char *name, char *cret) {
1622 sprintf(aaa, "CFLR %s|%d", name, for_real);
1623 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1629 int CtdlIPCDeleteFloor(CtdlIPC * ipc, int for_real, int floornum, char *cret) {
1637 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1638 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1643 int CtdlIPCEditFloor(CtdlIPC * ipc, int floornum, const char *floorname, char *cret) {
1654 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1655 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1663 * You only need to fill out hostname, the defaults will be used if any of the
1664 * other fields are not set properly.
1666 int CtdlIPCIdentifySoftware(CtdlIPC * ipc, int developerid, int clientid,
1667 int revision, const char *software_name, const char *hostname, char *cret) {
1671 if (developerid < 0 || clientid < 0 || revision < 0 || !software_name) {
1674 revision = CLIENT_VERSION - 600;
1675 software_name = "Citadel text client";
1680 aaa = (char *) malloc(strlen(software_name) + strlen(hostname) + 29);
1684 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid, revision, software_name, hostname);
1685 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1692 int CtdlIPCSendInstantMessage(CtdlIPC * ipc, const char *username, const char *text, char *cret) {
1701 aaa = (char *) malloc(strlen(username) + 8);
1706 sprintf(aaa, "SEXP %s|-", username);
1707 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1710 sprintf(aaa, "SEXP %s||", username);
1711 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1719 int CtdlIPCGetInstantMessage(CtdlIPC * ipc, char **listing, char *cret) {
1729 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1735 /* mode is 0 = enable, 1 = disable, 2 = status */
1736 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC * ipc, int mode, char *cret) {
1742 sprintf(aaa, "DEXP %d", mode);
1743 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1748 int CtdlIPCSetBio(CtdlIPC * ipc, char *bio, char *cret) {
1754 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio), NULL, NULL, cret);
1759 int CtdlIPCGetBio(CtdlIPC * ipc, const char *username, char **listing, char *cret) {
1773 aaa = (char *) malloc(strlen(username) + 6);
1777 sprintf(aaa, "RBIO %s", username);
1778 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1785 int CtdlIPCListUsersWithBios(CtdlIPC * ipc, char **listing, char *cret) {
1795 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1800 int CtdlIPCStealthMode(CtdlIPC * ipc, int mode, char *cret) {
1806 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1807 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1812 int CtdlIPCTerminateSession(CtdlIPC * ipc, int sid, char *cret) {
1818 sprintf(aaa, "TERM %d", sid);
1819 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1824 int CtdlIPCTerminateServerNow(CtdlIPC * ipc, char *cret) {
1828 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1833 int CtdlIPCTerminateServerScheduled(CtdlIPC * ipc, int mode, char *cret) {
1839 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1840 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1845 int CtdlIPCEnterSystemMessage(CtdlIPC * ipc, const char *filename, const char *text, char *cret) {
1856 aaa = (char *) malloc(strlen(filename) + 6);
1860 sprintf(aaa, "EMSG %s", filename);
1861 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1869 /* This function returns the actual server time reported, or 0 if error */
1870 time_t CtdlIPCServerTime(CtdlIPC * ipc, char *cret) {
1874 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1875 if (ret / 100 == 2) {
1876 tret = extract_long(cret, 0);
1886 int CtdlIPCAideGetUserParameters(CtdlIPC * ipc, const char *who, struct ctdluser **uret, char *cret) {
1895 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
1899 sprintf(aaa, "AGUP %s", who);
1900 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1902 if (ret / 100 == 2) {
1903 extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
1904 extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
1905 uret[0]->flags = extract_int(cret, 2);
1906 uret[0]->timescalled = extract_long(cret, 3);
1907 uret[0]->posted = extract_long(cret, 4);
1908 uret[0]->axlevel = extract_int(cret, 5);
1909 uret[0]->usernum = extract_long(cret, 6);
1910 uret[0]->lastcall = extract_long(cret, 7);
1911 uret[0]->USuserpurge = extract_int(cret, 8);
1918 int CtdlIPCAideSetUserParameters(CtdlIPC * ipc, const struct ctdluser *uret, char *cret) {
1927 aaa = (char *) malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1931 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1932 uret->fullname, uret->password, uret->flags, uret->timescalled,
1933 uret->posted, uret->axlevel, uret->usernum, uret->lastcall, uret->USuserpurge);
1934 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1941 int CtdlIPCAideGetEmailAddresses(CtdlIPC * ipc, const char *who, char *target_buf, char *cret) {
1944 char *emailaddrs = NULL;
1945 size_t emailaddrs_len = 0;
1947 sprintf(aaa, "AGEA %s", who);
1948 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &emailaddrs, &emailaddrs_len, cret);
1950 if (ret / 100 == 1) {
1951 strcpy(target_buf, emailaddrs);
1954 if (emailaddrs != NULL) {
1963 int CtdlIPCAideSetEmailAddresses(CtdlIPC * ipc, const char *who, char *emailaddrs, char *cret) {
1974 sprintf(aaa, "ASEA %s", who);
1975 ret = CtdlIPCGenericCommand(ipc, aaa, emailaddrs, 0, NULL, NULL, cret);
1982 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1984 /* caller must free the struct ExpirePolicy */
1985 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC * ipc, GPEXWhichPolicy which, struct ExpirePolicy **policy, char *cret) {
1986 static char *proto[] = {
1990 strof(mailboxespolicy)
2000 *policy = (struct ExpirePolicy *) calloc(1, sizeof(struct ExpirePolicy));
2003 if (which < 0 || which > 3)
2006 sprintf(cmd, "GPEX %s", proto[which]);
2007 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2008 if (ret / 100 == 2) {
2009 policy[0]->expire_mode = extract_int(cret, 0);
2010 policy[0]->expire_value = extract_int(cret, 1);
2018 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
2020 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
2021 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC * ipc, int which, struct ExpirePolicy *policy, char *cret) {
2023 char *whichvals[] = { "room", "floor", "site", "mailboxes" };
2027 if (which < 0 || which > 3)
2031 if (policy->expire_mode < 0 || policy->expire_mode > 3)
2033 if (policy->expire_mode >= 2 && policy->expire_value < 1)
2036 sprintf(aaa, "SPEX %s|%d|%d", whichvals[which], policy->expire_mode, policy->expire_value);
2037 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2042 int CtdlIPCGetSystemConfig(CtdlIPC * ipc, char **listing, char *cret) {
2052 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0, listing, &bytes, cret);
2057 int CtdlIPCSetSystemConfig(CtdlIPC * ipc, const char *listing, char *cret) {
2063 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing), NULL, NULL, cret);
2068 int CtdlIPCGetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, char **listing, char *cret) {
2082 aaa = malloc(strlen(mimetype) + 13);
2085 sprintf(aaa, "CONF GETSYS|%s", mimetype);
2086 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
2093 int CtdlIPCSetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, const char *listing, char *cret) {
2104 aaa = malloc(strlen(mimetype) + 13);
2107 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
2108 ret = CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing), NULL, NULL, cret);
2115 int CtdlIPCGetRoomNetworkConfig(CtdlIPC * ipc, char **listing, char *cret) {
2125 return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0, listing, &bytes, cret);
2130 int CtdlIPCSetRoomNetworkConfig(CtdlIPC * ipc, const char *listing, char *cret) {
2136 return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing), NULL, NULL, cret);
2141 int CtdlIPCRequestClientLogout(CtdlIPC * ipc, int session, char *cret) {
2149 sprintf(aaa, "REQT %d", session);
2150 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2155 int CtdlIPCSetMessageSeen(CtdlIPC * ipc, long msgnum, int seen, char *cret) {
2163 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
2164 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2169 int CtdlIPCStartEncryption(CtdlIPC * ipc, char *cret) {
2177 /* New SSL object */
2178 temp_ssl = SSL_new(ssl_ctx);
2180 error_printf("SSL_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2183 /* Pointless flag waving */
2184 #if SSLEAY_VERSION_NUMBER >= 0x0922
2185 SSL_set_session_id_context(temp_ssl, (const unsigned char *) "Citadel SID", 14);
2188 /* Associate network connection with SSL object */
2189 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
2190 error_printf("SSL_set_fd failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2194 if (status_hook != NULL) {
2195 status_hook("Requesting encryption...\r");
2198 /* Ready to start SSL/TLS */
2199 r = CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
2201 error_printf("Server can't start TLS: %s\n", buf);
2206 /* Do SSL/TLS handshake */
2207 if ((a = SSL_connect(temp_ssl)) < 1) {
2208 error_printf("SSL_connect failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2212 ipc->ssl = temp_ssl;
2214 error_printf("Encrypting with %s cipher %s\n",
2215 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)), SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl))
2220 #endif /* HAVE_OPENSSL */
2225 static void endtls(SSL * ssl) {
2235 int CtdlIPCDirectoryLookup(CtdlIPC * ipc, const char *address, char *cret) {
2244 aaa = (char *) malloc(strlen(address) + 6);
2248 sprintf(aaa, "QDIR %s", address);
2249 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2256 int CtdlIPCInternalProgram(CtdlIPC * ipc, int secret, char *cret) {
2261 sprintf(aaa, "IPGM %d", secret);
2262 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2268 /* ************************************************************************** */
2270 /* Stuff below this line is not for public consumption */
2272 /* ************************************************************************** */
2275 /* Read a listing from the server up to 000. Append to dest if it exists */
2276 char *CtdlIPCReadListing(CtdlIPC * ipc, char *dest) {
2284 length = strlen(ret);
2290 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2291 linelength = strlen(aaa);
2292 ret = (char *) realloc(ret, (size_t) (length + linelength + 2));
2294 strcpy(&ret[length], aaa);
2295 length += linelength;
2296 strcpy(&ret[length++], "\n");
2304 /* Send a listing to the server; generate the ending 000. */
2305 int CtdlIPCSendListing(CtdlIPC * ipc, const char *listing) {
2308 text = (char *) malloc(strlen(listing) + 6);
2310 strcpy(text, listing);
2311 while (text[strlen(text) - 1] == '\n')
2312 text[strlen(text) - 1] = '\0';
2313 strcat(text, "\n000");
2314 CtdlIPC_putline(ipc, text);
2319 /* Malloc failed but we are committed to send */
2320 /* This may result in extra blanks at the bottom */
2321 CtdlIPC_putline(ipc, text);
2322 CtdlIPC_putline(ipc, "000");
2328 /* Partial read of file from server */
2329 size_t CtdlIPCPartialRead(CtdlIPC * ipc, void **buf, size_t offset, size_t bytes, char *cret) {
2341 sprintf(aaa, "READ %d|%d", (int) offset, (int) bytes);
2342 CtdlIPC_putline(ipc, aaa);
2343 CtdlIPC_getline(ipc, aaa);
2345 strcpy(cret, &aaa[4]);
2347 len = extract_long(&aaa[4], 0);
2348 *buf = (void *) realloc(*buf, (size_t) (offset + len));
2350 /* I know what I'm doing */
2351 serv_read(ipc, ((char *) (*buf) + offset), len);
2354 /* We have to read regardless */
2355 serv_read(ipc, aaa, len);
2359 CtdlIPC_unlock(ipc);
2365 int CtdlIPCEndDownload(CtdlIPC * ipc, char *cret) {
2370 if (!ipc->downloading)
2373 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2375 ipc->downloading = 0;
2381 int CtdlIPCSpecifyPreferredFormats(CtdlIPC * ipc, char *cret, char *formats) {
2385 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2386 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2393 int CtdlIPCReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2394 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
2403 if (!ipc->downloading)
2407 if (progress_gauge_callback)
2408 progress_gauge_callback(ipc, len, bytes);
2409 while (len < bytes) {
2412 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2418 if (progress_gauge_callback)
2419 progress_gauge_callback(ipc, len, bytes);
2424 /* READ - pipelined */
2425 int CtdlIPCHighSpeedReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2426 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
2428 int calls; /* How many calls in the pipeline */
2429 int i; /* iterator */
2438 if (!ipc->downloading)
2441 *buf = (void *) realloc(*buf, bytes - resume);
2447 if (progress_gauge_callback)
2448 progress_gauge_callback(ipc, len, bytes);
2450 /* How many calls will be in the pipeline? */
2451 calls = (bytes - resume) / 4096;
2452 if ((bytes - resume) % 4096)
2455 /* Send all requests at once */
2456 for (i = 0; i < calls; i++) {
2457 sprintf(aaa, "READ %d|4096", (int) (i * 4096 + resume));
2458 CtdlIPC_putline(ipc, aaa);
2461 /* Receive all responses at once */
2462 for (i = 0; i < calls; i++) {
2463 CtdlIPC_getline(ipc, aaa);
2465 strcpy(cret, &aaa[4]);
2467 len = extract_long(&aaa[4], 0);
2468 /* I know what I'm doing */
2469 serv_read(ipc, ((char *) (*buf) + (i * 4096)), len);
2471 if (progress_gauge_callback)
2472 progress_gauge_callback(ipc, i * 4096 + len, bytes);
2474 CtdlIPC_unlock(ipc);
2480 int CtdlIPCEndUpload(CtdlIPC * ipc, int discard, char *cret) {
2486 if (!ipc->uploading)
2489 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2490 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2497 int CtdlIPCWriteUpload(CtdlIPC * ipc, FILE * uploadFP, void (*progress_gauge_callback)
2498 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
2504 FILE *fd = uploadFP;
2510 fseek(fd, 0L, SEEK_END);
2514 if (progress_gauge_callback)
2515 progress_gauge_callback(ipc, 0, bytes);
2517 while (offset < bytes) {
2520 /* Read some data in */
2521 to_write = fread(buf, 1, 4096, fd);
2523 if (feof(fd) || ferror(fd))
2526 sprintf(aaa, "WRIT %d", (int) to_write);
2527 CtdlIPC_putline(ipc, aaa);
2528 CtdlIPC_getline(ipc, aaa);
2529 strcpy(cret, &aaa[4]);
2531 if (aaa[0] == '7') {
2532 to_write = extract_long(&aaa[4], 0);
2534 serv_write(ipc, buf, to_write);
2536 if (progress_gauge_callback)
2537 progress_gauge_callback(ipc, offset, bytes);
2538 /* Detect short reads and back up if needed */
2539 /* offset will never be negative anyway */
2540 fseek(fd, (signed) offset, SEEK_SET);
2546 if (progress_gauge_callback)
2547 progress_gauge_callback(ipc, 1, 1);
2550 return (!ferr ? ret : -2);
2555 * Generic command method. This method should handle any server command
2556 * except for CHAT. It takes the following arguments:
2558 * ipc The server to speak with
2559 * command Preformatted command to send to server
2560 * to_send A text or binary file to send to server
2561 * (only sent if server requests it)
2562 * bytes_to_send The number of bytes in to_send (required if
2563 * sending binary, optional if sending listing)
2564 * to_receive Pointer to a NULL pointer, if the server
2565 * sends text or binary we will allocate memory
2566 * for the file and stuff it here
2567 * bytes_to_receive If a file is received, we will store its
2569 * proto_response The protocol response. Caller must provide
2570 * this buffer and ensure that it is at least
2571 * 128 bytes in length.
2573 * This function returns a number equal to the protocol response number,
2574 * -1 if an internal error occurred, -2 if caller provided bad values,
2575 * or 0 - the protocol response number if bad values were found during
2576 * the protocol exchange.
2578 * It stores the protocol response string (minus the number) in
2579 * protocol_response as described above. Some commands send additional
2580 * data in this string.
2582 int CtdlIPCGenericCommand(CtdlIPC * ipc,
2583 const char *command,
2584 const char *to_send,
2585 size_t bytes_to_send, char **to_receive, size_t *bytes_to_receive, char *proto_response) {
2592 if (!proto_response) {
2597 CtdlIPC_putline(ipc, command);
2599 CtdlIPC_getline(ipc, proto_response);
2600 if (proto_response[3] == '*')
2602 ret = atoi(proto_response);
2603 strcpy(proto_response, &proto_response[4]);
2604 switch (ret / 100) {
2605 default: /* Unknown, punt */
2607 case 3: /* MORE_DATA */
2609 /* Don't need to do anything */
2611 case 1: /* LISTING_FOLLOWS */
2612 if (to_receive && !*to_receive && bytes_to_receive) {
2613 *to_receive = CtdlIPCReadListing(ipc, NULL);
2616 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2620 case 4: /* SEND_LISTING */
2622 CtdlIPCSendListing(ipc, to_send);
2625 /* No listing given, fake it */
2626 CtdlIPC_putline(ipc, "000");
2630 case 6: /* BINARY_FOLLOWS */
2631 if (to_receive && !*to_receive && bytes_to_receive) {
2632 *bytes_to_receive = extract_long(proto_response, 0);
2633 *to_receive = (char *)
2634 malloc((size_t) *bytes_to_receive);
2639 serv_read(ipc, *to_receive, *bytes_to_receive);
2646 drain = extract_long(proto_response, 0);
2647 while (drain > SIZ) {
2648 serv_read(ipc, buf, SIZ);
2651 serv_read(ipc, buf, drain);
2655 case 7: /* SEND_BINARY */
2656 if (to_send && bytes_to_send) {
2657 serv_write(ipc, to_send, bytes_to_send);
2659 else if (bytes_to_send) {
2660 /* Fake it, send nulls */
2663 fake = bytes_to_send;
2664 memset(buf, '\0', SIZ);
2665 while (fake > SIZ) {
2666 serv_write(ipc, buf, SIZ);
2669 serv_write(ipc, buf, fake);
2671 } /* else who knows? DANGER WILL ROBINSON */
2673 case 8: /* START_CHAT_MODE */
2674 if (!strncasecmp(command, "CHAT", 4)) {
2675 /* Don't call chatmode with generic! */
2676 CtdlIPC_putline(ipc, "/quit");
2680 /* In this mode we send then receive listing */
2682 CtdlIPCSendListing(ipc, to_send);
2685 /* No listing given, fake it */
2686 CtdlIPC_putline(ipc, "000");
2689 if (to_receive && !*to_receive && bytes_to_receive) {
2690 *to_receive = CtdlIPCReadListing(ipc, NULL);
2693 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2698 case 9: /* ASYNC_MSG */
2699 /* CtdlIPCDoAsync(ret, proto_response); */
2700 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2706 CtdlIPC_unlock(ipc);
2712 * Connect to a Citadel on a remote host using a TCP/IP socket
2714 static int tcp_connectsock(char *host, char *service) {
2715 struct in6_addr serveraddr;
2716 struct addrinfo hints;
2717 struct addrinfo *res = NULL;
2718 struct addrinfo *ai = NULL;
2722 if ((host == NULL) || IsEmptyStr(host)) {
2723 service = DEFAULT_HOST;
2725 if ((service == NULL) || IsEmptyStr(service)) {
2726 service = DEFAULT_PORT;
2729 memset(&hints, 0x00, sizeof(hints));
2730 hints.ai_flags = AI_NUMERICSERV;
2731 hints.ai_family = AF_UNSPEC;
2732 hints.ai_socktype = SOCK_STREAM;
2735 * Handle numeric IPv4 and IPv6 addresses
2737 rc = inet_pton(AF_INET, host, &serveraddr);
2738 if (rc == 1) { /* dotted quad */
2739 hints.ai_family = AF_INET;
2740 hints.ai_flags |= AI_NUMERICHOST;
2743 rc = inet_pton(AF_INET6, host, &serveraddr);
2744 if (rc == 1) { /* IPv6 address */
2745 hints.ai_family = AF_INET6;
2746 hints.ai_flags |= AI_NUMERICHOST;
2750 /* Begin the connection process */
2752 rc = getaddrinfo(host, service, &hints, &res);
2758 * Try all available addresses until we connect to one or until we run out.
2760 for (ai = res; ai != NULL; ai = ai->ai_next) {
2761 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2765 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
2767 return (sock); /* Connected! */
2770 close(sock); /* Failed. Close the socket to avoid fd leak! */
2778 * Connect to a Citadel on the local host using a unix domain socket
2780 static int uds_connectsock(int *isLocal, char *sockpath) {
2781 struct sockaddr_un addr;
2784 memset(&addr, 0, sizeof(addr));
2785 addr.sun_family = AF_UNIX;
2786 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2788 s = socket(AF_UNIX, SOCK_STREAM, 0);
2793 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2804 * input binary data from socket
2806 static void serv_read(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2807 unsigned int len, rlen;
2809 #if defined(HAVE_OPENSSL)
2811 serv_read_ssl(ipc, buf, bytes);
2816 while (len < bytes) {
2817 rlen = read(ipc->sock, &buf[len], bytes - len);
2819 connection_died(ipc, 0);
2828 * send binary to server
2830 void serv_write(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2831 unsigned int bytes_written = 0;
2834 #if defined(HAVE_OPENSSL)
2836 serv_write_ssl(ipc, buf, nbytes);
2840 while (bytes_written < nbytes) {
2841 retval = write(ipc->sock, &buf[bytes_written], nbytes - bytes_written);
2843 connection_died(ipc, 0);
2846 bytes_written += retval;
2854 * input binary data from encrypted connection
2856 static void serv_read_ssl(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2861 while (len < bytes) {
2862 if (SSL_want_read(ipc->ssl)) {
2863 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2864 error_printf("SSL_write in serv_read:\n");
2865 ERR_print_errors_fp(stderr);
2868 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2872 errval = SSL_get_error(ipc->ssl, rlen);
2873 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2877 error_printf("SSL_read in serv_read: %s\n", ERR_reason_error_string(ERR_peek_error()));
2878 connection_died(ipc, 1);
2887 * send binary to server encrypted
2889 static void serv_write_ssl(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2890 unsigned int bytes_written = 0;
2894 while (bytes_written < nbytes) {
2895 if (SSL_want_write(ipc->ssl)) {
2896 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2897 error_printf("SSL_read in serv_write:\n");
2898 ERR_print_errors_fp(stderr);
2901 retval = SSL_write(ipc->ssl, &buf[bytes_written], nbytes - bytes_written);
2905 errval = SSL_get_error(ipc->ssl, retval);
2906 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2910 if (errval == SSL_ERROR_ZERO_RETURN || errval == SSL_ERROR_SSL) {
2911 serv_write(ipc, &buf[bytes_written], nbytes - bytes_written);
2914 error_printf("SSL_write in serv_write: %s\n", ERR_reason_error_string(ERR_peek_error()));
2915 connection_died(ipc, 1);
2918 bytes_written += retval;
2923 static void CtdlIPC_init_OpenSSL(void) {
2925 const SSL_METHOD *ssl_method;
2928 /* already done init */
2937 SSL_load_error_strings();
2938 SSLeay_add_ssl_algorithms();
2940 /* Set up the SSL context in which we will oeprate */
2941 ssl_method = SSLv23_client_method();
2942 ssl_ctx = SSL_CTX_new(ssl_method);
2944 error_printf("SSL_CTX_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2947 /* Any reasonable cipher we can get */
2948 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2949 error_printf("No ciphers available for encryption\n");
2952 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2954 /* Load DH parameters into the context */
2957 error_printf("Can't allocate a DH object: %s\n", ERR_reason_error_string(ERR_get_error()));
2961 if (!(DH_generate_parameters_ex(dh, 128, DH_GENERATOR_2, 0))) {
2962 error_printf("Can't generate DH parameters: %s\n", ERR_reason_error_string(ERR_get_error()));
2967 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2971 #endif /* HAVE_OPENSSL */
2974 int ReadNetworkChunk(CtdlIPC * ipc) {
2988 FD_SET(ipc->sock, &read_fd);
2989 ret = select(ipc->sock + 1, &read_fd, NULL, NULL, &tv);
2993 *(ipc->BufPtr) = '\0';
2994 n = recv(ipc->sock, ipc->BufPtr, ipc->BufSize - (ipc->BufPtr - ipc->Buf) - 1, 0);
2996 ipc->BufPtr[n] = '\0';
3005 if (!(errno == EINTR || errno == EAGAIN))
3006 error_printf("\nselect failed: %d %s\n", err, strerror(err));
3014 * input string from socket - implemented in terms of serv_read()
3018 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3020 char *aptr, *bptr, *aeptr, *beptr;
3024 #if defined(HAVE_OPENSSL)
3027 /* Read one character at a time. */
3029 serv_read(ipc, &buf[i], 1);
3030 if (buf[i] == '\n' || i == (SIZ - 1))
3034 /* If we got a long line, discard characters until the newline. */
3036 while (buf[i] != '\n')
3037 serv_read(ipc, &buf[i], 1);
3039 /* Strip the trailing newline (and carriage return, if present) */
3040 if (i >= 0 && buf[i] == 10)
3042 if (i >= 0 && buf[i] == 13)
3048 if (ipc->Buf == NULL) {
3050 ipc->Buf = (char *) malloc(ipc->BufSize + 10);
3052 ipc->BufPtr = ipc->Buf;
3056 if (ipc->BufUsed == 0)
3057 ReadNetworkChunk(ipc);
3062 aeptr = ipc->Buf + ipc->BufSize;
3063 while ((aptr < aeptr) && (bptr < beptr) && (*aptr != '\0') && (*aptr != '\n'))
3064 *(bptr++) = *(aptr++);
3065 if ((*aptr == '\n') && (aptr < aeptr)) {
3066 /* Terminate it right, remove the line breaks */
3067 while ((aptr < aeptr) && ((*aptr == '\n') || (*aptr == '\r')))
3069 while ((aptr < aeptr) && (*(aptr + 1) == '\0'))
3072 if ((bptr > buf + 1) && (*(bptr - 1) == '\r'))
3075 /* is there more in the buffer we need to read later? */
3076 if (ipc->Buf + ipc->BufUsed > aptr) {
3081 ipc->BufPtr = ipc->Buf;
3086 // should we move our read stuf to the bufferstart so we have more space at the end?
3087 else if ((ipc->BufPtr != ipc->Buf) && (ipc->BufUsed > (ipc->BufSize - (ipc->BufSize / 4)))) {
3088 size_t NewBufSize = ipc->BufSize * 2;
3089 int delta = (ipc->BufPtr - ipc->Buf);
3092 /* if the line would end after our buffer, we should use a bigger buffer. */
3093 NewBuf = (char *) malloc(NewBufSize + 10);
3094 memcpy(NewBuf, ipc->BufPtr, ipc->BufUsed - delta);
3096 ipc->Buf = ipc->BufPtr = NewBuf;
3097 ipc->BufUsed -= delta;
3098 ipc->BufSize = NewBufSize;
3100 if (ReadNetworkChunk(ipc) < 0) {
3107 #else /* CHUNKED_READ */
3109 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3112 /* Read one character at a time. */
3114 serv_read(ipc, &buf[i], 1);
3115 if (buf[i] == '\n' || i == (SIZ - 1))
3119 /* If we got a long line, discard characters until the newline. */
3121 while (buf[i] != '\n')
3122 serv_read(ipc, &buf[i], 1);
3124 /* Strip the trailing newline (and carriage return, if present) */
3125 if (i >= 0 && buf[i] == 10)
3127 if (i >= 0 && buf[i] == 13)
3132 #endif /* CHUNKED_READ */
3135 void CtdlIPC_chat_recv(CtdlIPC * ipc, char *buf) {
3136 CtdlIPC_getline(ipc, buf);
3140 * send line to server - implemented in terms of serv_write()
3142 static void CtdlIPC_putline(CtdlIPC * ipc, const char *buf) {
3147 cmd = malloc(len + 2);
3149 /* This requires no extra memory */
3150 serv_write(ipc, buf, len);
3151 serv_write(ipc, "\n", 1);
3154 /* This is network-optimized */
3155 strncpy(cmd, buf, len);
3156 strcpy(cmd + len, "\n");
3157 serv_write(ipc, cmd, len + 1);
3161 ipc->last_command_sent = time(NULL);
3165 void CtdlIPC_chat_send(CtdlIPC * ipc, const char *buf) {
3166 CtdlIPC_putline(ipc, buf);
3173 CtdlIPC *CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf) {
3180 ipc = malloc(sizeof(struct _CtdlIPC));
3184 #if defined(HAVE_OPENSSL)
3186 CtdlIPC_init_OpenSSL();
3188 ipc->sock = -1; /* Not connected */
3189 ipc->isLocal = 0; /* Not local, of course! */
3190 ipc->downloading = 0;
3192 ipc->last_command_sent = 0L;
3193 ipc->network_status_cb = NULL;
3198 strcpy(cithost, DEFAULT_HOST); /* default host */
3199 strcpy(citport, DEFAULT_PORT); /* default port */
3201 /* Allow caller to supply our values */
3202 if (hostbuf && strlen(hostbuf) > 0) {
3203 strcpy(cithost, hostbuf);
3205 if (portbuf && strlen(portbuf) > 0) {
3206 strcpy(citport, portbuf);
3209 /* Read host/port from command line if present */
3210 for (a = 0; a < argc; ++a) {
3215 strcpy(cithost, argv[a]);
3218 strcpy(citport, argv[a]);
3221 error_printf("%s: usage: ", argv[0]);
3222 error_printf("%s [host] [port] ", argv[0]);
3229 if ((!strcmp(cithost, "localhost")) || (!strcmp(cithost, "127.0.0.1"))) {
3233 /* If we're using a unix domain socket we can do a bunch of stuff */
3234 if (!strcmp(cithost, UDS)) {
3235 if (!strcasecmp(citport, DEFAULT_PORT)) {
3236 snprintf(sockpath, sizeof sockpath, "%s", file_citadel_socket);
3239 snprintf(sockpath, sizeof sockpath, "%s/%s", citport, "citadel.socket");
3241 printf("[%s]\n", sockpath);
3242 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
3243 if (ipc->sock == -1) {
3247 if (hostbuf != NULL)
3248 strcpy(hostbuf, cithost);
3249 if (portbuf != NULL)
3250 strcpy(portbuf, sockpath);
3251 strcpy(ipc->ip_hostname, "");
3252 strcpy(ipc->ip_address, "");
3256 printf("[%s:%s]\n", cithost, citport);
3257 ipc->sock = tcp_connectsock(cithost, citport);
3258 if (ipc->sock == -1) {
3264 /* Learn the actual network identity of the host to which we are connected */
3266 struct sockaddr_in6 clientaddr;
3267 unsigned int addrlen = sizeof(clientaddr);
3269 ipc->ip_hostname[0] = 0;
3270 ipc->ip_address[0] = 0;
3272 getpeername(ipc->sock, (struct sockaddr *) &clientaddr, &addrlen);
3273 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_hostname, sizeof ipc->ip_hostname, NULL, 0, 0);
3274 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_address, sizeof ipc->ip_address, NULL, 0, NI_NUMERICHOST);
3276 /* stuff other things elsewhere */
3278 if (hostbuf != NULL)
3279 strcpy(hostbuf, cithost);
3280 if (portbuf != NULL)
3281 strcpy(portbuf, citport);
3287 * Disconnect and delete the IPC class (destructor)
3289 void CtdlIPC_delete(CtdlIPC * ipc) {
3292 SSL_shutdown(ipc->ssl);
3297 if (ipc->sock > -1) {
3298 shutdown(ipc->sock, 2); /* Close it up */
3301 if (ipc->Buf != NULL)
3310 * Disconnect and delete the IPC class (destructor)
3311 * Also NULLs out the pointer
3313 void CtdlIPC_delete_ptr(CtdlIPC ** pipc) {
3314 CtdlIPC_delete(*pipc);