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 * Second stage of authentication - provide password. The server returns
287 * 200 and several arguments in cret relating to the user's account.
289 int CtdlIPCTryApopPassword(CtdlIPC * ipc, const char *response, char *cret)
299 aaa = (char *) malloc((size_t) (strlen(response) + 6));
303 sprintf(aaa, "PAS2 %s", response);
304 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
311 * Create a new user. This returns 200 plus the same arguments as TryPassword
312 * if selfservice is nonzero, unless there was a problem creating the account.
313 * If selfservice is zero, creates a new user but does not log out the existing
314 * user - intended for use by system administrators to create accounts on
315 * behalf of other users.
317 int CtdlIPCCreateUser(CtdlIPC * ipc, const char *username, int selfservice, char *cret)
327 aaa = (char *) malloc((size_t) (strlen(username) + 6));
331 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
332 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
339 * Changes the user's password. Returns 200 if changed, errors otherwise.
341 int CtdlIPCChangePassword(CtdlIPC * ipc, const char *passwd, char *cret)
351 aaa = (char *) malloc((size_t) (strlen(passwd) + 6));
355 sprintf(aaa, "SETP %s", passwd);
356 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
363 /* Caller must free the march list */
364 /* Room types are defined in enum RoomList; keep these in sync! */
365 /* floor is -1 for all, or floornum */
366 int CtdlIPCKnownRooms(CtdlIPC * ipc, enum RoomList which, int floor, struct march **listing, char *cret)
369 struct march *march = NULL;
370 static char *proto[] = { "LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
378 return -2; /* Free the listing first */
381 /* if (which < 0 || which > 4) return -2; */
383 return -2; /* Can't validate upper bound, sorry */
385 sprintf(aaa, "%s %d", proto[which], floor);
386 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
387 if (ret / 100 == 1) {
390 while (bbb && strlen(bbb)) {
393 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
395 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
396 mptr = (struct march *) malloc(sizeof(struct march));
399 extract_token(mptr->march_name, aaa, 0, '|', sizeof mptr->march_name);
400 mptr->march_flags = (unsigned int) extract_int(aaa, 1);
401 mptr->march_floor = (char) extract_int(aaa, 2);
402 mptr->march_order = (char) extract_int(aaa, 3);
403 mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
404 mptr->march_access = (char) extract_int(aaa, 5);
411 while (mptr2->next != NULL)
426 /* Caller must free the struct ctdluser; caller may pass an existing one */
427 int CtdlIPCGetConfig(CtdlIPC * ipc, struct ctdluser **uret, char *cret)
436 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
440 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
441 if (ret / 100 == 2) {
442 uret[0]->flags = extract_int(cret, 2);
449 int CtdlIPCSetConfig(CtdlIPC * ipc, struct ctdluser *uret, char *cret)
458 sprintf(aaa, "SETU 80|24|%d", uret->flags);
459 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
464 int CtdlIPCRenameUser(CtdlIPC * ipc, char *oldname, char *newname, char *cret)
476 snprintf(cmd, sizeof cmd, "RENU %s|%s", oldname, newname);
477 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
483 int CtdlIPCGotoRoom(CtdlIPC * ipc, const char *room, const char *passwd, struct ctdlipcroom **rret, char *cret)
493 *rret = (struct ctdlipcroom *) calloc(1, sizeof(struct ctdlipcroom));
498 aaa = (char *) malloc(strlen(room) + strlen(passwd) + 7);
503 sprintf(aaa, "GOTO %s|%s", room, passwd);
505 aaa = (char *) malloc(strlen(room) + 6);
510 sprintf(aaa, "GOTO %s", room);
512 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
513 if (ret / 100 == 2) {
514 extract_token(rret[0]->RRname, cret, 0, '|', sizeof rret[0]->RRname);
515 rret[0]->RRunread = extract_long(cret, 1);
516 rret[0]->RRtotal = extract_long(cret, 2);
517 rret[0]->RRinfoupdated = extract_int(cret, 3);
518 rret[0]->RRflags = extract_int(cret, 4);
519 rret[0]->RRhighest = extract_long(cret, 5);
520 rret[0]->RRlastread = extract_long(cret, 6);
521 rret[0]->RRismailbox = extract_int(cret, 7);
522 rret[0]->RRaide = extract_int(cret, 8);
523 rret[0]->RRnewmail = extract_long(cret, 9);
524 rret[0]->RRfloor = extract_int(cret, 10);
525 rret[0]->RRcurrentview = extract_int(cret, 11);
526 rret[0]->RRdefaultview = extract_int(cret, 12);
527 /* position 13 is a trash folder flag ... irrelevant in this client */
528 rret[0]->RRflags2 = extract_int(cret, 14);
539 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
540 /* whicharg is number of messages, applies to last, first, gt, lt */
541 int CtdlIPCGetMessages(CtdlIPC * ipc, enum MessageList which, int whicharg, const char *mtemplate, unsigned long **mret, char *cret)
544 unsigned long count = 0;
545 static char *proto[] = { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
556 if (which < 0 || which > 6)
560 sprintf(aaa, "MSGS %s||%d", proto[which], (mtemplate) ? 1 : 0);
562 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg, (mtemplate) ? 1 : 0);
564 count = strlen(mtemplate);
565 ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbb_len, cret);
569 *mret = (unsigned long *) calloc(1, sizeof(unsigned long));
572 while (bbb && strlen(bbb)) {
573 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
574 remove_token(bbb, 0, '\n');
575 *mret = (unsigned long *) realloc(*mret, (size_t) ((count + 2) * sizeof(unsigned long)));
577 (*mret)[count++] = atol(aaa);
590 int CtdlIPCGetSingleMessage(CtdlIPC * ipc, long msgnum, int headers, int as_mime, struct ctdlipcmessage **mret, char *cret)
596 int multipart_hunting = 0;
597 char multipart_prefix[128];
605 *mret = (struct ctdlipcmessage *) calloc(1, sizeof(struct ctdlipcmessage));
611 strcpy(encoding, "");
612 strcpy(mret[0]->content_type, "");
613 mret[0]->is_local = 0;
614 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
615 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
616 if (ret / 100 == 1) {
618 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
619 while (strlen(bbb) > 4 && bbb[4] == '=') {
620 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
621 remove_token(bbb, 0, '\n');
623 if (!strncasecmp(aaa, "nhdr=yes", 8))
625 else if (!strncasecmp(aaa, "from=", 5))
626 strncpy(mret[0]->author, &aaa[5], SIZ);
627 else if (!strncasecmp(aaa, "type=", 5))
628 mret[0]->type = atoi(&aaa[5]);
629 else if (!strncasecmp(aaa, "msgn=", 5))
630 strncpy(mret[0]->msgid, &aaa[5], SIZ);
631 else if (!strncasecmp(aaa, "subj=", 5))
632 strncpy(mret[0]->subject, &aaa[5], SIZ);
633 else if (!strncasecmp(aaa, "rfca=", 5))
634 strncpy(mret[0]->email, &aaa[5], SIZ);
635 else if (!strncasecmp(aaa, "room=", 5))
636 strncpy(mret[0]->room, &aaa[5], SIZ);
637 else if (!strncasecmp(aaa, "rcpt=", 5))
638 strncpy(mret[0]->recipient, &aaa[5], SIZ);
639 else if (!strncasecmp(aaa, "wefw=", 5))
640 strncpy(mret[0]->references, &aaa[5], SIZ);
641 else if (!strncasecmp(aaa, "time=", 5))
642 mret[0]->time = atol(&aaa[5]);
643 else if (!strncasecmp(aaa, "locl", 4))
644 mret[0]->is_local = 1;
646 /* Multipart/alternative prefix & suffix strings help
647 * us to determine which part we want to download.
649 else if (!strncasecmp(aaa, "pref=", 5)) {
650 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
651 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
654 } else if (!strncasecmp(aaa, "suff=", 5)) {
655 extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
656 if (!strcasecmp(multipart_prefix, "multipart/alternative")) {
661 else if (!strncasecmp(aaa, "part=", 5)) {
662 struct parts *ptr, *chain;
664 ptr = (struct parts *) calloc(1, sizeof(struct parts));
667 /* Fill the buffers for the caller */
668 extract_token(ptr->name, &aaa[5], 0, '|', sizeof ptr->name);
669 extract_token(ptr->filename, &aaa[5], 1, '|', sizeof ptr->filename);
670 extract_token(ptr->number, &aaa[5], 2, '|', sizeof ptr->number);
671 extract_token(ptr->disposition, &aaa[5], 3, '|', sizeof ptr->disposition);
672 extract_token(ptr->mimetype, &aaa[5], 4, '|', sizeof ptr->mimetype);
673 ptr->length = extract_long(&aaa[5], 5);
674 if (!mret[0]->attachments)
675 mret[0]->attachments = ptr;
677 chain = mret[0]->attachments;
683 /* Now handle multipart/alternative */
684 if (multipart_hunting > 0) {
685 if ((!strcasecmp(ptr->mimetype, "text/plain"))
686 || (!strcasecmp(ptr->mimetype, "text/html"))) {
687 strcpy(mret[0]->mime_chosen, ptr->number);
694 /* Eliminate "text\n" */
695 remove_token(bbb, 0, '\n');
697 /* If doing a MIME thing, pull out the extra headers */
700 if (!strncasecmp(bbb, "Content-type:", 13)) {
701 extract_token(mret[0]->content_type, bbb, 0, '\n', sizeof mret[0]->content_type);
702 strcpy(mret[0]->content_type, &mret[0]->content_type[13]);
703 striplt(mret[0]->content_type);
705 /* strip out ";charset=" portion. FIXME do something with
706 * the charset (like... convert it) instead of just throwing
709 if (strstr(mret[0]->content_type, ";") != NULL) {
710 strcpy(strstr(mret[0]->content_type, ";"), "");
714 if (!strncasecmp(bbb, "X-Citadel-MSG4-Partnum:", 23)) {
715 extract_token(mret[0]->mime_chosen, bbb, 0, '\n', sizeof mret[0]->mime_chosen);
716 strcpy(mret[0]->mime_chosen, &mret[0]->mime_chosen[23]);
717 striplt(mret[0]->mime_chosen);
719 if (!strncasecmp(bbb, "Content-transfer-encoding:", 26)) {
720 extract_token(encoding, bbb, 0, '\n', sizeof encoding);
721 strcpy(encoding, &encoding[26]);
724 remove_token(bbb, 0, '\n');
725 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
726 remove_token(bbb, 0, '\n');
733 if ((!strcasecmp(encoding, "base64")) || (!strcasecmp(encoding, "quoted-printable"))) {
735 int bytes_decoded = 0;
736 ccc = malloc(strlen(bbb) + 32768);
737 if (!strcasecmp(encoding, "base64")) {
738 bytes_decoded = CtdlDecodeBase64(ccc, bbb, strlen(bbb));
739 } else if (!strcasecmp(encoding, "quoted-printable")) {
740 bytes_decoded = CtdlDecodeQuotedPrintable(ccc, bbb, strlen(bbb));
742 ccc[bytes_decoded] = 0;
747 /* FIXME: Strip trailing whitespace */
748 bbb = (char *) realloc(bbb, (size_t) (strlen(bbb) + 1));
751 bbb = (char *) realloc(bbb, 1);
761 int CtdlIPCWhoKnowsRoom(CtdlIPC * ipc, char **listing, char *cret)
773 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
779 int CtdlIPCServerInfo(CtdlIPC * ipc, char *cret)
783 char *listing = NULL;
789 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
790 if (ret / 100 == 1) {
793 while (*listing && strlen(listing)) {
794 extract_token(buf, listing, 0, '\n', sizeof buf);
795 remove_token(listing, 0, '\n');
798 ipc->ServInfo.pid = atoi(buf);
801 strcpy(ipc->ServInfo.nodename, buf);
804 strcpy(ipc->ServInfo.humannode, buf);
807 strcpy(ipc->ServInfo.fqdn, buf);
810 strcpy(ipc->ServInfo.software, buf);
813 ipc->ServInfo.rev_level = atoi(buf);
816 strcpy(ipc->ServInfo.site_location, buf);
819 strcpy(ipc->ServInfo.sysadm, buf);
822 strcpy(ipc->ServInfo.moreprompt, buf);
825 ipc->ServInfo.ok_floors = atoi(buf);
828 ipc->ServInfo.supports_ldap = atoi(buf);
831 ipc->ServInfo.newuser_disabled = atoi(buf);
834 strcpy(ipc->ServInfo.default_cal_zone, buf);
837 ipc->ServInfo.load_avg = atof(buf);
840 ipc->ServInfo.worker_avg = atof(buf);
843 ipc->ServInfo.thread_count = atoi(buf);
846 ipc->ServInfo.has_sieve = atoi(buf);
849 ipc->ServInfo.fulltext_enabled = atoi(buf);
852 strcpy(ipc->ServInfo.svn_revision, buf);
855 ipc->ServInfo.guest_logins = atoi(buf);
868 int CtdlIPCReadDirectory(CtdlIPC * ipc, char **listing, char *cret)
880 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
886 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
888 int CtdlIPCSetLastRead(CtdlIPC * ipc, long msgnum, char *cret)
897 sprintf(aaa, "SLRP %ld", msgnum);
899 sprintf(aaa, "SLRP HIGHEST");
901 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
907 int CtdlIPCInviteUserToRoom(CtdlIPC * ipc, const char *username, char *cret)
917 aaa = (char *) malloc(strlen(username) + 6);
921 sprintf(aaa, "INVT %s", username);
922 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
929 int CtdlIPCKickoutUserFromRoom(CtdlIPC * ipc, const char *username, char *cret)
939 aaa = (char *) malloc(strlen(username) + 6);
941 sprintf(aaa, "KICK %s", username);
942 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
949 int CtdlIPCGetRoomAttributes(CtdlIPC * ipc, struct ctdlroom **qret, char *cret)
958 *qret = (struct ctdlroom *) calloc(1, sizeof(struct ctdlroom));
962 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
963 if (ret / 100 == 2) {
964 extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
965 extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
966 extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
967 qret[0]->QRflags = extract_int(cret, 3);
968 qret[0]->QRfloor = extract_int(cret, 4);
969 qret[0]->QRorder = extract_int(cret, 5);
970 qret[0]->QRdefaultview = extract_int(cret, 6);
971 qret[0]->QRflags2 = extract_int(cret, 7);
978 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc,
979 int forget, // if nonzero, kick all users out of the room
980 struct ctdlroom *qret,
991 cmd = (char *) malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) + strlen(qret->QRdirname) + 64);
995 sprintf(cmd, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
996 qret->QRname, qret->QRpasswd, qret->QRdirname,
997 qret->QRflags, forget, qret->QRfloor, qret->QRorder, qret->QRdefaultview, qret->QRflags2);
998 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
1005 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
1010 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
1015 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret) {
1024 aaa = (char *) malloc(strlen(username) + 6);
1028 sprintf(aaa, "SETA %s", username);
1029 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1036 int CtdlIPCPostMessage(CtdlIPC * ipc, int flag, int *subject_required, struct ctdlipcmessage *mr, char *cret)
1047 if (mr->references) {
1048 for (ptr = mr->references; *ptr != 0; ++ptr) {
1054 snprintf(cmd, sizeof cmd,
1055 "ENT0 %d|%s|%d|%d|%s|%s||||||%s|", flag, mr->recipient,
1056 mr->anonymous, mr->type, mr->subject, mr->author, mr->references);
1057 ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL, NULL, cret);
1058 if ((flag == 0) && (subject_required != NULL)) {
1059 /* Is the server strongly recommending that the user enter a message subject? */
1060 if ((cret[3] != '\0') && (cret[4] != '\0')) {
1061 *subject_required = extract_int(&cret[4], 1);
1071 int CtdlIPCRoomInfo(CtdlIPC * ipc, char **iret, char *cret) {
1081 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
1086 int CtdlIPCDeleteMessage(CtdlIPC * ipc, long msgnum, char *cret) {
1094 sprintf(aaa, "DELE %ld", msgnum);
1095 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1100 int CtdlIPCMoveMessage(CtdlIPC * ipc, int copy, long msgnum, const char *destroom, char *cret) {
1111 aaa = (char *) malloc(strlen(destroom) + 28);
1115 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
1116 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1123 int CtdlIPCDeleteRoom(CtdlIPC * ipc, int for_real, char *cret) {
1129 sprintf(aaa, "KILL %d", for_real);
1130 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1135 int CtdlIPCCreateRoom(CtdlIPC * ipc, int for_real, const char *roomname, int type, const char *password, int floor, char *cret)
1146 aaa = (char *) malloc(strlen(roomname) + strlen(password) + 40);
1149 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type, password, floor);
1152 aaa = (char *) malloc(strlen(roomname) + 40);
1155 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type, floor);
1157 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1164 int CtdlIPCForgetRoom(CtdlIPC * ipc, char *cret) {
1168 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
1173 int CtdlIPCSystemMessage(CtdlIPC * ipc, const char *message, char **mret, char *cret) {
1187 aaa = (char *) malloc(strlen(message) + 6);
1191 sprintf(aaa, "MESG %s", message);
1192 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
1199 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret) {
1203 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
1208 int CtdlIPCGetUserRegistration(CtdlIPC * ipc, const char *username, char **rret, char *cret) {
1221 aaa = (char *) malloc(strlen(username) + 6);
1223 aaa = (char *) malloc(12);
1228 sprintf(aaa, "GREG %s", username);
1230 sprintf(aaa, "GREG _SELF_");
1231 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
1238 int CtdlIPCValidateUser(CtdlIPC * ipc, const char *username, int axlevel, char *cret) {
1246 if (axlevel < AxDeleted || axlevel > AxAideU)
1249 aaa = (char *) malloc(strlen(username) + 17);
1253 sprintf(aaa, "VALI %s|%d", username, axlevel);
1254 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1261 int CtdlIPCSetRoomInfo(CtdlIPC * ipc, int for_real, const char *info, char *cret)
1270 sprintf(aaa, "EINF %d", for_real);
1271 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1276 int CtdlIPCUserListing(CtdlIPC * ipc, char *searchstring, char **listing, char *cret)
1291 cmd = malloc(strlen(searchstring) + 10);
1292 sprintf(cmd, "LIST %s", searchstring);
1294 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, listing, &bytes, cret);
1301 int CtdlIPCSetRegistration(CtdlIPC * ipc, const char *info, char *cret)
1308 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info), NULL, NULL, cret);
1313 int CtdlIPCMiscCheck(CtdlIPC * ipc, struct ctdlipcmisc *chek, char *cret)
1322 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1323 if (ret / 100 == 2) {
1324 chek->newmail = extract_long(cret, 0);
1325 chek->needregis = extract_int(cret, 1);
1326 chek->needvalid = extract_int(cret, 2);
1333 int CtdlIPCDeleteFile(CtdlIPC * ipc, const char *filename, char *cret)
1343 aaa = (char *) malloc(strlen(filename) + 6);
1347 sprintf(aaa, "DELF %s", filename);
1348 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1355 int CtdlIPCMoveFile(CtdlIPC * ipc, const char *filename, const char *destroom, char *cret)
1367 aaa = (char *) malloc(strlen(filename) + strlen(destroom) + 7);
1371 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1372 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1379 int CtdlIPCOnlineUsers(CtdlIPC * ipc, char **listing, time_t * stamp, char *cret)
1391 *stamp = CtdlIPCServerTime(ipc, cret);
1393 *stamp = time(NULL);
1394 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1400 int CtdlIPCFileDownload(CtdlIPC * ipc, const char *filename, void **buf, size_t resume, void (*progress_gauge_callback)
1401 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1417 if (ipc->downloading)
1420 aaa = (char *) malloc(strlen(filename) + 6);
1424 sprintf(aaa, "OPEN %s", filename);
1425 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1427 if (ret / 100 == 2) {
1428 ipc->downloading = 1;
1429 bytes = extract_long(cret, 0);
1430 last_mod = extract_int(cret, 1);
1431 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1432 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret);
1433 ret = CtdlIPCEndDownload(ipc, cret);
1435 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1442 int CtdlIPCAttachmentDownload(CtdlIPC * ipc, long msgnum, const char *part, void **buf, void (*progress_gauge_callback)
1443 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1462 if (ipc->downloading)
1465 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1466 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1467 if (ret / 100 == 2) {
1468 ipc->downloading = 1;
1469 bytes = extract_long(cret, 0);
1470 last_mod = extract_int(cret, 1);
1471 extract_token(filename, cret, 2, '|', sizeof filename);
1472 extract_token(mimetype, cret, 3, '|', sizeof mimetype);
1473 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1474 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1475 ret = CtdlIPCEndDownload(ipc, cret);
1477 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1484 int CtdlIPCImageDownload(CtdlIPC * ipc, const char *filename, void **buf, void (*progress_gauge_callback)
1485 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1501 if (ipc->downloading)
1504 aaa = (char *) malloc(strlen(filename) + 6);
1508 sprintf(aaa, "OIMG %s", filename);
1509 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1511 if (ret / 100 == 2) {
1512 ipc->downloading = 1;
1513 bytes = extract_long(cret, 0);
1514 last_mod = extract_int(cret, 1);
1515 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1516 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1517 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1518 ret = CtdlIPCEndDownload(ipc, cret);
1520 sprintf(cret, "%d|%ld|%s|%s", (int) bytes, last_mod, filename, mimetype);
1527 int CtdlIPCFileUpload(CtdlIPC * ipc, const char *save_as, const char *comment, const char *path, void (*progress_gauge_callback)
1528 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1533 char MimeTestBuf[64];
1534 const char *MimeType;
1550 uploadFP = fopen(path, "r");
1554 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1559 MimeType = GuessMimeType(&MimeTestBuf[0], len);
1560 aaa = (char *) malloc(strlen(save_as) + strlen(MimeType) + strlen(comment) + 7);
1564 sprintf(aaa, "UOPN %s|%s|%s", save_as, MimeType, comment);
1565 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1567 if (ret / 100 == 2) {
1569 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1570 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1578 int CtdlIPCImageUpload(CtdlIPC * ipc, int for_real, const char *path, const char *save_as, void (*progress_gauge_callback)
1579 (CtdlIPC *, unsigned long, unsigned long), char *cret)
1584 char MimeTestBuf[64];
1585 const char *MimeType;
1592 if (!path && for_real)
1594 if (!*path && for_real)
1599 aaa = (char *) malloc(strlen(save_as) + 17);
1603 uploadFP = fopen(path, "r");
1607 len = fread(&MimeTestBuf[0], 1, 64, uploadFP);
1611 MimeType = GuessMimeType(&MimeTestBuf[0], 64);
1613 sprintf(aaa, "UIMG %d|%s|%s", for_real, MimeType, save_as);
1614 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1616 if (ret / 100 == 2 && for_real) {
1618 ret = CtdlIPCWriteUpload(ipc, uploadFP, progress_gauge_callback, cret);
1619 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1627 int CtdlIPCQueryUsername(CtdlIPC * ipc, const char *username, char *cret)
1637 aaa = (char *) malloc(strlen(username) + 6);
1641 sprintf(aaa, "QUSR %s", username);
1642 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1649 int CtdlIPCFloorListing(CtdlIPC * ipc, char **listing, char *cret)
1660 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1665 int CtdlIPCCreateFloor(CtdlIPC * ipc, int for_real, const char *name, char *cret)
1675 sprintf(aaa, "CFLR %s|%d", name, for_real);
1676 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1682 int CtdlIPCDeleteFloor(CtdlIPC * ipc, int for_real, int floornum, char *cret)
1691 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1692 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1697 int CtdlIPCEditFloor(CtdlIPC * ipc, int floornum, const char *floorname, char *cret)
1709 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1710 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1718 * You only need to fill out hostname, the defaults will be used if any of the
1719 * other fields are not set properly.
1721 int CtdlIPCIdentifySoftware(CtdlIPC * ipc, int developerid, int clientid,
1722 int revision, const char *software_name, const char *hostname, char *cret)
1727 if (developerid < 0 || clientid < 0 || revision < 0 || !software_name) {
1730 revision = CLIENT_VERSION - 600;
1731 software_name = "Citadel (libcitadel)";
1736 aaa = (char *) malloc(strlen(software_name) + strlen(hostname) + 29);
1740 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid, revision, software_name, hostname);
1741 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1748 int CtdlIPCSendInstantMessage(CtdlIPC * ipc, const char *username, const char *text, char *cret)
1758 aaa = (char *) malloc(strlen(username) + 8);
1763 sprintf(aaa, "SEXP %s|-", username);
1764 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1766 sprintf(aaa, "SEXP %s||", username);
1767 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1775 int CtdlIPCGetInstantMessage(CtdlIPC * ipc, char **listing, char *cret)
1786 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1791 /* mode is 0 = enable, 1 = disable, 2 = status */
1792 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC * ipc, int mode, char *cret)
1799 sprintf(aaa, "DEXP %d", mode);
1800 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1805 int CtdlIPCSetBio(CtdlIPC * ipc, char *bio, char *cret)
1812 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio), NULL, NULL, cret);
1817 int CtdlIPCGetBio(CtdlIPC * ipc, const char *username, char **listing, char *cret)
1832 aaa = (char *) malloc(strlen(username) + 6);
1836 sprintf(aaa, "RBIO %s", username);
1837 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1844 int CtdlIPCListUsersWithBios(CtdlIPC * ipc, char **listing, char *cret)
1855 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1860 int CtdlIPCStealthMode(CtdlIPC * ipc, int mode, char *cret)
1867 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1868 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1873 int CtdlIPCTerminateSession(CtdlIPC * ipc, int sid, char *cret)
1880 sprintf(aaa, "TERM %d", sid);
1881 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1886 int CtdlIPCTerminateServerNow(CtdlIPC * ipc, char *cret)
1891 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1896 int CtdlIPCTerminateServerScheduled(CtdlIPC * ipc, int mode, char *cret)
1903 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1904 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1909 int CtdlIPCEnterSystemMessage(CtdlIPC * ipc, const char *filename, const char *text, char *cret)
1921 aaa = (char *) malloc(strlen(filename) + 6);
1925 sprintf(aaa, "EMSG %s", filename);
1926 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1933 /* This function returns the actual server time reported, or 0 if error */
1934 time_t CtdlIPCServerTime(CtdlIPC * ipc, char *cret)
1939 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1940 if (ret / 100 == 2) {
1941 tret = extract_long(cret, 0);
1950 int CtdlIPCAideGetUserParameters(CtdlIPC * ipc, const char *who, struct ctdluser **uret, char *cret)
1960 *uret = (struct ctdluser *) calloc(1, sizeof(struct ctdluser));
1964 sprintf(aaa, "AGUP %s", who);
1965 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1967 if (ret / 100 == 2) {
1968 extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
1969 extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
1970 uret[0]->flags = extract_int(cret, 2);
1971 uret[0]->timescalled = extract_long(cret, 3);
1972 uret[0]->posted = extract_long(cret, 4);
1973 uret[0]->axlevel = extract_int(cret, 5);
1974 uret[0]->usernum = extract_long(cret, 6);
1975 uret[0]->lastcall = extract_long(cret, 7);
1976 uret[0]->USuserpurge = extract_int(cret, 8);
1983 int CtdlIPCAideSetUserParameters(CtdlIPC * ipc, const struct ctdluser *uret, char *cret)
1993 aaa = (char *) malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1997 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1998 uret->fullname, uret->password, uret->flags, uret->timescalled,
1999 uret->posted, uret->axlevel, uret->usernum, uret->lastcall, uret->USuserpurge);
2000 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2007 int CtdlIPCAideGetEmailAddresses(CtdlIPC * ipc, const char *who, char *target_buf, char *cret)
2011 char *emailaddrs = NULL;
2012 size_t emailaddrs_len = 0;
2014 sprintf(aaa, "AGEA %s", who);
2015 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &emailaddrs, &emailaddrs_len, cret);
2017 if (ret / 100 == 1) {
2018 strcpy(target_buf, emailaddrs);
2021 if (emailaddrs != NULL) {
2030 int CtdlIPCAideSetEmailAddresses(CtdlIPC * ipc, const char *who, char *emailaddrs, char *cret)
2042 sprintf(aaa, "ASEA %s", who);
2043 ret = CtdlIPCGenericCommand(ipc, aaa, emailaddrs, 0, NULL, NULL, cret);
2049 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
2050 /* caller must free the struct ExpirePolicy */
2051 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC * ipc, GPEXWhichPolicy which, struct ExpirePolicy **policy, char *cret)
2053 static char *proto[] = {
2057 strof(mailboxespolicy)
2067 *policy = (struct ExpirePolicy *) calloc(1, sizeof(struct ExpirePolicy));
2070 if (which < 0 || which > 3)
2073 sprintf(cmd, "GPEX %s", proto[which]);
2074 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2075 if (ret / 100 == 2) {
2076 policy[0]->expire_mode = extract_int(cret, 0);
2077 policy[0]->expire_value = extract_int(cret, 1);
2084 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
2085 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
2086 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC * ipc, int which, struct ExpirePolicy *policy, char *cret)
2089 char *whichvals[] = { "room", "floor", "site", "mailboxes" };
2093 if (which < 0 || which > 3)
2097 if (policy->expire_mode < 0 || policy->expire_mode > 3)
2099 if (policy->expire_mode >= 2 && policy->expire_value < 1)
2102 sprintf(aaa, "SPEX %s|%d|%d", whichvals[which], policy->expire_mode, policy->expire_value);
2103 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2108 int CtdlIPCGetSystemConfig(CtdlIPC * ipc, char **listing, char *cret)
2119 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0, listing, &bytes, cret);
2124 int CtdlIPCSetSystemConfig(CtdlIPC * ipc, const char *listing, char *cret)
2131 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing), NULL, NULL, cret);
2136 int CtdlIPCGetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, char **listing, char *cret)
2151 aaa = malloc(strlen(mimetype) + 13);
2154 sprintf(aaa, "CONF GETSYS|%s", mimetype);
2155 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
2162 int CtdlIPCSetSystemConfigByType(CtdlIPC * ipc, const char *mimetype, const char *listing, char *cret)
2174 aaa = malloc(strlen(mimetype) + 13);
2177 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
2178 ret = CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing), NULL, NULL, cret);
2185 int CtdlIPCGetRoomNetworkConfig(CtdlIPC * ipc, char **listing, char *cret)
2196 return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0, listing, &bytes, cret);
2201 int CtdlIPCSetRoomNetworkConfig(CtdlIPC * ipc, const char *listing, char *cret)
2208 return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing), NULL, NULL, cret);
2213 int CtdlIPCRequestClientLogout(CtdlIPC * ipc, int session, char *cret)
2222 sprintf(aaa, "REQT %d", session);
2223 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2228 int CtdlIPCSetMessageSeen(CtdlIPC * ipc, long msgnum, int seen, char *cret)
2237 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
2238 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2243 int CtdlIPCStartEncryption(CtdlIPC * ipc, char *cret)
2252 /* New SSL object */
2253 temp_ssl = SSL_new(ssl_ctx);
2255 error_printf("SSL_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2258 /* Pointless flag waving */
2259 #if SSLEAY_VERSION_NUMBER >= 0x0922
2260 SSL_set_session_id_context(temp_ssl, (const unsigned char *) "Citadel SID", 14);
2263 /* Associate network connection with SSL object */
2264 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
2265 error_printf("SSL_set_fd failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2269 if (status_hook != NULL) {
2270 status_hook("Requesting encryption...\r");
2273 /* Ready to start SSL/TLS */
2274 r = CtdlIPCGenericCommand(ipc, "STLS", NULL, 0, NULL, NULL, cret);
2276 error_printf("Server can't start TLS: %s\n", buf);
2281 /* Do SSL/TLS handshake */
2282 if ((a = SSL_connect(temp_ssl)) < 1) {
2283 error_printf("SSL_connect failed: %s\n", ERR_reason_error_string(ERR_get_error()));
2287 ipc->ssl = temp_ssl;
2289 error_printf("Encrypting with %s cipher %s\n",
2290 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)), SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl))
2295 #endif /* HAVE_OPENSSL */
2300 static void endtls(SSL * ssl)
2311 int CtdlIPCDirectoryLookup(CtdlIPC * ipc, const char *address, char *cret)
2321 aaa = (char *) malloc(strlen(address) + 6);
2325 sprintf(aaa, "QDIR %s", address);
2326 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2333 int CtdlIPCInternalProgram(CtdlIPC * ipc, int secret, char *cret)
2339 sprintf(aaa, "IPGM %d", secret);
2340 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2346 /* ************************************************************************** */
2347 /* Stuff below this line is not for public consumption */
2348 /* ************************************************************************** */
2351 /* Read a listing from the server up to 000. Append to dest if it exists */
2352 char *CtdlIPCReadListing(CtdlIPC * ipc, char *dest)
2361 length = strlen(ret);
2366 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2367 linelength = strlen(aaa);
2368 ret = (char *) realloc(ret, (size_t) (length + linelength + 2));
2370 strcpy(&ret[length], aaa);
2371 length += linelength;
2372 strcpy(&ret[length++], "\n");
2380 /* Send a listing to the server; generate the ending 000. */
2381 int CtdlIPCSendListing(CtdlIPC * ipc, const char *listing)
2385 text = (char *) malloc(strlen(listing) + 6);
2387 strcpy(text, listing);
2388 while (text[strlen(text) - 1] == '\n')
2389 text[strlen(text) - 1] = '\0';
2390 strcat(text, "\n000");
2391 CtdlIPC_putline(ipc, text);
2395 /* Malloc failed but we are committed to send */
2396 /* This may result in extra blanks at the bottom */
2397 CtdlIPC_putline(ipc, text);
2398 CtdlIPC_putline(ipc, "000");
2404 /* Partial read of file from server */
2405 size_t CtdlIPCPartialRead(CtdlIPC * ipc, void **buf, size_t offset, size_t bytes, char *cret)
2418 sprintf(aaa, "READ %d|%d", (int) offset, (int) bytes);
2419 CtdlIPC_putline(ipc, aaa);
2420 CtdlIPC_getline(ipc, aaa);
2422 strcpy(cret, &aaa[4]);
2424 len = extract_long(&aaa[4], 0);
2425 *buf = (void *) realloc(*buf, (size_t) (offset + len));
2427 /* I know what I'm doing */
2428 serv_read(ipc, ((char *) (*buf) + offset), len);
2430 /* We have to read regardless */
2431 serv_read(ipc, aaa, len);
2435 CtdlIPC_unlock(ipc);
2441 int CtdlIPCEndDownload(CtdlIPC * ipc, char *cret)
2447 if (!ipc->downloading)
2450 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2452 ipc->downloading = 0;
2458 int CtdlIPCSpecifyPreferredFormats(CtdlIPC * ipc, char *cret, char *formats)
2463 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2464 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2471 int CtdlIPCReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2472 (CtdlIPC *, unsigned long, unsigned long), char *cret)
2482 if (!ipc->downloading)
2486 if (progress_gauge_callback)
2487 progress_gauge_callback(ipc, len, bytes);
2488 while (len < bytes) {
2491 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2497 if (progress_gauge_callback)
2498 progress_gauge_callback(ipc, len, bytes);
2503 /* READ - pipelined */
2504 int CtdlIPCHighSpeedReadDownload(CtdlIPC * ipc, void **buf, size_t bytes, size_t resume, void (*progress_gauge_callback)
2505 (CtdlIPC *, unsigned long, unsigned long), char *cret)
2508 int calls; /* How many calls in the pipeline */
2509 int i; /* iterator */
2518 if (!ipc->downloading)
2521 *buf = (void *) realloc(*buf, bytes - resume);
2527 if (progress_gauge_callback)
2528 progress_gauge_callback(ipc, len, bytes);
2530 /* How many calls will be in the pipeline? */
2531 calls = (bytes - resume) / 4096;
2532 if ((bytes - resume) % 4096)
2535 /* Send all requests at once */
2536 for (i = 0; i < calls; i++) {
2537 sprintf(aaa, "READ %d|4096", (int) (i * 4096 + resume));
2538 CtdlIPC_putline(ipc, aaa);
2541 /* Receive all responses at once */
2542 for (i = 0; i < calls; i++) {
2543 CtdlIPC_getline(ipc, aaa);
2545 strcpy(cret, &aaa[4]);
2547 len = extract_long(&aaa[4], 0);
2548 /* I know what I'm doing */
2549 serv_read(ipc, ((char *) (*buf) + (i * 4096)), len);
2551 if (progress_gauge_callback)
2552 progress_gauge_callback(ipc, i * 4096 + len, bytes);
2554 CtdlIPC_unlock(ipc);
2560 int CtdlIPCEndUpload(CtdlIPC * ipc, int discard, char *cret)
2567 if (!ipc->uploading)
2570 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2571 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2578 int CtdlIPCWriteUpload(CtdlIPC * ipc, FILE * uploadFP, void (*progress_gauge_callback)
2579 (CtdlIPC *, unsigned long, unsigned long), char *cret)
2586 FILE *fd = uploadFP;
2592 fseek(fd, 0L, SEEK_END);
2596 if (progress_gauge_callback)
2597 progress_gauge_callback(ipc, 0, bytes);
2599 while (offset < bytes) {
2602 /* Read some data in */
2603 to_write = fread(buf, 1, 4096, fd);
2605 if (feof(fd) || ferror(fd))
2608 sprintf(aaa, "WRIT %d", (int) to_write);
2609 CtdlIPC_putline(ipc, aaa);
2610 CtdlIPC_getline(ipc, aaa);
2611 strcpy(cret, &aaa[4]);
2613 if (aaa[0] == '7') {
2614 to_write = extract_long(&aaa[4], 0);
2616 serv_write(ipc, buf, to_write);
2618 if (progress_gauge_callback)
2619 progress_gauge_callback(ipc, offset, bytes);
2620 /* Detect short reads and back up if needed */
2621 /* offset will never be negative anyway */
2622 fseek(fd, (signed) offset, SEEK_SET);
2627 if (progress_gauge_callback)
2628 progress_gauge_callback(ipc, 1, 1);
2631 return (!ferr ? ret : -2);
2636 * Generic command method. This method should handle any server command
2637 * except for CHAT. It takes the following arguments:
2639 * ipc The server to speak with
2640 * command Preformatted command to send to server
2641 * to_send A text or binary file to send to server
2642 * (only sent if server requests it)
2643 * bytes_to_send The number of bytes in to_send (required if
2644 * sending binary, optional if sending listing)
2645 * to_receive Pointer to a NULL pointer, if the server
2646 * sends text or binary we will allocate memory
2647 * for the file and stuff it here
2648 * bytes_to_receive If a file is received, we will store its
2650 * proto_response The protocol response. Caller must provide
2651 * this buffer and ensure that it is at least
2652 * 128 bytes in length.
2654 * This function returns a number equal to the protocol response number,
2655 * -1 if an internal error occurred, -2 if caller provided bad values,
2656 * or 0 - the protocol response number if bad values were found during
2657 * the protocol exchange.
2659 * It stores the protocol response string (minus the number) in
2660 * protocol_response as described above. Some commands send additional
2661 * data in this string.
2663 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2664 const char *command,
2665 const char *to_send,
2666 size_t bytes_to_send,
2668 size_t *bytes_to_receive,
2669 char *proto_response
2677 if (!proto_response) {
2682 CtdlIPC_putline(ipc, command);
2684 CtdlIPC_getline(ipc, proto_response);
2685 if (proto_response[3] == '*')
2687 ret = atoi(proto_response);
2688 strcpy(proto_response, &proto_response[4]);
2689 switch (ret / 100) {
2690 default: /* Unknown, punt */
2692 case 3: /* MORE_DATA */
2694 /* Don't need to do anything */
2696 case 1: /* LISTING_FOLLOWS */
2697 if (to_receive && !*to_receive && bytes_to_receive) {
2698 *to_receive = CtdlIPCReadListing(ipc, NULL);
2699 } else { /* Drain */
2700 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2704 case 4: /* SEND_LISTING */
2706 CtdlIPCSendListing(ipc, to_send);
2708 /* No listing given, fake it */
2709 CtdlIPC_putline(ipc, "000");
2713 case 6: /* BINARY_FOLLOWS */
2714 if (to_receive && !*to_receive && bytes_to_receive) {
2715 *bytes_to_receive = extract_long(proto_response, 0);
2716 *to_receive = (char *)
2717 malloc((size_t) * bytes_to_receive);
2721 serv_read(ipc, *to_receive, *bytes_to_receive);
2727 drain = extract_long(proto_response, 0);
2728 while (drain > SIZ) {
2729 serv_read(ipc, buf, SIZ);
2732 serv_read(ipc, buf, drain);
2736 case 7: /* SEND_BINARY */
2737 if (to_send && bytes_to_send) {
2738 serv_write(ipc, to_send, bytes_to_send);
2739 } else if (bytes_to_send) {
2740 /* Fake it, send nulls */
2743 fake = bytes_to_send;
2744 memset(buf, '\0', SIZ);
2745 while (fake > SIZ) {
2746 serv_write(ipc, buf, SIZ);
2749 serv_write(ipc, buf, fake);
2751 } /* else who knows? DANGER WILL ROBINSON */
2753 case 8: /* START_CHAT_MODE */
2754 if (!strncasecmp(command, "CHAT", 4)) {
2755 /* Don't call chatmode with generic! */
2756 CtdlIPC_putline(ipc, "/quit");
2760 /* In this mode we send then receive listing */
2762 CtdlIPCSendListing(ipc, to_send);
2765 /* No listing given, fake it */
2766 CtdlIPC_putline(ipc, "000");
2769 if (to_receive && !*to_receive && bytes_to_receive) {
2770 *to_receive = CtdlIPCReadListing(ipc, NULL);
2773 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000"));
2778 case 9: /* ASYNC_MSG */
2779 /* CtdlIPCDoAsync(ret, proto_response); */
2780 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2786 CtdlIPC_unlock(ipc);
2792 * Connect to a Citadel on a remote host using a TCP/IP socket
2794 static int tcp_connectsock(char *host, char *service) {
2795 struct in6_addr serveraddr;
2796 struct addrinfo hints;
2797 struct addrinfo *res = NULL;
2798 struct addrinfo *ai = NULL;
2802 if ((host == NULL) || IsEmptyStr(host)) {
2803 service = DEFAULT_HOST;
2805 if ((service == NULL) || IsEmptyStr(service)) {
2806 service = DEFAULT_PORT;
2809 memset(&hints, 0x00, sizeof(hints));
2810 hints.ai_flags = AI_NUMERICSERV;
2811 hints.ai_family = AF_UNSPEC;
2812 hints.ai_socktype = SOCK_STREAM;
2815 * Handle numeric IPv4 and IPv6 addresses
2817 rc = inet_pton(AF_INET, host, &serveraddr);
2818 if (rc == 1) { /* dotted quad */
2819 hints.ai_family = AF_INET;
2820 hints.ai_flags |= AI_NUMERICHOST;
2823 rc = inet_pton(AF_INET6, host, &serveraddr);
2824 if (rc == 1) { /* IPv6 address */
2825 hints.ai_family = AF_INET6;
2826 hints.ai_flags |= AI_NUMERICHOST;
2830 /* Begin the connection process */
2832 rc = getaddrinfo(host, service, &hints, &res);
2838 * Try all available addresses until we connect to one or until we run out.
2840 for (ai = res; ai != NULL; ai = ai->ai_next) {
2841 sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
2845 rc = connect(sock, ai->ai_addr, ai->ai_addrlen);
2847 return (sock); /* Connected! */
2850 close(sock); /* Failed. Close the socket to avoid fd leak! */
2858 * Connect to a Citadel on the local host using a unix domain socket
2860 static int uds_connectsock(int *isLocal, char *sockpath) {
2861 struct sockaddr_un addr;
2864 memset(&addr, 0, sizeof(addr));
2865 addr.sun_family = AF_UNIX;
2866 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2868 s = socket(AF_UNIX, SOCK_STREAM, 0);
2873 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2884 * input binary data from socket
2886 static void serv_read(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2887 unsigned int len, rlen;
2889 #if defined(HAVE_OPENSSL)
2891 serv_read_ssl(ipc, buf, bytes);
2896 while (len < bytes) {
2897 rlen = read(ipc->sock, &buf[len], bytes - len);
2899 connection_died(ipc, 0);
2908 * send binary to server
2910 void serv_write(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2911 unsigned int bytes_written = 0;
2914 #if defined(HAVE_OPENSSL)
2916 serv_write_ssl(ipc, buf, nbytes);
2920 while (bytes_written < nbytes) {
2921 retval = write(ipc->sock, &buf[bytes_written], nbytes - bytes_written);
2923 connection_died(ipc, 0);
2926 bytes_written += retval;
2933 * input binary data from encrypted connection
2935 static void serv_read_ssl(CtdlIPC * ipc, char *buf, unsigned int bytes) {
2940 while (len < bytes) {
2941 if (SSL_want_read(ipc->ssl)) {
2942 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2943 error_printf("SSL_write in serv_read:\n");
2944 ERR_print_errors_fp(stderr);
2947 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2951 errval = SSL_get_error(ipc->ssl, rlen);
2952 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2956 error_printf("SSL_read in serv_read: %s\n", ERR_reason_error_string(ERR_peek_error()));
2957 connection_died(ipc, 1);
2966 * send binary to server encrypted
2968 static void serv_write_ssl(CtdlIPC * ipc, const char *buf, unsigned int nbytes) {
2969 unsigned int bytes_written = 0;
2973 while (bytes_written < nbytes) {
2974 if (SSL_want_write(ipc->ssl)) {
2975 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2976 error_printf("SSL_read in serv_write:\n");
2977 ERR_print_errors_fp(stderr);
2980 retval = SSL_write(ipc->ssl, &buf[bytes_written], nbytes - bytes_written);
2984 errval = SSL_get_error(ipc->ssl, retval);
2985 if (errval == SSL_ERROR_WANT_READ || errval == SSL_ERROR_WANT_WRITE) {
2989 if (errval == SSL_ERROR_ZERO_RETURN || errval == SSL_ERROR_SSL) {
2990 serv_write(ipc, &buf[bytes_written], nbytes - bytes_written);
2993 error_printf("SSL_write in serv_write: %s\n", ERR_reason_error_string(ERR_peek_error()));
2994 connection_died(ipc, 1);
2997 bytes_written += retval;
3002 static void CtdlIPC_init_OpenSSL(void) {
3004 const SSL_METHOD *ssl_method;
3007 /* already done init */
3016 SSL_load_error_strings();
3017 SSLeay_add_ssl_algorithms();
3019 /* Set up the SSL context in which we will oeprate */
3020 ssl_method = SSLv23_client_method();
3021 ssl_ctx = SSL_CTX_new(ssl_method);
3023 error_printf("SSL_CTX_new failed: %s\n", ERR_reason_error_string(ERR_get_error()));
3026 /* Any reasonable cipher we can get */
3027 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
3028 error_printf("No ciphers available for encryption\n");
3031 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
3033 /* Load DH parameters into the context */
3036 error_printf("Can't allocate a DH object: %s\n", ERR_reason_error_string(ERR_get_error()));
3040 if (!(DH_generate_parameters_ex(dh, 128, DH_GENERATOR_2, 0))) {
3041 error_printf("Can't generate DH parameters: %s\n", ERR_reason_error_string(ERR_get_error()));
3046 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
3050 #endif /* HAVE_OPENSSL */
3053 int ReadNetworkChunk(CtdlIPC * ipc) {
3067 FD_SET(ipc->sock, &read_fd);
3068 ret = select(ipc->sock + 1, &read_fd, NULL, NULL, &tv);
3072 *(ipc->BufPtr) = '\0';
3073 n = recv(ipc->sock, ipc->BufPtr, ipc->BufSize - (ipc->BufPtr - ipc->Buf) - 1, 0);
3075 ipc->BufPtr[n] = '\0';
3084 if (!(errno == EINTR || errno == EAGAIN))
3085 error_printf("\nselect failed: %d %s\n", err, strerror(err));
3093 * input string from socket - implemented in terms of serv_read()
3097 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3099 char *aptr, *bptr, *aeptr, *beptr;
3103 #if defined(HAVE_OPENSSL)
3106 /* Read one character at a time. */
3108 serv_read(ipc, &buf[i], 1);
3109 if (buf[i] == '\n' || i == (SIZ - 1))
3113 /* If we got a long line, discard characters until the newline. */
3115 while (buf[i] != '\n')
3116 serv_read(ipc, &buf[i], 1);
3118 /* Strip the trailing newline (and carriage return, if present) */
3119 if (i >= 0 && buf[i] == 10)
3121 if (i >= 0 && buf[i] == 13)
3126 if (ipc->Buf == NULL) {
3128 ipc->Buf = (char *) malloc(ipc->BufSize + 10);
3130 ipc->BufPtr = ipc->Buf;
3134 if (ipc->BufUsed == 0)
3135 ReadNetworkChunk(ipc);
3140 aeptr = ipc->Buf + ipc->BufSize;
3141 while ((aptr < aeptr) && (bptr < beptr) && (*aptr != '\0') && (*aptr != '\n'))
3142 *(bptr++) = *(aptr++);
3143 if ((*aptr == '\n') && (aptr < aeptr)) {
3144 /* Terminate it right, remove the line breaks */
3145 while ((aptr < aeptr) && ((*aptr == '\n') || (*aptr == '\r')))
3147 while ((aptr < aeptr) && (*(aptr + 1) == '\0'))
3150 if ((bptr > buf + 1) && (*(bptr - 1) == '\r'))
3153 /* is there more in the buffer we need to read later? */
3154 if (ipc->Buf + ipc->BufUsed > aptr) {
3158 ipc->BufPtr = ipc->Buf;
3163 // should we move our read stuf to the bufferstart so we have more space at the end?
3164 else if ((ipc->BufPtr != ipc->Buf) && (ipc->BufUsed > (ipc->BufSize - (ipc->BufSize / 4)))) {
3165 size_t NewBufSize = ipc->BufSize * 2;
3166 int delta = (ipc->BufPtr - ipc->Buf);
3169 /* if the line would end after our buffer, we should use a bigger buffer. */
3170 NewBuf = (char *) malloc(NewBufSize + 10);
3171 memcpy(NewBuf, ipc->BufPtr, ipc->BufUsed - delta);
3173 ipc->Buf = ipc->BufPtr = NewBuf;
3174 ipc->BufUsed -= delta;
3175 ipc->BufSize = NewBufSize;
3177 if (ReadNetworkChunk(ipc) < 0) {
3184 #else /* CHUNKED_READ */
3186 static void CtdlIPC_getline(CtdlIPC * ipc, char *buf) {
3189 /* Read one character at a time. */
3191 serv_read(ipc, &buf[i], 1);
3192 if (buf[i] == '\n' || i == (SIZ - 1))
3196 /* If we got a long line, discard characters until the newline. */
3198 while (buf[i] != '\n')
3199 serv_read(ipc, &buf[i], 1);
3201 /* Strip the trailing newline (and carriage return, if present) */
3202 if (i >= 0 && buf[i] == 10)
3204 if (i >= 0 && buf[i] == 13)
3209 #endif /* CHUNKED_READ */
3212 void CtdlIPC_chat_recv(CtdlIPC * ipc, char *buf) {
3213 CtdlIPC_getline(ipc, buf);
3217 * send line to server - implemented in terms of serv_write()
3219 static void CtdlIPC_putline(CtdlIPC * ipc, const char *buf) {
3224 cmd = malloc(len + 2);
3226 /* This requires no extra memory */
3227 serv_write(ipc, buf, len);
3228 serv_write(ipc, "\n", 1);
3231 /* This is network-optimized */
3232 strncpy(cmd, buf, len);
3233 strcpy(cmd + len, "\n");
3234 serv_write(ipc, cmd, len + 1);
3238 ipc->last_command_sent = time(NULL);
3242 void CtdlIPC_chat_send(CtdlIPC * ipc, const char *buf) {
3243 CtdlIPC_putline(ipc, buf);
3250 CtdlIPC *CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf) {
3257 ipc = malloc(sizeof(struct _CtdlIPC));
3261 #if defined(HAVE_OPENSSL)
3263 CtdlIPC_init_OpenSSL();
3265 ipc->sock = -1; /* Not connected */
3266 ipc->isLocal = 0; /* Not local, of course! */
3267 ipc->downloading = 0;
3269 ipc->last_command_sent = 0L;
3270 ipc->network_status_cb = NULL;
3275 strcpy(cithost, DEFAULT_HOST); /* default host */
3276 strcpy(citport, DEFAULT_PORT); /* default port */
3278 /* Allow caller to supply our values */
3279 if (hostbuf && strlen(hostbuf) > 0) {
3280 strcpy(cithost, hostbuf);
3282 if (portbuf && strlen(portbuf) > 0) {
3283 strcpy(citport, portbuf);
3286 /* Read host/port from command line if present */
3287 for (a = 0; a < argc; ++a) {
3292 strcpy(cithost, argv[a]);
3295 strcpy(citport, argv[a]);
3298 error_printf("%s: usage: ", argv[0]);
3299 error_printf("%s [host] [port] ", argv[0]);
3306 if ((!strcmp(cithost, "localhost")) || (!strcmp(cithost, "127.0.0.1"))) {
3310 /* If we're using a unix domain socket we can do a bunch of stuff */
3311 if (!strcmp(cithost, UDS)) {
3312 if (!strcasecmp(citport, DEFAULT_PORT)) {
3313 snprintf(sockpath, sizeof sockpath, "%s", file_citadel_socket);
3316 snprintf(sockpath, sizeof sockpath, "%s/%s", citport, "citadel.socket");
3318 printf("[%s]\n", sockpath);
3319 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
3320 if (ipc->sock == -1) {
3324 if (hostbuf != NULL)
3325 strcpy(hostbuf, cithost);
3326 if (portbuf != NULL)
3327 strcpy(portbuf, sockpath);
3328 strcpy(ipc->ip_hostname, "");
3329 strcpy(ipc->ip_address, "");
3333 printf("[%s:%s]\n", cithost, citport);
3334 ipc->sock = tcp_connectsock(cithost, citport);
3335 if (ipc->sock == -1) {
3341 /* Learn the actual network identity of the host to which we are connected */
3343 struct sockaddr_in6 clientaddr;
3344 unsigned int addrlen = sizeof(clientaddr);
3346 ipc->ip_hostname[0] = 0;
3347 ipc->ip_address[0] = 0;
3349 getpeername(ipc->sock, (struct sockaddr *) &clientaddr, &addrlen);
3350 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_hostname, sizeof ipc->ip_hostname, NULL, 0, 0);
3351 getnameinfo((struct sockaddr *) &clientaddr, addrlen, ipc->ip_address, sizeof ipc->ip_address, NULL, 0, NI_NUMERICHOST);
3353 /* stuff other things elsewhere */
3355 if (hostbuf != NULL)
3356 strcpy(hostbuf, cithost);
3357 if (portbuf != NULL)
3358 strcpy(portbuf, citport);
3364 * Disconnect and delete the IPC class (destructor)
3366 void CtdlIPC_delete(CtdlIPC *ipc) {
3369 SSL_shutdown(ipc->ssl);
3374 if (ipc->sock > -1) {
3375 shutdown(ipc->sock, 2); /* Close it up */
3378 if (ipc->Buf != NULL)
3387 * Disconnect and delete the IPC class (destructor)
3388 * Also NULLs out the pointer
3390 void CtdlIPC_delete_ptr(CtdlIPC **pipc) {
3391 CtdlIPC_delete(*pipc);