4 #define DEFAULT_HOST UDS
5 #define DEFAULT_PORT "citadel"
13 # include <sys/time.h>
20 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
33 #ifdef THREADED_CLIENT
37 #include "citadel_ipc.h"
38 #include "citadel_decls.h"
41 #ifdef THREADED_CLIENT
42 pthread_mutex_t rwlock;
46 static SSL_CTX *ssl_ctx;
49 #ifdef THREADED_CLIENT
50 pthread_mutex_t **Critters; /* Things that need locking */
51 #endif /* THREADED_CLIENT */
53 #endif /* HAVE_OPENSSL */
56 #define INADDR_NONE 0xffffffff
59 static void (*status_hook)(char *s) = NULL;
61 void setCryptoStatusHook(void (*hook)(char *s)) {
66 char express_msgs = 0;
69 static void serv_read(CtdlIPC *ipc, char *buf, int bytes);
70 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes);
72 static void serv_read_ssl(CtdlIPC *ipc, char *buf, int bytes);
73 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes);
74 static void ssl_lock(int mode, int n, const char *file, int line);
75 static void endtls(SSL *ssl);
76 #ifdef THREADED_CLIENT
77 static unsigned long id_callback(void);
78 #endif /* THREADED_CLIENT */
79 #endif /* HAVE_OPENSSL */
83 * Does nothing. The server should always return 200.
85 int CtdlIPCNoop(CtdlIPC *ipc)
89 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
94 * Does nothing interesting. The server should always return 200
95 * along with your string.
97 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
103 if (!cret) return -2;
105 aaa = (char *)malloc((size_t)(strlen(arg) + 6));
108 sprintf(aaa, "ECHO %s", arg);
109 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
116 * Asks the server to close the connecction.
117 * Should always return 200.
119 int CtdlIPCQuit(CtdlIPC *ipc)
125 CtdlIPC_putline(ipc, "QUIT");
126 CtdlIPC_getline(ipc, aaa);
134 * Asks the server to logout. Should always return 200, even if no user
135 * was logged in. The user will not be logged in after this!
137 int CtdlIPCLogout(CtdlIPC *ipc)
143 CtdlIPC_putline(ipc, "LOUT");
144 CtdlIPC_getline(ipc, aaa);
152 * First stage of authentication - pass the username. Returns 300 if the
153 * username is able to log in, with the username correctly spelled in cret.
154 * Returns various 500 error codes if the user doesn't exist, etc.
156 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
161 if (!username) return -2;
162 if (!cret) return -2;
164 aaa = (char *)malloc((size_t)(strlen(username) + 6));
167 sprintf(aaa, "USER %s", username);
168 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
175 * Second stage of authentication - provide password. The server returns
176 * 200 and several arguments in cret relating to the user's account.
178 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
183 if (!passwd) return -2;
184 if (!cret) return -2;
186 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
189 sprintf(aaa, "PASS %s", passwd);
190 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
197 * Create a new user. This returns 200 plus the same arguments as TryPassword
198 * if selfservice is nonzero, unless there was a problem creating the account.
199 * If selfservice is zero, creates a new user but does not log out the existing
200 * user - intended for use by system administrators to create accounts on
201 * behalf of other users.
203 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
208 if (!username) return -2;
209 if (!cret) return -2;
211 aaa = (char *)malloc((size_t)(strlen(username) + 6));
214 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
215 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
222 * Changes the user's password. Returns 200 if changed, errors otherwise.
224 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
229 if (!passwd) return -2;
230 if (!cret) return -2;
232 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
235 sprintf(aaa, "SETP %s", passwd);
236 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
243 /* Caller must free the march list */
244 /* which is 0 = LRMS, 1 = LKRN, 2 = LKRO, 3 = LKRA, 4 = LZRM */
245 /* floor is -1 for all, or floornum */
246 int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing, char *cret)
249 struct march *march = NULL;
250 static char *proto[] = {"LRMS", "LKRN", "LKRO", "LKRA", "LZRM" };
255 if (!listing) return -2;
256 if (*listing) return -2; /* Free the listing first */
257 if (!cret) return -2;
258 if (which < 0 || which > 4) return -2;
259 if (floor < -1) return -2; /* Can't validate upper bound, sorry */
261 sprintf(aaa, "%s %d", proto[which], floor);
262 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
263 if (ret / 100 == 1) {
266 while (bbb && strlen(bbb)) {
269 extract_token(aaa, bbb, 0, '\n');
271 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
272 mptr = (struct march *) malloc(sizeof (struct march));
275 extract(mptr->march_name, aaa, 0);
276 mptr->march_floor = (char) extract_int(aaa, 2);
277 mptr->march_order = (char) extract_int(aaa, 3);
284 while (mptr2->next != NULL)
297 /* Caller must free the struct usersupp; caller may pass an existing one */
298 int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
302 if (!cret) return -2;
303 if (!uret) return -2;
304 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
305 if (!*uret) return -1;
307 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
308 if (ret / 100 == 2) {
309 uret[0]->USscreenwidth = extract_int(cret, 0);
310 uret[0]->USscreenheight = extract_int(cret, 1);
311 uret[0]->flags = extract_int(cret, 2);
318 int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
322 if (!uret) return -2;
323 if (!cret) return -2;
325 sprintf(aaa, "SETU %d|%d|%d",
326 uret->USscreenwidth, uret->USscreenheight,
328 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
333 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
334 struct ctdlipcroom **rret, char *cret)
339 if (!cret) return -2;
340 if (!rret) return -2;
341 if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
342 if (!*rret) return -1;
345 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
350 sprintf(aaa, "GOTO %s|%s", room, passwd);
352 aaa = (char *)malloc(strlen(room) + 6);
357 sprintf(aaa, "GOTO %s", room);
359 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
360 if (ret / 100 == 2) {
361 extract(rret[0]->RRname, cret, 0);
362 rret[0]->RRunread = extract_long(cret, 1);
363 rret[0]->RRtotal = extract_long(cret, 2);
364 rret[0]->RRinfoupdated = extract_int(cret, 3);
365 rret[0]->RRflags = extract_int(cret, 4);
366 rret[0]->RRhighest = extract_long(cret, 5);
367 rret[0]->RRlastread = extract_long(cret, 6);
368 rret[0]->RRismailbox = extract_int(cret, 7);
369 rret[0]->RRaide = extract_int(cret, 8);
370 rret[0]->RRnewmail = extract_long(cret, 9);
371 rret[0]->RRfloor = extract_int(cret, 10);
380 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
381 /* whicharg is number of messages, applies to last, first, gt, lt */
382 int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
383 long **mret, char *cret)
386 register long count = 0;
387 static char *proto[] =
388 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
393 if (!cret) return -2;
394 if (!mret) return -2;
395 if (*mret) return -2;
396 if (which < 0 || which > 6) return -2;
399 sprintf(aaa, "MSGS %s||%d", proto[which],
402 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
404 if (template) count = strlen(template);
405 ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
407 while (strlen(bbb)) {
410 extract_token(aaa, bbb, 0, '\n');
412 memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
413 *mret = (long *)realloc(mret,
414 (size_t)((count + 1) * sizeof (long)));
416 *mret[count++] = atol(aaa);
424 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
425 struct ctdlipcmessage **mret, char *cret)
431 int multipart_hunting = 0;
432 char multipart_prefix[SIZ];
434 if (!cret) return -1;
435 if (!mret) return -1;
436 if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
437 if (!*mret) return -1;
438 if (!msgnum) return -1;
440 strcpy(mret[0]->content_type, "");
441 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
442 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
443 if (ret / 100 == 1) {
445 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
446 while (strlen(bbb) > 4 && bbb[4] == '=') {
447 extract_token(aaa, bbb, 0, '\n');
448 remove_token(bbb, 0, '\n');
450 if (!strncasecmp(aaa, "nhdr=yes", 8))
452 else if (!strncasecmp(aaa, "from=", 5))
453 strcpy(mret[0]->author, &aaa[5]);
454 else if (!strncasecmp(aaa, "type=", 5))
455 mret[0]->type = atoi(&aaa[5]);
456 else if (!strncasecmp(aaa, "msgn=", 5))
457 strcpy(mret[0]->msgid, &aaa[5]);
458 else if (!strncasecmp(aaa, "subj=", 5))
459 strcpy(mret[0]->subject, &aaa[5]);
460 else if (!strncasecmp(aaa, "rfca=", 5))
461 strcpy(mret[0]->email, &aaa[5]);
462 else if (!strncasecmp(aaa, "hnod=", 5))
463 strcpy(mret[0]->hnod, &aaa[5]);
464 else if (!strncasecmp(aaa, "room=", 5))
465 strcpy(mret[0]->room, &aaa[5]);
466 else if (!strncasecmp(aaa, "node=", 5))
467 strcpy(mret[0]->node, &aaa[5]);
468 else if (!strncasecmp(aaa, "rcpt=", 5))
469 strcpy(mret[0]->recipient, &aaa[5]);
470 else if (!strncasecmp(aaa, "time=", 5))
471 mret[0]->time = atol(&aaa[5]);
473 /* Multipart/alternative prefix & suffix strings help
474 * us to determine which part we want to download.
476 else if (!strncasecmp(aaa, "pref=", 5)) {
477 extract(multipart_prefix, &aaa[5], 1);
478 if (!strcasecmp(multipart_prefix,
479 "multipart/alternative")) {
483 else if (!strncasecmp(aaa, "suff=", 5)) {
484 extract(multipart_prefix, &aaa[5], 1);
485 if (!strcasecmp(multipart_prefix,
486 "multipart/alternative")) {
491 else if (!strncasecmp(aaa, "part=", 5)) {
492 struct parts *ptr, *chain;
494 ptr = (struct parts *)calloc(1, sizeof (struct parts));
497 /* Fill the buffers for the caller */
498 extract(ptr->name, &aaa[5], 0);
499 extract(ptr->filename, &aaa[5], 1);
500 extract(ptr->number, &aaa[5], 2);
501 extract(ptr->disposition, &aaa[5], 3);
502 extract(ptr->mimetype, &aaa[5], 4);
503 ptr->length = extract_long(&aaa[5], 5);
504 if (!mret[0]->attachments)
505 mret[0]->attachments = ptr;
507 chain = mret[0]->attachments;
513 /* Now handle multipart/alternative */
514 if (multipart_hunting > 0) {
515 if ( (!strcasecmp(ptr->mimetype,
517 || (!strcasecmp(ptr->mimetype,
519 strcpy(mret[0]->mime_chosen,
527 /* Eliminate "text\n" */
528 remove_token(bbb, 0, '\n');
530 /* If doing a MIME thing, pull out the extra headers */
533 if (!strncasecmp(bbb, "Content-type: ", 14)) {
534 extract_token(mret[0]->content_type, bbb, 0, '\n');
535 strcpy(mret[0]->content_type,
536 &mret[0]->content_type[14]);
537 striplt(mret[0]->content_type);
539 remove_token(bbb, 0, '\n');
540 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
546 /* Strip trailing whitespace */
547 bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
549 bbb = (char *)realloc(bbb, 1);
559 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
564 if (!cret) return -2;
565 if (!listing) return -2;
566 if (*listing) return -2;
568 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
574 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
578 char *listing = NULL;
581 if (!cret) return -2;
582 if (!ServInfo) return -2;
584 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
585 if (ret / 100 == 1) {
588 while (*listing && strlen(listing)) {
589 extract_token(buf, listing, 0, '\n');
590 remove_token(listing, 0, '\n');
592 case 0: ServInfo->serv_pid = atoi(buf);
594 case 1: strcpy(ServInfo->serv_nodename,buf);
596 case 2: strcpy(ServInfo->serv_humannode,buf);
598 case 3: strcpy(ServInfo->serv_fqdn,buf);
600 case 4: strcpy(ServInfo->serv_software,buf);
602 case 5: ServInfo->serv_rev_level = atoi(buf);
604 case 6: strcpy(ServInfo->serv_bbs_city,buf);
606 case 7: strcpy(ServInfo->serv_sysadm,buf);
608 case 9: strcpy(ServInfo->serv_moreprompt,buf);
610 case 10: ServInfo->serv_ok_floors = atoi(buf);
612 case 11: ServInfo->serv_paging_level = atoi(buf);
614 case 13: ServInfo->serv_supports_qnop = atoi(buf);
625 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
630 if (!cret) return -2;
631 if (!listing) return -2;
632 if (*listing) return -2;
634 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
640 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
642 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
647 if (!cret) return -2;
650 sprintf(aaa, "SLRP %ld", msgnum);
652 sprintf(aaa, "SLRP HIGHEST");
653 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
659 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
664 if (!cret) return -2;
665 if (!username) return -2;
667 aaa = (char *)malloc(strlen(username) + 6);
670 sprintf(aaa, "INVT %s", username);
671 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
678 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
683 if (!cret) return -1;
684 if (!username) return -1;
686 aaa = (char *)malloc(strlen(username) + 6);
688 sprintf(aaa, "KICK %s", username);
689 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
696 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
700 if (!cret) return -2;
701 if (!qret) return -2;
702 if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
703 if (!*qret) return -1;
705 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
706 if (ret / 100 == 2) {
707 extract(qret[0]->QRname, cret, 0);
708 extract(qret[0]->QRpasswd, cret, 1);
709 extract(qret[0]->QRdirname, cret, 2);
710 qret[0]->QRflags = extract_int(cret, 3);
711 qret[0]->QRfloor = extract_int(cret, 4);
712 qret[0]->QRorder = extract_int(cret, 5);
719 /* set forget to kick all users out of room */
720 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
725 if (!cret) return -2;
726 if (!qret) return -2;
728 aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
729 strlen(qret->QRdirname) + 52);
732 sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
733 qret->QRname, qret->QRpasswd, qret->QRdirname,
734 qret->QRflags, forget, qret->QRfloor, qret->QRorder);
735 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
742 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
744 if (!cret) return -1;
746 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
751 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
756 if (!cret) return -2;
757 if (!username) return -2;
759 aaa = (char *)malloc(strlen(username) + 6);
762 sprintf(aaa, "SETA %s", username);
763 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
770 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
775 if (!cret) return -2;
778 aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
781 sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
782 mr->type, mr->author);
783 ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
791 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
795 if (!cret) return -2;
796 if (!iret) return -2;
797 if (*iret) return -2;
799 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
804 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
808 if (!cret) return -2;
809 if (!msgnum) return -2;
811 sprintf(aaa, "DELE %ld", msgnum);
812 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
817 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
822 if (!cret) return -2;
823 if (!destroom) return -2;
824 if (!msgnum) return -2;
826 aaa = (char *)malloc(strlen(destroom) + 28);
829 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
830 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
837 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
841 if (!cret) return -2;
843 sprintf(aaa, "KILL %d", for_real);
844 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
849 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
850 const char *password, int floor, char *cret)
855 if (!cret) return -2;
856 if (!roomname) return -2;
859 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
861 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
864 aaa = (char *)malloc(strlen(roomname) + 40);
866 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
869 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
876 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
878 if (!cret) return -2;
880 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
885 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
891 if (!cret) return -2;
892 if (!mret) return -2;
893 if (*mret) return -2;
894 if (!message) return -2;
896 aaa = (char *)malloc(strlen(message) + 6);
899 sprintf(aaa, "MESG %s", message);
900 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
907 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
909 if (!cret) return -2;
911 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
916 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
922 if (!cret) return -2;
923 if (!rret) return -2;
924 if (*rret) return -2;
927 aaa = (char *)malloc(strlen(username) + 6);
929 aaa = (char *)malloc(12);
933 sprintf(aaa, "GREG %s", username);
935 sprintf(aaa, "GREG _SELF_");
936 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
943 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
948 if (!cret) return -2;
949 if (!username) return -2;
950 if (axlevel < 0 || axlevel > 7) return -2;
952 aaa = (char *)malloc(strlen(username) + 17);
955 sprintf(aaa, "VALI %s|%d", username, axlevel);
956 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
963 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
967 if (!cret) return -1;
968 if (!info) return -1;
970 sprintf(aaa, "EINF %d", for_real);
971 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
976 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
980 if (!cret) return -1;
981 if (!listing) return -1;
982 if (*listing) return -1;
984 return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
989 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
991 if (!cret) return -1;
992 if (!info) return -1;
994 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
1000 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1004 if (!cret) return -1;
1005 if (!chek) return -1;
1007 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1008 if (ret / 100 == 2) {
1009 chek->newmail = extract_long(cret, 0);
1010 chek->needregis = extract_int(cret, 1);
1011 chek->needvalid = extract_int(cret, 2);
1018 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1023 if (!cret) return -2;
1024 if (!filename) return -2;
1026 aaa = (char *)malloc(strlen(filename) + 6);
1027 if (!aaa) return -1;
1029 sprintf(aaa, "DELF %s", filename);
1030 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1037 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1042 if (!cret) return -2;
1043 if (!filename) return -2;
1044 if (!destroom) return -2;
1046 aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1047 if (!aaa) return -1;
1049 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1050 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1057 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1062 if (!cret) return -2;
1063 if (!filename) return -2;
1064 if (!destnode) return -2;
1066 aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1067 if (!aaa) return -1;
1069 sprintf(aaa, "NETF %s|%s", filename, destnode);
1070 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1077 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1082 if (!cret) return -1;
1083 if (!listing) return -1;
1084 if (*listing) return -1;
1086 *stamp = CtdlIPCServerTime(ipc, cret);
1088 *stamp = time(NULL);
1089 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1095 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1096 size_t resume, void (*progress_gauge_callback)(long, long),
1105 if (!cret) return -2;
1106 if (!filename) return -2;
1107 if (!buf) return -2;
1108 if (*buf) return -2;
1109 if (ipc->downloading) return -2;
1111 aaa = (char *)malloc(strlen(filename) + 6);
1112 if (!aaa) return -1;
1114 sprintf(aaa, "OPEN %s", filename);
1115 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1117 if (ret / 100 == 2) {
1118 ipc->downloading = 1;
1119 bytes = extract_long(cret, 0);
1120 last_mod = extract_int(cret, 1);
1121 extract(mimetype, cret, 2);
1122 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret);
1123 /* ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret); */
1124 ret = CtdlIPCEndDownload(ipc, cret);
1126 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1127 filename, mimetype);
1134 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
1135 void (*progress_gauge_callback)(long, long), char *cret)
1144 if (!cret) return -2;
1145 if (!buf) return -2;
1146 if (*buf) return -2;
1147 if (!part) return -2;
1148 if (!msgnum) return -2;
1149 if (ipc->downloading) return -2;
1151 aaa = (char *)malloc(strlen(part) + 17);
1152 if (!aaa) return -1;
1154 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1155 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1157 if (ret / 100 == 2) {
1158 ipc->downloading = 1;
1159 bytes = extract_long(cret, 0);
1160 last_mod = extract_int(cret, 1);
1161 extract(mimetype, cret, 2);
1162 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1163 ret = CtdlIPCEndDownload(ipc, cret);
1165 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1166 filename, mimetype);
1173 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1174 void (*progress_gauge_callback)(long, long), char *cret)
1182 if (!cret) return -1;
1183 if (!buf) return -1;
1184 if (*buf) return -1;
1185 if (!filename) return -1;
1186 if (ipc->downloading) return -1;
1188 aaa = (char *)malloc(strlen(filename) + 6);
1189 if (!aaa) return -1;
1191 sprintf(aaa, "OIMG %s", filename);
1192 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1194 if (ret / 100 == 2) {
1195 ipc->downloading = 1;
1196 bytes = extract_long(cret, 0);
1197 last_mod = extract_int(cret, 1);
1198 extract(mimetype, cret, 2);
1199 ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1200 ret = CtdlIPCEndDownload(ipc, cret);
1202 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1203 filename, mimetype);
1210 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1211 const char *path, void (*progress_gauge_callback)(long, long),
1217 if (!cret) return -1;
1218 if (!save_as) return -1;
1219 if (!comment) return -1;
1220 if (!path) return -1;
1221 if (!*path) return -1;
1222 if (ipc->uploading) return -1;
1224 aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1225 if (!aaa) return -1;
1227 sprintf(aaa, "UOPN %s|%s", save_as, comment);
1228 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1230 if (ret / 100 == 2) {
1232 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1233 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1241 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1242 const char *save_as,
1243 void (*progress_gauge_callback)(long, long), char *cret)
1248 if (!cret) return -1;
1249 if (!save_as) return -1;
1250 if (!path && for_real) return -1;
1251 if (!*path && for_real) return -1;
1252 if (ipc->uploading) return -1;
1254 aaa = (char *)malloc(strlen(save_as) + 17);
1255 if (!aaa) return -1;
1257 sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1258 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1260 if (ret / 100 == 2 && for_real) {
1262 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1263 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1271 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1276 if (!cret) return -2;
1277 if (!username) return -2;
1279 aaa = (char *)malloc(strlen(username) + 6);
1280 if (!aaa) return -1;
1282 sprintf(aaa, "QUSR %s", username);
1283 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1290 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1294 if (!cret) return -2;
1295 if (!listing) return -2;
1296 if (*listing) return -2;
1298 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1303 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1308 if (!cret) return -2;
1309 if (!name) return -2;
1311 aaa = (char *)malloc(strlen(name) + 17);
1312 if (!aaa) return -1;
1314 sprintf(aaa, "CFLR %s|%d", name, for_real);
1315 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1322 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1326 if (!cret) return -1;
1327 if (floornum < 0) return -1;
1329 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1330 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1335 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1340 if (!cret) return -2;
1341 if (!floorname) return -2;
1342 if (floornum < 0) return -2;
1344 aaa = (char *)malloc(strlen(floorname) + 17);
1345 if (!aaa) return -1;
1347 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1348 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1355 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1356 const char *software_name, const char *hostname, char *cret)
1361 if (developerid < 0) return -2;
1362 if (clientid < 0) return -2;
1363 if (revision < 0) return -2;
1364 if (!software_name) return -2;
1365 if (!hostname) return -2;
1367 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1368 if (!aaa) return -1;
1370 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1371 revision, software_name, hostname);
1372 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1379 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1385 if (!cret) return -2;
1386 if (!username) return -2;
1388 aaa = (char *)malloc(strlen(username) + 8);
1389 if (!aaa) return -1;
1392 sprintf(aaa, "SEXP %s|-", username);
1393 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1396 sprintf(aaa, "SEXP %s||", username);
1397 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1405 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1409 if (!cret) return -2;
1410 if (!listing) return -2;
1411 if (*listing) return -2;
1413 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1418 /* mode is 0 = enable, 1 = disable, 2 = status */
1419 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1423 if (!cret) return -2;
1425 sprintf(aaa, "DEXP %d", mode);
1426 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1431 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1433 if (!cret) return -2;
1434 if (!bio) return -2;
1436 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1442 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1448 if (!cret) return -2;
1449 if (!username) return -2;
1450 if (!listing) return -2;
1451 if (*listing) return -2;
1453 aaa = (char *)malloc(strlen(username) + 6);
1454 if (!aaa) return -1;
1456 sprintf(aaa, "RBIO %s", username);
1457 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1464 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1468 if (!cret) return -2;
1469 if (!listing) return -2;
1470 if (*listing) return -2;
1472 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1477 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1481 if (!cret) return -1;
1483 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1484 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1489 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1493 if (!cret) return -1;
1495 sprintf(aaa, "TERM %d", sid);
1496 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1501 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1503 if (!cret) return -1;
1505 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1510 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1514 if (!cret) return -1;
1516 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1517 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1522 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1528 if (!cret) return -2;
1529 if (!text) return -2;
1530 if (!filename) return -2;
1532 aaa = (char *)malloc(strlen(filename) + 6);
1533 if (!aaa) return -1;
1535 sprintf(aaa, "EMSG %s", filename);
1536 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1543 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1548 if (!cret) return -2;
1549 if (!hostname) return -2;
1551 aaa = (char *)malloc(strlen(hostname) + 6);
1552 if (!aaa) return -1;
1554 sprintf(aaa, "HCHG %s", hostname);
1555 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1562 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1567 if (!cret) return -2;
1568 if (!roomname) return -2;
1570 aaa = (char *)malloc(strlen(roomname) + 6);
1571 if (!aaa) return -1;
1573 sprintf(aaa, "RCHG %s", roomname);
1574 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1581 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1586 if (!cret) return -2;
1587 if (!username) return -2;
1589 aaa = (char *)malloc(strlen(username) + 6);
1590 if (!aaa) return -1;
1592 sprintf(aaa, "UCHG %s", username);
1593 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1600 /* This function returns the actual server time reported, or 0 if error */
1601 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1603 register time_t tret;
1606 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1607 if (ret / 100 == 2) {
1608 tret = extract_long(cret, 0);
1617 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1618 struct usersupp **uret, char *cret)
1623 if (!cret) return -2;
1624 if (!uret) return -2;
1625 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1626 if (!*uret) return -1;
1628 sprintf(aaa, "AGUP %s", who);
1629 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1631 if (ret / 100 == 2) {
1632 extract(uret[0]->fullname, cret, 0);
1633 extract(uret[0]->password, cret, 1);
1634 uret[0]->flags = extract_int(cret, 2);
1635 uret[0]->timescalled = extract_long(cret, 3);
1636 uret[0]->posted = extract_long(cret, 4);
1637 uret[0]->axlevel = extract_int(cret, 5);
1638 uret[0]->usernum = extract_long(cret, 6);
1639 uret[0]->lastcall = extract_long(cret, 7);
1640 uret[0]->USuserpurge = extract_int(cret, 8);
1647 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1652 if (!cret) return -2;
1653 if (!uret) return -2;
1655 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1656 if (!aaa) return -1;
1658 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1659 uret->fullname, uret->password, uret->flags,
1660 uret->timescalled, uret->posted, uret->axlevel,
1661 uret->usernum, uret->lastcall, uret->USuserpurge);
1662 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1669 /* which is 0 = room, 1 = floor, 2 = site */
1670 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
1672 static char *proto[] = {"room", "floor", "site"};
1675 if (!cret) return -2;
1676 if (which < 0 || which > 2) return -2;
1678 sprintf(aaa, "GPEX %s", proto[which]);
1679 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1684 /* which is 0 = room, 1 = floor, 2 = site */
1685 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1686 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
1691 if (!cret) return -2;
1692 if (which < 0 || which > 2) return -2;
1693 if (policy < 0 || policy > 3) return -2;
1694 if (policy >= 2 && value < 1) return -2;
1696 sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
1697 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1702 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1706 if (!cret) return -2;
1707 if (!listing) return -2;
1708 if (*listing) return -2;
1710 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1711 listing, &bytes, cret);
1716 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1718 if (!cret) return -2;
1719 if (!listing) return -2;
1721 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1727 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1728 char **listing, char *cret)
1733 if (!cret) return -2;
1734 if (!mimetype) return -2;
1735 if (!listing) return -2;
1736 if (*listing) return -2;
1738 aaa = malloc(strlen(mimetype) + 13);
1739 if (!aaa) return -1;
1740 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1741 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1742 listing, &bytes, cret);
1747 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1748 const char *listing, char *cret)
1752 if (!cret) return -2;
1753 if (!mimetype) return -2;
1754 if (!listing) return -2;
1756 aaa = malloc(strlen(mimetype) + 13);
1757 if (!aaa) return -1;
1758 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1759 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1764 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1768 if (!cret) return -2;
1769 if (!msgnum) return -2;
1771 sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1772 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1777 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1781 if (!cret) return -2;
1782 if (session < 0) return -2;
1784 sprintf(aaa, "REQT %d", session);
1785 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1790 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1794 if (!cret) return -2;
1795 if (msgnum < 0) return -2;
1797 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1798 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1803 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1812 /* New SSL object */
1813 temp_ssl = SSL_new(ssl_ctx);
1815 error_printf("SSL_new failed: %s\n",
1816 ERR_reason_error_string(ERR_get_error()));
1819 /* Pointless flag waving */
1820 #if SSLEAY_VERSION_NUMBER >= 0x0922
1821 SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
1824 if (!access("/var/run/egd-pool", F_OK))
1825 RAND_egd("/var/run/egd-pool");
1827 if (!RAND_status()) {
1828 error_printf("PRNG not properly seeded\n");
1832 /* Associate network connection with SSL object */
1833 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1834 error_printf("SSL_set_fd failed: %s\n",
1835 ERR_reason_error_string(ERR_get_error()));
1839 if (status_hook != NULL)
1840 status_hook("Requesting encryption...\r");
1842 /* Ready to start SSL/TLS */
1844 CtdlIPC_putline(ipc, "STLS");
1845 CtdlIPC_getline(ipc, buf);
1846 if (buf[0] != '2') {
1847 error_printf("Server can't start TLS: %s\n", buf);
1851 r = CtdlIPCGenericCommand(ipc,
1852 "STLS", NULL, 0, NULL, NULL, cret);
1854 error_printf("Server can't start TLS: %s\n", buf);
1859 /* Do SSL/TLS handshake */
1860 if ((a = SSL_connect(temp_ssl)) < 1) {
1861 error_printf("SSL_connect failed: %s\n",
1862 ERR_reason_error_string(ERR_get_error()));
1866 ipc->ssl = temp_ssl;
1868 BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1872 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1873 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1874 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1875 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1881 #endif /* HAVE_OPENSSL */
1886 static void endtls(SSL *ssl)
1897 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1901 if (!address) return -2;
1902 if (!cret) return -2;
1904 aaa = (char *)malloc(strlen(address) + 6);
1905 if (!aaa) return -1;
1907 sprintf(aaa, "QDIR %s", address);
1908 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1913 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1917 if (!cret) return -2;
1918 sprintf(aaa, "IPGM %d", secret);
1919 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1940 /* ************************************************************************** */
1941 /* Stuff below this line is not for public consumption */
1942 /* ************************************************************************** */
1945 inline void CtdlIPC_lock(CtdlIPC *ipc)
1947 #ifdef THREADED_CLIENT
1948 pthread_mutex_lock(&(ipc->mutex));
1953 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1955 #ifdef THREADED_CLIENT
1956 pthread_mutex_unlock(&(ipc->mutex));
1961 /* Read a listing from the server up to 000. Append to dest if it exists */
1962 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
1971 length = strlen(ret);
1978 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
1979 linelength = strlen(aaa);
1980 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
1982 strcpy(&ret[length], aaa);
1983 length += linelength;
1984 strcpy(&ret[length++], "\n");
1992 /* Send a listing to the server; generate the ending 000. */
1993 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
1997 text = (char *)malloc(strlen(listing) + 6);
1999 strcpy(text, listing);
2000 while (text[strlen(text) - 1] == '\n')
2001 text[strlen(text) - 1] = '\0';
2002 strcat(text, "\n000");
2003 CtdlIPC_putline(ipc, text);
2007 /* Malloc failed but we are committed to send */
2008 /* This may result in extra blanks at the bottom */
2009 CtdlIPC_putline(ipc, text);
2010 CtdlIPC_putline(ipc, "000");
2016 /* Partial read of file from server */
2017 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2019 register size_t len = 0;
2022 if (!buf) return -1;
2023 if (!cret) return -1;
2024 if (bytes < 1) return -1;
2025 if (offset < 0) return -1;
2028 sprintf(aaa, "READ %d|%d", (int)offset, (int)bytes);
2029 CtdlIPC_putline(ipc, aaa);
2030 CtdlIPC_getline(ipc, aaa);
2032 strcpy(cret, &aaa[4]);
2034 len = extract_long(&aaa[4], 0);
2035 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2037 /* I know what I'm doing */
2038 serv_read(ipc, (*buf + offset), len);
2040 /* We have to read regardless */
2041 serv_read(ipc, aaa, len);
2045 CtdlIPC_unlock(ipc);
2051 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2055 if (!cret) return -2;
2056 if (!ipc->downloading) return -2;
2058 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2060 ipc->downloading = 0;
2066 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2070 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2071 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2078 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
2079 void (*progress_gauge_callback)(long, long), char *cret)
2081 register size_t len;
2083 if (!cret) return -1;
2084 if (!buf) return -1;
2085 if (*buf) return -1;
2086 if (!ipc->downloading) return -1;
2089 if (progress_gauge_callback)
2090 progress_gauge_callback(len, bytes);
2091 while (len < bytes) {
2092 register size_t block;
2094 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2100 if (progress_gauge_callback)
2101 progress_gauge_callback(len, bytes);
2107 /* READ - pipelined */
2108 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2109 size_t resume, void (*progress_gauge_callback)(long, long),
2112 register size_t len;
2113 register int calls; /* How many calls in the pipeline */
2114 register int i; /* iterator */
2117 if (!cret) return -1;
2118 if (!buf) return -1;
2119 if (*buf) return -1;
2120 if (!ipc->downloading) return -1;
2122 *buf = (void *)realloc(*buf, bytes - resume);
2123 if (!*buf) return -1;
2127 if (progress_gauge_callback)
2128 progress_gauge_callback(len, bytes);
2130 /* How many calls will be in the pipeline? */
2131 calls = (bytes - resume) / 4096;
2132 if ((bytes - resume) % 4096) calls++;
2134 /* Send all requests at once */
2135 for (i = 0; i < calls; i++) {
2136 sprintf(aaa, "READ %d|4096", (int)(i * 4096 + resume) );
2137 CtdlIPC_putline(ipc, aaa);
2140 /* Receive all responses at once */
2141 for (i = 0; i < calls; i++) {
2142 CtdlIPC_getline(ipc, aaa);
2144 strcpy(cret, &aaa[4]);
2146 len = extract_long(&aaa[4], 0);
2147 /* I know what I'm doing */
2148 serv_read(ipc, ((*buf) + (i * 4096)), len);
2150 if (progress_gauge_callback)
2151 progress_gauge_callback(i * 4096 + len, bytes);
2153 CtdlIPC_unlock(ipc);
2159 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2164 if (!cret) return -1;
2165 if (!ipc->uploading) return -1;
2167 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2168 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2175 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2176 void (*progress_gauge_callback)(long, long), char *cret)
2178 register int ret = -1;
2179 register size_t offset = 0;
2185 if (!cret) return -1;
2186 if (!path) return -1;
2187 if (!*path) return -1;
2189 fd = fopen(path, "r");
2192 fseek(fd, 0L, SEEK_END);
2196 if (progress_gauge_callback)
2197 progress_gauge_callback(0, bytes);
2199 while (offset < bytes) {
2200 register size_t to_write;
2202 /* Read some data in */
2203 to_write = fread(buf, 1, 4096, fd);
2205 if (feof(fd) || ferror(fd)) break;
2207 sprintf(aaa, "WRIT %d", (int)to_write);
2208 CtdlIPC_putline(ipc, aaa);
2209 CtdlIPC_getline(ipc, aaa);
2210 strcpy(cret, &aaa[4]);
2212 if (aaa[0] == '7') {
2213 to_write = extract_long(&aaa[4], 0);
2215 serv_write(ipc, buf, to_write);
2217 if (progress_gauge_callback)
2218 progress_gauge_callback(offset, bytes);
2219 /* Detect short reads and back up if needed */
2220 fseek(fd, offset, SEEK_SET);
2225 if (progress_gauge_callback)
2226 progress_gauge_callback(1, 1);
2227 return (!ferror(fd) ? ret : -2);
2232 * Generic command method. This method should handle any server command
2233 * except for CHAT. It takes the following arguments:
2235 * ipc The server to speak with
2236 * command Preformatted command to send to server
2237 * to_send A text or binary file to send to server
2238 * (only sent if server requests it)
2239 * bytes_to_send The number of bytes in to_send (required if
2240 * sending binary, optional if sending listing)
2241 * to_receive Pointer to a NULL pointer, if the server
2242 * sends text or binary we will allocate memory
2243 * for the file and stuff it here
2244 * bytes_to_receive If a file is received, we will store its
2246 * proto_response The protocol response. Caller must provide
2247 * this buffer and ensure that it is at least
2248 * 128 bytes in length.
2250 * This function returns a number equal to the protocol response number,
2251 * -1 if an internal error occurred, -2 if caller provided bad values,
2252 * or 0 - the protocol response number if bad values were found during
2253 * the protocol exchange.
2254 * It stores the protocol response string (minus the number) in
2255 * protocol_response as described above. Some commands send additional
2256 * data in this string.
2258 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2259 const char *command, const char *to_send,
2260 size_t bytes_to_send, char **to_receive,
2261 size_t *bytes_to_receive, char *proto_response)
2267 if (!command) return -2;
2268 if (!proto_response) return -2;
2271 if (ipc->ssl) watch_ssl = 1;
2275 CtdlIPC_putline(ipc, command);
2277 CtdlIPC_getline(ipc, proto_response);
2278 if (proto_response[3] == '*')
2280 ret = atoi(proto_response);
2281 strcpy(proto_response, &proto_response[4]);
2282 switch (ret / 100) {
2283 default: /* Unknown, punt */
2285 case 3: /* MORE_DATA */
2287 /* Don't need to do anything */
2289 case 1: /* LISTING_FOLLOWS */
2290 if (to_receive && !*to_receive && bytes_to_receive) {
2291 *to_receive = CtdlIPCReadListing(ipc, NULL);
2292 } else { /* Drain */
2293 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2297 case 4: /* SEND_LISTING */
2299 CtdlIPCSendListing(ipc, to_send);
2301 /* No listing given, fake it */
2302 CtdlIPC_putline(ipc, "000");
2306 case 6: /* BINARY_FOLLOWS */
2307 if (to_receive && !*to_receive && bytes_to_receive) {
2309 extract_long(proto_response, 0);
2310 *to_receive = (char *)
2311 malloc((size_t)*bytes_to_receive);
2315 serv_read(ipc, *to_receive,
2322 drain = extract_long(proto_response, 0);
2323 while (drain > SIZ) {
2324 serv_read(ipc, buf, SIZ);
2327 serv_read(ipc, buf, drain);
2331 case 7: /* SEND_BINARY */
2332 if (to_send && bytes_to_send) {
2333 serv_write(ipc, to_send, bytes_to_send);
2334 } else if (bytes_to_send) {
2335 /* Fake it, send nulls */
2338 fake = bytes_to_send;
2339 memset(buf, '\0', SIZ);
2340 while (fake > SIZ) {
2341 serv_write(ipc, buf, SIZ);
2344 serv_write(ipc, buf, fake);
2346 } /* else who knows? DANGER WILL ROBINSON */
2348 case 8: /* START_CHAT_MODE */
2349 if (!strncasecmp(command, "CHAT", 4)) {
2350 /* Don't call chatmode with generic! */
2351 CtdlIPC_putline(ipc, "/quit");
2354 /* In this mode we send then receive listing */
2356 CtdlIPCSendListing(ipc, to_send);
2358 /* No listing given, fake it */
2359 CtdlIPC_putline(ipc, "000");
2362 if (to_receive && !*to_receive
2363 && bytes_to_receive) {
2364 *to_receive = CtdlIPCReadListing(ipc, NULL);
2365 } else { /* Drain */
2366 while (CtdlIPC_getline(ipc, buf),
2367 strcmp(buf, "000")) ;
2372 case 9: /* ASYNC_MSG */
2373 /* CtdlIPCDoAsync(ret, proto_response); */
2374 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2380 CtdlIPC_unlock(ipc);
2385 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2387 struct hostent *phe;
2388 struct servent *pse;
2389 struct protoent *ppe;
2390 struct sockaddr_in sin;
2393 memset(&sin, 0, sizeof(sin));
2394 sin.sin_family = AF_INET;
2396 pse = getservbyname(service, protocol);
2398 sin.sin_port = pse->s_port;
2400 else if (atoi(service) > 0) {
2401 sin.sin_port = htons(atoi(service));
2404 sin.sin_port = htons(defaultPort);
2406 phe = gethostbyname(host);
2408 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2409 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2412 if ((ppe = getprotobyname(protocol)) == 0) {
2415 if (!strcmp(protocol, "udp")) {
2421 s = socket(PF_INET, type, ppe->p_proto);
2426 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2433 static int uds_connectsock(int *isLocal, char *sockpath)
2435 struct sockaddr_un addr;
2438 memset(&addr, 0, sizeof(addr));
2439 addr.sun_family = AF_UNIX;
2440 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2442 s = socket(AF_UNIX, SOCK_STREAM, 0);
2447 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2457 * input binary data from socket
2459 static void serv_read(CtdlIPC *ipc, char *buf, int bytes)
2463 #if defined(HAVE_OPENSSL)
2465 serv_read_ssl(ipc, buf, bytes);
2470 while (len < bytes) {
2471 rlen = read(ipc->sock, &buf[len], bytes - len);
2473 connection_died(ipc);
2482 * send binary to server
2484 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
2486 int bytes_written = 0;
2489 #if defined(HAVE_OPENSSL)
2491 serv_write_ssl(ipc, buf, nbytes);
2495 while (bytes_written < nbytes) {
2496 retval = write(ipc->sock, &buf[bytes_written],
2497 nbytes - bytes_written);
2499 connection_died(ipc);
2502 bytes_written += retval;
2509 * input binary data from encrypted connection
2511 static void serv_read_ssl(CtdlIPC* ipc, char *buf, int bytes)
2517 while (len < bytes) {
2518 if (SSL_want_read(ipc->ssl)) {
2519 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2520 error_printf("SSL_write in serv_read:\n");
2521 ERR_print_errors_fp(stderr);
2524 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2528 errval = SSL_get_error(ipc->ssl, rlen);
2529 if (errval == SSL_ERROR_WANT_READ ||
2530 errval == SSL_ERROR_WANT_WRITE) {
2534 if (errval == SSL_ERROR_ZERO_RETURN ||
2535 errval == SSL_ERROR_SSL) {
2536 serv_read(ipc, &buf[len], bytes - len);
2539 error_printf("SSL_read in serv_read:\n");
2540 ERR_print_errors_fp(stderr);
2541 connection_died(NULL);
2550 * send binary to server encrypted
2552 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes)
2554 int bytes_written = 0;
2558 while (bytes_written < nbytes) {
2559 if (SSL_want_write(ipc->ssl)) {
2560 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2561 error_printf("SSL_read in serv_write:\n");
2562 ERR_print_errors_fp(stderr);
2565 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2566 nbytes - bytes_written);
2570 errval = SSL_get_error(ipc->ssl, retval);
2571 if (errval == SSL_ERROR_WANT_READ ||
2572 errval == SSL_ERROR_WANT_WRITE) {
2576 if (errval == SSL_ERROR_ZERO_RETURN ||
2577 errval == SSL_ERROR_SSL) {
2578 serv_write(ipc, &buf[bytes_written],
2579 nbytes - bytes_written);
2582 error_printf("SSL_write in serv_write:\n");
2583 ERR_print_errors_fp(stderr);
2584 connection_died(NULL);
2587 bytes_written += retval;
2592 static void CtdlIPC_init_OpenSSL(void)
2595 SSL_METHOD *ssl_method;
2598 /* already done init */
2606 SSL_load_error_strings();
2607 SSLeay_add_ssl_algorithms();
2609 /* Set up the SSL context in which we will oeprate */
2610 ssl_method = SSLv23_client_method();
2611 ssl_ctx = SSL_CTX_new(ssl_method);
2613 error_printf("SSL_CTX_new failed: %s\n",
2614 ERR_reason_error_string(ERR_get_error()));
2617 /* Any reasonable cipher we can get */
2618 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2619 error_printf("No ciphers available for encryption\n");
2622 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2624 /* Load DH parameters into the context */
2627 error_printf("Can't allocate a DH object: %s\n",
2628 ERR_reason_error_string(ERR_get_error()));
2631 if (!(BN_hex2bn(&(dh->p), DH_P))) {
2632 error_printf("Can't assign DH_P: %s\n",
2633 ERR_reason_error_string(ERR_get_error()));
2637 if (!(BN_hex2bn(&(dh->g), DH_G))) {
2638 error_printf("Can't assign DH_G: %s\n",
2639 ERR_reason_error_string(ERR_get_error()));
2644 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2647 #ifdef THREADED_CLIENT
2648 /* OpenSSL requires callbacks for threaded clients */
2649 CRYPTO_set_locking_callback(ssl_lock);
2650 CRYPTO_set_id_callback(id_callback);
2652 /* OpenSSL requires us to do semaphores for threaded clients */
2653 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2655 perror("malloc failed");
2658 for (a = 0; a < CRYPTO_num_locks(); a++) {
2659 Critters[a] = malloc(sizeof (pthread_mutex_t));
2661 perror("malloc failed");
2664 pthread_mutex_init(Critters[a], NULL);
2667 #endif /* THREADED_CLIENT */
2671 static void ssl_lock(int mode, int n, const char *file, int line)
2673 #ifdef THREADED_CLIENT
2674 if (mode & CRYPTO_LOCK)
2675 pthread_mutex_lock(Critters[n]);
2677 pthread_mutex_unlock(Critters[n]);
2678 #endif /* THREADED_CLIENT */
2681 #ifdef THREADED_CLIENT
2682 static unsigned long id_callback(void) {
2683 return (unsigned long)pthread_self();
2685 #endif /* THREADED_CLIENT */
2686 #endif /* HAVE_OPENSSL */
2690 * input string from socket - implemented in terms of serv_read()
2692 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2696 /* Read one character at a time. */
2698 serv_read(ipc, &buf[i], 1);
2699 if (buf[i] == '\n' || i == (SIZ-1))
2703 /* If we got a long line, discard characters until the newline. */
2705 while (buf[i] != '\n')
2706 serv_read(ipc, &buf[i], 1);
2708 /* Strip the trailing newline.
2715 * send line to server - implemented in terms of serv_write()
2717 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2719 /* error_printf("< %s\n", buf); */
2720 serv_write(ipc, buf, strlen(buf));
2721 serv_write(ipc, "\n", 1);
2723 ipc->last_command_sent = time(NULL);
2730 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2737 CtdlIPC *ipc = ialloc(CtdlIPC);
2741 #if defined(HAVE_OPENSSL)
2743 CtdlIPC_init_OpenSSL();
2745 #if defined(HAVE_PTHREAD_H)
2746 pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2748 ipc->sock = -1; /* Not connected */
2749 ipc->isLocal = 0; /* Not local, of course! */
2750 ipc->downloading = 0;
2752 ipc->last_command_sent = 0L;
2754 strcpy(cithost, DEFAULT_HOST); /* default host */
2755 strcpy(citport, DEFAULT_PORT); /* default port */
2757 for (a = 0; a < argc; ++a) {
2760 } else if (a == 1) {
2761 strcpy(cithost, argv[a]);
2762 } else if (a == 2) {
2763 strcpy(citport, argv[a]);
2765 error_printf("%s: usage: ",argv[0]);
2766 error_printf("%s [host] [port] ",argv[0]);
2773 if ((!strcmp(cithost, "localhost"))
2774 || (!strcmp(cithost, "127.0.0.1"))) {
2778 /* If we're using a unix domain socket we can do a bunch of stuff */
2779 if (!strcmp(cithost, UDS)) {
2780 snprintf(sockpath, sizeof sockpath, BBSDIR "/citadel.socket");
2781 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2782 if (ipc->sock == -1) {
2786 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2787 if (portbuf != NULL) strcpy(portbuf, sockpath);
2791 ipc->sock = connectsock(cithost, citport, "tcp", 504);
2792 if (ipc->sock == -1) {
2796 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2797 if (portbuf != NULL) strcpy(portbuf, citport);
2802 * return the file descriptor of the server socket so we can select() on it.
2804 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2807 int CtdlIPC_getsockfd(CtdlIPC* ipc)
2814 * return one character
2816 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2819 char CtdlIPC_get(CtdlIPC* ipc)
2824 serv_read(ipc, buf, 1);