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>
30 #ifdef THREADED_CLIENT
34 #include "citadel_ipc.h"
35 #include "citadel_decls.h"
38 #ifdef THREADED_CLIENT
39 pthread_mutex_t rwlock;
43 static SSL_CTX *ssl_ctx;
46 #ifdef THREADED_CLIENT
47 pthread_mutex_t **Critters; /* Things that need locking */
48 #endif /* THREADED_CLIENT */
50 #endif /* HAVE_OPENSSL */
53 static void (*status_hook)(char *s) = NULL;
55 void setCryptoStatusHook(void (*hook)(char *s)) {
60 char express_msgs = 0;
63 static void serv_read(CtdlIPC *ipc, char *buf, int bytes);
64 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes);
66 static void serv_read_ssl(CtdlIPC *ipc, char *buf, int bytes);
67 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes);
68 static void ssl_lock(int mode, int n, const char *file, int line);
69 static void endtls(SSL *ssl);
70 #ifdef THREADED_CLIENT
71 static unsigned long id_callback(void);
72 #endif /* THREADED_CLIENT */
73 #endif /* HAVE_OPENSSL */
77 * Does nothing. The server should always return 200.
79 int CtdlIPCNoop(CtdlIPC *ipc)
83 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
88 * Does nothing interesting. The server should always return 200
89 * along with your string.
91 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
99 aaa = (char *)malloc((size_t)(strlen(arg) + 6));
102 sprintf(aaa, "ECHO %s", arg);
103 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
110 * Asks the server to close the connecction.
111 * Should always return 200.
113 int CtdlIPCQuit(CtdlIPC *ipc)
119 CtdlIPC_putline(ipc, "QUIT");
120 CtdlIPC_getline(ipc, aaa);
128 * Asks the server to logout. Should always return 200, even if no user
129 * was logged in. The user will not be logged in after this!
131 int CtdlIPCLogout(CtdlIPC *ipc)
137 CtdlIPC_putline(ipc, "LOUT");
138 CtdlIPC_getline(ipc, aaa);
146 * First stage of authentication - pass the username. Returns 300 if the
147 * username is able to log in, with the username correctly spelled in cret.
148 * Returns various 500 error codes if the user doesn't exist, etc.
150 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
155 if (!username) return -2;
156 if (!cret) return -2;
158 aaa = (char *)malloc((size_t)(strlen(username) + 6));
161 sprintf(aaa, "USER %s", username);
162 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
169 * Second stage of authentication - provide password. The server returns
170 * 200 and several arguments in cret relating to the user's account.
172 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
177 if (!passwd) return -2;
178 if (!cret) return -2;
180 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
183 sprintf(aaa, "PASS %s", passwd);
184 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
191 * Create a new user. This returns 200 plus the same arguments as TryPassword
192 * if selfservice is nonzero, unless there was a problem creating the account.
193 * If selfservice is zero, creates a new user but does not log out the existing
194 * user - intended for use by system administrators to create accounts on
195 * behalf of other users.
197 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
202 if (!username) return -2;
203 if (!cret) return -2;
205 aaa = (char *)malloc((size_t)(strlen(username) + 6));
208 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
209 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
216 * Changes the user's password. Returns 200 if changed, errors otherwise.
218 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
223 if (!passwd) return -2;
224 if (!cret) return -2;
226 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
229 sprintf(aaa, "SETP %s", passwd);
230 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
237 /* Caller must free the march list */
238 /* which is 0 = LRMS, 1 = LKRN, 2 = LKRO, 3 = LKRA, 4 = LZRM */
239 /* floor is -1 for all, or floornum */
240 int CtdlIPCKnownRooms(CtdlIPC *ipc, int which, int floor, struct march **listing, char *cret)
243 struct march *march = NULL;
244 static char *proto[] = {"LRMS", "LKRN", "LKRO", "LKRA", "LZRM" };
249 if (!listing) return -2;
250 if (*listing) return -2; /* Free the listing first */
251 if (!cret) return -2;
252 if (which < 0 || which > 4) return -2;
253 if (floor < -1) return -2; /* Can't validate upper bound, sorry */
255 sprintf(aaa, "%s %d", proto[which], floor);
256 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
257 if (ret / 100 == 1) {
260 while (bbb && strlen(bbb)) {
263 extract_token(aaa, bbb, 0, '\n');
265 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
266 mptr = (struct march *) malloc(sizeof (struct march));
269 extract(mptr->march_name, aaa, 0);
270 mptr->march_floor = (char) extract_int(aaa, 2);
271 mptr->march_order = (char) extract_int(aaa, 3);
278 while (mptr2->next != NULL)
291 /* Caller must free the struct usersupp; caller may pass an existing one */
292 int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
296 if (!cret) return -2;
297 if (!uret) return -2;
298 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
299 if (!*uret) return -1;
301 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
302 if (ret / 100 == 2) {
303 uret[0]->USscreenwidth = extract_int(cret, 0);
304 uret[0]->USscreenheight = extract_int(cret, 1);
305 uret[0]->flags = extract_int(cret, 2);
312 int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
316 if (!uret) return -2;
317 if (!cret) return -2;
319 sprintf(aaa, "SETU %d|%d|%d",
320 uret->USscreenwidth, uret->USscreenheight,
322 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
327 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
328 struct ctdlipcroom **rret, char *cret)
333 if (!cret) return -2;
334 if (!rret) return -2;
335 if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
336 if (!*rret) return -1;
339 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
344 sprintf(aaa, "GOTO %s|%s", room, passwd);
346 aaa = (char *)malloc(strlen(room) + 6);
351 sprintf(aaa, "GOTO %s", room);
353 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
354 if (ret / 100 == 2) {
355 extract(rret[0]->RRname, cret, 0);
356 rret[0]->RRunread = extract_long(cret, 1);
357 rret[0]->RRtotal = extract_long(cret, 2);
358 rret[0]->RRinfoupdated = extract_int(cret, 3);
359 rret[0]->RRflags = extract_int(cret, 4);
360 rret[0]->RRhighest = extract_long(cret, 5);
361 rret[0]->RRlastread = extract_long(cret, 6);
362 rret[0]->RRismailbox = extract_int(cret, 7);
363 rret[0]->RRaide = extract_int(cret, 8);
364 rret[0]->RRnewmail = extract_long(cret, 9);
365 rret[0]->RRfloor = extract_int(cret, 10);
374 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
375 /* whicharg is number of messages, applies to last, first, gt, lt */
376 int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
377 long **mret, char *cret)
380 register long count = 0;
381 static char *proto[] =
382 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
387 if (!cret) return -2;
388 if (!mret) return -2;
389 if (*mret) return -2;
390 if (which < 0 || which > 6) return -2;
393 sprintf(aaa, "MSGS %s||%d", proto[which],
396 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
398 if (template) count = strlen(template);
399 ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
401 while (strlen(bbb)) {
404 extract_token(aaa, bbb, 0, '\n');
406 memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
407 *mret = (long *)realloc(mret,
408 (size_t)((count + 1) * sizeof (long)));
410 *mret[count++] = atol(aaa);
418 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
419 struct ctdlipcmessage **mret, char *cret)
425 int multipart_hunting = 0;
426 char multipart_prefix[SIZ];
428 if (!cret) return -1;
429 if (!mret) return -1;
430 if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
431 if (!*mret) return -1;
432 if (!msgnum) return -1;
434 strcpy(mret[0]->content_type, "");
435 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
436 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
437 if (ret / 100 == 1) {
439 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
440 while (strlen(bbb) > 4 && bbb[4] == '=') {
441 extract_token(aaa, bbb, 0, '\n');
442 remove_token(bbb, 0, '\n');
444 if (!strncasecmp(aaa, "nhdr=yes", 8))
446 else if (!strncasecmp(aaa, "from=", 5))
447 strcpy(mret[0]->author, &aaa[5]);
448 else if (!strncasecmp(aaa, "type=", 5))
449 mret[0]->type = atoi(&aaa[5]);
450 else if (!strncasecmp(aaa, "msgn=", 5))
451 strcpy(mret[0]->msgid, &aaa[5]);
452 else if (!strncasecmp(aaa, "subj=", 5))
453 strcpy(mret[0]->subject, &aaa[5]);
454 else if (!strncasecmp(aaa, "rfca=", 5))
455 strcpy(mret[0]->email, &aaa[5]);
456 else if (!strncasecmp(aaa, "hnod=", 5))
457 strcpy(mret[0]->hnod, &aaa[5]);
458 else if (!strncasecmp(aaa, "room=", 5))
459 strcpy(mret[0]->room, &aaa[5]);
460 else if (!strncasecmp(aaa, "node=", 5))
461 strcpy(mret[0]->node, &aaa[5]);
462 else if (!strncasecmp(aaa, "rcpt=", 5))
463 strcpy(mret[0]->recipient, &aaa[5]);
464 else if (!strncasecmp(aaa, "time=", 5))
465 mret[0]->time = atol(&aaa[5]);
467 /* Multipart/alternative prefix & suffix strings help
468 * us to determine which part we want to download.
470 else if (!strncasecmp(aaa, "pref=", 5)) {
471 extract(multipart_prefix, &aaa[5], 1);
472 if (!strcasecmp(multipart_prefix,
473 "multipart/alternative")) {
477 else if (!strncasecmp(aaa, "suff=", 5)) {
478 extract(multipart_prefix, &aaa[5], 1);
479 if (!strcasecmp(multipart_prefix,
480 "multipart/alternative")) {
485 else if (!strncasecmp(aaa, "part=", 5)) {
486 struct parts *ptr, *chain;
488 ptr = (struct parts *)calloc(1, sizeof (struct parts));
491 /* Fill the buffers for the caller */
492 extract(ptr->name, &aaa[5], 0);
493 extract(ptr->filename, &aaa[5], 1);
494 extract(ptr->number, &aaa[5], 2);
495 extract(ptr->disposition, &aaa[5], 3);
496 extract(ptr->mimetype, &aaa[5], 4);
497 ptr->length = extract_long(&aaa[5], 5);
498 if (!mret[0]->attachments)
499 mret[0]->attachments = ptr;
501 chain = mret[0]->attachments;
507 /* Now handle multipart/alternative */
508 if (multipart_hunting > 0) {
509 if ( (!strcasecmp(ptr->mimetype,
511 || (!strcasecmp(ptr->mimetype,
513 strcpy(mret[0]->mime_chosen,
521 /* Eliminate "text\n" */
522 remove_token(bbb, 0, '\n');
524 /* If doing a MIME thing, pull out the extra headers */
527 if (!strncasecmp(bbb, "Content-type: ", 14)) {
528 extract_token(mret[0]->content_type, bbb, 0, '\n');
529 strcpy(mret[0]->content_type,
530 &mret[0]->content_type[14]);
531 striplt(mret[0]->content_type);
533 remove_token(bbb, 0, '\n');
534 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
540 /* Strip trailing whitespace */
541 bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
543 bbb = (char *)realloc(bbb, 1);
553 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
558 if (!cret) return -2;
559 if (!listing) return -2;
560 if (*listing) return -2;
562 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
568 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
572 char *listing = NULL;
575 if (!cret) return -2;
576 if (!ServInfo) return -2;
578 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
579 if (ret / 100 == 1) {
582 while (*listing && strlen(listing)) {
583 extract_token(buf, listing, 0, '\n');
584 remove_token(listing, 0, '\n');
586 case 0: ServInfo->serv_pid = atoi(buf);
588 case 1: strcpy(ServInfo->serv_nodename,buf);
590 case 2: strcpy(ServInfo->serv_humannode,buf);
592 case 3: strcpy(ServInfo->serv_fqdn,buf);
594 case 4: strcpy(ServInfo->serv_software,buf);
596 case 5: ServInfo->serv_rev_level = atoi(buf);
598 case 6: strcpy(ServInfo->serv_bbs_city,buf);
600 case 7: strcpy(ServInfo->serv_sysadm,buf);
602 case 9: strcpy(ServInfo->serv_moreprompt,buf);
604 case 10: ServInfo->serv_ok_floors = atoi(buf);
606 case 11: ServInfo->serv_paging_level = atoi(buf);
608 case 13: ServInfo->serv_supports_qnop = atoi(buf);
619 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
624 if (!cret) return -2;
625 if (!listing) return -2;
626 if (*listing) return -2;
628 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
634 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
636 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
641 if (!cret) return -2;
644 sprintf(aaa, "SLRP %ld", msgnum);
646 sprintf(aaa, "SLRP HIGHEST");
647 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
653 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
658 if (!cret) return -2;
659 if (!username) return -2;
661 aaa = (char *)malloc(strlen(username) + 6);
664 sprintf(aaa, "INVT %s", username);
665 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
672 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
677 if (!cret) return -1;
678 if (!username) return -1;
680 aaa = (char *)malloc(strlen(username) + 6);
682 sprintf(aaa, "KICK %s", username);
683 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
690 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
694 if (!cret) return -2;
695 if (!qret) return -2;
696 if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
697 if (!*qret) return -1;
699 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
700 if (ret / 100 == 2) {
701 extract(qret[0]->QRname, cret, 0);
702 extract(qret[0]->QRpasswd, cret, 1);
703 extract(qret[0]->QRdirname, cret, 2);
704 qret[0]->QRflags = extract_int(cret, 3);
705 qret[0]->QRfloor = extract_int(cret, 4);
706 qret[0]->QRorder = extract_int(cret, 5);
713 /* set forget to kick all users out of room */
714 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
719 if (!cret) return -2;
720 if (!qret) return -2;
722 aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
723 strlen(qret->QRdirname) + 52);
726 sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
727 qret->QRname, qret->QRpasswd, qret->QRdirname,
728 qret->QRflags, forget, qret->QRfloor, qret->QRorder);
729 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
736 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
738 if (!cret) return -1;
740 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
745 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
750 if (!cret) return -2;
751 if (!username) return -2;
753 aaa = (char *)malloc(strlen(username) + 6);
756 sprintf(aaa, "SETA %s", username);
757 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
764 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
769 if (!cret) return -2;
772 aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
775 sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
776 mr->type, mr->author);
777 ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
785 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
789 if (!cret) return -2;
790 if (!iret) return -2;
791 if (*iret) return -2;
793 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
798 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
802 if (!cret) return -2;
803 if (!msgnum) return -2;
805 sprintf(aaa, "DELE %ld", msgnum);
806 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
811 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
816 if (!cret) return -2;
817 if (!destroom) return -2;
818 if (!msgnum) return -2;
820 aaa = (char *)malloc(strlen(destroom) + 28);
823 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
824 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
831 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
835 if (!cret) return -2;
837 sprintf(aaa, "KILL %d", for_real);
838 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
843 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
844 const char *password, int floor, char *cret)
849 if (!cret) return -2;
850 if (!roomname) return -2;
853 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
855 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
858 aaa = (char *)malloc(strlen(roomname) + 40);
860 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
863 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
870 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
872 if (!cret) return -2;
874 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
879 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
885 if (!cret) return -2;
886 if (!mret) return -2;
887 if (*mret) return -2;
888 if (!message) return -2;
890 aaa = (char *)malloc(strlen(message) + 6);
893 sprintf(aaa, "MESG %s", message);
894 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
901 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
903 if (!cret) return -2;
905 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
910 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
916 if (!cret) return -2;
917 if (!rret) return -2;
918 if (*rret) return -2;
921 aaa = (char *)malloc(strlen(username) + 6);
923 aaa = (char *)malloc(12);
927 sprintf(aaa, "GREG %s", username);
929 sprintf(aaa, "GREG _SELF_");
930 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
937 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
942 if (!cret) return -2;
943 if (!username) return -2;
944 if (axlevel < 0 || axlevel > 7) return -2;
946 aaa = (char *)malloc(strlen(username) + 17);
949 sprintf(aaa, "VALI %s|%d", username, axlevel);
950 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
957 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
961 if (!cret) return -1;
962 if (!info) return -1;
964 sprintf(aaa, "EINF %d", for_real);
965 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
970 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
974 if (!cret) return -1;
975 if (!listing) return -1;
976 if (*listing) return -1;
978 return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
983 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
985 if (!cret) return -1;
986 if (!info) return -1;
988 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
994 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
998 if (!cret) return -1;
999 if (!chek) return -1;
1001 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1002 if (ret / 100 == 2) {
1003 chek->newmail = extract_long(cret, 0);
1004 chek->needregis = extract_int(cret, 1);
1005 chek->needvalid = extract_int(cret, 2);
1012 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1017 if (!cret) return -2;
1018 if (!filename) return -2;
1020 aaa = (char *)malloc(strlen(filename) + 6);
1021 if (!aaa) return -1;
1023 sprintf(aaa, "DELF %s", filename);
1024 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1031 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1036 if (!cret) return -2;
1037 if (!filename) return -2;
1038 if (!destroom) return -2;
1040 aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1041 if (!aaa) return -1;
1043 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1044 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1051 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1056 if (!cret) return -2;
1057 if (!filename) return -2;
1058 if (!destnode) return -2;
1060 aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1061 if (!aaa) return -1;
1063 sprintf(aaa, "NETF %s|%s", filename, destnode);
1064 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1071 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1076 if (!cret) return -1;
1077 if (!listing) return -1;
1078 if (*listing) return -1;
1080 *stamp = CtdlIPCServerTime(ipc, cret);
1082 *stamp = time(NULL);
1083 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1089 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1090 void (*progress_gauge_callback)(long, long), char *cret)
1098 if (!cret) return -2;
1099 if (!filename) return -2;
1100 if (!buf) return -2;
1101 if (*buf) return -2;
1102 if (ipc->downloading) return -2;
1104 aaa = (char *)malloc(strlen(filename) + 6);
1105 if (!aaa) return -1;
1107 sprintf(aaa, "OPEN %s", filename);
1108 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1110 if (ret / 100 == 2) {
1111 ipc->downloading = 1;
1112 bytes = extract_long(cret, 0);
1113 last_mod = extract_int(cret, 1);
1114 extract(mimetype, cret, 2);
1115 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1116 /* ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret); */
1117 ret = CtdlIPCEndDownload(ipc, cret);
1119 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1120 filename, mimetype);
1127 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
1128 void (*progress_gauge_callback)(long, long), char *cret)
1137 if (!cret) return -2;
1138 if (!buf) return -2;
1139 if (*buf) return -2;
1140 if (!part) return -2;
1141 if (!msgnum) return -2;
1142 if (ipc->downloading) return -2;
1144 aaa = (char *)malloc(strlen(part) + 17);
1145 if (!aaa) return -1;
1147 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1148 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1150 if (ret / 100 == 2) {
1151 ipc->downloading = 1;
1152 bytes = extract_long(cret, 0);
1153 last_mod = extract_int(cret, 1);
1154 extract(mimetype, cret, 2);
1155 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1156 ret = CtdlIPCEndDownload(ipc, cret);
1158 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1159 filename, mimetype);
1166 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1167 void (*progress_gauge_callback)(long, long), char *cret)
1175 if (!cret) return -1;
1176 if (!buf) return -1;
1177 if (*buf) return -1;
1178 if (!filename) return -1;
1179 if (ipc->downloading) return -1;
1181 aaa = (char *)malloc(strlen(filename) + 6);
1182 if (!aaa) return -1;
1184 sprintf(aaa, "OIMG %s", filename);
1185 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1187 if (ret / 100 == 2) {
1188 ipc->downloading = 1;
1189 bytes = extract_long(cret, 0);
1190 last_mod = extract_int(cret, 1);
1191 extract(mimetype, cret, 2);
1192 ret = CtdlIPCReadDownload(ipc, buf, bytes, progress_gauge_callback, cret);
1193 ret = CtdlIPCEndDownload(ipc, cret);
1195 sprintf(cret, "%d|%ld|%s|%s", bytes, last_mod,
1196 filename, mimetype);
1203 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1204 const char *path, void (*progress_gauge_callback)(long, long),
1210 if (!cret) return -1;
1211 if (!save_as) return -1;
1212 if (!comment) return -1;
1213 if (!path) return -1;
1214 if (!*path) return -1;
1215 if (ipc->uploading) return -1;
1217 aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1218 if (!aaa) return -1;
1220 sprintf(aaa, "UOPN %s|%s", save_as, comment);
1221 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1223 if (ret / 100 == 2) {
1225 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1226 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1234 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1235 const char *save_as,
1236 void (*progress_gauge_callback)(long, long), char *cret)
1241 if (!cret) return -1;
1242 if (!save_as) return -1;
1243 if (!path && for_real) return -1;
1244 if (!*path && for_real) return -1;
1245 if (ipc->uploading) return -1;
1247 aaa = (char *)malloc(strlen(save_as) + 17);
1248 if (!aaa) return -1;
1250 sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1251 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1253 if (ret / 100 == 2 && for_real) {
1255 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1256 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1264 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1269 if (!cret) return -2;
1270 if (!username) return -2;
1272 aaa = (char *)malloc(strlen(username) + 6);
1273 if (!aaa) return -1;
1275 sprintf(aaa, "QUSR %s", username);
1276 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1283 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1287 if (!cret) return -2;
1288 if (!listing) return -2;
1289 if (*listing) return -2;
1291 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1296 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1301 if (!cret) return -2;
1302 if (!name) return -2;
1304 aaa = (char *)malloc(strlen(name) + 17);
1305 if (!aaa) return -1;
1307 sprintf(aaa, "CFLR %s|%d", name, for_real);
1308 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1315 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1319 if (!cret) return -1;
1320 if (floornum < 0) return -1;
1322 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1323 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1328 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1333 if (!cret) return -2;
1334 if (!floorname) return -2;
1335 if (floornum < 0) return -2;
1337 aaa = (char *)malloc(strlen(floorname) + 17);
1338 if (!aaa) return -1;
1340 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1341 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1348 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1349 const char *software_name, const char *hostname, char *cret)
1354 if (developerid < 0) return -2;
1355 if (clientid < 0) return -2;
1356 if (revision < 0) return -2;
1357 if (!software_name) return -2;
1358 if (!hostname) return -2;
1360 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1361 if (!aaa) return -1;
1363 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1364 revision, software_name, hostname);
1365 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1372 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1378 if (!cret) return -2;
1379 if (!username) return -2;
1381 aaa = (char *)malloc(strlen(username) + 8);
1382 if (!aaa) return -1;
1385 sprintf(aaa, "SEXP %s|-", username);
1386 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1389 sprintf(aaa, "SEXP %s||", username);
1390 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1398 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1402 if (!cret) return -2;
1403 if (!listing) return -2;
1404 if (*listing) return -2;
1406 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1411 /* mode is 0 = enable, 1 = disable, 2 = status */
1412 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1416 if (!cret) return -2;
1418 sprintf(aaa, "DEXP %d", mode);
1419 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1424 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1426 if (!cret) return -2;
1427 if (!bio) return -2;
1429 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1435 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1441 if (!cret) return -2;
1442 if (!username) return -2;
1443 if (!listing) return -2;
1444 if (*listing) return -2;
1446 aaa = (char *)malloc(strlen(username) + 6);
1447 if (!aaa) return -1;
1449 sprintf(aaa, "RBIO %s", username);
1450 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1457 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1461 if (!cret) return -2;
1462 if (!listing) return -2;
1463 if (*listing) return -2;
1465 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1470 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1474 if (!cret) return -1;
1476 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1477 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1482 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1486 if (!cret) return -1;
1488 sprintf(aaa, "TERM %d", sid);
1489 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1494 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1496 if (!cret) return -1;
1498 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1503 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1507 if (!cret) return -1;
1509 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1510 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1515 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1521 if (!cret) return -2;
1522 if (!text) return -2;
1523 if (!filename) return -2;
1525 aaa = (char *)malloc(strlen(filename) + 6);
1526 if (!aaa) return -1;
1528 sprintf(aaa, "EMSG %s", filename);
1529 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1536 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1541 if (!cret) return -2;
1542 if (!hostname) return -2;
1544 aaa = (char *)malloc(strlen(hostname) + 6);
1545 if (!aaa) return -1;
1547 sprintf(aaa, "HCHG %s", hostname);
1548 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1555 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1560 if (!cret) return -2;
1561 if (!roomname) return -2;
1563 aaa = (char *)malloc(strlen(roomname) + 6);
1564 if (!aaa) return -1;
1566 sprintf(aaa, "RCHG %s", roomname);
1567 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1574 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1579 if (!cret) return -2;
1580 if (!username) return -2;
1582 aaa = (char *)malloc(strlen(username) + 6);
1583 if (!aaa) return -1;
1585 sprintf(aaa, "UCHG %s", username);
1586 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1593 /* This function returns the actual server time reported, or 0 if error */
1594 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1596 register time_t tret;
1599 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1600 if (ret / 100 == 2) {
1601 tret = extract_long(cret, 0);
1610 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1611 struct usersupp **uret, char *cret)
1616 if (!cret) return -2;
1617 if (!uret) return -2;
1618 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1619 if (!*uret) return -1;
1621 sprintf(aaa, "AGUP %s", who);
1622 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1624 if (ret / 100 == 2) {
1625 extract(uret[0]->fullname, cret, 0);
1626 extract(uret[0]->password, cret, 1);
1627 uret[0]->flags = extract_int(cret, 2);
1628 uret[0]->timescalled = extract_long(cret, 3);
1629 uret[0]->posted = extract_long(cret, 4);
1630 uret[0]->axlevel = extract_int(cret, 5);
1631 uret[0]->usernum = extract_long(cret, 6);
1632 uret[0]->lastcall = extract_long(cret, 7);
1633 uret[0]->USuserpurge = extract_int(cret, 8);
1640 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1645 if (!cret) return -2;
1646 if (!uret) return -2;
1648 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1649 if (!aaa) return -1;
1651 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1652 uret->fullname, uret->password, uret->flags,
1653 uret->timescalled, uret->posted, uret->axlevel,
1654 uret->usernum, uret->lastcall, uret->USuserpurge);
1655 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1662 /* which is 0 = room, 1 = floor, 2 = site */
1663 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which, char *cret)
1665 static char *proto[] = {"room", "floor", "site"};
1668 if (!cret) return -2;
1669 if (which < 0 || which > 2) return -2;
1671 sprintf(aaa, "GPEX %s", proto[which]);
1672 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1677 /* which is 0 = room, 1 = floor, 2 = site */
1678 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1679 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which, int policy, int value,
1684 if (!cret) return -2;
1685 if (which < 0 || which > 2) return -2;
1686 if (policy < 0 || policy > 3) return -2;
1687 if (policy >= 2 && value < 1) return -2;
1689 sprintf(aaa, "SPEX %d|%d|%d", which, policy, value);
1690 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1695 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1699 if (!cret) return -2;
1700 if (!listing) return -2;
1701 if (*listing) return -2;
1703 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1704 listing, &bytes, cret);
1709 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1711 if (!cret) return -2;
1712 if (!listing) return -2;
1714 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1720 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1721 char **listing, char *cret)
1726 if (!cret) return -2;
1727 if (!mimetype) return -2;
1728 if (!listing) return -2;
1729 if (*listing) return -2;
1731 aaa = malloc(strlen(mimetype) + 13);
1732 if (!aaa) return -1;
1733 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1734 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1735 listing, &bytes, cret);
1740 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1741 const char *listing, char *cret)
1745 if (!cret) return -2;
1746 if (!mimetype) return -2;
1747 if (!listing) return -2;
1749 aaa = malloc(strlen(mimetype) + 13);
1750 if (!aaa) return -1;
1751 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1752 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1757 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1761 if (!cret) return -2;
1762 if (!msgnum) return -2;
1764 sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1765 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1770 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1774 if (!cret) return -2;
1775 if (session < 0) return -2;
1777 sprintf(aaa, "REQT %d", session);
1778 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1783 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1787 if (!cret) return -2;
1788 if (msgnum < 0) return -2;
1790 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1791 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1796 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1805 /* New SSL object */
1806 temp_ssl = SSL_new(ssl_ctx);
1808 error_printf("SSL_new failed: %s\n",
1809 ERR_reason_error_string(ERR_get_error()));
1812 /* Pointless flag waving */
1813 #if SSLEAY_VERSION_NUMBER >= 0x0922
1814 SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
1817 if (!access("/var/run/egd-pool", F_OK))
1818 RAND_egd("/var/run/egd-pool");
1820 if (!RAND_status()) {
1821 error_printf("PRNG not properly seeded\n");
1825 /* Associate network connection with SSL object */
1826 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1827 error_printf("SSL_set_fd failed: %s\n",
1828 ERR_reason_error_string(ERR_get_error()));
1832 if (status_hook != NULL)
1833 status_hook("Requesting encryption...\r");
1835 /* Ready to start SSL/TLS */
1837 CtdlIPC_putline(ipc, "STLS");
1838 CtdlIPC_getline(ipc, buf);
1839 if (buf[0] != '2') {
1840 error_printf("Server can't start TLS: %s\n", buf);
1844 r = CtdlIPCGenericCommand(ipc,
1845 "STLS", NULL, 0, NULL, NULL, cret);
1847 error_printf("Server can't start TLS: %s\n", buf);
1852 /* Do SSL/TLS handshake */
1853 if ((a = SSL_connect(temp_ssl)) < 1) {
1854 error_printf("SSL_connect failed: %s\n",
1855 ERR_reason_error_string(ERR_get_error()));
1859 ipc->ssl = temp_ssl;
1861 BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1865 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1866 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1867 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1868 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1874 #endif /* HAVE_OPENSSL */
1879 static void endtls(SSL *ssl)
1890 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1894 if (!address) return -2;
1895 if (!cret) return -2;
1897 aaa = (char *)malloc(strlen(address) + 6);
1898 if (!aaa) return -1;
1900 sprintf(aaa, "QDIR %s", address);
1901 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1906 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1910 if (!cret) return -2;
1911 sprintf(aaa, "IPGM %d", secret);
1912 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1933 /* ************************************************************************** */
1934 /* Stuff below this line is not for public consumption */
1935 /* ************************************************************************** */
1938 inline void CtdlIPC_lock(CtdlIPC *ipc)
1940 #ifdef THREADED_CLIENT
1941 pthread_mutex_lock(&(ipc->mutex));
1946 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1948 #ifdef THREADED_CLIENT
1949 pthread_mutex_unlock(&(ipc->mutex));
1954 /* Read a listing from the server up to 000. Append to dest if it exists */
1955 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
1964 length = strlen(ret);
1971 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
1972 linelength = strlen(aaa);
1973 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
1975 strcpy(&ret[length], aaa);
1976 length += linelength;
1977 strcpy(&ret[length++], "\n");
1985 /* Send a listing to the server; generate the ending 000. */
1986 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
1990 text = (char *)malloc(strlen(listing) + 6);
1992 strcpy(text, listing);
1993 while (text[strlen(text) - 1] == '\n')
1994 text[strlen(text) - 1] = '\0';
1995 strcat(text, "\n000");
1996 CtdlIPC_putline(ipc, text);
2000 /* Malloc failed but we are committed to send */
2001 /* This may result in extra blanks at the bottom */
2002 CtdlIPC_putline(ipc, text);
2003 CtdlIPC_putline(ipc, "000");
2009 /* Partial read of file from server */
2010 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2012 register size_t len = 0;
2015 if (!buf) return -1;
2016 if (!cret) return -1;
2017 if (bytes < 1) return -1;
2018 if (offset < 0) return -1;
2021 sprintf(aaa, "READ %d|%d", offset, bytes);
2022 CtdlIPC_putline(ipc, aaa);
2023 CtdlIPC_getline(ipc, aaa);
2025 strcpy(cret, &aaa[4]);
2027 len = extract_long(&aaa[4], 0);
2028 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2030 /* I know what I'm doing */
2031 serv_read(ipc, (*buf + offset), len);
2033 /* We have to read regardless */
2034 serv_read(ipc, aaa, len);
2038 CtdlIPC_unlock(ipc);
2044 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2048 if (!cret) return -2;
2049 if (!ipc->downloading) return -2;
2051 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2053 ipc->downloading = 0;
2059 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2063 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2064 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2071 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2072 void (*progress_gauge_callback)(long, long), char *cret)
2074 register size_t len;
2076 if (!cret) return -1;
2077 if (!buf) return -1;
2078 if (*buf) return -1;
2079 if (!ipc->downloading) return -1;
2082 if (progress_gauge_callback)
2083 progress_gauge_callback(len, bytes);
2084 while (len < bytes) {
2085 register size_t block;
2087 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2093 if (progress_gauge_callback)
2094 progress_gauge_callback(len, bytes);
2100 /* READ - pipelined */
2101 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2102 void (*progress_gauge_callback)(long, long), char *cret)
2104 register size_t len;
2105 register int calls; /* How many calls in the pipeline */
2106 register int i; /* iterator */
2109 if (!cret) return -1;
2110 if (!buf) return -1;
2111 if (*buf) return -1;
2112 if (!ipc->downloading) return -1;
2114 *buf = (void *)realloc(*buf, bytes);
2115 if (!*buf) return -1;
2119 if (progress_gauge_callback)
2120 progress_gauge_callback(len, bytes);
2122 /* How many calls will be in the pipeline? */
2123 calls = bytes / 4096;
2124 if (bytes % 4096) calls++;
2126 /* Send all requests at once */
2127 for (i = 0; i < calls; i++) {
2128 sprintf(aaa, "READ %d|4096", i * 4096);
2129 CtdlIPC_putline(ipc, aaa);
2132 /* Receive all responses at once */
2133 for (i = 0; i < calls; i++) {
2134 CtdlIPC_getline(ipc, aaa);
2136 strcpy(cret, &aaa[4]);
2138 len = extract_long(&aaa[4], 0);
2139 /* I know what I'm doing */
2140 serv_read(ipc, ((*buf) + (i * 4096)), len);
2142 if (progress_gauge_callback)
2143 progress_gauge_callback(i * 4096 + len, bytes);
2145 CtdlIPC_unlock(ipc);
2151 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2156 if (!cret) return -1;
2157 if (!ipc->uploading) return -1;
2159 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2160 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2167 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2168 void (*progress_gauge_callback)(long, long), char *cret)
2170 register int ret = -1;
2171 register size_t offset = 0;
2177 if (!cret) return -1;
2178 if (!path) return -1;
2179 if (!*path) return -1;
2181 fd = fopen(path, "r");
2184 fseek(fd, 0L, SEEK_END);
2188 if (progress_gauge_callback)
2189 progress_gauge_callback(0, bytes);
2191 while (offset < bytes) {
2192 register size_t to_write;
2194 /* Read some data in */
2195 to_write = fread(buf, 1, 4096, fd);
2197 if (feof(fd) || ferror(fd)) break;
2199 sprintf(aaa, "WRIT %d", to_write);
2200 CtdlIPC_putline(ipc, aaa);
2201 CtdlIPC_getline(ipc, aaa);
2202 strcpy(cret, &aaa[4]);
2204 if (aaa[0] == '7') {
2205 to_write = extract_long(&aaa[4], 0);
2207 serv_write(ipc, buf, to_write);
2209 if (progress_gauge_callback)
2210 progress_gauge_callback(offset, bytes);
2211 /* Detect short reads and back up if needed */
2212 fseek(fd, offset, SEEK_SET);
2217 if (progress_gauge_callback)
2218 progress_gauge_callback(1, 1);
2219 return (!ferror(fd) ? ret : -2);
2224 * Generic command method. This method should handle any server command
2225 * except for CHAT. It takes the following arguments:
2227 * ipc The server to speak with
2228 * command Preformatted command to send to server
2229 * to_send A text or binary file to send to server
2230 * (only sent if server requests it)
2231 * bytes_to_send The number of bytes in to_send (required if
2232 * sending binary, optional if sending listing)
2233 * to_receive Pointer to a NULL pointer, if the server
2234 * sends text or binary we will allocate memory
2235 * for the file and stuff it here
2236 * bytes_to_receive If a file is received, we will store its
2238 * proto_response The protocol response. Caller must provide
2239 * this buffer and ensure that it is at least
2240 * 128 bytes in length.
2242 * This function returns a number equal to the protocol response number,
2243 * -1 if an internal error occurred, -2 if caller provided bad values,
2244 * or 0 - the protocol response number if bad values were found during
2245 * the protocol exchange.
2246 * It stores the protocol response string (minus the number) in
2247 * protocol_response as described above. Some commands send additional
2248 * data in this string.
2250 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2251 const char *command, const char *to_send,
2252 size_t bytes_to_send, char **to_receive,
2253 size_t *bytes_to_receive, char *proto_response)
2259 if (!command) return -2;
2260 if (!proto_response) return -2;
2262 if (ipc->ssl) watch_ssl = 1;
2265 CtdlIPC_putline(ipc, command);
2267 CtdlIPC_getline(ipc, proto_response);
2268 if (proto_response[3] == '*')
2270 ret = atoi(proto_response);
2271 strcpy(proto_response, &proto_response[4]);
2272 switch (ret / 100) {
2273 default: /* Unknown, punt */
2275 case 3: /* MORE_DATA */
2277 /* Don't need to do anything */
2279 case 1: /* LISTING_FOLLOWS */
2280 if (to_receive && !*to_receive && bytes_to_receive) {
2281 *to_receive = CtdlIPCReadListing(ipc, NULL);
2282 } else { /* Drain */
2283 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2287 case 4: /* SEND_LISTING */
2289 CtdlIPCSendListing(ipc, to_send);
2291 /* No listing given, fake it */
2292 CtdlIPC_putline(ipc, "000");
2296 case 6: /* BINARY_FOLLOWS */
2297 if (to_receive && !*to_receive && bytes_to_receive) {
2299 extract_long(proto_response, 0);
2300 *to_receive = (char *)
2301 malloc((size_t)*bytes_to_receive);
2305 serv_read(ipc, *to_receive,
2312 drain = extract_long(proto_response, 0);
2313 while (drain > SIZ) {
2314 serv_read(ipc, buf, SIZ);
2317 serv_read(ipc, buf, drain);
2321 case 7: /* SEND_BINARY */
2322 if (to_send && bytes_to_send) {
2323 serv_write(ipc, to_send, bytes_to_send);
2324 } else if (bytes_to_send) {
2325 /* Fake it, send nulls */
2328 fake = bytes_to_send;
2329 memset(buf, '\0', SIZ);
2330 while (fake > SIZ) {
2331 serv_write(ipc, buf, SIZ);
2334 serv_write(ipc, buf, fake);
2336 } /* else who knows? DANGER WILL ROBINSON */
2338 case 8: /* START_CHAT_MODE */
2339 if (!strncasecmp(command, "CHAT", 4)) {
2340 /* Don't call chatmode with generic! */
2341 CtdlIPC_putline(ipc, "/quit");
2344 /* In this mode we send then receive listing */
2346 CtdlIPCSendListing(ipc, to_send);
2348 /* No listing given, fake it */
2349 CtdlIPC_putline(ipc, "000");
2352 if (to_receive && !*to_receive
2353 && bytes_to_receive) {
2354 *to_receive = CtdlIPCReadListing(ipc, NULL);
2355 } else { /* Drain */
2356 while (CtdlIPC_getline(ipc, buf),
2357 strcmp(buf, "000")) ;
2362 case 9: /* ASYNC_MSG */
2363 /* CtdlIPCDoAsync(ret, proto_response); */
2364 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2370 CtdlIPC_unlock(ipc);
2375 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2377 struct hostent *phe;
2378 struct servent *pse;
2379 struct protoent *ppe;
2380 struct sockaddr_in sin;
2383 memset(&sin, 0, sizeof(sin));
2384 sin.sin_family = AF_INET;
2386 pse = getservbyname(service, protocol);
2388 sin.sin_port = pse->s_port;
2390 else if (atoi(service) > 0) {
2391 sin.sin_port = htons(atoi(service));
2394 sin.sin_port = htons(defaultPort);
2396 phe = gethostbyname(host);
2398 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2399 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2402 if ((ppe = getprotobyname(protocol)) == 0) {
2405 if (!strcmp(protocol, "udp")) {
2411 s = socket(PF_INET, type, ppe->p_proto);
2416 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2423 static int uds_connectsock(int *isLocal, char *sockpath)
2425 struct sockaddr_un addr;
2428 memset(&addr, 0, sizeof(addr));
2429 addr.sun_family = AF_UNIX;
2430 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2432 s = socket(AF_UNIX, SOCK_STREAM, 0);
2437 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2447 * input binary data from socket
2449 static void serv_read(CtdlIPC *ipc, char *buf, int bytes)
2453 #if defined(HAVE_OPENSSL)
2455 serv_read_ssl(ipc, buf, bytes);
2460 while (len < bytes) {
2461 rlen = read(ipc->sock, &buf[len], bytes - len);
2463 connection_died(ipc);
2472 * send binary to server
2474 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
2476 int bytes_written = 0;
2479 #if defined(HAVE_OPENSSL)
2481 serv_write_ssl(ipc, buf, nbytes);
2485 while (bytes_written < nbytes) {
2486 retval = write(ipc->sock, &buf[bytes_written],
2487 nbytes - bytes_written);
2489 connection_died(ipc);
2492 bytes_written += retval;
2499 * input binary data from encrypted connection
2501 static void serv_read_ssl(CtdlIPC* ipc, char *buf, int bytes)
2507 while (len < bytes) {
2508 if (SSL_want_read(ipc->ssl)) {
2509 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2510 error_printf("SSL_write in serv_read:\n");
2511 ERR_print_errors_fp(stderr);
2514 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2518 errval = SSL_get_error(ipc->ssl, rlen);
2519 if (errval == SSL_ERROR_WANT_READ ||
2520 errval == SSL_ERROR_WANT_WRITE) {
2524 if (errval == SSL_ERROR_ZERO_RETURN ||
2525 errval == SSL_ERROR_SSL) {
2526 serv_read(ipc, &buf[len], bytes - len);
2529 error_printf("SSL_read in serv_read:\n");
2530 ERR_print_errors_fp(stderr);
2540 * send binary to server encrypted
2542 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes)
2544 int bytes_written = 0;
2548 while (bytes_written < nbytes) {
2549 if (SSL_want_write(ipc->ssl)) {
2550 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2551 error_printf("SSL_read in serv_write:\n");
2552 ERR_print_errors_fp(stderr);
2555 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2556 nbytes - bytes_written);
2560 errval = SSL_get_error(ipc->ssl, retval);
2561 if (errval == SSL_ERROR_WANT_READ ||
2562 errval == SSL_ERROR_WANT_WRITE) {
2566 if (errval == SSL_ERROR_ZERO_RETURN ||
2567 errval == SSL_ERROR_SSL) {
2568 serv_write(ipc, &buf[bytes_written],
2569 nbytes - bytes_written);
2572 error_printf("SSL_write in serv_write:\n");
2573 ERR_print_errors_fp(stderr);
2577 bytes_written += retval;
2582 static void CtdlIPC_init_OpenSSL(void)
2585 SSL_METHOD *ssl_method;
2588 /* already done init */
2596 SSL_load_error_strings();
2597 SSLeay_add_ssl_algorithms();
2599 /* Set up the SSL context in which we will oeprate */
2600 ssl_method = SSLv23_client_method();
2601 ssl_ctx = SSL_CTX_new(ssl_method);
2603 error_printf("SSL_CTX_new failed: %s\n",
2604 ERR_reason_error_string(ERR_get_error()));
2607 /* Any reasonable cipher we can get */
2608 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2609 error_printf("No ciphers available for encryption\n");
2612 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2614 /* Load DH parameters into the context */
2617 error_printf("Can't allocate a DH object: %s\n",
2618 ERR_reason_error_string(ERR_get_error()));
2621 if (!(BN_hex2bn(&(dh->p), DH_P))) {
2622 error_printf("Can't assign DH_P: %s\n",
2623 ERR_reason_error_string(ERR_get_error()));
2627 if (!(BN_hex2bn(&(dh->g), DH_G))) {
2628 error_printf("Can't assign DH_G: %s\n",
2629 ERR_reason_error_string(ERR_get_error()));
2634 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2637 #ifdef THREADED_CLIENT
2638 /* OpenSSL requires callbacks for threaded clients */
2639 CRYPTO_set_locking_callback(ssl_lock);
2640 CRYPTO_set_id_callback(id_callback);
2642 /* OpenSSL requires us to do semaphores for threaded clients */
2643 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2645 perror("malloc failed");
2648 for (a = 0; a < CRYPTO_num_locks(); a++) {
2649 Critters[a] = malloc(sizeof (pthread_mutex_t));
2651 perror("malloc failed");
2654 pthread_mutex_init(Critters[a], NULL);
2657 #endif /* THREADED_CLIENT */
2661 static void ssl_lock(int mode, int n, const char *file, int line)
2663 #ifdef THREADED_CLIENT
2664 if (mode & CRYPTO_LOCK)
2665 pthread_mutex_lock(Critters[n]);
2667 pthread_mutex_unlock(Critters[n]);
2668 #endif /* THREADED_CLIENT */
2671 #ifdef THREADED_CLIENT
2672 static unsigned long id_callback(void) {
2673 return (unsigned long)pthread_self();
2675 #endif /* THREADED_CLIENT */
2676 #endif /* HAVE_OPENSSL */
2680 * input string from socket - implemented in terms of serv_read()
2682 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2686 /* Read one character at a time. */
2688 serv_read(ipc, &buf[i], 1);
2689 if (buf[i] == '\n' || i == (SIZ-1))
2693 /* If we got a long line, discard characters until the newline. */
2695 while (buf[i] != '\n')
2696 serv_read(ipc, &buf[i], 1);
2698 /* Strip the trailing newline.
2705 * send line to server - implemented in terms of serv_write()
2707 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2709 /* error_printf("< %s\n", buf); */
2710 serv_write(ipc, buf, strlen(buf));
2711 serv_write(ipc, "\n", 1);
2713 ipc->last_command_sent = time(NULL);
2720 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2727 CtdlIPC *ipc = ialloc(CtdlIPC);
2731 #if defined(HAVE_OPENSSL)
2733 CtdlIPC_init_OpenSSL();
2735 #if defined(HAVE_PTHREAD_H)
2736 pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2738 ipc->sock = -1; /* Not connected */
2739 ipc->isLocal = 0; /* Not local, of course! */
2740 ipc->downloading = 0;
2742 ipc->last_command_sent = 0L;
2744 strcpy(cithost, DEFAULT_HOST); /* default host */
2745 strcpy(citport, DEFAULT_PORT); /* default port */
2747 for (a = 0; a < argc; ++a) {
2750 } else if (a == 1) {
2751 strcpy(cithost, argv[a]);
2752 } else if (a == 2) {
2753 strcpy(citport, argv[a]);
2755 error_printf("%s: usage: ",argv[0]);
2756 error_printf("%s [host] [port] ",argv[0]);
2763 if ((!strcmp(cithost, "localhost"))
2764 || (!strcmp(cithost, "127.0.0.1"))) {
2768 /* If we're using a unix domain socket we can do a bunch of stuff */
2769 if (!strcmp(cithost, UDS)) {
2770 snprintf(sockpath, sizeof sockpath, "citadel.socket");
2771 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2772 if (ipc->sock == -1) {
2776 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2777 if (portbuf != NULL) strcpy(portbuf, sockpath);
2781 ipc->sock = connectsock(cithost, citport, "tcp", 504);
2782 if (ipc->sock == -1) {
2786 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2787 if (portbuf != NULL) strcpy(portbuf, citport);
2792 * return the file descriptor of the server socket so we can select() on it.
2794 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2797 int CtdlIPC_getsockfd(CtdlIPC* ipc)
2804 * return one character
2806 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2809 char CtdlIPC_get(CtdlIPC* ipc)
2814 serv_read(ipc, buf, 1);