1 // Copyright (c) 1987-2022 by the citadel.org team
3 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License version 3.
5 #include "textclient.h"
8 static SSL_CTX *ssl_ctx;
11 #endif /* HAVE_OPENSSL */
14 #define INADDR_NONE 0xffffffff
17 static void (*status_hook)(char *s) = NULL;
18 char ctdl_autoetc_dir[PATH_MAX] = "";
19 char file_citadel_rc[PATH_MAX] = "";
20 char ctdl_run_dir[PATH_MAX] = "";
21 char ctdl_etc_dir[PATH_MAX] = "";
22 char ctdl_home_directory[PATH_MAX] = "";
23 char file_citadel_socket[PATH_MAX] = "";
45 void CtdlIPC_lock(CtdlIPC * ipc) {
46 if (ipc->network_status_cb)
47 ipc->network_status_cb(1);
51 void CtdlIPC_unlock(CtdlIPC * ipc) {
52 if (ipc->network_status_cb)
53 ipc->network_status_cb(0);
57 char *libcitadelclient_version_string(void) {
58 return "libcitadelclient(unnumbered)";
62 #define COMPUTE_DIRECTORY(SUBDIR) memcpy(dirbuffer,SUBDIR, sizeof dirbuffer);\
63 snprintf(SUBDIR,sizeof SUBDIR, "%s%s%s%s%s%s%s", \
64 (home&!relh)?ctdl_home_directory:basedir, \
65 ((basedir!=ctdldir)&(home&!relh))?basedir:"/", \
66 ((basedir!=ctdldir)&(home&!relh))?"/":"", \
68 (relhome[0]!='\0')?"/":"",\
70 (dirbuffer[0]!='\0')?"/":"");
72 #define DBG_PRINT(A) if (dbg==1) fprintf (stderr,"%s : %s \n", #A, A)
75 void calc_dirs_n_files(int relh, int home, const char *relhome, char *ctdldir, int dbg) {
76 const char *basedir = "";
77 char dirbuffer[PATH_MAX] = "";
79 StripSlashes(ctdldir, 1);
86 COMPUTE_DIRECTORY(ctdl_run_dir);
87 StripSlashes(ctdl_run_dir, 1);
90 #ifndef HAVE_AUTO_ETC_DIR
93 basedir = AUTO_ETC_DIR;
95 COMPUTE_DIRECTORY(ctdl_autoetc_dir);
96 StripSlashes(ctdl_autoetc_dir, 1);
104 COMPUTE_DIRECTORY(ctdl_etc_dir);
105 StripSlashes(ctdl_etc_dir, 1);
109 snprintf(file_citadel_rc, sizeof file_citadel_rc, "%scitadel.rc", ctdl_etc_dir);
110 StripSlashes(file_citadel_rc, 0);
112 snprintf(file_citadel_socket, sizeof file_citadel_socket, "%scitadel.socket", ctdl_run_dir);
113 StripSlashes(file_citadel_socket, 0);
115 DBG_PRINT(ctdl_run_dir);
116 DBG_PRINT(file_citadel_socket);
117 DBG_PRINT(ctdl_etc_dir);
118 DBG_PRINT(file_citadel_rc);
121 void setCryptoStatusHook(void (*hook)(char *s)) {
125 void CtdlIPC_SetNetworkStatusCallback(CtdlIPC * ipc, void (*hook)(int state)) {
126 ipc->network_status_cb = hook;
130 char instant_msgs = 0;
133 static void serv_read(CtdlIPC * ipc, char *buf, unsigned int bytes);
134 static void serv_write(CtdlIPC * ipc, const char *buf, unsigned int nbytes);
136 static void serv_read_ssl(CtdlIPC * ipc, char *buf, unsigned int bytes);
137 static void serv_write_ssl(CtdlIPC * ipc, const char *buf, unsigned int nbytes);
138 static void endtls(SSL * ssl);
139 #endif /* HAVE_OPENSSL */
140 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf);
141 static void CtdlIPC_putline(CtdlIPC * ipc, const char *buf);
145 const char *svn_revision(void);
148 * Does nothing. The server should always return 200.
150 int CtdlIPCNoop(CtdlIPC * ipc) {
153 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
158 * Does nothing interesting. The server should always return 200
159 * along with your string.
161 int CtdlIPCEcho(CtdlIPC * ipc, const char *arg, char *cret) {
170 aaa = (char *) malloc((size_t) (strlen(arg) + 6));
174 sprintf(aaa, "ECHO %s", arg);
175 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
182 * Asks the server to close the connecction.
183 * Should always return 200.
185 int CtdlIPCQuit(CtdlIPC * ipc) {
186 int ret = 221; /* Default to successful quit */
190 if (ipc->sock > -1) {
191 CtdlIPC_putline(ipc, "QUIT");
192 CtdlIPC_getline(ipc, aaa);
197 SSL_shutdown(ipc->ssl);
201 shutdown(ipc->sock, 2); /* Close connection; we're dead */
209 * Asks the server to log out. Should always return 200, even if no user
210 * was logged in. The user will not be logged in after this!
212 int CtdlIPCLogout(CtdlIPC * ipc) {
217 CtdlIPC_putline(ipc, "LOUT");
218 CtdlIPC_getline(ipc, aaa);
226 * First stage of authentication - pass the username. Returns 300 if the
227 * username is able to log in, with the username correctly spelled in cret.
228 * Returns various 500 error codes if the user doesn't exist, etc.
230 int CtdlIPCTryLogin(CtdlIPC * ipc, const char *username, char *cret) {
239 aaa = (char *) malloc((size_t) (strlen(username) + 6));
243 sprintf(aaa, "USER %s", username);
244 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
251 * Second stage of authentication - provide password. The server returns
252 * 200 and several arguments in cret relating to the user's account.
254 int CtdlIPCTryPassword(CtdlIPC * ipc, const char *passwd, char *cret) {
263 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
267 sprintf(aaa, "PASS %s", passwd);
268 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
275 * Create a new user. This returns 200 plus the same arguments as TryPassword
276 * if selfservice is nonzero, unless there was a problem creating the account.
277 * If selfservice is zero, creates a new user but does not log out the existing
278 * user - intended for use by system administrators to create accounts on
279 * behalf of other users.
281 int CtdlIPCCreateUser(CtdlIPC * ipc, const char *username, int selfservice, char *cret) {
290 aaa = (char *) malloc((size_t) (strlen(username) + 6));
294 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
295 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
302 * Changes the user's password. Returns 200 if changed, errors otherwise.
304 int CtdlIPCChangePassword(CtdlIPC * ipc, const char *passwd, char *cret) {
313 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
317 sprintf(aaa, "SETP %s", passwd);
318 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
326 /* Caller must free the march list */
328 /* Room types are defined in enum RoomList; keep these in sync! */
330 /* floor is -1 for all, or floornum */
331 int CtdlIPCKnownRooms(CtdlIPC * ipc, enum RoomList which, int floor, struct march **listing, char *cret) {
333 struct march *march = NULL;
334 static char *proto[] = { "LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
342 return -2; /* Free the listing first */
345 /* if (which < 0 || which > 4) return -2; */
347 return -2; /* Can't validate upper bound, sorry */
349 sprintf(aaa, "%s %d", proto[which], floor);
350 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
351 if (ret / 100 == 1) {
354 while (bbb && strlen(bbb)) {
357 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
359 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
360 mptr = (struct march *) malloc(sizeof(struct march));
363 extract_token(mptr->march_name, aaa, 0, '|', sizeof mptr->march_name);
364 mptr->march_flags = (unsigned int) extract_int(aaa, 1);
365 mptr->march_floor = (char) extract_int(aaa, 2);
366 mptr->march_order = (char) extract_int(aaa, 3);
367 mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
368 mptr->march_access = (char) extract_int(aaa, 5);
375 while (mptr2->next != NULL)
391 /* Caller must free the struct ctdluser; caller may pass an existing one */
392 int CtdlIPCGetConfig(CtdlIPC * ipc, struct ctdluser **uret, char *cret) {
400 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
404 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
405 if (ret / 100 == 2) {
406 uret[0]->flags = extract_int(cret, 2);
413 int CtdlIPCSetConfig(CtdlIPC * ipc, struct ctdluser *uret, char *cret) {
421 sprintf(aaa, "SETU 80|24|%d", uret->flags);
422 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
427 int CtdlIPCRenameUser(CtdlIPC * ipc, char *oldname, char *newname, char *cret) {
438 snprintf(cmd, sizeof cmd, "RENU %s|%s", oldname, newname);
439 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
445 int CtdlIPCGotoRoom(CtdlIPC * ipc, const char *room, const char *passwd, struct ctdlipcroom **rret, char *cret) {
454 *rret = (struct ctdlipcroom *) calloc(1, sizeof(struct ctdlipcroom));
459 aaa = (char *) malloc(strlen(room) + strlen(passwd) + 7);
464 sprintf(aaa, "GOTO %s|%s", room, passwd);
467 aaa = (char *) malloc(strlen(room) + 6);
472 sprintf(aaa, "GOTO %s", room);
474 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
475 if (ret / 100 == 2) {
476 extract_token(rret[0]->RRname, cret, 0, '|', sizeof rret[0]->RRname);
477 rret[0]->RRunread = extract_long(cret, 1);
478 rret[0]->RRtotal = extract_long(cret, 2);
479 rret[0]->RRinfoupdated = extract_int(cret, 3);
480 rret[0]->RRflags = extract_int(cret, 4);
481 rret[0]->RRhighest = extract_long(cret, 5);
482 rret[0]->RRlastread = extract_long(cret, 6);
483 rret[0]->RRismailbox = extract_int(cret, 7);
484 rret[0]->RRaide = extract_int(cret, 8);
485 // position 9 is no longer used
486 rret[0]->RRfloor = extract_int(cret, 10);
487 rret[0]->RRcurrentview = extract_int(cret, 11);
488 rret[0]->RRdefaultview = extract_int(cret, 12);
489 // position 13 is a trash folder flag ... irrelevant in this client
490 rret[0]->RRflags2 = extract_int(cret, 14);
503 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
505 /* whicharg is number of messages, applies to last, first, gt, lt */
506 int CtdlIPCGetMessages(CtdlIPC * ipc, enum MessageList which, int whicharg, const char *mtemplate, unsigned long **mret, char *cret) {
508 unsigned long count = 0;
509 static char *proto[] = { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
520 if (which < 0 || which > 6)
524 sprintf(aaa, "MSGS %s||%d", proto[which], (mtemplate) ? 1 : 0);
526 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg, (mtemplate) ? 1 : 0);
528 count = strlen(mtemplate);
529 ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbb_len, cret);
533 *mret = (unsigned long *) calloc(1, sizeof(unsigned long));
536 while (bbb && strlen(bbb)) {
537 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
538 remove_token(bbb, 0, '\n');
539 *mret = (unsigned long *) realloc(*mret, (size_t) ((count + 2) * sizeof(unsigned long)));
541 (*mret)[count++] = atol(aaa);
555 int CtdlIPCGetSingleMessage(CtdlIPC * ipc, long msgnum, int headers, int as_mime, struct ctdlipcmessage **mret, char *cret) {
560 int multipart_hunting = 0;
561 char multipart_prefix[128];
569 *mret = (struct ctdlipcmessage *) calloc(1, sizeof(struct ctdlipcmessage));
575 strcpy(encoding, "");
576 strcpy(mret[0]->content_type, "");
577 mret[0]->is_local = 0;
578 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
579 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
580 if (ret / 100 == 1) {
582 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
583 while (strlen(bbb) > 4 && bbb[4] == '=') {
584 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
585 remove_token(bbb, 0, '\n');
587 if (!strncasecmp(aaa, "nhdr=yes", 8))
589 else if (!strncasecmp(aaa, "from=", 5))
590 strncpy(mret[0]->author, &aaa[5], SIZ);
591 else if (!strncasecmp(aaa, "type=", 5))
592 mret[0]->type = atoi(&aaa[5]);
593 else if (!strncasecmp(aaa, "msgn=", 5))
594 strncpy(mret[0]->msgid, &aaa[5], SIZ);
595 else if (!strncasecmp(aaa, "subj=", 5))
596 strncpy(mret[0]->subject, &aaa[5], SIZ);
597 else if (!strncasecmp(aaa, "rfca=", 5))
598 strncpy(mret[0]->email, &aaa[5], SIZ);
599 else if (!strncasecmp(aaa, "room=", 5))
600 strncpy(mret[0]->room, &aaa[5], SIZ);
601 else if (!strncasecmp(aaa, "rcpt=", 5))
602 strncpy(mret[0]->recipient, &aaa[5], SIZ);
603 else if (!strncasecmp(aaa, "wefw=", 5))
604 strncpy(mret[0]->references, &aaa[5], SIZ);
605 else if (!strncasecmp(aaa, "time=", 5))
606 mret[0]->time = atol(&aaa[5]);
607 else if (!strncasecmp(aaa, "locl", 4))
608 mret[0]->is_local = 1;
610 /* Multipart/alternative prefix & suffix strings help
611 * us to determine which part we want to download.
613 else if (!strncasecmp(aaa, "pref=", 5)) {
614 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
615 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
619 else if (!strncasecmp(aaa, "suff=", 5)) {
620 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
621 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
626 else if (!strncasecmp(aaa, "part=", 5)) {
627 struct parts *ptr, *chain;
629 ptr = (struct parts *) calloc(1, sizeof(struct parts));
632 /* Fill the buffers for the caller */
633 extract_token(ptr->name, &aaa[5], 0, '|', sizeof ptr->name);
634 extract_token(ptr->filename, &aaa[5], 1, '|', sizeof ptr->filename);
635 extract_token(ptr->number, &aaa[5], 2, '|', sizeof ptr->number);
636 extract_token(ptr->disposition, &aaa[5], 3, '|', sizeof ptr->disposition);
637 extract_token(ptr->mimetype, &aaa[5], 4, '|', sizeof ptr->mimetype);
638 ptr->length = extract_long(&aaa[5], 5);
639 if (!mret[0]->attachments)
640 mret[0]->attachments = ptr;
642 chain = mret[0]->attachments;
648 /* Now handle multipart/alternative */
649 if (multipart_hunting > 0) {
650 if ((!strcasecmp(ptr->mimetype, "text/plain"))
651 || (!strcasecmp(ptr->mimetype, "text/html"))) {
652 strcpy(mret[0]->mime_chosen, ptr->number);
659 /* Eliminate "text\n" */
660 remove_token(bbb, 0, '\n');
662 /* If doing a MIME thing, pull out the extra headers */
665 if (!strncasecmp(bbb, "Content-type:", 13)) {
666 extract_token(mret[0]->content_type, bbb, 0, '\n', sizeof mret[0]->content_type);
667 strcpy(mret[0]->content_type, &mret[0]->content_type[13]);
668 string_trim(mret[0]->content_type);
670 /* strip out ";charset=" portion. FIXME do something with
671 * the charset (like... convert it) instead of just throwing
674 if (strstr(mret[0]->content_type, ";") != NULL) {
675 strcpy(strstr(mret[0]->content_type, ";"), "");
679 if (!strncasecmp(bbb, "X-Citadel-MSG4-Partnum:", 23)) {
680 extract_token(mret[0]->mime_chosen, bbb, 0, '\n', sizeof mret[0]->mime_chosen);
681 strcpy(mret[0]->mime_chosen, &mret[0]->mime_chosen[23]);
682 string_trim(mret[0]->mime_chosen);
684 if (!strncasecmp(bbb, "Content-transfer-encoding:", 26)) {
685 extract_token(encoding, bbb, 0, '\n', sizeof encoding);
686 strcpy(encoding, &encoding[26]);
687 string_trim(encoding);
689 remove_token(bbb, 0, '\n');
690 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
691 remove_token(bbb, 0, '\n');
698 if ((!strcasecmp(encoding, "base64")) || (!strcasecmp(encoding, "quoted-printable"))) {
700 int bytes_decoded = 0;
701 ccc = malloc(strlen(bbb) + 32768);
702 if (!strcasecmp(encoding, "base64")) {
703 bytes_decoded = CtdlDecodeBase64(ccc, bbb, strlen(bbb));
705 else if (!strcasecmp(encoding, "quoted-printable")) {
706 bytes_decoded = CtdlDecodeQuotedPrintable(ccc, bbb, strlen(bbb));
708 ccc[bytes_decoded] = 0;
713 /* FIXME: Strip trailing whitespace */
714 bbb = (char *) realloc(bbb, (size_t) (strlen(bbb) + 1));
718 bbb = (char *) realloc(bbb, 1);
728 int CtdlIPCWhoKnowsRoom(CtdlIPC * ipc, char **listing, char *cret) {
739 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
745 int CtdlIPCServerInfo(CtdlIPC * ipc, char *cret) {
748 char *listing = NULL;
754 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
755 if (ret / 100 == 1) {
758 while (*listing && strlen(listing)) {
759 extract_token(buf, listing, 0, '\n', sizeof buf);
760 remove_token(listing, 0, '\n');
763 ipc->ServInfo.pid = atoi(buf);
766 strcpy(ipc->ServInfo.nodename, buf);
769 strcpy(ipc->ServInfo.humannode, buf);
772 strcpy(ipc->ServInfo.fqdn, buf);
775 strcpy(ipc->ServInfo.software, buf);
778 ipc->ServInfo.rev_level = atoi(buf);
781 strcpy(ipc->ServInfo.site_location, buf);
784 strcpy(ipc->ServInfo.sysadm, buf);
787 strcpy(ipc->ServInfo.moreprompt, buf);
790 ipc->ServInfo.ok_floors = atoi(buf);
793 ipc->ServInfo.supports_ldap = atoi(buf);
796 ipc->ServInfo.newuser_disabled = atoi(buf);
799 strcpy(ipc->ServInfo.default_cal_zone, buf);
802 ipc->ServInfo.load_avg = atof(buf);
805 ipc->ServInfo.worker_avg = atof(buf);
808 ipc->ServInfo.thread_count = atoi(buf);
811 ipc->ServInfo.has_sieve = atoi(buf);
814 ipc->ServInfo.fulltext_enabled = atoi(buf);
817 strcpy(ipc->ServInfo.svn_revision, buf);
820 ipc->ServInfo.guest_logins = atoi(buf);
833 int CtdlIPCReadDirectory(CtdlIPC * ipc, char **listing, char *cret) {
844 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
850 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
852 int CtdlIPCSetLastRead(CtdlIPC * ipc, long msgnum, char *cret) {
860 sprintf(aaa, "SLRP %ld", msgnum);
863 sprintf(aaa, "SLRP HIGHEST");
865 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
871 int CtdlIPCInviteUserToRoom(CtdlIPC * ipc, const char *username, char *cret) {
880 aaa = (char *) malloc(strlen(username) + 6);
884 sprintf(aaa, "INVT %s", username);
885 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
892 int CtdlIPCKickoutUserFromRoom(CtdlIPC * ipc, const char *username, char *cret) {
901 aaa = (char *) malloc(strlen(username) + 6);
903 sprintf(aaa, "KICK %s", username);
904 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
911 int CtdlIPCGetRoomAttributes(CtdlIPC * ipc, struct ctdlroom **qret, char *cret) {
919 *qret = (struct ctdlroom *) calloc(1, sizeof(struct ctdlroom));
923 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
924 if (ret / 100 == 2) {
925 extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
926 extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
927 extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
928 qret[0]->QRflags = extract_int(cret, 3);
929 qret[0]->QRfloor = extract_int(cret, 4);
930 qret[0]->QRorder = extract_int(cret, 5);
931 qret[0]->QRdefaultview = extract_int(cret, 6);
932 qret[0]->QRflags2 = extract_int(cret, 7);
939 int CtdlIPCSetRoomAttributes(CtdlIPC * ipc, int forget, // if nonzero, kick all users out of the room
940 struct ctdlroom *qret, char *cret) {
949 cmd = (char *) malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) + strlen(qret->QRdirname) + 64);
953 sprintf(cmd, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
954 qret->QRname, qret->QRpasswd, qret->QRdirname,
955 qret->QRflags, forget, qret->QRfloor, qret->QRorder, qret->QRdefaultview, qret->QRflags2);
956 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
963 int CtdlIPCGetRoomAide(CtdlIPC * ipc, char *cret) {
967 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
972 int CtdlIPCSetRoomAide(CtdlIPC * ipc, const char *username, char *cret) {
981 aaa = (char *) malloc(strlen(username) + 6);
985 sprintf(aaa, "SETA %s", username);
986 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
993 int CtdlIPCPostMessage(CtdlIPC * ipc, int flag, int *subject_required, struct ctdlipcmessage *mr, char *cret) {
1003 if (mr->references) {
1004 for (ptr = mr->references; *ptr != 0; ++ptr) {
1010 snprintf(cmd, sizeof cmd,
1011 "ENT0 %d|%s|%d|%d|%s|%s||||||%s|", flag, mr->recipient,
1012 mr->anonymous, mr->type, mr->subject, mr->author, mr->references);
1013 ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL, NULL, cret);
1014 if ((flag == 0) && (subject_required != NULL)) {
1015 /* Is the server strongly recommending that the user enter a message subject? */
1016 if ((cret[3] != '\0') && (cret[4] != '\0')) {
1017 *subject_required = extract_int(&cret[4], 1);
1027 int CtdlIPCRoomInfo(CtdlIPC * ipc, char **iret, char *cret) {
1037 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
1042 int CtdlIPCDeleteMessage(CtdlIPC * ipc, long msgnum, char *cret) {
1050 sprintf(aaa, "DELE %ld", msgnum);
1051 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1056 int CtdlIPCMoveMessage(CtdlIPC * ipc, int copy, long msgnum, const char *destroom, char *cret) {
1067 aaa = (char *) malloc(strlen(destroom) + 28);
1071 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
1072 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1079 int CtdlIPCDeleteRoom(CtdlIPC * ipc, int for_real, char *cret) {
1085 sprintf(aaa, "KILL %d", for_real);
1086 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1091 int CtdlIPCCreateRoom(CtdlIPC * ipc, int for_real, const char *roomname, int type, const char *password, int floor, char *cret) {
1101 aaa = (char *) malloc(strlen(roomname) + strlen(password) + 40);
1104 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type, password, floor);
1107 aaa = (char *) malloc(strlen(roomname) + 40);
1110 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type, floor);
1112 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1119 int CtdlIPCForgetRoom(CtdlIPC * ipc, char *cret) {
1123 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
1128 int CtdlIPCSystemMessage(CtdlIPC * ipc, const char *message, char **mret, char *cret) {
1142 aaa = (char *) malloc(strlen(message) + 6);
1146 sprintf(aaa, "MESG %s", message);
1147 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
1154 int CtdlIPCNextUnvalidatedUser(CtdlIPC * ipc, char *cret) {
1158 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
1163 int CtdlIPCGetUserRegistration(CtdlIPC * ipc, const char *username, char **rret, char *cret) {
1176 aaa = (char *) malloc(strlen(username) + 6);
1178 aaa = (char *) malloc(12);
1183 sprintf(aaa, "GREG %s", username);
1185 sprintf(aaa, "GREG _SELF_");
1186 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
1193 int CtdlIPCValidateUser(CtdlIPC * ipc, const char *username, int axlevel, char *cret) {
1201 if (axlevel < AxDeleted || axlevel > AxAideU)
1204 aaa = (char *) malloc(strlen(username) + 17);
1208 sprintf(aaa, "VALI %s|%d", username, axlevel);
1209 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1216 int CtdlIPCSetRoomInfo(CtdlIPC * ipc, int for_real, const char *info, char *cret) {
1224 sprintf(aaa, "EINF %d", for_real);
1225 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1230 int CtdlIPCUserListing(CtdlIPC * ipc, char *searchstring, char **listing, char *cret) {
1244 cmd = malloc(strlen(searchstring) + 10);
1245 sprintf(cmd, "LIST %s", searchstring);
1247 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, listing, &bytes, cret);
1254 int CtdlIPCSetRegistration(CtdlIPC * ipc, const char *info, char *cret) {
1260 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info), NULL, NULL, cret);
1265 int CtdlIPCMiscCheck(CtdlIPC * ipc, struct ctdlipcmisc *chek, char *cret) {
1273 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1274 if (ret / 100 == 2) {
1275 chek->newmail = extract_long(cret, 0);
1276 chek->needregis = extract_int(cret, 1);
1277 chek->needvalid = extract_int(cret, 2);
1284 int CtdlIPCDeleteFile(CtdlIPC * ipc, const char *filename, char *cret) {
1293 aaa = (char *) malloc(strlen(filename) + 6);
1297 sprintf(aaa, "DELF %s", filename);
1298 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1305 int CtdlIPCMoveFile(CtdlIPC * ipc, const char *filename, const char *destroom, char *cret) {
1316 aaa = (char *) malloc(strlen(filename) + strlen(destroom) + 7);
1320 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1321 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1328 int CtdlIPCOnlineUsers(CtdlIPC * ipc, char **listing, time_t * stamp, char *cret) {
1339 *stamp = CtdlIPCServerTime(ipc, cret);
1341 *stamp = time(NULL);
1342 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1348 int CtdlIPCFileDownload(CtdlIPC * ipc, const char *filename, void **buf, size_t resume, void (*progress_gauge_callback)
1349 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1364 if (ipc->downloading)
1367 aaa = (char *) malloc(strlen(filename) + 6);
1371 sprintf(aaa, "OPEN %s", filename);
1372 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1374 if (ret / 100 == 2) {
1375 ipc->downloading = 1;
1376 bytes = extract_long(cret, 0);
1377 last_mod = extract_int(cret, 1);
1378 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1379 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret);
1380 ret = CtdlIPCEndDownload(ipc, cret);
1382 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1389 int CtdlIPCAttachmentDownload(CtdlIPC * ipc, long msgnum, const char *part, void **buf, void (*progress_gauge_callback)
1390 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1408 if (ipc->downloading)
1411 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1412 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1413 if (ret / 100 == 2) {
1414 ipc->downloading = 1;
1415 bytes = extract_long(cret, 0);
1416 last_mod = extract_int(cret, 1);
1417 extract_token(filename, cret, 2, '|', sizeof filename);
1418 extract_token(mimetype, cret, 3, '|', sizeof mimetype);
1419 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1420 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1421 ret = CtdlIPCEndDownload(ipc, cret);
1423 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1430 int CtdlIPCImageDownload(CtdlIPC * ipc, const char *filename, void **buf, void (*progress_gauge_callback)
1431 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1446 if (ipc->downloading)
1449 aaa = (char *) malloc(strlen(filename) + 6);
1453 sprintf(aaa, "OIMG %s", filename);
1454 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1456 if (ret / 100 == 2) {
1457 ipc->downloading = 1;
1458 bytes = extract_long(cret, 0);
1459 last_mod = extract_int(cret, 1);
1460 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1462 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1463 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1464 ret = CtdlIPCEndDownload(ipc, cret);
1466 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1473 int CtdlIPCFileUpload(CtdlIPC * ipc, const char *save_as, const char *comment, const char *path, void (*progress_gauge_callback)
1474 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1478 char MimeTestBuf[64];
1479 const char *MimeType;
1495 uploadFP = fopen(path, "r");
1499 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1504 MimeType = GuessMimeType(&MimeTestBuf[0], len);
1505 aaa = (char *) malloc(strlen(save_as) + strlen(MimeType) + strlen(comment) + 7);
1509 sprintf(aaa, "UOPN %s|%s|%s", save_as, MimeType, comment);
1510 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1512 if (ret / 100 == 2) {
1514 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1515 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1523 int CtdlIPCImageUpload(CtdlIPC * ipc, int for_real, const char *path, const char *save_as, void (*progress_gauge_callback)
1524 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
1528 char MimeTestBuf[64];
1529 const char *MimeType;
1536 if (!path && for_real)
1538 if (!*path && for_real)
1543 aaa = (char *) malloc(strlen(save_as) + 17);
1547 uploadFP = fopen(path, "r");
1551 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1555 MimeType = GuessMimeType(&MimeTestBuf[0], 64);
1557 sprintf(aaa, "UIMG %d|%s|%s", for_real, MimeType, save_as);
1558 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1560 if (ret / 100 == 2 && for_real) {
1562 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1563 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1571 int CtdlIPCQueryUsername(CtdlIPC * ipc, const char *username, char *cret) {
1580 aaa = (char *) malloc(strlen(username) + 6);
1584 sprintf(aaa, "QUSR %s", username);
1585 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1592 int CtdlIPCFloorListing(CtdlIPC * ipc, char **listing, char *cret) {
1602 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1607 int CtdlIPCCreateFloor(CtdlIPC * ipc, int for_real, const char *name, char *cret) {
1616 sprintf(aaa, "CFLR %s|%d", name, for_real);
1617 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1623 int CtdlIPCDeleteFloor(CtdlIPC * ipc, int for_real, int floornum, char *cret) {
1631 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1632 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1637 int CtdlIPCEditFloor(CtdlIPC * ipc, int floornum, const char *floorname, char *cret) {
1648 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1649 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1657 * You only need to fill out hostname, the defaults will be used if any of the
1658 * other fields are not set properly.
1660 int CtdlIPCIdentifySoftware(CtdlIPC * ipc, int developerid, int clientid,
1661 int revision, const char *software_name, const char *hostname, char *cret) {
1665 if (developerid < 0 || clientid < 0 || revision < 0 || !software_name) {
1668 revision = CLIENT_VERSION - 600;
1669 software_name = "Citadel text client";
1674 aaa = (char *) malloc(strlen(software_name) + strlen(hostname) + 29);
1678 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid, revision, software_name, hostname);
1679 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1686 int CtdlIPCSendInstantMessage(CtdlIPC * ipc, const char *username, const char *text, char *cret) {
1695 aaa = (char *) malloc(strlen(username) + 8);
1700 sprintf(aaa, "SEXP %s|-", username);
1701 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1704 sprintf(aaa, "SEXP %s||", username);
1705 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1713 int CtdlIPCGetInstantMessage(CtdlIPC * ipc, char **listing, char *cret) {
1723 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1729 /* mode is 0 = enable, 1 = disable, 2 = status */
1730 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC * ipc, int mode, char *cret) {
1736 sprintf(aaa, "DEXP %d", mode);
1737 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1742 int CtdlIPCSetBio(CtdlIPC * ipc, char *bio, char *cret) {
1748 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio), NULL, NULL, cret);
1753 int CtdlIPCGetBio(CtdlIPC * ipc, const char *username, char **listing, char *cret) {
1767 aaa = (char *) malloc(strlen(username) + 6);
1771 sprintf(aaa, "RBIO %s", username);
1772 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1779 int CtdlIPCListUsersWithBios(CtdlIPC * ipc, char **listing, char *cret) {
1789 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1794 int CtdlIPCStealthMode(CtdlIPC * ipc, int mode, char *cret) {
1800 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1801 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1806 int CtdlIPCTerminateSession(CtdlIPC * ipc, int sid, char *cret) {
1812 sprintf(aaa, "TERM %d", sid);
1813 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1818 int CtdlIPCTerminateServerNow(CtdlIPC * ipc, char *cret) {
1822 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1827 int CtdlIPCTerminateServerScheduled(CtdlIPC * ipc, int mode, char *cret) {
1833 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1834 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1839 int CtdlIPCEnterSystemMessage(CtdlIPC * ipc, const char *filename, const char *text, char *cret) {
1850 aaa = (char *) malloc(strlen(filename) + 6);
1854 sprintf(aaa, "EMSG %s", filename);
1855 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1863 /* This function returns the actual server time reported, or 0 if error */
1864 time_t CtdlIPCServerTime(CtdlIPC * ipc, char *cret) {
1868 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1869 if (ret / 100 == 2) {
1870 tret = extract_long(cret, 0);
1880 int CtdlIPCAideGetUserParameters(CtdlIPC * ipc, const char *who, struct ctdluser **uret, char *cret) {
1889 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
1893 sprintf(aaa, "AGUP %s", who);
1894 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1896 if (ret / 100 == 2) {
1897 extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
1898 extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
1899 uret[0]->flags = extract_int(cret, 2);
1900 uret[0]->axlevel = extract_int(cret, 5);
1901 uret[0]->usernum = extract_long(cret, 6);
1902 uret[0]->lastcall = extract_long(cret, 7);
1903 uret[0]->USuserpurge = extract_int(cret, 8);
1910 int CtdlIPCAideSetUserParameters(CtdlIPC * ipc, const struct ctdluser *uret, char *cret) {
1919 aaa = (char *) malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1923 sprintf(aaa, "ASUP %s|%s|%d|0|0|%d|%ld|%ld|%d",
1924 uret->fullname, uret->password, uret->flags, uret->axlevel, uret->usernum, uret->lastcall, uret->USuserpurge);
1925 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1932 int CtdlIPCAideGetEmailAddresses(CtdlIPC * ipc, const char *who, char *target_buf, char *cret) {
1935 char *emailaddrs = NULL;
1936 size_t emailaddrs_len = 0;
1938 sprintf(aaa, "AGEA %s", who);
1939 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &emailaddrs, &emailaddrs_len, cret);
1941 if (ret / 100 == 1) {
1942 strcpy(target_buf, emailaddrs);
1945 if (emailaddrs != NULL) {
1954 int CtdlIPCAideSetEmailAddresses(CtdlIPC * ipc, const char *who, char *emailaddrs, char *cret) {
1965 sprintf(aaa, "ASEA %s", who);
1966 ret = CtdlIPCGenericCommand(ipc, aaa, emailaddrs, 0, NULL, NULL, cret);
1973 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1975 /* caller must free the struct ExpirePolicy */
1976 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC * ipc, GPEXWhichPolicy which, struct ExpirePolicy **policy, char *cret) {
1977 static char *proto[] = {
1981 strof(mailboxespolicy)
1991 *policy = (struct ExpirePolicy *) calloc(1, sizeof(struct ExpirePolicy));
1994 if (which < 0 || which > 3)
1997 sprintf(cmd, "GPEX %s", proto[which]);
1998 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
1999 if (ret / 100 == 2) {
2000 policy[0]->expire_mode = extract_int(cret, 0);
2001 policy[0]->expire_value = extract_int(cret, 1);
2009 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
2011 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
2012 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC * ipc, int which, struct ExpirePolicy *policy, char *cret) {
2014 char *whichvals[] = { "room", "floor", "site", "mailboxes" };
2018 if (which < 0 || which > 3)
2022 if (policy->expire_mode < 0 || policy->expire_mode > 3)
2024 if (policy->expire_mode >= 2 && policy->expire_value < 1)
2027 sprintf(aaa, "SPEX %s|%d|%d", whichvals[which], policy->expire_mode, policy->expire_value);
2028 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2033 int CtdlIPCGetSystemConfig(CtdlIPC * ipc, char **listing, char *cret) {
2043 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0, listing, &bytes, cret);
2048 int CtdlIPCSetSystemConfig(CtdlIPC * ipc, const char *listing, char *cret) {
2054 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing), NULL, NULL, cret);
2059 int CtdlIPCGetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, char **listing, char *cret) {
2073 aaa = malloc(strlen(mimetype) + 13);
2076 sprintf(aaa, "CONF GETSYS|%s", mimetype);
2077 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
2084 int CtdlIPCSetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, const char *listing, char *cret) {
2095 aaa = malloc(strlen(mimetype) + 13);
2098 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
2099 ret = CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing), NULL, NULL, cret);
2106 int CtdlIPCGetRoomNetworkConfig(CtdlIPC * ipc, char **listing, char *cret) {
2116 return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0, listing, &bytes, cret);
2121 int CtdlIPCSetRoomNetworkConfig(CtdlIPC * ipc, const char *listing, char *cret) {
2127 return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing), NULL, NULL, cret);
2132 int CtdlIPCRequestClientLogout(CtdlIPC * ipc, int session, char *cret) {
2140 sprintf(aaa, "REQT %d", session);
2141 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2146 int CtdlIPCSetMessageSeen(CtdlIPC * ipc, long msgnum, int seen, char *cret) {
2154 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
2155 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2160 int CtdlIPCStartEncryption(CtdlIPC * ipc, char *cret) {
2168 /* New SSL object */
2169 temp_ssl = SSL_new(ssl_ctx);
2171 error_printf("SSL_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2174 /* Pointless flag waving */
2175 #if SSLEAY_VERSION_NUMBER >= 0x0922
2176 SSL_set_session_id_context(temp_ssl, (const unsigned char *) "Citadel SID", 14);
2179 /* Associate network connection with SSL object */
2180 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
2181 error_printf("SSL_set_fd failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2185 if (status_hook != NULL) {
2186 status_hook("Requesting encryption...\r");
2189 /* Ready to start SSL/TLS */
2190 r = CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
2192 error_printf("Server can't start TLS: %s\n", buf);
2197 /* Do SSL/TLS handshake */
2198 if ((a = SSL_connect(temp_ssl)) < 1) {
2199 error_printf("SSL_connect failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2203 ipc->ssl = temp_ssl;
2205 error_printf("Encrypting with %s cipher %s\n",
2206 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)), SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl))
2211 #endif /* HAVE_OPENSSL */
2216 static void endtls(SSL * ssl) {
2226 int CtdlIPCDirectoryLookup(CtdlIPC * ipc, const char *address, char *cret) {
2235 aaa = (char *) malloc(strlen(address) + 6);
2239 sprintf(aaa, "QDIR %s", address);
2240 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2247 int CtdlIPCInternalProgram(CtdlIPC * ipc, int secret, char *cret) {
2252 sprintf(aaa, "IPGM %d", secret);
2253 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2259 /* ************************************************************************** */
2261 /* Stuff below this line is not for public consumption */
2263 /* ************************************************************************** */
2266 /* Read a listing from the server up to 000. Append to dest if it exists */
2267 char *CtdlIPCReadListing(CtdlIPC * ipc, char *dest) {
2275 length = strlen(ret);
2281 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2282 linelength = strlen(aaa);
2283 ret = (char *) realloc(ret, (size_t) (length + linelength + 2));
2285 strcpy(&ret[length], aaa);
2286 length += linelength;
2287 strcpy(&ret[length++], "\n");
2295 /* Send a listing to the server; generate the ending 000. */
2296 int CtdlIPCSendListing(CtdlIPC * ipc, const char *listing) {
2299 text = (char *) malloc(strlen(listing) + 6);
2301 strcpy(text, listing);
2302 while (text[strlen(text) - 1] == '\n')
2303 text[strlen(text) - 1] = '\0';
2304 strcat(text, "\n000");
2305 CtdlIPC_putline(ipc, text);
2310 /* Malloc failed but we are committed to send */
2311 /* This may result in extra blanks at the bottom */
2312 CtdlIPC_putline(ipc, text);
2313 CtdlIPC_putline(ipc, "000");
2319 /* Partial read of file from server */
2320 size_t CtdlIPCPartialRead(CtdlIPC * ipc, void **buf, size_t offset, size_t bytes, char *cret) {
2332 sprintf(aaa, "READ %d|%d", (int) offset, (int) bytes);
2333 CtdlIPC_putline(ipc, aaa);
2334 CtdlIPC_getline(ipc, aaa);
2336 strcpy(cret, &aaa[4]);
2338 len = extract_long(&aaa[4], 0);
2339 *buf = (void *) realloc(*buf, (size_t) (offset + len));
2341 /* I know what I'm doing */
2342 serv_read(ipc, ((char *) (*buf) + offset), len);
2345 /* We have to read regardless */
2346 serv_read(ipc, aaa, len);
2350 CtdlIPC_unlock(ipc);
2356 int CtdlIPCEndDownload(CtdlIPC * ipc, char *cret) {
2361 if (!ipc->downloading)
2364 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2366 ipc->downloading = 0;
2372 int CtdlIPCSpecifyPreferredFormats(CtdlIPC * ipc, char *cret, char *formats) {
2376 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2377 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2384 int CtdlIPCReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2385 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
2394 if (!ipc->downloading)
2398 if (progress_gauge_callback)
2399 progress_gauge_callback(ipc, len, bytes);
2400 while (len < bytes) {
2403 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2409 if (progress_gauge_callback)
2410 progress_gauge_callback(ipc, len, bytes);
2415 /* READ - pipelined */
2416 int CtdlIPCHighSpeedReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2417 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
2419 int calls; /* How many calls in the pipeline */
2420 int i; /* iterator */
2429 if (!ipc->downloading)
2432 *buf = (void *) realloc(*buf, bytes - resume);
2438 if (progress_gauge_callback)
2439 progress_gauge_callback(ipc, len, bytes);
2441 /* How many calls will be in the pipeline? */
2442 calls = (bytes - resume) / 4096;
2443 if ((bytes - resume) % 4096)
2446 /* Send all requests at once */
2447 for (i = 0; i < calls; i++) {
2448 sprintf(aaa, "READ %d|4096", (int) (i * 4096 + resume));
2449 CtdlIPC_putline(ipc, aaa);
2452 /* Receive all responses at once */
2453 for (i = 0; i < calls; i++) {
2454 CtdlIPC_getline(ipc, aaa);
2456 strcpy(cret, &aaa[4]);
2458 len = extract_long(&aaa[4], 0);
2459 /* I know what I'm doing */
2460 serv_read(ipc, ((char *) (*buf) + (i * 4096)), len);
2462 if (progress_gauge_callback)
2463 progress_gauge_callback(ipc, i * 4096 + len, bytes);
2465 CtdlIPC_unlock(ipc);
2471 int CtdlIPCEndUpload(CtdlIPC * ipc, int discard, char *cret) {
2477 if (!ipc->uploading)
2480 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2481 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2488 int CtdlIPCWriteUpload(CtdlIPC * ipc, FILE * uploadFP, void (*progress_gauge_callback)
2489 (CtdlIPC *, unsigned long, unsigned long), char *cret) {
2495 FILE *fd = uploadFP;
2501 fseek(fd, 0L, SEEK_END);
2505 if (progress_gauge_callback)
2506 progress_gauge_callback(ipc, 0, bytes);
2508 while (offset < bytes) {
2511 /* Read some data in */
2512 to_write = fread(buf, 1, 4096, fd);
2514 if (feof(fd) || ferror(fd))
2517 sprintf(aaa, "WRIT %d", (int) to_write);
2518 CtdlIPC_putline(ipc, aaa);
2519 CtdlIPC_getline(ipc, aaa);
2520 strcpy(cret, &aaa[4]);
2522 if (aaa[0] == '7') {
2523 to_write = extract_long(&aaa[4], 0);
2525 serv_write(ipc, buf, to_write);
2527 if (progress_gauge_callback)
2528 progress_gauge_callback(ipc, offset, bytes);
2529 /* Detect short reads and back up if needed */
2530 /* offset will never be negative anyway */
2531 fseek(fd, (signed) offset, SEEK_SET);
2537 if (progress_gauge_callback)
2538 progress_gauge_callback(ipc, 1, 1);
2541 return (!ferr ? ret : -2);
2546 * Generic command method. This method should handle any server command
2547 * except for CHAT. It takes the following arguments:
2549 * ipc The server to speak with
2550 * command Preformatted command to send to server
2551 * to_send A text or binary file to send to server
2552 * (only sent if server requests it)
2553 * bytes_to_send The number of bytes in to_send (required if
2554 * sending binary, optional if sending listing)
2555 * to_receive Pointer to a NULL pointer, if the server
2556 * sends text or binary we will allocate memory
2557 * for the file and stuff it here
2558 * bytes_to_receive If a file is received, we will store its
2560 * proto_response The protocol response. Caller must provide
2561 * this buffer and ensure that it is at least
2562 * 128 bytes in length.
2564 * This function returns a number equal to the protocol response number,
2565 * -1 if an internal error occurred, -2 if caller provided bad values,
2566 * or 0 - the protocol response number if bad values were found during
2567 * the protocol exchange.
2569 * It stores the protocol response string (minus the number) in
2570 * protocol_response as described above. Some commands send additional
2571 * data in this string.
2573 int CtdlIPCGenericCommand(CtdlIPC * ipc,
2574 const char *command,
2575 const char *to_send,
2576 size_t bytes_to_send, char **to_receive, size_t *bytes_to_receive, char *proto_response) {
2583 if (!proto_response) {
2588 CtdlIPC_putline(ipc, command);
2590 CtdlIPC_getline(ipc, proto_response);
2591 if (proto_response[3] == '*')
2593 ret = atoi(proto_response);
2594 strcpy(proto_response, &proto_response[4]);
2595 switch (ret / 100) {
2596 default: /* Unknown, punt */
2598 case 3: /* MORE_DATA */
2600 /* Don't need to do anything */
2602 case 1: /* LISTING_FOLLOWS */
2603 if (to_receive && !*to_receive && bytes_to_receive) {
2604 *to_receive = CtdlIPCReadListing(ipc, NULL);
2607 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2611 case 4: /* SEND_LISTING */
2613 CtdlIPCSendListing(ipc, to_send);
2616 /* No listing given, fake it */
2617 CtdlIPC_putline(ipc, "000");
2621 case 6: /* BINARY_FOLLOWS */
2622 if (to_receive && !*to_receive && bytes_to_receive) {
2623 *bytes_to_receive = extract_long(proto_response, 0);
2624 *to_receive = (char *)
2625 malloc((size_t) *bytes_to_receive);
2630 serv_read(ipc, *to_receive, *bytes_to_receive);
2637 drain = extract_long(proto_response, 0);
2638 while (drain > SIZ) {
2639 serv_read(ipc, buf, SIZ);
2642 serv_read(ipc, buf, drain);
2646 case 7: /* SEND_BINARY */
2647 if (to_send && bytes_to_send) {
2648 serv_write(ipc, to_send, bytes_to_send);
2650 else if (bytes_to_send) {
2651 /* Fake it, send nulls */
2654 fake = bytes_to_send;
2655 memset(buf, '\0', SIZ);
2656 while (fake > SIZ) {
2657 serv_write(ipc, buf, SIZ);
2660 serv_write(ipc, buf, fake);
2662 } /* else who knows? DANGER WILL ROBINSON */
2664 case 8: /* SEND_THEN_RECV */
2665 if (!strncasecmp(command, "CHAT", 4)) {
2666 /* Don't call chatmode with generic! */
2667 CtdlIPC_putline(ipc, "/quit");
2671 /* In this mode we send then receive listing */
2673 CtdlIPCSendListing(ipc, to_send);
2676 /* No listing given, fake it */
2677 CtdlIPC_putline(ipc, "000");
2680 if (to_receive && !*to_receive && bytes_to_receive) {
2681 *to_receive = CtdlIPCReadListing(ipc, NULL);
2684 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2689 case 9: /* ASYNC_MSG */
2690 /* CtdlIPCDoAsync(ret, proto_response); */
2691 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2697 CtdlIPC_unlock(ipc);
2703 * Connect to a Citadel on a remote host using a TCP/IP socket
2705 static int tcp_connectsock(char *host, char *service) {
2706 struct in6_addr serveraddr;
2707 struct addrinfo hints;
2708 struct addrinfo *res = NULL;
2709 struct addrinfo *ai = NULL;
2713 if ((host == NULL) || IsEmptyStr(host)) {
2714 service = DEFAULT_HOST;
2716 if ((service == NULL) || IsEmptyStr(service)) {
2717 service = DEFAULT_PORT;
2720 memset(&hints, 0x00, sizeof(hints));
2721 hints.ai_flags = AI_NUMERICSERV;
2722 hints.ai_family = AF_UNSPEC;
2723 hints.ai_socktype = SOCK_STREAM;
2726 * Handle numeric IPv4 and IPv6 addresses
2728 rc = inet_pton(AF_INET, host, &serveraddr);
2729 if (rc == 1) { /* dotted quad */
2730 hints.ai_family = AF_INET;
2731 hints.ai_flags |= AI_NUMERICHOST;
2734 rc = inet_pton(AF_INET6, host, &serveraddr);
2735 if (rc == 1) { /* IPv6 address */
2736 hints.ai_family = AF_INET6;
2737 hints.ai_flags |= AI_NUMERICHOST;
2741 /* Begin the connection process */
2743 rc = getaddrinfo(host, service, &hints, &res);
2749 * Try all available addresses until we connect to one or until we run out.
2751 for (ai = res; ai != NULL; ai = ai->ai_next) {
2752 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2756 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
2758 return (sock); /* Connected! */
2761 close(sock); /* Failed. Close the socket to avoid fd leak! */
2769 * Connect to a Citadel on the local host using a unix domain socket
2771 static int uds_connectsock(int *isLocal, char *sockpath) {
2772 struct sockaddr_un addr;
2775 memset(&addr, 0, sizeof(addr));
2776 addr.sun_family = AF_UNIX;
2777 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2779 s = socket(AF_UNIX, SOCK_STREAM, 0);
2784 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2795 * input binary data from socket
2797 static void serv_read(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2798 unsigned int len, rlen;
2800 #if defined(HAVE_OPENSSL)
2802 serv_read_ssl(ipc, buf, bytes);
2807 while (len < bytes) {
2808 rlen = read(ipc->sock, &buf[len], bytes - len);
2810 connection_died(ipc, 0);
2819 * send binary to server
2821 void serv_write(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2822 unsigned int bytes_written = 0;
2825 #if defined(HAVE_OPENSSL)
2827 serv_write_ssl(ipc, buf, nbytes);
2831 while (bytes_written < nbytes) {
2832 retval = write(ipc->sock, &buf[bytes_written], nbytes - bytes_written);
2834 connection_died(ipc, 0);
2837 bytes_written += retval;
2845 * input binary data from encrypted connection
2847 static void serv_read_ssl(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2852 while (len < bytes) {
2853 if (SSL_want_read(ipc->ssl)) {
2854 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2855 error_printf("SSL_write in serv_read:\n");
2856 ERR_print_errors_fp(stderr);
2859 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2863 errval = SSL_get_error(ipc->ssl, rlen);
2864 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2868 error_printf("SSL_read in serv_read: %s\n", ERR_reason_error_string(ERR_peek_error()));
2869 connection_died(ipc, 1);
2878 * send binary to server encrypted
2880 static void serv_write_ssl(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2881 unsigned int bytes_written = 0;
2885 while (bytes_written < nbytes) {
2886 if (SSL_want_write(ipc->ssl)) {
2887 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2888 error_printf("SSL_read in serv_write:\n");
2889 ERR_print_errors_fp(stderr);
2892 retval = SSL_write(ipc->ssl, &buf[bytes_written], nbytes - bytes_written);
2896 errval = SSL_get_error(ipc->ssl, retval);
2897 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2901 if (errval == SSL_ERROR_ZERO_RETURN || errval == SSL_ERROR_SSL) {
2902 serv_write(ipc, &buf[bytes_written], nbytes - bytes_written);
2905 error_printf("SSL_write in serv_write: %s\n", ERR_reason_error_string(ERR_peek_error()));
2906 connection_died(ipc, 1);
2909 bytes_written += retval;
2914 static void CtdlIPC_init_OpenSSL(void) {
2916 const SSL_METHOD *ssl_method;
2919 /* already done init */
2928 SSL_load_error_strings();
2929 SSLeay_add_ssl_algorithms();
2931 /* Set up the SSL context in which we will oeprate */
2932 ssl_method = SSLv23_client_method();
2933 ssl_ctx = SSL_CTX_new(ssl_method);
2935 error_printf("SSL_CTX_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2938 /* Any reasonable cipher we can get */
2939 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2940 error_printf("No ciphers available for encryption\n");
2943 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2945 /* Load DH parameters into the context */
2948 error_printf("Can't allocate a DH object: %s\n", ERR_reason_error_string(ERR_get_error()));
2952 if (!(DH_generate_parameters_ex(dh, 128, DH_GENERATOR_2, 0))) {
2953 error_printf("Can't generate DH parameters: %s\n", ERR_reason_error_string(ERR_get_error()));
2958 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2962 #endif /* HAVE_OPENSSL */
2965 int ReadNetworkChunk(CtdlIPC * ipc) {
2979 FD_SET(ipc->sock, &read_fd);
2980 ret = select(ipc->sock + 1, &read_fd, NULL, NULL, &tv);
2984 *(ipc->BufPtr) = '\0';
2985 n = recv(ipc->sock, ipc->BufPtr, ipc->BufSize - (ipc->BufPtr - ipc->Buf) - 1, 0);
2987 ipc->BufPtr[n] = '\0';
2996 if (!(errno == EINTR || errno == EAGAIN))
2997 error_printf("\nselect failed: %d %s\n", err, strerror(err));
3005 * input string from socket - implemented in terms of serv_read()
3009 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3011 char *aptr, *bptr, *aeptr, *beptr;
3015 #if defined(HAVE_OPENSSL)
3018 /* Read one character at a time. */
3020 serv_read(ipc, &buf[i], 1);
3021 if (buf[i] == '\n' || i == (SIZ - 1))
3025 /* If we got a long line, discard characters until the newline. */
3027 while (buf[i] != '\n')
3028 serv_read(ipc, &buf[i], 1);
3030 /* Strip the trailing newline (and carriage return, if present) */
3031 if (i >= 0 && buf[i] == 10)
3033 if (i >= 0 && buf[i] == 13)
3039 if (ipc->Buf == NULL) {
3041 ipc->Buf = (char *) malloc(ipc->BufSize + 10);
3043 ipc->BufPtr = ipc->Buf;
3047 if (ipc->BufUsed == 0)
3048 ReadNetworkChunk(ipc);
3053 aeptr = ipc->Buf + ipc->BufSize;
3054 while ((aptr < aeptr) && (bptr < beptr) && (*aptr != '\0') && (*aptr != '\n'))
3055 *(bptr++) = *(aptr++);
3056 if ((*aptr == '\n') && (aptr < aeptr)) {
3057 /* Terminate it right, remove the line breaks */
3058 while ((aptr < aeptr) && ((*aptr == '\n') || (*aptr == '\r')))
3060 while ((aptr < aeptr) && (*(aptr + 1) == '\0'))
3063 if ((bptr > buf + 1) && (*(bptr - 1) == '\r'))
3066 /* is there more in the buffer we need to read later? */
3067 if (ipc->Buf + ipc->BufUsed > aptr) {
3072 ipc->BufPtr = ipc->Buf;
3077 // should we move our read stuf to the bufferstart so we have more space at the end?
3078 else if ((ipc->BufPtr != ipc->Buf) && (ipc->BufUsed > (ipc->BufSize - (ipc->BufSize / 4)))) {
3079 size_t NewBufSize = ipc->BufSize * 2;
3080 int delta = (ipc->BufPtr - ipc->Buf);
3083 /* if the line would end after our buffer, we should use a bigger buffer. */
3084 NewBuf = (char *) malloc(NewBufSize + 10);
3085 memcpy(NewBuf, ipc->BufPtr, ipc->BufUsed - delta);
3087 ipc->Buf = ipc->BufPtr = NewBuf;
3088 ipc->BufUsed -= delta;
3089 ipc->BufSize = NewBufSize;
3091 if (ReadNetworkChunk(ipc) < 0) {
3098 #else /* CHUNKED_READ */
3100 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3103 /* Read one character at a time. */
3105 serv_read(ipc, &buf[i], 1);
3106 if (buf[i] == '\n' || i == (SIZ - 1))
3110 /* If we got a long line, discard characters until the newline. */
3112 while (buf[i] != '\n')
3113 serv_read(ipc, &buf[i], 1);
3115 /* Strip the trailing newline (and carriage return, if present) */
3116 if (i >= 0 && buf[i] == 10)
3118 if (i >= 0 && buf[i] == 13)
3123 #endif /* CHUNKED_READ */
3126 void CtdlIPC_chat_recv(CtdlIPC * ipc, char *buf) {
3127 CtdlIPC_getline(ipc, buf);
3131 * send line to server - implemented in terms of serv_write()
3133 static void CtdlIPC_putline(CtdlIPC * ipc, const char *buf) {
3138 cmd = malloc(len + 2);
3140 /* This requires no extra memory */
3141 serv_write(ipc, buf, len);
3142 serv_write(ipc, "\n", 1);
3145 /* This is network-optimized */
3146 strncpy(cmd, buf, len);
3147 strcpy(cmd + len, "\n");
3148 serv_write(ipc, cmd, len + 1);
3152 ipc->last_command_sent = time(NULL);
3156 void CtdlIPC_chat_send(CtdlIPC * ipc, const char *buf) {
3157 CtdlIPC_putline(ipc, buf);
3164 CtdlIPC *CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf) {
3171 ipc = malloc(sizeof(struct _CtdlIPC));
3175 #if defined(HAVE_OPENSSL)
3177 CtdlIPC_init_OpenSSL();
3179 ipc->sock = -1; /* Not connected */
3180 ipc->isLocal = 0; /* Not local, of course! */
3181 ipc->downloading = 0;
3183 ipc->last_command_sent = 0L;
3184 ipc->network_status_cb = NULL;
3189 strcpy(cithost, DEFAULT_HOST); /* default host */
3190 strcpy(citport, DEFAULT_PORT); /* default port */
3192 /* Allow caller to supply our values */
3193 if (hostbuf && strlen(hostbuf) > 0) {
3194 strcpy(cithost, hostbuf);
3196 if (portbuf && strlen(portbuf) > 0) {
3197 strcpy(citport, portbuf);
3200 /* Read host/port from command line if present */
3201 for (a = 0; a < argc; ++a) {
3206 strcpy(cithost, argv[a]);
3209 strcpy(citport, argv[a]);
3212 error_printf("%s: usage: ", argv[0]);
3213 error_printf("%s [host] [port] ", argv[0]);
3220 if ((!strcmp(cithost, "localhost")) || (!strcmp(cithost, "127.0.0.1"))) {
3224 /* If we're using a unix domain socket we can do a bunch of stuff */
3225 if (!strcmp(cithost, UDS)) {
3226 if (!strcasecmp(citport, DEFAULT_PORT)) {
3227 snprintf(sockpath, sizeof sockpath, "%s", file_citadel_socket);
3230 snprintf(sockpath, sizeof sockpath, "%s/%s", citport, "citadel.socket");
3232 printf("[%s]\n", sockpath);
3233 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
3234 if (ipc->sock == -1) {
3238 if (hostbuf != NULL)
3239 strcpy(hostbuf, cithost);
3240 if (portbuf != NULL)
3241 strcpy(portbuf, sockpath);
3242 strcpy(ipc->ip_hostname, "");
3243 strcpy(ipc->ip_address, "");
3247 printf("[%s:%s]\n", cithost, citport);
3248 ipc->sock = tcp_connectsock(cithost, citport);
3249 if (ipc->sock == -1) {
3255 /* Learn the actual network identity of the host to which we are connected */
3257 struct sockaddr_in6 clientaddr;
3258 unsigned int addrlen = sizeof(clientaddr);
3260 ipc->ip_hostname[0] = 0;
3261 ipc->ip_address[0] = 0;
3263 getpeername(ipc->sock, (struct sockaddr *) &clientaddr, &addrlen);
3264 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_hostname, sizeof ipc->ip_hostname, NULL, 0, 0);
3265 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_address, sizeof ipc->ip_address, NULL, 0, NI_NUMERICHOST);
3267 /* stuff other things elsewhere */
3269 if (hostbuf != NULL)
3270 strcpy(hostbuf, cithost);
3271 if (portbuf != NULL)
3272 strcpy(portbuf, citport);
3278 * Disconnect and delete the IPC class (destructor)
3280 void CtdlIPC_delete(CtdlIPC * ipc) {
3283 SSL_shutdown(ipc->ssl);
3288 if (ipc->sock > -1) {
3289 shutdown(ipc->sock, 2); /* Close it up */
3292 if (ipc->Buf != NULL)
3301 * Disconnect and delete the IPC class (destructor)
3302 * Also NULLs out the pointer
3304 void CtdlIPC_delete_ptr(CtdlIPC ** pipc) {
3305 CtdlIPC_delete(*pipc);