4 #define DEFAULT_HOST UDS
5 #define DEFAULT_PORT "citadel"
13 # include <sys/time.h>
20 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
31 #ifdef THREADED_CLIENT
35 #include "citadel_ipc.h"
36 #include "citadel_decls.h"
39 #ifdef THREADED_CLIENT
40 pthread_mutex_t rwlock;
44 static SSL_CTX *ssl_ctx;
47 #ifdef THREADED_CLIENT
48 pthread_mutex_t **Critters; /* Things that need locking */
49 #endif /* THREADED_CLIENT */
51 #endif /* HAVE_OPENSSL */
54 #define INADDR_NONE 0xffffffff
57 static void (*status_hook)(char *s) = NULL;
59 void setCryptoStatusHook(void (*hook)(char *s)) {
64 char express_msgs = 0;
67 static void serv_read(CtdlIPC *ipc, char *buf, int bytes);
68 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes);
70 static void serv_read_ssl(CtdlIPC *ipc, char *buf, int bytes);
71 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes);
72 static void ssl_lock(int mode, int n, const char *file, int line);
73 static void endtls(SSL *ssl);
74 #ifdef THREADED_CLIENT
75 static unsigned long id_callback(void);
76 #endif /* THREADED_CLIENT */
77 #endif /* HAVE_OPENSSL */
81 * Does nothing. The server should always return 200.
83 int CtdlIPCNoop(CtdlIPC *ipc)
87 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
92 * Does nothing interesting. The server should always return 200
93 * along with your string.
95 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
101 if (!cret) return -2;
103 aaa = (char *)malloc((size_t)(strlen(arg) + 6));
106 sprintf(aaa, "ECHO %s", arg);
107 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
114 * Asks the server to close the connecction.
115 * Should always return 200.
117 int CtdlIPCQuit(CtdlIPC *ipc)
123 CtdlIPC_putline(ipc, "QUIT");
124 CtdlIPC_getline(ipc, aaa);
132 * Asks the server to logout. Should always return 200, even if no user
133 * was logged in. The user will not be logged in after this!
135 int CtdlIPCLogout(CtdlIPC *ipc)
141 CtdlIPC_putline(ipc, "LOUT");
142 CtdlIPC_getline(ipc, aaa);
150 * First stage of authentication - pass the username. Returns 300 if the
151 * username is able to log in, with the username correctly spelled in cret.
152 * Returns various 500 error codes if the user doesn't exist, etc.
154 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
159 if (!username) return -2;
160 if (!cret) return -2;
162 aaa = (char *)malloc((size_t)(strlen(username) + 6));
165 sprintf(aaa, "USER %s", username);
166 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
173 * Second stage of authentication - provide password. The server returns
174 * 200 and several arguments in cret relating to the user's account.
176 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
181 if (!passwd) return -2;
182 if (!cret) return -2;
184 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
187 sprintf(aaa, "PASS %s", passwd);
188 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
195 * Create a new user. This returns 200 plus the same arguments as TryPassword
196 * if selfservice is nonzero, unless there was a problem creating the account.
197 * If selfservice is zero, creates a new user but does not log out the existing
198 * user - intended for use by system administrators to create accounts on
199 * behalf of other users.
201 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
206 if (!username) return -2;
207 if (!cret) return -2;
209 aaa = (char *)malloc((size_t)(strlen(username) + 6));
212 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
213 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
220 * Changes the user's password. Returns 200 if changed, errors otherwise.
222 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
227 if (!passwd) return -2;
228 if (!cret) return -2;
230 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
233 sprintf(aaa, "SETP %s", passwd);
234 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
241 /* Caller must free the march list */
242 /* which is 0 = LRMS, 1 = LKRN, 2 = LKRO, 3 = LKRA, 4 = LZRM */
243 /* floor is -1 for all, or floornum */
244 int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing, char *cret)
247 struct march *march = NULL;
248 static char *proto[] = {"LRMS", "LKRN", "LKRO", "LKRA", "LZRM" };
253 if (!listing) return -2;
254 if (*listing) return -2; /* Free the listing first */
255 if (!cret) return -2;
256 if (which < 0 || which > 4) return -2;
257 if (floor < -1) return -2; /* Can't validate upper bound, sorry */
259 sprintf(aaa, "%s %d", proto[which], floor);
260 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
261 if (ret / 100 == 1) {
264 while (bbb && strlen(bbb)) {
267 extract_token(aaa, bbb, 0, '\n');
269 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
270 mptr = (struct march *) malloc(sizeof (struct march));
273 extract(mptr->march_name, aaa, 0);
274 mptr->march_floor = (char) extract_int(aaa, 2);
275 mptr->march_order = (char) extract_int(aaa, 3);
282 while (mptr2->next != NULL)
295 /* Caller must free the struct usersupp; caller may pass an existing one */
296 int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
300 if (!cret) return -2;
301 if (!uret) return -2;
302 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
303 if (!*uret) return -1;
305 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
306 if (ret / 100 == 2) {
307 uret[0]->USscreenwidth = extract_int(cret, 0);
308 uret[0]->USscreenheight = extract_int(cret, 1);
309 uret[0]->flags = extract_int(cret, 2);
316 int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
320 if (!uret) return -2;
321 if (!cret) return -2;
323 sprintf(aaa, "SETU %d|%d|%d",
324 uret->USscreenwidth, uret->USscreenheight,
326 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
331 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
332 struct ctdlipcroom **rret, char *cret)
337 if (!cret) return -2;
338 if (!rret) return -2;
339 if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
340 if (!*rret) return -1;
343 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
348 sprintf(aaa, "GOTO %s|%s", room, passwd);
350 aaa = (char *)malloc(strlen(room) + 6);
355 sprintf(aaa, "GOTO %s", room);
357 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
358 if (ret / 100 == 2) {
359 extract(rret[0]->RRname, cret, 0);
360 rret[0]->RRunread = extract_long(cret, 1);
361 rret[0]->RRtotal = extract_long(cret, 2);
362 rret[0]->RRinfoupdated = extract_int(cret, 3);
363 rret[0]->RRflags = extract_int(cret, 4);
364 rret[0]->RRhighest = extract_long(cret, 5);
365 rret[0]->RRlastread = extract_long(cret, 6);
366 rret[0]->RRismailbox = extract_int(cret, 7);
367 rret[0]->RRaide = extract_int(cret, 8);
368 rret[0]->RRnewmail = extract_long(cret, 9);
369 rret[0]->RRfloor = extract_int(cret, 10);
378 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
379 /* whicharg is number of messages, applies to last, first, gt, lt */
380 int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
381 long **mret, char *cret)
384 register long count = 0;
385 static char *proto[] =
386 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
391 if (!cret) return -2;
392 if (!mret) return -2;
393 if (*mret) return -2;
394 if (which < 0 || which > 6) return -2;
397 sprintf(aaa, "MSGS %s||%d", proto[which],
400 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
402 if (template) count = strlen(template);
403 ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
405 while (strlen(bbb)) {
408 extract_token(aaa, bbb, 0, '\n');
410 memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
411 *mret = (long *)realloc(mret,
412 (size_t)((count + 1) * sizeof (long)));
414 *mret[count++] = atol(aaa);
422 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
423 struct ctdlipcmessage **mret, char *cret)
429 int multipart_hunting = 0;
430 char multipart_prefix[SIZ];
432 if (!cret) return -1;
433 if (!mret) return -1;
434 if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
435 if (!*mret) return -1;
436 if (!msgnum) return -1;
438 strcpy(mret[0]->content_type, "");
439 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
440 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
441 if (ret / 100 == 1) {
443 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
444 while (strlen(bbb) > 4 && bbb[4] == '=') {
445 extract_token(aaa, bbb, 0, '\n');
446 remove_token(bbb, 0, '\n');
448 if (!strncasecmp(aaa, "nhdr=yes", 8))
450 else if (!strncasecmp(aaa, "from=", 5))
451 strcpy(mret[0]->author, &aaa[5]);
452 else if (!strncasecmp(aaa, "type=", 5))
453 mret[0]->type = atoi(&aaa[5]);
454 else if (!strncasecmp(aaa, "msgn=", 5))
455 strcpy(mret[0]->msgid, &aaa[5]);
456 else if (!strncasecmp(aaa, "subj=", 5))
457 strcpy(mret[0]->subject, &aaa[5]);
458 else if (!strncasecmp(aaa, "rfca=", 5))
459 strcpy(mret[0]->email, &aaa[5]);
460 else if (!strncasecmp(aaa, "hnod=", 5))
461 strcpy(mret[0]->hnod, &aaa[5]);
462 else if (!strncasecmp(aaa, "room=", 5))
463 strcpy(mret[0]->room, &aaa[5]);
464 else if (!strncasecmp(aaa, "node=", 5))
465 strcpy(mret[0]->node, &aaa[5]);
466 else if (!strncasecmp(aaa, "rcpt=", 5))
467 strcpy(mret[0]->recipient, &aaa[5]);
468 else if (!strncasecmp(aaa, "time=", 5))
469 mret[0]->time = atol(&aaa[5]);
471 /* Multipart/alternative prefix & suffix strings help
472 * us to determine which part we want to download.
474 else if (!strncasecmp(aaa, "pref=", 5)) {
475 extract(multipart_prefix, &aaa[5], 1);
476 if (!strcasecmp(multipart_prefix,
477 "multipart/alternative")) {
481 else if (!strncasecmp(aaa, "suff=", 5)) {
482 extract(multipart_prefix, &aaa[5], 1);
483 if (!strcasecmp(multipart_prefix,
484 "multipart/alternative")) {
489 else if (!strncasecmp(aaa, "part=", 5)) {
490 struct parts *ptr, *chain;
492 ptr = (struct parts *)calloc(1, sizeof (struct parts));
495 /* Fill the buffers for the caller */
496 extract(ptr->name, &aaa[5], 0);
497 extract(ptr->filename, &aaa[5], 1);
498 extract(ptr->number, &aaa[5], 2);
499 extract(ptr->disposition, &aaa[5], 3);
500 extract(ptr->mimetype, &aaa[5], 4);
501 ptr->length = extract_long(&aaa[5], 5);
502 if (!mret[0]->attachments)
503 mret[0]->attachments = ptr;
505 chain = mret[0]->attachments;
511 /* Now handle multipart/alternative */
512 if (multipart_hunting > 0) {
513 if ( (!strcasecmp(ptr->mimetype,
515 || (!strcasecmp(ptr->mimetype,
517 strcpy(mret[0]->mime_chosen,
525 /* Eliminate "text\n" */
526 remove_token(bbb, 0, '\n');
528 /* If doing a MIME thing, pull out the extra headers */
531 if (!strncasecmp(bbb, "Content-type: ", 14)) {
532 extract_token(mret[0]->content_type, bbb, 0, '\n');
533 strcpy(mret[0]->content_type,
534 &mret[0]->content_type[14]);
535 striplt(mret[0]->content_type);
537 remove_token(bbb, 0, '\n');
538 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
544 /* Strip trailing whitespace */
545 bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
547 bbb = (char *)realloc(bbb, 1);
557 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
562 if (!cret) return -2;
563 if (!listing) return -2;
564 if (*listing) return -2;
566 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
572 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
576 char *listing = NULL;
579 if (!cret) return -2;
580 if (!ServInfo) return -2;
582 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
583 if (ret / 100 == 1) {
586 while (*listing && strlen(listing)) {
587 extract_token(buf, listing, 0, '\n');
588 remove_token(listing, 0, '\n');
590 case 0: ServInfo->serv_pid = atoi(buf);
592 case 1: strcpy(ServInfo->serv_nodename,buf);
594 case 2: strcpy(ServInfo->serv_humannode,buf);
596 case 3: strcpy(ServInfo->serv_fqdn,buf);
598 case 4: strcpy(ServInfo->serv_software,buf);
600 case 5: ServInfo->serv_rev_level = atoi(buf);
602 case 6: strcpy(ServInfo->serv_bbs_city,buf);
604 case 7: strcpy(ServInfo->serv_sysadm,buf);
606 case 9: strcpy(ServInfo->serv_moreprompt,buf);
608 case 10: ServInfo->serv_ok_floors = atoi(buf);
610 case 11: ServInfo->serv_paging_level = atoi(buf);
612 case 13: ServInfo->serv_supports_qnop = atoi(buf);
623 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
628 if (!cret) return -2;
629 if (!listing) return -2;
630 if (*listing) return -2;
632 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
638 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
640 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
645 if (!cret) return -2;
648 sprintf(aaa, "SLRP %ld", msgnum);
650 sprintf(aaa, "SLRP HIGHEST");
651 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
657 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
662 if (!cret) return -2;
663 if (!username) return -2;
665 aaa = (char *)malloc(strlen(username) + 6);
668 sprintf(aaa, "INVT %s", username);
669 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
676 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
681 if (!cret) return -1;
682 if (!username) return -1;
684 aaa = (char *)malloc(strlen(username) + 6);
686 sprintf(aaa, "KICK %s", username);
687 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
694 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
698 if (!cret) return -2;
699 if (!qret) return -2;
700 if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
701 if (!*qret) return -1;
703 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
704 if (ret / 100 == 2) {
705 extract(qret[0]->QRname, cret, 0);
706 extract(qret[0]->QRpasswd, cret, 1);
707 extract(qret[0]->QRdirname, cret, 2);
708 qret[0]->QRflags = extract_int(cret, 3);
709 qret[0]->QRfloor = extract_int(cret, 4);
710 qret[0]->QRorder = extract_int(cret, 5);
717 /* set forget to kick all users out of room */
718 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
723 if (!cret) return -2;
724 if (!qret) return -2;
726 aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
727 strlen(qret->QRdirname) + 52);
730 sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
731 qret->QRname, qret->QRpasswd, qret->QRdirname,
732 qret->QRflags, forget, qret->QRfloor, qret->QRorder);
733 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
740 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
742 if (!cret) return -1;
744 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
749 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
754 if (!cret) return -2;
755 if (!username) return -2;
757 aaa = (char *)malloc(strlen(username) + 6);
760 sprintf(aaa, "SETA %s", username);
761 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
768 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
773 if (!cret) return -2;
776 aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
779 sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
780 mr->type, mr->author);
781 ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
789 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
793 if (!cret) return -2;
794 if (!iret) return -2;
795 if (*iret) return -2;
797 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
802 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
806 if (!cret) return -2;
807 if (!msgnum) return -2;
809 sprintf(aaa, "DELE %ld", msgnum);
810 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
815 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
820 if (!cret) return -2;
821 if (!destroom) return -2;
822 if (!msgnum) return -2;
824 aaa = (char *)malloc(strlen(destroom) + 28);
827 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
828 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
835 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
839 if (!cret) return -2;
841 sprintf(aaa, "KILL %d", for_real);
842 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
847 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
848 const char *password, int floor, char *cret)
853 if (!cret) return -2;
854 if (!roomname) return -2;
857 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
859 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
862 aaa = (char *)malloc(strlen(roomname) + 40);
864 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
867 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
874 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
876 if (!cret) return -2;
878 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
883 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
889 if (!cret) return -2;
890 if (!mret) return -2;
891 if (*mret) return -2;
892 if (!message) return -2;
894 aaa = (char *)malloc(strlen(message) + 6);
897 sprintf(aaa, "MESG %s", message);
898 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
905 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
907 if (!cret) return -2;
909 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
914 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
920 if (!cret) return -2;
921 if (!rret) return -2;
922 if (*rret) return -2;
925 aaa = (char *)malloc(strlen(username) + 6);
927 aaa = (char *)malloc(12);
931 sprintf(aaa, "GREG %s", username);
933 sprintf(aaa, "GREG _SELF_");
934 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
941 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
946 if (!cret) return -2;
947 if (!username) return -2;
948 if (axlevel < 0 || axlevel > 7) return -2;
950 aaa = (char *)malloc(strlen(username) + 17);
953 sprintf(aaa, "VALI %s|%d", username, axlevel);
954 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
961 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
965 if (!cret) return -1;
966 if (!info) return -1;
968 sprintf(aaa, "EINF %d", for_real);
969 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
974 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
978 if (!cret) return -1;
979 if (!listing) return -1;
980 if (*listing) return -1;
982 return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
987 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
989 if (!cret) return -1;
990 if (!info) return -1;
992 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
998 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1002 if (!cret) return -1;
1003 if (!chek) return -1;
1005 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1006 if (ret / 100 == 2) {
1007 chek->newmail = extract_long(cret, 0);
1008 chek->needregis = extract_int(cret, 1);
1009 chek->needvalid = extract_int(cret, 2);
1016 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1021 if (!cret) return -2;
1022 if (!filename) return -2;
1024 aaa = (char *)malloc(strlen(filename) + 6);
1025 if (!aaa) return -1;
1027 sprintf(aaa, "DELF %s", filename);
1028 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1035 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1040 if (!cret) return -2;
1041 if (!filename) return -2;
1042 if (!destroom) return -2;
1044 aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1045 if (!aaa) return -1;
1047 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1048 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1055 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1060 if (!cret) return -2;
1061 if (!filename) return -2;
1062 if (!destnode) return -2;
1064 aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1065 if (!aaa) return -1;
1067 sprintf(aaa, "NETF %s|%s", filename, destnode);
1068 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1075 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1080 if (!cret) return -1;
1081 if (!listing) return -1;
1082 if (*listing) return -1;
1084 *stamp = CtdlIPCServerTime(ipc, cret);
1086 *stamp = time(NULL);
1087 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1093 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1094 void (*progress_gauge_callback)(long, long), char *cret)
1102 if (!cret) return -2;
1103 if (!filename) return -2;
1104 if (!buf) return -2;
1105 if (*buf) return -2;
1106 if (ipc->downloading) return -2;
1108 aaa = (char *)malloc(strlen(filename) + 6);
1109 if (!aaa) return -1;
1111 sprintf(aaa, "OPEN %s", filename);
1112 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1114 if (ret / 100 == 2) {
1115 ipc->downloading = 1;
1116 bytes = extract_long(cret, 0);
1117 last_mod = extract_int(cret, 1);
1118 extract(mimetype, cret, 2);
1119 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1120 /* ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret); */
1121 ret = CtdlIPCEndDownload(ipc, cret);
1123 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1124 filename, mimetype);
1131 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
1132 void (*progress_gauge_callback)(long, long), char *cret)
1141 if (!cret) return -2;
1142 if (!buf) return -2;
1143 if (*buf) return -2;
1144 if (!part) return -2;
1145 if (!msgnum) return -2;
1146 if (ipc->downloading) return -2;
1148 aaa = (char *)malloc(strlen(part) + 17);
1149 if (!aaa) return -1;
1151 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1152 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1154 if (ret / 100 == 2) {
1155 ipc->downloading = 1;
1156 bytes = extract_long(cret, 0);
1157 last_mod = extract_int(cret, 1);
1158 extract(mimetype, cret, 2);
1159 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1160 ret = CtdlIPCEndDownload(ipc, cret);
1162 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1163 filename, mimetype);
1170 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1171 void (*progress_gauge_callback)(long, long), char *cret)
1179 if (!cret) return -1;
1180 if (!buf) return -1;
1181 if (*buf) return -1;
1182 if (!filename) return -1;
1183 if (ipc->downloading) return -1;
1185 aaa = (char *)malloc(strlen(filename) + 6);
1186 if (!aaa) return -1;
1188 sprintf(aaa, "OIMG %s", filename);
1189 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1191 if (ret / 100 == 2) {
1192 ipc->downloading = 1;
1193 bytes = extract_long(cret, 0);
1194 last_mod = extract_int(cret, 1);
1195 extract(mimetype, cret, 2);
1196 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1197 ret = CtdlIPCEndDownload(ipc, cret);
1199 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1200 filename, mimetype);
1207 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1208 const char *path, void (*progress_gauge_callback)(long, long),
1214 if (!cret) return -1;
1215 if (!save_as) return -1;
1216 if (!comment) return -1;
1217 if (!path) return -1;
1218 if (!*path) return -1;
1219 if (ipc->uploading) return -1;
1221 aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1222 if (!aaa) return -1;
1224 sprintf(aaa, "UOPN %s|%s", save_as, comment);
1225 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1227 if (ret / 100 == 2) {
1229 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1230 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1238 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1239 const char *save_as,
1240 void (*progress_gauge_callback)(long, long), char *cret)
1245 if (!cret) return -1;
1246 if (!save_as) return -1;
1247 if (!path && for_real) return -1;
1248 if (!*path && for_real) return -1;
1249 if (ipc->uploading) return -1;
1251 aaa = (char *)malloc(strlen(save_as) + 17);
1252 if (!aaa) return -1;
1254 sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1255 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1257 if (ret / 100 == 2 && for_real) {
1259 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1260 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1268 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1273 if (!cret) return -2;
1274 if (!username) return -2;
1276 aaa = (char *)malloc(strlen(username) + 6);
1277 if (!aaa) return -1;
1279 sprintf(aaa, "QUSR %s", username);
1280 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1287 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1291 if (!cret) return -2;
1292 if (!listing) return -2;
1293 if (*listing) return -2;
1295 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1300 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1305 if (!cret) return -2;
1306 if (!name) return -2;
1308 aaa = (char *)malloc(strlen(name) + 17);
1309 if (!aaa) return -1;
1311 sprintf(aaa, "CFLR %s|%d", name, for_real);
1312 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1319 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1323 if (!cret) return -1;
1324 if (floornum < 0) return -1;
1326 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1327 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1332 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1337 if (!cret) return -2;
1338 if (!floorname) return -2;
1339 if (floornum < 0) return -2;
1341 aaa = (char *)malloc(strlen(floorname) + 17);
1342 if (!aaa) return -1;
1344 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1345 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1352 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1353 const char *software_name, const char *hostname, char *cret)
1358 if (developerid < 0) return -2;
1359 if (clientid < 0) return -2;
1360 if (revision < 0) return -2;
1361 if (!software_name) return -2;
1362 if (!hostname) return -2;
1364 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1365 if (!aaa) return -1;
1367 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1368 revision, software_name, hostname);
1369 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1376 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1382 if (!cret) return -2;
1383 if (!username) return -2;
1385 aaa = (char *)malloc(strlen(username) + 8);
1386 if (!aaa) return -1;
1389 sprintf(aaa, "SEXP %s|-", username);
1390 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1393 sprintf(aaa, "SEXP %s||", username);
1394 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1402 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1406 if (!cret) return -2;
1407 if (!listing) return -2;
1408 if (*listing) return -2;
1410 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1415 /* mode is 0 = enable, 1 = disable, 2 = status */
1416 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1420 if (!cret) return -2;
1422 sprintf(aaa, "DEXP %d", mode);
1423 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1428 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1430 if (!cret) return -2;
1431 if (!bio) return -2;
1433 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1439 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1445 if (!cret) return -2;
1446 if (!username) return -2;
1447 if (!listing) return -2;
1448 if (*listing) return -2;
1450 aaa = (char *)malloc(strlen(username) + 6);
1451 if (!aaa) return -1;
1453 sprintf(aaa, "RBIO %s", username);
1454 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1461 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1465 if (!cret) return -2;
1466 if (!listing) return -2;
1467 if (*listing) return -2;
1469 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1474 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1478 if (!cret) return -1;
1480 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1481 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1486 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1490 if (!cret) return -1;
1492 sprintf(aaa, "TERM %d", sid);
1493 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1498 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1500 if (!cret) return -1;
1502 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1507 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1511 if (!cret) return -1;
1513 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1514 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1519 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1525 if (!cret) return -2;
1526 if (!text) return -2;
1527 if (!filename) return -2;
1529 aaa = (char *)malloc(strlen(filename) + 6);
1530 if (!aaa) return -1;
1532 sprintf(aaa, "EMSG %s", filename);
1533 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1540 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1545 if (!cret) return -2;
1546 if (!hostname) return -2;
1548 aaa = (char *)malloc(strlen(hostname) + 6);
1549 if (!aaa) return -1;
1551 sprintf(aaa, "HCHG %s", hostname);
1552 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1559 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1564 if (!cret) return -2;
1565 if (!roomname) return -2;
1567 aaa = (char *)malloc(strlen(roomname) + 6);
1568 if (!aaa) return -1;
1570 sprintf(aaa, "RCHG %s", roomname);
1571 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1578 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1583 if (!cret) return -2;
1584 if (!username) return -2;
1586 aaa = (char *)malloc(strlen(username) + 6);
1587 if (!aaa) return -1;
1589 sprintf(aaa, "UCHG %s", username);
1590 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1597 /* This function returns the actual server time reported, or 0 if error */
1598 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1600 register time_t tret;
1603 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1604 if (ret / 100 == 2) {
1605 tret = extract_long(cret, 0);
1614 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1615 struct usersupp **uret, char *cret)
1620 if (!cret) return -2;
1621 if (!uret) return -2;
1622 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1623 if (!*uret) return -1;
1625 sprintf(aaa, "AGUP %s", who);
1626 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1628 if (ret / 100 == 2) {
1629 extract(uret[0]->fullname, cret, 0);
1630 extract(uret[0]->password, cret, 1);
1631 uret[0]->flags = extract_int(cret, 2);
1632 uret[0]->timescalled = extract_long(cret, 3);
1633 uret[0]->posted = extract_long(cret, 4);
1634 uret[0]->axlevel = extract_int(cret, 5);
1635 uret[0]->usernum = extract_long(cret, 6);
1636 uret[0]->lastcall = extract_long(cret, 7);
1637 uret[0]->USuserpurge = extract_int(cret, 8);
1644 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1649 if (!cret) return -2;
1650 if (!uret) return -2;
1652 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1653 if (!aaa) return -1;
1655 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1656 uret->fullname, uret->password, uret->flags,
1657 uret->timescalled, uret->posted, uret->axlevel,
1658 uret->usernum, uret->lastcall, uret->USuserpurge);
1659 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1666 /* which is 0 = room, 1 = floor, 2 = site */
1667 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
1669 static char *proto[] = {"room", "floor", "site"};
1672 if (!cret) return -2;
1673 if (which < 0 || which > 2) return -2;
1675 sprintf(aaa, "GPEX %s", proto[which]);
1676 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1681 /* which is 0 = room, 1 = floor, 2 = site */
1682 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1683 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
1688 if (!cret) return -2;
1689 if (which < 0 || which > 2) return -2;
1690 if (policy < 0 || policy > 3) return -2;
1691 if (policy >= 2 && value < 1) return -2;
1693 sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
1694 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1699 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1703 if (!cret) return -2;
1704 if (!listing) return -2;
1705 if (*listing) return -2;
1707 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1708 listing, &bytes, cret);
1713 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1715 if (!cret) return -2;
1716 if (!listing) return -2;
1718 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1724 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1725 char **listing, char *cret)
1730 if (!cret) return -2;
1731 if (!mimetype) return -2;
1732 if (!listing) return -2;
1733 if (*listing) return -2;
1735 aaa = malloc(strlen(mimetype) + 13);
1736 if (!aaa) return -1;
1737 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1738 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1739 listing, &bytes, cret);
1744 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1745 const char *listing, char *cret)
1749 if (!cret) return -2;
1750 if (!mimetype) return -2;
1751 if (!listing) return -2;
1753 aaa = malloc(strlen(mimetype) + 13);
1754 if (!aaa) return -1;
1755 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1756 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1761 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1765 if (!cret) return -2;
1766 if (!msgnum) return -2;
1768 sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1769 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1774 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1778 if (!cret) return -2;
1779 if (session < 0) return -2;
1781 sprintf(aaa, "REQT %d", session);
1782 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1787 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1791 if (!cret) return -2;
1792 if (msgnum < 0) return -2;
1794 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1795 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1800 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1809 /* New SSL object */
1810 temp_ssl = SSL_new(ssl_ctx);
1812 error_printf("SSL_new failed: %s\n",
1813 ERR_reason_error_string(ERR_get_error()));
1816 /* Pointless flag waving */
1817 #if SSLEAY_VERSION_NUMBER >= 0x0922
1818 SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
1821 if (!access("/var/run/egd-pool", F_OK))
1822 RAND_egd("/var/run/egd-pool");
1824 if (!RAND_status()) {
1825 error_printf("PRNG not properly seeded\n");
1829 /* Associate network connection with SSL object */
1830 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1831 error_printf("SSL_set_fd failed: %s\n",
1832 ERR_reason_error_string(ERR_get_error()));
1836 if (status_hook != NULL)
1837 status_hook("Requesting encryption...\r");
1839 /* Ready to start SSL/TLS */
1841 CtdlIPC_putline(ipc, "STLS");
1842 CtdlIPC_getline(ipc, buf);
1843 if (buf[0] != '2') {
1844 error_printf("Server can't start TLS: %s\n", buf);
1848 r = CtdlIPCGenericCommand(ipc,
1849 "STLS", NULL, 0, NULL, NULL, cret);
1851 error_printf("Server can't start TLS: %s\n", buf);
1856 /* Do SSL/TLS handshake */
1857 if ((a = SSL_connect(temp_ssl)) < 1) {
1858 error_printf("SSL_connect failed: %s\n",
1859 ERR_reason_error_string(ERR_get_error()));
1863 ipc->ssl = temp_ssl;
1865 BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1869 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1870 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1871 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1872 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1878 #endif /* HAVE_OPENSSL */
1883 static void endtls(SSL *ssl)
1894 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1898 if (!address) return -2;
1899 if (!cret) return -2;
1901 aaa = (char *)malloc(strlen(address) + 6);
1902 if (!aaa) return -1;
1904 sprintf(aaa, "QDIR %s", address);
1905 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1910 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1914 if (!cret) return -2;
1915 sprintf(aaa, "IPGM %d", secret);
1916 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1937 /* ************************************************************************** */
1938 /* Stuff below this line is not for public consumption */
1939 /* ************************************************************************** */
1942 inline void CtdlIPC_lock(CtdlIPC *ipc)
1944 #ifdef THREADED_CLIENT
1945 pthread_mutex_lock(&(ipc->mutex));
1950 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1952 #ifdef THREADED_CLIENT
1953 pthread_mutex_unlock(&(ipc->mutex));
1958 /* Read a listing from the server up to 000. Append to dest if it exists */
1959 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
1968 length = strlen(ret);
1975 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
1976 linelength = strlen(aaa);
1977 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
1979 strcpy(&ret[length], aaa);
1980 length += linelength;
1981 strcpy(&ret[length++], "\n");
1989 /* Send a listing to the server; generate the ending 000. */
1990 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
1994 text = (char *)malloc(strlen(listing) + 6);
1996 strcpy(text, listing);
1997 while (text[strlen(text) - 1] == '\n')
1998 text[strlen(text) - 1] = '\0';
1999 strcat(text, "\n000");
2000 CtdlIPC_putline(ipc, text);
2004 /* Malloc failed but we are committed to send */
2005 /* This may result in extra blanks at the bottom */
2006 CtdlIPC_putline(ipc, text);
2007 CtdlIPC_putline(ipc, "000");
2013 /* Partial read of file from server */
2014 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2016 register size_t len = 0;
2019 if (!buf) return -1;
2020 if (!cret) return -1;
2021 if (bytes < 1) return -1;
2022 if (offset < 0) return -1;
2025 sprintf(aaa, "READ %d|%d", offset, bytes);
2026 CtdlIPC_putline(ipc, aaa);
2027 CtdlIPC_getline(ipc, aaa);
2029 strcpy(cret, &aaa[4]);
2031 len = extract_long(&aaa[4], 0);
2032 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2034 /* I know what I'm doing */
2035 serv_read(ipc, (*buf + offset), len);
2037 /* We have to read regardless */
2038 serv_read(ipc, aaa, len);
2042 CtdlIPC_unlock(ipc);
2048 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2052 if (!cret) return -2;
2053 if (!ipc->downloading) return -2;
2055 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2057 ipc->downloading = 0;
2063 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2067 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2068 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2075 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2076 void (*progress_gauge_callback)(long, long), char *cret)
2078 register size_t len;
2080 if (!cret) return -1;
2081 if (!buf) return -1;
2082 if (*buf) return -1;
2083 if (!ipc->downloading) return -1;
2086 if (progress_gauge_callback)
2087 progress_gauge_callback(len, bytes);
2088 while (len < bytes) {
2089 register size_t block;
2091 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2097 if (progress_gauge_callback)
2098 progress_gauge_callback(len, bytes);
2104 /* READ - pipelined */
2105 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2106 void (*progress_gauge_callback)(long, long), char *cret)
2108 register size_t len;
2109 register int calls; /* How many calls in the pipeline */
2110 register int i; /* iterator */
2113 if (!cret) return -1;
2114 if (!buf) return -1;
2115 if (*buf) return -1;
2116 if (!ipc->downloading) return -1;
2118 *buf = (void *)realloc(*buf, bytes);
2119 if (!*buf) return -1;
2123 if (progress_gauge_callback)
2124 progress_gauge_callback(len, bytes);
2126 /* How many calls will be in the pipeline? */
2127 calls = bytes / 4096;
2128 if (bytes % 4096) calls++;
2130 /* Send all requests at once */
2131 for (i = 0; i < calls; i++) {
2132 sprintf(aaa, "READ %d|4096", i * 4096);
2133 CtdlIPC_putline(ipc, aaa);
2136 /* Receive all responses at once */
2137 for (i = 0; i < calls; i++) {
2138 CtdlIPC_getline(ipc, aaa);
2140 strcpy(cret, &aaa[4]);
2142 len = extract_long(&aaa[4], 0);
2143 /* I know what I'm doing */
2144 serv_read(ipc, ((*buf) + (i * 4096)), len);
2146 if (progress_gauge_callback)
2147 progress_gauge_callback(i * 4096 + len, bytes);
2149 CtdlIPC_unlock(ipc);
2155 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2160 if (!cret) return -1;
2161 if (!ipc->uploading) return -1;
2163 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2164 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2171 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2172 void (*progress_gauge_callback)(long, long), char *cret)
2174 register int ret = -1;
2175 register size_t offset = 0;
2181 if (!cret) return -1;
2182 if (!path) return -1;
2183 if (!*path) return -1;
2185 fd = fopen(path, "r");
2188 fseek(fd, 0L, SEEK_END);
2192 if (progress_gauge_callback)
2193 progress_gauge_callback(0, bytes);
2195 while (offset < bytes) {
2196 register size_t to_write;
2198 /* Read some data in */
2199 to_write = fread(buf, 1, 4096, fd);
2201 if (feof(fd) || ferror(fd)) break;
2203 sprintf(aaa, "WRIT %d", to_write);
2204 CtdlIPC_putline(ipc, aaa);
2205 CtdlIPC_getline(ipc, aaa);
2206 strcpy(cret, &aaa[4]);
2208 if (aaa[0] == '7') {
2209 to_write = extract_long(&aaa[4], 0);
2211 serv_write(ipc, buf, to_write);
2213 if (progress_gauge_callback)
2214 progress_gauge_callback(offset, bytes);
2215 /* Detect short reads and back up if needed */
2216 fseek(fd, offset, SEEK_SET);
2221 if (progress_gauge_callback)
2222 progress_gauge_callback(1, 1);
2223 return (!ferror(fd) ? ret : -2);
2228 * Generic command method. This method should handle any server command
2229 * except for CHAT. It takes the following arguments:
2231 * ipc The server to speak with
2232 * command Preformatted command to send to server
2233 * to_send A text or binary file to send to server
2234 * (only sent if server requests it)
2235 * bytes_to_send The number of bytes in to_send (required if
2236 * sending binary, optional if sending listing)
2237 * to_receive Pointer to a NULL pointer, if the server
2238 * sends text or binary we will allocate memory
2239 * for the file and stuff it here
2240 * bytes_to_receive If a file is received, we will store its
2242 * proto_response The protocol response. Caller must provide
2243 * this buffer and ensure that it is at least
2244 * 128 bytes in length.
2246 * This function returns a number equal to the protocol response number,
2247 * -1 if an internal error occurred, -2 if caller provided bad values,
2248 * or 0 - the protocol response number if bad values were found during
2249 * the protocol exchange.
2250 * It stores the protocol response string (minus the number) in
2251 * protocol_response as described above. Some commands send additional
2252 * data in this string.
2254 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2255 const char *command, const char *to_send,
2256 size_t bytes_to_send, char **to_receive,
2257 size_t *bytes_to_receive, char *proto_response)
2263 if (!command) return -2;
2264 if (!proto_response) return -2;
2267 if (ipc->ssl) watch_ssl = 1;
2271 CtdlIPC_putline(ipc, command);
2273 CtdlIPC_getline(ipc, proto_response);
2274 if (proto_response[3] == '*')
2276 ret = atoi(proto_response);
2277 strcpy(proto_response, &proto_response[4]);
2278 switch (ret / 100) {
2279 default: /* Unknown, punt */
2281 case 3: /* MORE_DATA */
2283 /* Don't need to do anything */
2285 case 1: /* LISTING_FOLLOWS */
2286 if (to_receive && !*to_receive && bytes_to_receive) {
2287 *to_receive = CtdlIPCReadListing(ipc, NULL);
2288 } else { /* Drain */
2289 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2293 case 4: /* SEND_LISTING */
2295 CtdlIPCSendListing(ipc, to_send);
2297 /* No listing given, fake it */
2298 CtdlIPC_putline(ipc, "000");
2302 case 6: /* BINARY_FOLLOWS */
2303 if (to_receive && !*to_receive && bytes_to_receive) {
2305 extract_long(proto_response, 0);
2306 *to_receive = (char *)
2307 malloc((size_t)*bytes_to_receive);
2311 serv_read(ipc, *to_receive,
2318 drain = extract_long(proto_response, 0);
2319 while (drain > SIZ) {
2320 serv_read(ipc, buf, SIZ);
2323 serv_read(ipc, buf, drain);
2327 case 7: /* SEND_BINARY */
2328 if (to_send && bytes_to_send) {
2329 serv_write(ipc, to_send, bytes_to_send);
2330 } else if (bytes_to_send) {
2331 /* Fake it, send nulls */
2334 fake = bytes_to_send;
2335 memset(buf, '\0', SIZ);
2336 while (fake > SIZ) {
2337 serv_write(ipc, buf, SIZ);
2340 serv_write(ipc, buf, fake);
2342 } /* else who knows? DANGER WILL ROBINSON */
2344 case 8: /* START_CHAT_MODE */
2345 if (!strncasecmp(command, "CHAT", 4)) {
2346 /* Don't call chatmode with generic! */
2347 CtdlIPC_putline(ipc, "/quit");
2350 /* In this mode we send then receive listing */
2352 CtdlIPCSendListing(ipc, to_send);
2354 /* No listing given, fake it */
2355 CtdlIPC_putline(ipc, "000");
2358 if (to_receive && !*to_receive
2359 && bytes_to_receive) {
2360 *to_receive = CtdlIPCReadListing(ipc, NULL);
2361 } else { /* Drain */
2362 while (CtdlIPC_getline(ipc, buf),
2363 strcmp(buf, "000")) ;
2368 case 9: /* ASYNC_MSG */
2369 /* CtdlIPCDoAsync(ret, proto_response); */
2370 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2376 CtdlIPC_unlock(ipc);
2381 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2383 struct hostent *phe;
2384 struct servent *pse;
2385 struct protoent *ppe;
2386 struct sockaddr_in sin;
2389 memset(&sin, 0, sizeof(sin));
2390 sin.sin_family = AF_INET;
2392 pse = getservbyname(service, protocol);
2394 sin.sin_port = pse->s_port;
2396 else if (atoi(service) > 0) {
2397 sin.sin_port = htons(atoi(service));
2400 sin.sin_port = htons(defaultPort);
2402 phe = gethostbyname(host);
2404 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2405 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2408 if ((ppe = getprotobyname(protocol)) == 0) {
2411 if (!strcmp(protocol, "udp")) {
2417 s = socket(PF_INET, type, ppe->p_proto);
2422 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2429 static int uds_connectsock(int *isLocal, char *sockpath)
2431 struct sockaddr_un addr;
2434 memset(&addr, 0, sizeof(addr));
2435 addr.sun_family = AF_UNIX;
2436 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2438 s = socket(AF_UNIX, SOCK_STREAM, 0);
2443 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2453 * input binary data from socket
2455 static void serv_read(CtdlIPC *ipc, char *buf, int bytes)
2459 #if defined(HAVE_OPENSSL)
2461 serv_read_ssl(ipc, buf, bytes);
2466 while (len < bytes) {
2467 rlen = read(ipc->sock, &buf[len], bytes - len);
2469 connection_died(ipc);
2478 * send binary to server
2480 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
2482 int bytes_written = 0;
2485 #if defined(HAVE_OPENSSL)
2487 serv_write_ssl(ipc, buf, nbytes);
2491 while (bytes_written < nbytes) {
2492 retval = write(ipc->sock, &buf[bytes_written],
2493 nbytes - bytes_written);
2495 connection_died(ipc);
2498 bytes_written += retval;
2505 * input binary data from encrypted connection
2507 static void serv_read_ssl(CtdlIPC* ipc, char *buf, int bytes)
2513 while (len < bytes) {
2514 if (SSL_want_read(ipc->ssl)) {
2515 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2516 error_printf("SSL_write in serv_read:\n");
2517 ERR_print_errors_fp(stderr);
2520 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2524 errval = SSL_get_error(ipc->ssl, rlen);
2525 if (errval == SSL_ERROR_WANT_READ ||
2526 errval == SSL_ERROR_WANT_WRITE) {
2530 if (errval == SSL_ERROR_ZERO_RETURN ||
2531 errval == SSL_ERROR_SSL) {
2532 serv_read(ipc, &buf[len], bytes - len);
2535 error_printf("SSL_read in serv_read:\n");
2536 ERR_print_errors_fp(stderr);
2546 * send binary to server encrypted
2548 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes)
2550 int bytes_written = 0;
2554 while (bytes_written < nbytes) {
2555 if (SSL_want_write(ipc->ssl)) {
2556 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2557 error_printf("SSL_read in serv_write:\n");
2558 ERR_print_errors_fp(stderr);
2561 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2562 nbytes - bytes_written);
2566 errval = SSL_get_error(ipc->ssl, retval);
2567 if (errval == SSL_ERROR_WANT_READ ||
2568 errval == SSL_ERROR_WANT_WRITE) {
2572 if (errval == SSL_ERROR_ZERO_RETURN ||
2573 errval == SSL_ERROR_SSL) {
2574 serv_write(ipc, &buf[bytes_written],
2575 nbytes - bytes_written);
2578 error_printf("SSL_write in serv_write:\n");
2579 ERR_print_errors_fp(stderr);
2583 bytes_written += retval;
2588 static void CtdlIPC_init_OpenSSL(void)
2591 SSL_METHOD *ssl_method;
2594 /* already done init */
2602 SSL_load_error_strings();
2603 SSLeay_add_ssl_algorithms();
2605 /* Set up the SSL context in which we will oeprate */
2606 ssl_method = SSLv23_client_method();
2607 ssl_ctx = SSL_CTX_new(ssl_method);
2609 error_printf("SSL_CTX_new failed: %s\n",
2610 ERR_reason_error_string(ERR_get_error()));
2613 /* Any reasonable cipher we can get */
2614 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2615 error_printf("No ciphers available for encryption\n");
2618 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2620 /* Load DH parameters into the context */
2623 error_printf("Can't allocate a DH object: %s\n",
2624 ERR_reason_error_string(ERR_get_error()));
2627 if (!(BN_hex2bn(&(dh->p), DH_P))) {
2628 error_printf("Can't assign DH_P: %s\n",
2629 ERR_reason_error_string(ERR_get_error()));
2633 if (!(BN_hex2bn(&(dh->g), DH_G))) {
2634 error_printf("Can't assign DH_G: %s\n",
2635 ERR_reason_error_string(ERR_get_error()));
2640 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2643 #ifdef THREADED_CLIENT
2644 /* OpenSSL requires callbacks for threaded clients */
2645 CRYPTO_set_locking_callback(ssl_lock);
2646 CRYPTO_set_id_callback(id_callback);
2648 /* OpenSSL requires us to do semaphores for threaded clients */
2649 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2651 perror("malloc failed");
2654 for (a = 0; a < CRYPTO_num_locks(); a++) {
2655 Critters[a] = malloc(sizeof (pthread_mutex_t));
2657 perror("malloc failed");
2660 pthread_mutex_init(Critters[a], NULL);
2663 #endif /* THREADED_CLIENT */
2667 static void ssl_lock(int mode, int n, const char *file, int line)
2669 #ifdef THREADED_CLIENT
2670 if (mode & CRYPTO_LOCK)
2671 pthread_mutex_lock(Critters[n]);
2673 pthread_mutex_unlock(Critters[n]);
2674 #endif /* THREADED_CLIENT */
2677 #ifdef THREADED_CLIENT
2678 static unsigned long id_callback(void) {
2679 return (unsigned long)pthread_self();
2681 #endif /* THREADED_CLIENT */
2682 #endif /* HAVE_OPENSSL */
2686 * input string from socket - implemented in terms of serv_read()
2688 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2692 /* Read one character at a time. */
2694 serv_read(ipc, &buf[i], 1);
2695 if (buf[i] == '\n' || i == (SIZ-1))
2699 /* If we got a long line, discard characters until the newline. */
2701 while (buf[i] != '\n')
2702 serv_read(ipc, &buf[i], 1);
2704 /* Strip the trailing newline.
2711 * send line to server - implemented in terms of serv_write()
2713 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2715 /* error_printf("< %s\n", buf); */
2716 serv_write(ipc, buf, strlen(buf));
2717 serv_write(ipc, "\n", 1);
2719 ipc->last_command_sent = time(NULL);
2726 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2733 CtdlIPC *ipc = ialloc(CtdlIPC);
2737 #if defined(HAVE_OPENSSL)
2739 CtdlIPC_init_OpenSSL();
2741 #if defined(HAVE_PTHREAD_H)
2742 pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2744 ipc->sock = -1; /* Not connected */
2745 ipc->isLocal = 0; /* Not local, of course! */
2746 ipc->downloading = 0;
2748 ipc->last_command_sent = 0L;
2750 strcpy(cithost, DEFAULT_HOST); /* default host */
2751 strcpy(citport, DEFAULT_PORT); /* default port */
2753 for (a = 0; a < argc; ++a) {
2756 } else if (a == 1) {
2757 strcpy(cithost, argv[a]);
2758 } else if (a == 2) {
2759 strcpy(citport, argv[a]);
2761 error_printf("%s: usage: ",argv[0]);
2762 error_printf("%s [host] [port] ",argv[0]);
2769 if ((!strcmp(cithost, "localhost"))
2770 || (!strcmp(cithost, "127.0.0.1"))) {
2774 /* If we're using a unix domain socket we can do a bunch of stuff */
2775 if (!strcmp(cithost, UDS)) {
2776 snprintf(sockpath, sizeof sockpath, "citadel.socket");
2777 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2778 if (ipc->sock == -1) {
2782 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2783 if (portbuf != NULL) strcpy(portbuf, sockpath);
2787 ipc->sock = connectsock(cithost, citport, "tcp", 504);
2788 if (ipc->sock == -1) {
2792 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2793 if (portbuf != NULL) strcpy(portbuf, citport);
2798 * return the file descriptor of the server socket so we can select() on it.
2800 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2803 int CtdlIPC_getsockfd(CtdlIPC* ipc)
2810 * return one character
2812 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2815 char CtdlIPC_get(CtdlIPC* ipc)
2820 serv_read(ipc, buf, 1);