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)
160 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
165 * Does nothing interesting. The server should always return 200
166 * along with your string.
168 int CtdlIPCEcho(CtdlIPC * ipc, const char *arg, char *cret)
178 aaa = (char *) malloc((size_t) (strlen(arg) + 6));
182 sprintf(aaa, "ECHO %s", arg);
183 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
190 * Asks the server to close the connecction.
191 * Should always return 200.
193 int CtdlIPCQuit(CtdlIPC * ipc) {
194 int ret = 221; /* Default to successful quit */
198 if (ipc->sock > -1) {
199 CtdlIPC_putline(ipc, "QUIT");
200 CtdlIPC_getline(ipc, aaa);
205 SSL_shutdown(ipc->ssl);
209 shutdown(ipc->sock, 2); /* Close connection; we're dead */
217 * Asks the server to log out. Should always return 200, even if no user
218 * was logged in. The user will not be logged in after this!
220 int CtdlIPCLogout(CtdlIPC * ipc)
226 CtdlIPC_putline(ipc, "LOUT");
227 CtdlIPC_getline(ipc, aaa);
235 * First stage of authentication - pass the username. Returns 300 if the
236 * username is able to log in, with the username correctly spelled in cret.
237 * Returns various 500 error codes if the user doesn't exist, etc.
239 int CtdlIPCTryLogin(CtdlIPC * ipc, const char *username, char *cret)
249 aaa = (char *) malloc((size_t) (strlen(username) + 6));
253 sprintf(aaa, "USER %s", username);
254 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
261 * Second stage of authentication - provide password. The server returns
262 * 200 and several arguments in cret relating to the user's account.
264 int CtdlIPCTryPassword(CtdlIPC * ipc, const char *passwd, char *cret)
274 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
278 sprintf(aaa, "PASS %s", passwd);
279 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
286 * Create a new user. This returns 200 plus the same arguments as TryPassword
287 * if selfservice is nonzero, unless there was a problem creating the account.
288 * If selfservice is zero, creates a new user but does not log out the existing
289 * user - intended for use by system administrators to create accounts on
290 * behalf of other users.
292 int CtdlIPCCreateUser(CtdlIPC * ipc, const char *username, int selfservice, char *cret)
302 aaa = (char *) malloc((size_t) (strlen(username) + 6));
306 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
307 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
314 * Changes the user's password. Returns 200 if changed, errors otherwise.
316 int CtdlIPCChangePassword(CtdlIPC * ipc, const char *passwd, char *cret)
326 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
330 sprintf(aaa, "SETP %s", passwd);
331 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
338 /* Caller must free the march list */
339 /* Room types are defined in enum RoomList; keep these in sync! */
340 /* floor is -1 for all, or floornum */
341 int CtdlIPCKnownRooms(CtdlIPC * ipc, enum RoomList which, int floor, struct march **listing, char *cret)
344 struct march *march = NULL;
345 static char *proto[] = { "LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
353 return -2; /* Free the listing first */
356 /* if (which < 0 || which > 4) return -2; */
358 return -2; /* Can't validate upper bound, sorry */
360 sprintf(aaa, "%s %d", proto[which], floor);
361 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
362 if (ret / 100 == 1) {
365 while (bbb && strlen(bbb)) {
368 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
370 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
371 mptr = (struct march *) malloc(sizeof(struct march));
374 extract_token(mptr->march_name, aaa, 0, '|', sizeof mptr->march_name);
375 mptr->march_flags = (unsigned int) extract_int(aaa, 1);
376 mptr->march_floor = (char) extract_int(aaa, 2);
377 mptr->march_order = (char) extract_int(aaa, 3);
378 mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
379 mptr->march_access = (char) extract_int(aaa, 5);
386 while (mptr2->next != NULL)
401 /* Caller must free the struct ctdluser; caller may pass an existing one */
402 int CtdlIPCGetConfig(CtdlIPC * ipc, struct ctdluser **uret, char *cret)
411 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
415 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
416 if (ret / 100 == 2) {
417 uret[0]->flags = extract_int(cret, 2);
424 int CtdlIPCSetConfig(CtdlIPC * ipc, struct ctdluser *uret, char *cret)
433 sprintf(aaa, "SETU 80|24|%d", uret->flags);
434 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
439 int CtdlIPCRenameUser(CtdlIPC * ipc, char *oldname, char *newname, char *cret)
451 snprintf(cmd, sizeof cmd, "RENU %s|%s", oldname, newname);
452 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
458 int CtdlIPCGotoRoom(CtdlIPC * ipc, const char *room, const char *passwd, struct ctdlipcroom **rret, char *cret)
468 *rret = (struct ctdlipcroom *) calloc(1, sizeof(struct ctdlipcroom));
473 aaa = (char *) malloc(strlen(room) + strlen(passwd) + 7);
478 sprintf(aaa, "GOTO %s|%s", room, passwd);
480 aaa = (char *) malloc(strlen(room) + 6);
485 sprintf(aaa, "GOTO %s", room);
487 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
488 if (ret / 100 == 2) {
489 extract_token(rret[0]->RRname, cret, 0, '|', sizeof rret[0]->RRname);
490 rret[0]->RRunread = extract_long(cret, 1);
491 rret[0]->RRtotal = extract_long(cret, 2);
492 rret[0]->RRinfoupdated = extract_int(cret, 3);
493 rret[0]->RRflags = extract_int(cret, 4);
494 rret[0]->RRhighest = extract_long(cret, 5);
495 rret[0]->RRlastread = extract_long(cret, 6);
496 rret[0]->RRismailbox = extract_int(cret, 7);
497 rret[0]->RRaide = extract_int(cret, 8);
498 rret[0]->RRnewmail = extract_long(cret, 9);
499 rret[0]->RRfloor = extract_int(cret, 10);
500 rret[0]->RRcurrentview = extract_int(cret, 11);
501 rret[0]->RRdefaultview = extract_int(cret, 12);
502 /* position 13 is a trash folder flag ... irrelevant in this client */
503 rret[0]->RRflags2 = extract_int(cret, 14);
514 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
515 /* whicharg is number of messages, applies to last, first, gt, lt */
516 int CtdlIPCGetMessages(CtdlIPC * ipc, enum MessageList which, int whicharg, const char *mtemplate, unsigned long **mret, char *cret)
519 unsigned long count = 0;
520 static char *proto[] = { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
531 if (which < 0 || which > 6)
535 sprintf(aaa, "MSGS %s||%d", proto[which], (mtemplate) ? 1 : 0);
537 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg, (mtemplate) ? 1 : 0);
539 count = strlen(mtemplate);
540 ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbb_len, cret);
544 *mret = (unsigned long *) calloc(1, sizeof(unsigned long));
547 while (bbb && strlen(bbb)) {
548 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
549 remove_token(bbb, 0, '\n');
550 *mret = (unsigned long *) realloc(*mret, (size_t) ((count + 2) * sizeof(unsigned long)));
552 (*mret)[count++] = atol(aaa);
565 int CtdlIPCGetSingleMessage(CtdlIPC * ipc, long msgnum, int headers, int as_mime, struct ctdlipcmessage **mret, char *cret)
571 int multipart_hunting = 0;
572 char multipart_prefix[128];
580 *mret = (struct ctdlipcmessage *) calloc(1, sizeof(struct ctdlipcmessage));
586 strcpy(encoding, "");
587 strcpy(mret[0]->content_type, "");
588 mret[0]->is_local = 0;
589 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
590 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
591 if (ret / 100 == 1) {
593 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
594 while (strlen(bbb) > 4 && bbb[4] == '=') {
595 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
596 remove_token(bbb, 0, '\n');
598 if (!strncasecmp(aaa, "nhdr=yes", 8))
600 else if (!strncasecmp(aaa, "from=", 5))
601 strncpy(mret[0]->author, &aaa[5], SIZ);
602 else if (!strncasecmp(aaa, "type=", 5))
603 mret[0]->type = atoi(&aaa[5]);
604 else if (!strncasecmp(aaa, "msgn=", 5))
605 strncpy(mret[0]->msgid, &aaa[5], SIZ);
606 else if (!strncasecmp(aaa, "subj=", 5))
607 strncpy(mret[0]->subject, &aaa[5], SIZ);
608 else if (!strncasecmp(aaa, "rfca=", 5))
609 strncpy(mret[0]->email, &aaa[5], SIZ);
610 else if (!strncasecmp(aaa, "room=", 5))
611 strncpy(mret[0]->room, &aaa[5], SIZ);
612 else if (!strncasecmp(aaa, "rcpt=", 5))
613 strncpy(mret[0]->recipient, &aaa[5], SIZ);
614 else if (!strncasecmp(aaa, "wefw=", 5))
615 strncpy(mret[0]->references, &aaa[5], SIZ);
616 else if (!strncasecmp(aaa, "time=", 5))
617 mret[0]->time = atol(&aaa[5]);
618 else if (!strncasecmp(aaa, "locl", 4))
619 mret[0]->is_local = 1;
621 /* Multipart/alternative prefix & suffix strings help
622 * us to determine which part we want to download.
624 else if (!strncasecmp(aaa, "pref=", 5)) {
625 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
626 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
629 } else if (!strncasecmp(aaa, "suff=", 5)) {
630 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
631 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
636 else if (!strncasecmp(aaa, "part=", 5)) {
637 struct parts *ptr, *chain;
639 ptr = (struct parts *) calloc(1, sizeof(struct parts));
642 /* Fill the buffers for the caller */
643 extract_token(ptr->name, &aaa[5], 0, '|', sizeof ptr->name);
644 extract_token(ptr->filename, &aaa[5], 1, '|', sizeof ptr->filename);
645 extract_token(ptr->number, &aaa[5], 2, '|', sizeof ptr->number);
646 extract_token(ptr->disposition, &aaa[5], 3, '|', sizeof ptr->disposition);
647 extract_token(ptr->mimetype, &aaa[5], 4, '|', sizeof ptr->mimetype);
648 ptr->length = extract_long(&aaa[5], 5);
649 if (!mret[0]->attachments)
650 mret[0]->attachments = ptr;
652 chain = mret[0]->attachments;
658 /* Now handle multipart/alternative */
659 if (multipart_hunting > 0) {
660 if ((!strcasecmp(ptr->mimetype, "text/plain"))
661 || (!strcasecmp(ptr->mimetype, "text/html"))) {
662 strcpy(mret[0]->mime_chosen, ptr->number);
669 /* Eliminate "text\n" */
670 remove_token(bbb, 0, '\n');
672 /* If doing a MIME thing, pull out the extra headers */
675 if (!strncasecmp(bbb, "Content-type:", 13)) {
676 extract_token(mret[0]->content_type, bbb, 0, '\n', sizeof mret[0]->content_type);
677 strcpy(mret[0]->content_type, &mret[0]->content_type[13]);
678 striplt(mret[0]->content_type);
680 /* strip out ";charset=" portion. FIXME do something with
681 * the charset (like... convert it) instead of just throwing
684 if (strstr(mret[0]->content_type, ";") != NULL) {
685 strcpy(strstr(mret[0]->content_type, ";"), "");
689 if (!strncasecmp(bbb, "X-Citadel-MSG4-Partnum:", 23)) {
690 extract_token(mret[0]->mime_chosen, bbb, 0, '\n', sizeof mret[0]->mime_chosen);
691 strcpy(mret[0]->mime_chosen, &mret[0]->mime_chosen[23]);
692 striplt(mret[0]->mime_chosen);
694 if (!strncasecmp(bbb, "Content-transfer-encoding:", 26)) {
695 extract_token(encoding, bbb, 0, '\n', sizeof encoding);
696 strcpy(encoding, &encoding[26]);
699 remove_token(bbb, 0, '\n');
700 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
701 remove_token(bbb, 0, '\n');
708 if ((!strcasecmp(encoding, "base64")) || (!strcasecmp(encoding, "quoted-printable"))) {
710 int bytes_decoded = 0;
711 ccc = malloc(strlen(bbb) + 32768);
712 if (!strcasecmp(encoding, "base64")) {
713 bytes_decoded = CtdlDecodeBase64(ccc, bbb, strlen(bbb));
714 } else if (!strcasecmp(encoding, "quoted-printable")) {
715 bytes_decoded = CtdlDecodeQuotedPrintable(ccc, bbb, strlen(bbb));
717 ccc[bytes_decoded] = 0;
722 /* FIXME: Strip trailing whitespace */
723 bbb = (char *) realloc(bbb, (size_t) (strlen(bbb) + 1));
726 bbb = (char *) realloc(bbb, 1);
736 int CtdlIPCWhoKnowsRoom(CtdlIPC * ipc, char **listing, char *cret)
748 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
754 int CtdlIPCServerInfo(CtdlIPC * ipc, char *cret)
758 char *listing = NULL;
764 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
765 if (ret / 100 == 1) {
768 while (*listing && strlen(listing)) {
769 extract_token(buf, listing, 0, '\n', sizeof buf);
770 remove_token(listing, 0, '\n');
773 ipc->ServInfo.pid = atoi(buf);
776 strcpy(ipc->ServInfo.nodename, buf);
779 strcpy(ipc->ServInfo.humannode, buf);
782 strcpy(ipc->ServInfo.fqdn, buf);
785 strcpy(ipc->ServInfo.software, buf);
788 ipc->ServInfo.rev_level = atoi(buf);
791 strcpy(ipc->ServInfo.site_location, buf);
794 strcpy(ipc->ServInfo.sysadm, buf);
797 strcpy(ipc->ServInfo.moreprompt, buf);
800 ipc->ServInfo.ok_floors = atoi(buf);
803 ipc->ServInfo.supports_ldap = atoi(buf);
806 ipc->ServInfo.newuser_disabled = atoi(buf);
809 strcpy(ipc->ServInfo.default_cal_zone, buf);
812 ipc->ServInfo.load_avg = atof(buf);
815 ipc->ServInfo.worker_avg = atof(buf);
818 ipc->ServInfo.thread_count = atoi(buf);
821 ipc->ServInfo.has_sieve = atoi(buf);
824 ipc->ServInfo.fulltext_enabled = atoi(buf);
827 strcpy(ipc->ServInfo.svn_revision, buf);
830 ipc->ServInfo.guest_logins = atoi(buf);
843 int CtdlIPCReadDirectory(CtdlIPC * ipc, char **listing, char *cret)
855 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
861 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
863 int CtdlIPCSetLastRead(CtdlIPC * ipc, long msgnum, char *cret)
872 sprintf(aaa, "SLRP %ld", msgnum);
874 sprintf(aaa, "SLRP HIGHEST");
876 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
882 int CtdlIPCInviteUserToRoom(CtdlIPC * ipc, const char *username, char *cret)
892 aaa = (char *) malloc(strlen(username) + 6);
896 sprintf(aaa, "INVT %s", username);
897 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
904 int CtdlIPCKickoutUserFromRoom(CtdlIPC * ipc, const char *username, char *cret)
914 aaa = (char *) malloc(strlen(username) + 6);
916 sprintf(aaa, "KICK %s", username);
917 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
924 int CtdlIPCGetRoomAttributes(CtdlIPC * ipc, struct ctdlroom **qret, char *cret)
933 *qret = (struct ctdlroom *) calloc(1, sizeof(struct ctdlroom));
937 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
938 if (ret / 100 == 2) {
939 extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
940 extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
941 extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
942 qret[0]->QRflags = extract_int(cret, 3);
943 qret[0]->QRfloor = extract_int(cret, 4);
944 qret[0]->QRorder = extract_int(cret, 5);
945 qret[0]->QRdefaultview = extract_int(cret, 6);
946 qret[0]->QRflags2 = extract_int(cret, 7);
953 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc,
954 int forget, // if nonzero, kick all users out of the room
955 struct ctdlroom *qret,
966 cmd = (char *) malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) + strlen(qret->QRdirname) + 64);
970 sprintf(cmd, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
971 qret->QRname, qret->QRpasswd, qret->QRdirname,
972 qret->QRflags, forget, qret->QRfloor, qret->QRorder, qret->QRdefaultview, qret->QRflags2);
973 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
980 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
985 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
990 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret) {
999 aaa = (char *) malloc(strlen(username) + 6);
1003 sprintf(aaa, "SETA %s", username);
1004 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1011 int CtdlIPCPostMessage(CtdlIPC * ipc, int flag, int *subject_required, struct ctdlipcmessage *mr, char *cret)
1022 if (mr->references) {
1023 for (ptr = mr->references; *ptr != 0; ++ptr) {
1029 snprintf(cmd, sizeof cmd,
1030 "ENT0 %d|%s|%d|%d|%s|%s||||||%s|", flag, mr->recipient,
1031 mr->anonymous, mr->type, mr->subject, mr->author, mr->references);
1032 ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL, NULL, cret);
1033 if ((flag == 0) && (subject_required != NULL)) {
1034 /* Is the server strongly recommending that the user enter a message subject? */
1035 if ((cret[3] != '\0') && (cret[4] != '\0')) {
1036 *subject_required = extract_int(&cret[4], 1);
1046 int CtdlIPCRoomInfo(CtdlIPC * ipc, char **iret, char *cret) {
1056 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
1061 int CtdlIPCDeleteMessage(CtdlIPC * ipc, long msgnum, char *cret) {
1069 sprintf(aaa, "DELE %ld", msgnum);
1070 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1075 int CtdlIPCMoveMessage(CtdlIPC * ipc, int copy, long msgnum, const char *destroom, char *cret) {
1086 aaa = (char *) malloc(strlen(destroom) + 28);
1090 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
1091 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1098 int CtdlIPCDeleteRoom(CtdlIPC * ipc, int for_real, char *cret) {
1104 sprintf(aaa, "KILL %d", for_real);
1105 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1110 int CtdlIPCCreateRoom(CtdlIPC * ipc, int for_real, const char *roomname, int type, const char *password, int floor, char *cret)
1121 aaa = (char *) malloc(strlen(roomname) + strlen(password) + 40);
1124 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type, password, floor);
1127 aaa = (char *) malloc(strlen(roomname) + 40);
1130 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type, floor);
1132 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1139 int CtdlIPCForgetRoom(CtdlIPC * ipc, char *cret) {
1143 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
1148 int CtdlIPCSystemMessage(CtdlIPC * ipc, const char *message, char **mret, char *cret) {
1162 aaa = (char *) malloc(strlen(message) + 6);
1166 sprintf(aaa, "MESG %s", message);
1167 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
1174 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret) {
1178 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
1183 int CtdlIPCGetUserRegistration(CtdlIPC * ipc, const char *username, char **rret, char *cret) {
1196 aaa = (char *) malloc(strlen(username) + 6);
1198 aaa = (char *) malloc(12);
1203 sprintf(aaa, "GREG %s", username);
1205 sprintf(aaa, "GREG _SELF_");
1206 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
1213 int CtdlIPCValidateUser(CtdlIPC * ipc, const char *username, int axlevel, char *cret) {
1221 if (axlevel < AxDeleted || axlevel > AxAideU)
1224 aaa = (char *) malloc(strlen(username) + 17);
1228 sprintf(aaa, "VALI %s|%d", username, axlevel);
1229 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1236 int CtdlIPCSetRoomInfo(CtdlIPC * ipc, int for_real, const char *info, char *cret)
1245 sprintf(aaa, "EINF %d", for_real);
1246 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1251 int CtdlIPCUserListing(CtdlIPC * ipc, char *searchstring, char **listing, char *cret)
1266 cmd = malloc(strlen(searchstring) + 10);
1267 sprintf(cmd, "LIST %s", searchstring);
1269 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, listing, &bytes, cret);
1276 int CtdlIPCSetRegistration(CtdlIPC * ipc, const char *info, char *cret)
1283 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info), NULL, NULL, cret);
1288 int CtdlIPCMiscCheck(CtdlIPC * ipc, struct ctdlipcmisc *chek, char *cret)
1297 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1298 if (ret / 100 == 2) {
1299 chek->newmail = extract_long(cret, 0);
1300 chek->needregis = extract_int(cret, 1);
1301 chek->needvalid = extract_int(cret, 2);
1308 int CtdlIPCDeleteFile(CtdlIPC * ipc, const char *filename, char *cret)
1318 aaa = (char *) malloc(strlen(filename) + 6);
1322 sprintf(aaa, "DELF %s", filename);
1323 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1330 int CtdlIPCMoveFile(CtdlIPC * ipc, const char *filename, const char *destroom, char *cret)
1342 aaa = (char *) malloc(strlen(filename) + strlen(destroom) + 7);
1346 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1347 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1354 int CtdlIPCOnlineUsers(CtdlIPC * ipc, char **listing, time_t * stamp, char *cret)
1366 *stamp = CtdlIPCServerTime(ipc, cret);
1368 *stamp = time(NULL);
1369 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1375 int CtdlIPCFileDownload(CtdlIPC * ipc, const char *filename, void **buf, size_t resume, void (*progress_gauge_callback)
1376 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1392 if (ipc->downloading)
1395 aaa = (char *) malloc(strlen(filename) + 6);
1399 sprintf(aaa, "OPEN %s", filename);
1400 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1402 if (ret / 100 == 2) {
1403 ipc->downloading = 1;
1404 bytes = extract_long(cret, 0);
1405 last_mod = extract_int(cret, 1);
1406 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1407 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret);
1408 ret = CtdlIPCEndDownload(ipc, cret);
1410 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1417 int CtdlIPCAttachmentDownload(CtdlIPC * ipc, long msgnum, const char *part, void **buf, void (*progress_gauge_callback)
1418 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1437 if (ipc->downloading)
1440 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1441 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1442 if (ret / 100 == 2) {
1443 ipc->downloading = 1;
1444 bytes = extract_long(cret, 0);
1445 last_mod = extract_int(cret, 1);
1446 extract_token(filename, cret, 2, '|', sizeof filename);
1447 extract_token(mimetype, cret, 3, '|', sizeof mimetype);
1448 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1449 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1450 ret = CtdlIPCEndDownload(ipc, cret);
1452 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1459 int CtdlIPCImageDownload(CtdlIPC * ipc, const char *filename, void **buf, void (*progress_gauge_callback)
1460 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1476 if (ipc->downloading)
1479 aaa = (char *) malloc(strlen(filename) + 6);
1483 sprintf(aaa, "OIMG %s", filename);
1484 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1486 if (ret / 100 == 2) {
1487 ipc->downloading = 1;
1488 bytes = extract_long(cret, 0);
1489 last_mod = extract_int(cret, 1);
1490 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1491 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1492 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1493 ret = CtdlIPCEndDownload(ipc, cret);
1495 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1502 int CtdlIPCFileUpload(CtdlIPC * ipc, const char *save_as, const char *comment, const char *path, void (*progress_gauge_callback)
1503 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1508 char MimeTestBuf[64];
1509 const char *MimeType;
1525 uploadFP = fopen(path, "r");
1529 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1534 MimeType = GuessMimeType(&MimeTestBuf[0], len);
1535 aaa = (char *) malloc(strlen(save_as) + strlen(MimeType) + strlen(comment) + 7);
1539 sprintf(aaa, "UOPN %s|%s|%s", save_as, MimeType, comment);
1540 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1542 if (ret / 100 == 2) {
1544 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1545 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1553 int CtdlIPCImageUpload(CtdlIPC * ipc, int for_real, const char *path, const char *save_as, void (*progress_gauge_callback)
1554 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1559 char MimeTestBuf[64];
1560 const char *MimeType;
1567 if (!path && for_real)
1569 if (!*path && for_real)
1574 aaa = (char *) malloc(strlen(save_as) + 17);
1578 uploadFP = fopen(path, "r");
1582 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1586 MimeType = GuessMimeType(&MimeTestBuf[0], 64);
1588 sprintf(aaa, "UIMG %d|%s|%s", for_real, MimeType, save_as);
1589 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1591 if (ret / 100 == 2 && for_real) {
1593 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1594 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1602 int CtdlIPCQueryUsername(CtdlIPC * ipc, const char *username, char *cret)
1612 aaa = (char *) malloc(strlen(username) + 6);
1616 sprintf(aaa, "QUSR %s", username);
1617 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1624 int CtdlIPCFloorListing(CtdlIPC * ipc, char **listing, char *cret)
1635 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1640 int CtdlIPCCreateFloor(CtdlIPC * ipc, int for_real, const char *name, char *cret)
1650 sprintf(aaa, "CFLR %s|%d", name, for_real);
1651 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1657 int CtdlIPCDeleteFloor(CtdlIPC * ipc, int for_real, int floornum, char *cret)
1666 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1667 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1672 int CtdlIPCEditFloor(CtdlIPC * ipc, int floornum, const char *floorname, char *cret)
1684 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1685 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1693 * You only need to fill out hostname, the defaults will be used if any of the
1694 * other fields are not set properly.
1696 int CtdlIPCIdentifySoftware(CtdlIPC * ipc, int developerid, int clientid,
1697 int revision, const char *software_name, const char *hostname, char *cret)
1702 if (developerid < 0 || clientid < 0 || revision < 0 || !software_name) {
1705 revision = CLIENT_VERSION - 600;
1706 software_name = "Citadel (libcitadel)";
1711 aaa = (char *) malloc(strlen(software_name) + strlen(hostname) + 29);
1715 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid, revision, software_name, hostname);
1716 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1723 int CtdlIPCSendInstantMessage(CtdlIPC * ipc, const char *username, const char *text, char *cret)
1733 aaa = (char *) malloc(strlen(username) + 8);
1738 sprintf(aaa, "SEXP %s|-", username);
1739 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1741 sprintf(aaa, "SEXP %s||", username);
1742 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1750 int CtdlIPCGetInstantMessage(CtdlIPC * ipc, char **listing, char *cret)
1761 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1766 /* mode is 0 = enable, 1 = disable, 2 = status */
1767 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC * ipc, int mode, char *cret)
1774 sprintf(aaa, "DEXP %d", mode);
1775 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1780 int CtdlIPCSetBio(CtdlIPC * ipc, char *bio, char *cret)
1787 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio), NULL, NULL, cret);
1792 int CtdlIPCGetBio(CtdlIPC * ipc, const char *username, char **listing, char *cret)
1807 aaa = (char *) malloc(strlen(username) + 6);
1811 sprintf(aaa, "RBIO %s", username);
1812 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1819 int CtdlIPCListUsersWithBios(CtdlIPC * ipc, char **listing, char *cret)
1830 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1835 int CtdlIPCStealthMode(CtdlIPC * ipc, int mode, char *cret)
1842 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1843 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1848 int CtdlIPCTerminateSession(CtdlIPC * ipc, int sid, char *cret)
1855 sprintf(aaa, "TERM %d", sid);
1856 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1861 int CtdlIPCTerminateServerNow(CtdlIPC * ipc, char *cret)
1866 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1871 int CtdlIPCTerminateServerScheduled(CtdlIPC * ipc, int mode, char *cret)
1878 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1879 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1884 int CtdlIPCEnterSystemMessage(CtdlIPC * ipc, const char *filename, const char *text, char *cret)
1896 aaa = (char *) malloc(strlen(filename) + 6);
1900 sprintf(aaa, "EMSG %s", filename);
1901 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1908 /* This function returns the actual server time reported, or 0 if error */
1909 time_t CtdlIPCServerTime(CtdlIPC * ipc, char *cret)
1914 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1915 if (ret / 100 == 2) {
1916 tret = extract_long(cret, 0);
1925 int CtdlIPCAideGetUserParameters(CtdlIPC * ipc, const char *who, struct ctdluser **uret, char *cret)
1935 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
1939 sprintf(aaa, "AGUP %s", who);
1940 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1942 if (ret / 100 == 2) {
1943 extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
1944 extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
1945 uret[0]->flags = extract_int(cret, 2);
1946 uret[0]->timescalled = extract_long(cret, 3);
1947 uret[0]->posted = extract_long(cret, 4);
1948 uret[0]->axlevel = extract_int(cret, 5);
1949 uret[0]->usernum = extract_long(cret, 6);
1950 uret[0]->lastcall = extract_long(cret, 7);
1951 uret[0]->USuserpurge = extract_int(cret, 8);
1958 int CtdlIPCAideSetUserParameters(CtdlIPC * ipc, const struct ctdluser *uret, char *cret)
1968 aaa = (char *) malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1972 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1973 uret->fullname, uret->password, uret->flags, uret->timescalled,
1974 uret->posted, uret->axlevel, uret->usernum, uret->lastcall, uret->USuserpurge);
1975 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1982 int CtdlIPCAideGetEmailAddresses(CtdlIPC * ipc, const char *who, char *target_buf, char *cret)
1986 char *emailaddrs = NULL;
1987 size_t emailaddrs_len = 0;
1989 sprintf(aaa, "AGEA %s", who);
1990 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &emailaddrs, &emailaddrs_len, cret);
1992 if (ret / 100 == 1) {
1993 strcpy(target_buf, emailaddrs);
1996 if (emailaddrs != NULL) {
2005 int CtdlIPCAideSetEmailAddresses(CtdlIPC * ipc, const char *who, char *emailaddrs, char *cret)
2017 sprintf(aaa, "ASEA %s", who);
2018 ret = CtdlIPCGenericCommand(ipc, aaa, emailaddrs, 0, NULL, NULL, cret);
2024 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
2025 /* caller must free the struct ExpirePolicy */
2026 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC * ipc, GPEXWhichPolicy which, struct ExpirePolicy **policy, char *cret)
2028 static char *proto[] = {
2032 strof(mailboxespolicy)
2042 *policy = (struct ExpirePolicy *) calloc(1, sizeof(struct ExpirePolicy));
2045 if (which < 0 || which > 3)
2048 sprintf(cmd, "GPEX %s", proto[which]);
2049 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2050 if (ret / 100 == 2) {
2051 policy[0]->expire_mode = extract_int(cret, 0);
2052 policy[0]->expire_value = extract_int(cret, 1);
2059 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
2060 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
2061 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC * ipc, int which, struct ExpirePolicy *policy, char *cret)
2064 char *whichvals[] = { "room", "floor", "site", "mailboxes" };
2068 if (which < 0 || which > 3)
2072 if (policy->expire_mode < 0 || policy->expire_mode > 3)
2074 if (policy->expire_mode >= 2 && policy->expire_value < 1)
2077 sprintf(aaa, "SPEX %s|%d|%d", whichvals[which], policy->expire_mode, policy->expire_value);
2078 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2083 int CtdlIPCGetSystemConfig(CtdlIPC * ipc, char **listing, char *cret)
2094 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0, listing, &bytes, cret);
2099 int CtdlIPCSetSystemConfig(CtdlIPC * ipc, const char *listing, char *cret)
2106 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing), NULL, NULL, cret);
2111 int CtdlIPCGetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, char **listing, char *cret)
2126 aaa = malloc(strlen(mimetype) + 13);
2129 sprintf(aaa, "CONF GETSYS|%s", mimetype);
2130 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
2137 int CtdlIPCSetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, const char *listing, char *cret)
2149 aaa = malloc(strlen(mimetype) + 13);
2152 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
2153 ret = CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing), NULL, NULL, cret);
2160 int CtdlIPCGetRoomNetworkConfig(CtdlIPC * ipc, char **listing, char *cret)
2171 return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0, listing, &bytes, cret);
2176 int CtdlIPCSetRoomNetworkConfig(CtdlIPC * ipc, const char *listing, char *cret)
2183 return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing), NULL, NULL, cret);
2188 int CtdlIPCRequestClientLogout(CtdlIPC * ipc, int session, char *cret)
2197 sprintf(aaa, "REQT %d", session);
2198 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2203 int CtdlIPCSetMessageSeen(CtdlIPC * ipc, long msgnum, int seen, char *cret)
2212 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
2213 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2218 int CtdlIPCStartEncryption(CtdlIPC * ipc, char *cret)
2227 /* New SSL object */
2228 temp_ssl = SSL_new(ssl_ctx);
2230 error_printf("SSL_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2233 /* Pointless flag waving */
2234 #if SSLEAY_VERSION_NUMBER >= 0x0922
2235 SSL_set_session_id_context(temp_ssl, (const unsigned char *) "Citadel SID", 14);
2238 /* Associate network connection with SSL object */
2239 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
2240 error_printf("SSL_set_fd failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2244 if (status_hook != NULL) {
2245 status_hook("Requesting encryption...\r");
2248 /* Ready to start SSL/TLS */
2249 r = CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
2251 error_printf("Server can't start TLS: %s\n", buf);
2256 /* Do SSL/TLS handshake */
2257 if ((a = SSL_connect(temp_ssl)) < 1) {
2258 error_printf("SSL_connect failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2262 ipc->ssl = temp_ssl;
2264 error_printf("Encrypting with %s cipher %s\n",
2265 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)), SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl))
2270 #endif /* HAVE_OPENSSL */
2275 static void endtls(SSL * ssl)
2286 int CtdlIPCDirectoryLookup(CtdlIPC * ipc, const char *address, char *cret)
2296 aaa = (char *) malloc(strlen(address) + 6);
2300 sprintf(aaa, "QDIR %s", address);
2301 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2308 int CtdlIPCInternalProgram(CtdlIPC * ipc, int secret, char *cret)
2314 sprintf(aaa, "IPGM %d", secret);
2315 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2321 /* ************************************************************************** */
2322 /* Stuff below this line is not for public consumption */
2323 /* ************************************************************************** */
2326 /* Read a listing from the server up to 000. Append to dest if it exists */
2327 char *CtdlIPCReadListing(CtdlIPC * ipc, char *dest)
2336 length = strlen(ret);
2341 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2342 linelength = strlen(aaa);
2343 ret = (char *) realloc(ret, (size_t) (length + linelength + 2));
2345 strcpy(&ret[length], aaa);
2346 length += linelength;
2347 strcpy(&ret[length++], "\n");
2355 /* Send a listing to the server; generate the ending 000. */
2356 int CtdlIPCSendListing(CtdlIPC * ipc, const char *listing)
2360 text = (char *) malloc(strlen(listing) + 6);
2362 strcpy(text, listing);
2363 while (text[strlen(text) - 1] == '\n')
2364 text[strlen(text) - 1] = '\0';
2365 strcat(text, "\n000");
2366 CtdlIPC_putline(ipc, text);
2370 /* Malloc failed but we are committed to send */
2371 /* This may result in extra blanks at the bottom */
2372 CtdlIPC_putline(ipc, text);
2373 CtdlIPC_putline(ipc, "000");
2379 /* Partial read of file from server */
2380 size_t CtdlIPCPartialRead(CtdlIPC * ipc, void **buf, size_t offset, size_t bytes, char *cret)
2393 sprintf(aaa, "READ %d|%d", (int) offset, (int) bytes);
2394 CtdlIPC_putline(ipc, aaa);
2395 CtdlIPC_getline(ipc, aaa);
2397 strcpy(cret, &aaa[4]);
2399 len = extract_long(&aaa[4], 0);
2400 *buf = (void *) realloc(*buf, (size_t) (offset + len));
2402 /* I know what I'm doing */
2403 serv_read(ipc, ((char *) (*buf) + offset), len);
2405 /* We have to read regardless */
2406 serv_read(ipc, aaa, len);
2410 CtdlIPC_unlock(ipc);
2416 int CtdlIPCEndDownload(CtdlIPC * ipc, char *cret)
2422 if (!ipc->downloading)
2425 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2427 ipc->downloading = 0;
2433 int CtdlIPCSpecifyPreferredFormats(CtdlIPC * ipc, char *cret, char *formats)
2438 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2439 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2446 int CtdlIPCReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2447 (CtdlIPC *, unsigned long, unsigned long), char *cret)
2457 if (!ipc->downloading)
2461 if (progress_gauge_callback)
2462 progress_gauge_callback(ipc, len, bytes);
2463 while (len < bytes) {
2466 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2472 if (progress_gauge_callback)
2473 progress_gauge_callback(ipc, len, bytes);
2478 /* READ - pipelined */
2479 int CtdlIPCHighSpeedReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2480 (CtdlIPC *, unsigned long, unsigned long), char *cret)
2483 int calls; /* How many calls in the pipeline */
2484 int i; /* iterator */
2493 if (!ipc->downloading)
2496 *buf = (void *) realloc(*buf, bytes - resume);
2502 if (progress_gauge_callback)
2503 progress_gauge_callback(ipc, len, bytes);
2505 /* How many calls will be in the pipeline? */
2506 calls = (bytes - resume) / 4096;
2507 if ((bytes - resume) % 4096)
2510 /* Send all requests at once */
2511 for (i = 0; i < calls; i++) {
2512 sprintf(aaa, "READ %d|4096", (int) (i * 4096 + resume));
2513 CtdlIPC_putline(ipc, aaa);
2516 /* Receive all responses at once */
2517 for (i = 0; i < calls; i++) {
2518 CtdlIPC_getline(ipc, aaa);
2520 strcpy(cret, &aaa[4]);
2522 len = extract_long(&aaa[4], 0);
2523 /* I know what I'm doing */
2524 serv_read(ipc, ((char *) (*buf) + (i * 4096)), len);
2526 if (progress_gauge_callback)
2527 progress_gauge_callback(ipc, i * 4096 + len, bytes);
2529 CtdlIPC_unlock(ipc);
2535 int CtdlIPCEndUpload(CtdlIPC * ipc, int discard, char *cret)
2542 if (!ipc->uploading)
2545 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2546 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2553 int CtdlIPCWriteUpload(CtdlIPC * ipc, FILE * uploadFP, void (*progress_gauge_callback)
2554 (CtdlIPC *, unsigned long, unsigned long), char *cret)
2561 FILE *fd = uploadFP;
2567 fseek(fd, 0L, SEEK_END);
2571 if (progress_gauge_callback)
2572 progress_gauge_callback(ipc, 0, bytes);
2574 while (offset < bytes) {
2577 /* Read some data in */
2578 to_write = fread(buf, 1, 4096, fd);
2580 if (feof(fd) || ferror(fd))
2583 sprintf(aaa, "WRIT %d", (int) to_write);
2584 CtdlIPC_putline(ipc, aaa);
2585 CtdlIPC_getline(ipc, aaa);
2586 strcpy(cret, &aaa[4]);
2588 if (aaa[0] == '7') {
2589 to_write = extract_long(&aaa[4], 0);
2591 serv_write(ipc, buf, to_write);
2593 if (progress_gauge_callback)
2594 progress_gauge_callback(ipc, offset, bytes);
2595 /* Detect short reads and back up if needed */
2596 /* offset will never be negative anyway */
2597 fseek(fd, (signed) offset, SEEK_SET);
2602 if (progress_gauge_callback)
2603 progress_gauge_callback(ipc, 1, 1);
2606 return (!ferr ? ret : -2);
2611 * Generic command method. This method should handle any server command
2612 * except for CHAT. It takes the following arguments:
2614 * ipc The server to speak with
2615 * command Preformatted command to send to server
2616 * to_send A text or binary file to send to server
2617 * (only sent if server requests it)
2618 * bytes_to_send The number of bytes in to_send (required if
2619 * sending binary, optional if sending listing)
2620 * to_receive Pointer to a NULL pointer, if the server
2621 * sends text or binary we will allocate memory
2622 * for the file and stuff it here
2623 * bytes_to_receive If a file is received, we will store its
2625 * proto_response The protocol response. Caller must provide
2626 * this buffer and ensure that it is at least
2627 * 128 bytes in length.
2629 * This function returns a number equal to the protocol response number,
2630 * -1 if an internal error occurred, -2 if caller provided bad values,
2631 * or 0 - the protocol response number if bad values were found during
2632 * the protocol exchange.
2634 * It stores the protocol response string (minus the number) in
2635 * protocol_response as described above. Some commands send additional
2636 * data in this string.
2638 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2639 const char *command,
2640 const char *to_send,
2641 size_t bytes_to_send,
2643 size_t *bytes_to_receive,
2644 char *proto_response
2652 if (!proto_response) {
2657 CtdlIPC_putline(ipc, command);
2659 CtdlIPC_getline(ipc, proto_response);
2660 if (proto_response[3] == '*')
2662 ret = atoi(proto_response);
2663 strcpy(proto_response, &proto_response[4]);
2664 switch (ret / 100) {
2665 default: /* Unknown, punt */
2667 case 3: /* MORE_DATA */
2669 /* Don't need to do anything */
2671 case 1: /* LISTING_FOLLOWS */
2672 if (to_receive && !*to_receive && bytes_to_receive) {
2673 *to_receive = CtdlIPCReadListing(ipc, NULL);
2674 } else { /* Drain */
2675 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2679 case 4: /* SEND_LISTING */
2681 CtdlIPCSendListing(ipc, to_send);
2683 /* No listing given, fake it */
2684 CtdlIPC_putline(ipc, "000");
2688 case 6: /* BINARY_FOLLOWS */
2689 if (to_receive && !*to_receive && bytes_to_receive) {
2690 *bytes_to_receive = extract_long(proto_response, 0);
2691 *to_receive = (char *)
2692 malloc((size_t) * bytes_to_receive);
2696 serv_read(ipc, *to_receive, *bytes_to_receive);
2702 drain = extract_long(proto_response, 0);
2703 while (drain > SIZ) {
2704 serv_read(ipc, buf, SIZ);
2707 serv_read(ipc, buf, drain);
2711 case 7: /* SEND_BINARY */
2712 if (to_send && bytes_to_send) {
2713 serv_write(ipc, to_send, bytes_to_send);
2714 } else if (bytes_to_send) {
2715 /* Fake it, send nulls */
2718 fake = bytes_to_send;
2719 memset(buf, '\0', SIZ);
2720 while (fake > SIZ) {
2721 serv_write(ipc, buf, SIZ);
2724 serv_write(ipc, buf, fake);
2726 } /* else who knows? DANGER WILL ROBINSON */
2728 case 8: /* START_CHAT_MODE */
2729 if (!strncasecmp(command, "CHAT", 4)) {
2730 /* Don't call chatmode with generic! */
2731 CtdlIPC_putline(ipc, "/quit");
2735 /* In this mode we send then receive listing */
2737 CtdlIPCSendListing(ipc, to_send);
2740 /* No listing given, fake it */
2741 CtdlIPC_putline(ipc, "000");
2744 if (to_receive && !*to_receive && bytes_to_receive) {
2745 *to_receive = CtdlIPCReadListing(ipc, NULL);
2748 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2753 case 9: /* ASYNC_MSG */
2754 /* CtdlIPCDoAsync(ret, proto_response); */
2755 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2761 CtdlIPC_unlock(ipc);
2767 * Connect to a Citadel on a remote host using a TCP/IP socket
2769 static int tcp_connectsock(char *host, char *service) {
2770 struct in6_addr serveraddr;
2771 struct addrinfo hints;
2772 struct addrinfo *res = NULL;
2773 struct addrinfo *ai = NULL;
2777 if ((host == NULL) || IsEmptyStr(host)) {
2778 service = DEFAULT_HOST;
2780 if ((service == NULL) || IsEmptyStr(service)) {
2781 service = DEFAULT_PORT;
2784 memset(&hints, 0x00, sizeof(hints));
2785 hints.ai_flags = AI_NUMERICSERV;
2786 hints.ai_family = AF_UNSPEC;
2787 hints.ai_socktype = SOCK_STREAM;
2790 * Handle numeric IPv4 and IPv6 addresses
2792 rc = inet_pton(AF_INET, host, &serveraddr);
2793 if (rc == 1) { /* dotted quad */
2794 hints.ai_family = AF_INET;
2795 hints.ai_flags |= AI_NUMERICHOST;
2798 rc = inet_pton(AF_INET6, host, &serveraddr);
2799 if (rc == 1) { /* IPv6 address */
2800 hints.ai_family = AF_INET6;
2801 hints.ai_flags |= AI_NUMERICHOST;
2805 /* Begin the connection process */
2807 rc = getaddrinfo(host, service, &hints, &res);
2813 * Try all available addresses until we connect to one or until we run out.
2815 for (ai = res; ai != NULL; ai = ai->ai_next) {
2816 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2820 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
2822 return (sock); /* Connected! */
2825 close(sock); /* Failed. Close the socket to avoid fd leak! */
2833 * Connect to a Citadel on the local host using a unix domain socket
2835 static int uds_connectsock(int *isLocal, char *sockpath) {
2836 struct sockaddr_un addr;
2839 memset(&addr, 0, sizeof(addr));
2840 addr.sun_family = AF_UNIX;
2841 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2843 s = socket(AF_UNIX, SOCK_STREAM, 0);
2848 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2859 * input binary data from socket
2861 static void serv_read(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2862 unsigned int len, rlen;
2864 #if defined(HAVE_OPENSSL)
2866 serv_read_ssl(ipc, buf, bytes);
2871 while (len < bytes) {
2872 rlen = read(ipc->sock, &buf[len], bytes - len);
2874 connection_died(ipc, 0);
2883 * send binary to server
2885 void serv_write(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2886 unsigned int bytes_written = 0;
2889 #if defined(HAVE_OPENSSL)
2891 serv_write_ssl(ipc, buf, nbytes);
2895 while (bytes_written < nbytes) {
2896 retval = write(ipc->sock, &buf[bytes_written], nbytes - bytes_written);
2898 connection_died(ipc, 0);
2901 bytes_written += retval;
2908 * input binary data from encrypted connection
2910 static void serv_read_ssl(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2915 while (len < bytes) {
2916 if (SSL_want_read(ipc->ssl)) {
2917 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2918 error_printf("SSL_write in serv_read:\n");
2919 ERR_print_errors_fp(stderr);
2922 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2926 errval = SSL_get_error(ipc->ssl, rlen);
2927 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2931 error_printf("SSL_read in serv_read: %s\n", ERR_reason_error_string(ERR_peek_error()));
2932 connection_died(ipc, 1);
2941 * send binary to server encrypted
2943 static void serv_write_ssl(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2944 unsigned int bytes_written = 0;
2948 while (bytes_written < nbytes) {
2949 if (SSL_want_write(ipc->ssl)) {
2950 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2951 error_printf("SSL_read in serv_write:\n");
2952 ERR_print_errors_fp(stderr);
2955 retval = SSL_write(ipc->ssl, &buf[bytes_written], nbytes - bytes_written);
2959 errval = SSL_get_error(ipc->ssl, retval);
2960 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2964 if (errval == SSL_ERROR_ZERO_RETURN || errval == SSL_ERROR_SSL) {
2965 serv_write(ipc, &buf[bytes_written], nbytes - bytes_written);
2968 error_printf("SSL_write in serv_write: %s\n", ERR_reason_error_string(ERR_peek_error()));
2969 connection_died(ipc, 1);
2972 bytes_written += retval;
2977 static void CtdlIPC_init_OpenSSL(void) {
2979 const SSL_METHOD *ssl_method;
2982 /* already done init */
2991 SSL_load_error_strings();
2992 SSLeay_add_ssl_algorithms();
2994 /* Set up the SSL context in which we will oeprate */
2995 ssl_method = SSLv23_client_method();
2996 ssl_ctx = SSL_CTX_new(ssl_method);
2998 error_printf("SSL_CTX_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
3001 /* Any reasonable cipher we can get */
3002 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
3003 error_printf("No ciphers available for encryption\n");
3006 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
3008 /* Load DH parameters into the context */
3011 error_printf("Can't allocate a DH object: %s\n", ERR_reason_error_string(ERR_get_error()));
3015 if (!(DH_generate_parameters_ex(dh, 128, DH_GENERATOR_2, 0))) {
3016 error_printf("Can't generate DH parameters: %s\n", ERR_reason_error_string(ERR_get_error()));
3021 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
3025 #endif /* HAVE_OPENSSL */
3028 int ReadNetworkChunk(CtdlIPC * ipc) {
3042 FD_SET(ipc->sock, &read_fd);
3043 ret = select(ipc->sock + 1, &read_fd, NULL, NULL, &tv);
3047 *(ipc->BufPtr) = '\0';
3048 n = recv(ipc->sock, ipc->BufPtr, ipc->BufSize - (ipc->BufPtr - ipc->Buf) - 1, 0);
3050 ipc->BufPtr[n] = '\0';
3059 if (!(errno == EINTR || errno == EAGAIN))
3060 error_printf("\nselect failed: %d %s\n", err, strerror(err));
3068 * input string from socket - implemented in terms of serv_read()
3072 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3074 char *aptr, *bptr, *aeptr, *beptr;
3078 #if defined(HAVE_OPENSSL)
3081 /* Read one character at a time. */
3083 serv_read(ipc, &buf[i], 1);
3084 if (buf[i] == '\n' || i == (SIZ - 1))
3088 /* If we got a long line, discard characters until the newline. */
3090 while (buf[i] != '\n')
3091 serv_read(ipc, &buf[i], 1);
3093 /* Strip the trailing newline (and carriage return, if present) */
3094 if (i >= 0 && buf[i] == 10)
3096 if (i >= 0 && buf[i] == 13)
3101 if (ipc->Buf == NULL) {
3103 ipc->Buf = (char *) malloc(ipc->BufSize + 10);
3105 ipc->BufPtr = ipc->Buf;
3109 if (ipc->BufUsed == 0)
3110 ReadNetworkChunk(ipc);
3115 aeptr = ipc->Buf + ipc->BufSize;
3116 while ((aptr < aeptr) && (bptr < beptr) && (*aptr != '\0') && (*aptr != '\n'))
3117 *(bptr++) = *(aptr++);
3118 if ((*aptr == '\n') && (aptr < aeptr)) {
3119 /* Terminate it right, remove the line breaks */
3120 while ((aptr < aeptr) && ((*aptr == '\n') || (*aptr == '\r')))
3122 while ((aptr < aeptr) && (*(aptr + 1) == '\0'))
3125 if ((bptr > buf + 1) && (*(bptr - 1) == '\r'))
3128 /* is there more in the buffer we need to read later? */
3129 if (ipc->Buf + ipc->BufUsed > aptr) {
3133 ipc->BufPtr = ipc->Buf;
3138 // should we move our read stuf to the bufferstart so we have more space at the end?
3139 else if ((ipc->BufPtr != ipc->Buf) && (ipc->BufUsed > (ipc->BufSize - (ipc->BufSize / 4)))) {
3140 size_t NewBufSize = ipc->BufSize * 2;
3141 int delta = (ipc->BufPtr - ipc->Buf);
3144 /* if the line would end after our buffer, we should use a bigger buffer. */
3145 NewBuf = (char *) malloc(NewBufSize + 10);
3146 memcpy(NewBuf, ipc->BufPtr, ipc->BufUsed - delta);
3148 ipc->Buf = ipc->BufPtr = NewBuf;
3149 ipc->BufUsed -= delta;
3150 ipc->BufSize = NewBufSize;
3152 if (ReadNetworkChunk(ipc) < 0) {
3159 #else /* CHUNKED_READ */
3161 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3164 /* Read one character at a time. */
3166 serv_read(ipc, &buf[i], 1);
3167 if (buf[i] == '\n' || i == (SIZ - 1))
3171 /* If we got a long line, discard characters until the newline. */
3173 while (buf[i] != '\n')
3174 serv_read(ipc, &buf[i], 1);
3176 /* Strip the trailing newline (and carriage return, if present) */
3177 if (i >= 0 && buf[i] == 10)
3179 if (i >= 0 && buf[i] == 13)
3184 #endif /* CHUNKED_READ */
3187 void CtdlIPC_chat_recv(CtdlIPC * ipc, char *buf) {
3188 CtdlIPC_getline(ipc, buf);
3192 * send line to server - implemented in terms of serv_write()
3194 static void CtdlIPC_putline(CtdlIPC * ipc, const char *buf) {
3199 cmd = malloc(len + 2);
3201 /* This requires no extra memory */
3202 serv_write(ipc, buf, len);
3203 serv_write(ipc, "\n", 1);
3206 /* This is network-optimized */
3207 strncpy(cmd, buf, len);
3208 strcpy(cmd + len, "\n");
3209 serv_write(ipc, cmd, len + 1);
3213 ipc->last_command_sent = time(NULL);
3217 void CtdlIPC_chat_send(CtdlIPC * ipc, const char *buf) {
3218 CtdlIPC_putline(ipc, buf);
3225 CtdlIPC *CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf) {
3232 ipc = malloc(sizeof(struct _CtdlIPC));
3236 #if defined(HAVE_OPENSSL)
3238 CtdlIPC_init_OpenSSL();
3240 ipc->sock = -1; /* Not connected */
3241 ipc->isLocal = 0; /* Not local, of course! */
3242 ipc->downloading = 0;
3244 ipc->last_command_sent = 0L;
3245 ipc->network_status_cb = NULL;
3250 strcpy(cithost, DEFAULT_HOST); /* default host */
3251 strcpy(citport, DEFAULT_PORT); /* default port */
3253 /* Allow caller to supply our values */
3254 if (hostbuf && strlen(hostbuf) > 0) {
3255 strcpy(cithost, hostbuf);
3257 if (portbuf && strlen(portbuf) > 0) {
3258 strcpy(citport, portbuf);
3261 /* Read host/port from command line if present */
3262 for (a = 0; a < argc; ++a) {
3267 strcpy(cithost, argv[a]);
3270 strcpy(citport, argv[a]);
3273 error_printf("%s: usage: ", argv[0]);
3274 error_printf("%s [host] [port] ", argv[0]);
3281 if ((!strcmp(cithost, "localhost")) || (!strcmp(cithost, "127.0.0.1"))) {
3285 /* If we're using a unix domain socket we can do a bunch of stuff */
3286 if (!strcmp(cithost, UDS)) {
3287 if (!strcasecmp(citport, DEFAULT_PORT)) {
3288 snprintf(sockpath, sizeof sockpath, "%s", file_citadel_socket);
3291 snprintf(sockpath, sizeof sockpath, "%s/%s", citport, "citadel.socket");
3293 printf("[%s]\n", sockpath);
3294 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
3295 if (ipc->sock == -1) {
3299 if (hostbuf != NULL)
3300 strcpy(hostbuf, cithost);
3301 if (portbuf != NULL)
3302 strcpy(portbuf, sockpath);
3303 strcpy(ipc->ip_hostname, "");
3304 strcpy(ipc->ip_address, "");
3308 printf("[%s:%s]\n", cithost, citport);
3309 ipc->sock = tcp_connectsock(cithost, citport);
3310 if (ipc->sock == -1) {
3316 /* Learn the actual network identity of the host to which we are connected */
3318 struct sockaddr_in6 clientaddr;
3319 unsigned int addrlen = sizeof(clientaddr);
3321 ipc->ip_hostname[0] = 0;
3322 ipc->ip_address[0] = 0;
3324 getpeername(ipc->sock, (struct sockaddr *) &clientaddr, &addrlen);
3325 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_hostname, sizeof ipc->ip_hostname, NULL, 0, 0);
3326 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_address, sizeof ipc->ip_address, NULL, 0, NI_NUMERICHOST);
3328 /* stuff other things elsewhere */
3330 if (hostbuf != NULL)
3331 strcpy(hostbuf, cithost);
3332 if (portbuf != NULL)
3333 strcpy(portbuf, citport);
3339 * Disconnect and delete the IPC class (destructor)
3341 void CtdlIPC_delete(CtdlIPC *ipc) {
3344 SSL_shutdown(ipc->ssl);
3349 if (ipc->sock > -1) {
3350 shutdown(ipc->sock, 2); /* Close it up */
3353 if (ipc->Buf != NULL)
3362 * Disconnect and delete the IPC class (destructor)
3363 * Also NULLs out the pointer
3365 void CtdlIPC_delete_ptr(CtdlIPC **pipc) {
3366 CtdlIPC_delete(*pipc);