4 #define DEFAULT_HOST UDS
5 #define DEFAULT_PORT "citadel"
13 # include <sys/time.h>
20 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #include <netinet/in.h>
33 #ifdef THREADED_CLIENT
37 #include "citadel_ipc.h"
38 #include "citadel_decls.h"
41 #ifdef THREADED_CLIENT
42 pthread_mutex_t rwlock;
46 static SSL_CTX *ssl_ctx;
49 #ifdef THREADED_CLIENT
50 pthread_mutex_t **Critters; /* Things that need locking */
51 #endif /* THREADED_CLIENT */
53 #endif /* HAVE_OPENSSL */
56 #define INADDR_NONE 0xffffffff
59 static void (*status_hook)(char *s) = NULL;
61 void setCryptoStatusHook(void (*hook)(char *s)) {
66 char express_msgs = 0;
69 static void serv_read(CtdlIPC *ipc, char *buf, int bytes);
70 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes);
72 static void serv_read_ssl(CtdlIPC *ipc, char *buf, int bytes);
73 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes);
74 static void ssl_lock(int mode, int n, const char *file, int line);
75 static void endtls(SSL *ssl);
76 #ifdef THREADED_CLIENT
77 static unsigned long id_callback(void);
78 #endif /* THREADED_CLIENT */
79 #endif /* HAVE_OPENSSL */
83 * Does nothing. The server should always return 200.
85 int CtdlIPCNoop(CtdlIPC *ipc)
89 return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
94 * Does nothing interesting. The server should always return 200
95 * along with your string.
97 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
103 if (!cret) return -2;
105 aaa = (char *)malloc((size_t)(strlen(arg) + 6));
108 sprintf(aaa, "ECHO %s", arg);
109 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
116 * Asks the server to close the connecction.
117 * Should always return 200.
119 int CtdlIPCQuit(CtdlIPC *ipc)
125 CtdlIPC_putline(ipc, "QUIT");
126 CtdlIPC_getline(ipc, aaa);
134 * Asks the server to logout. Should always return 200, even if no user
135 * was logged in. The user will not be logged in after this!
137 int CtdlIPCLogout(CtdlIPC *ipc)
143 CtdlIPC_putline(ipc, "LOUT");
144 CtdlIPC_getline(ipc, aaa);
152 * First stage of authentication - pass the username. Returns 300 if the
153 * username is able to log in, with the username correctly spelled in cret.
154 * Returns various 500 error codes if the user doesn't exist, etc.
156 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
161 if (!username) return -2;
162 if (!cret) return -2;
164 aaa = (char *)malloc((size_t)(strlen(username) + 6));
167 sprintf(aaa, "USER %s", username);
168 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
175 * Second stage of authentication - provide password. The server returns
176 * 200 and several arguments in cret relating to the user's account.
178 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
183 if (!passwd) return -2;
184 if (!cret) return -2;
186 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
189 sprintf(aaa, "PASS %s", passwd);
190 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
197 * Second stage of authentication - provide password. The server returns
198 * 200 and several arguments in cret relating to the user's account.
200 int CtdlIPCTryApopPassword(CtdlIPC *ipc, const char *response, char *cret)
205 if (!response) return -2;
206 if (!cret) return -2;
208 aaa = (char *)malloc((size_t)(strlen(response) + 6));
211 sprintf(aaa, "PAS2 %s", response);
212 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
219 * Create a new user. This returns 200 plus the same arguments as TryPassword
220 * if selfservice is nonzero, unless there was a problem creating the account.
221 * If selfservice is zero, creates a new user but does not log out the existing
222 * user - intended for use by system administrators to create accounts on
223 * behalf of other users.
225 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
230 if (!username) return -2;
231 if (!cret) return -2;
233 aaa = (char *)malloc((size_t)(strlen(username) + 6));
236 sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU", username);
237 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
244 * Changes the user's password. Returns 200 if changed, errors otherwise.
246 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
251 if (!passwd) return -2;
252 if (!cret) return -2;
254 aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
257 sprintf(aaa, "SETP %s", passwd);
258 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
265 /* Caller must free the march list */
266 /* Room types are defined in enum RoomList; keep these in sync! */
267 /* floor is -1 for all, or floornum */
268 int CtdlIPCKnownRooms(CtdlIPC *ipc, enum RoomList which, int floor, struct march **listing, char *cret)
271 struct march *march = NULL;
272 static char *proto[] =
273 {"LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
278 if (!listing) return -2;
279 if (*listing) return -2; /* Free the listing first */
280 if (!cret) return -2;
281 /* if (which < 0 || which > 4) return -2; */
282 if (floor < -1) return -2; /* Can't validate upper bound, sorry */
284 sprintf(aaa, "%s %d", proto[which], floor);
285 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
286 if (ret / 100 == 1) {
289 while (bbb && strlen(bbb)) {
292 extract_token(aaa, bbb, 0, '\n');
294 memmove(bbb, bbb + a + 1, strlen(bbb) - a);
295 mptr = (struct march *) malloc(sizeof (struct march));
298 extract(mptr->march_name, aaa, 0);
299 mptr->march_flags = (unsigned int) extract_int(aaa, 1);
300 mptr->march_floor = (char) extract_int(aaa, 2);
301 mptr->march_order = (char) extract_int(aaa, 3);
308 while (mptr2->next != NULL)
321 /* Caller must free the struct usersupp; caller may pass an existing one */
322 int CtdlIPCGetConfig(CtdlIPC *ipc, struct usersupp **uret, char *cret)
326 if (!cret) return -2;
327 if (!uret) return -2;
328 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof (struct usersupp));
329 if (!*uret) return -1;
331 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
332 if (ret / 100 == 2) {
333 uret[0]->USscreenwidth = extract_int(cret, 0);
334 uret[0]->USscreenheight = extract_int(cret, 1);
335 uret[0]->flags = extract_int(cret, 2);
342 int CtdlIPCSetConfig(CtdlIPC *ipc, struct usersupp *uret, char *cret)
346 if (!uret) return -2;
347 if (!cret) return -2;
349 sprintf(aaa, "SETU %d|%d|%d",
350 uret->USscreenwidth, uret->USscreenheight,
352 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
357 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
358 struct ctdlipcroom **rret, char *cret)
363 if (!cret) return -2;
364 if (!rret) return -2;
365 if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
366 if (!*rret) return -1;
369 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
374 sprintf(aaa, "GOTO %s|%s", room, passwd);
376 aaa = (char *)malloc(strlen(room) + 6);
381 sprintf(aaa, "GOTO %s", room);
383 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
384 if (ret / 100 == 2) {
385 extract(rret[0]->RRname, cret, 0);
386 rret[0]->RRunread = extract_long(cret, 1);
387 rret[0]->RRtotal = extract_long(cret, 2);
388 rret[0]->RRinfoupdated = extract_int(cret, 3);
389 rret[0]->RRflags = extract_int(cret, 4);
390 rret[0]->RRhighest = extract_long(cret, 5);
391 rret[0]->RRlastread = extract_long(cret, 6);
392 rret[0]->RRismailbox = extract_int(cret, 7);
393 rret[0]->RRaide = extract_int(cret, 8);
394 rret[0]->RRnewmail = extract_long(cret, 9);
395 rret[0]->RRfloor = extract_int(cret, 10);
404 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
405 /* whicharg is number of messages, applies to last, first, gt, lt */
406 int CtdlIPCGetMessages(CtdlIPC *ipc, int which, int whicharg, const char *template,
407 long **mret, char *cret)
410 register long count = 0;
411 static char *proto[] =
412 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
417 if (!cret) return -2;
418 if (!mret) return -2;
419 if (*mret) return -2;
420 if (which < 0 || which > 6) return -2;
423 sprintf(aaa, "MSGS %s||%d", proto[which],
426 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
428 if (template) count = strlen(template);
429 ret = CtdlIPCGenericCommand(ipc, aaa, template, count, &bbb, &bbbsize, cret);
431 while (strlen(bbb)) {
434 extract_token(aaa, bbb, 0, '\n');
436 memmove(aaa, bbb + a + 1, strlen(bbb) - a - 1);
437 *mret = (long *)realloc(mret,
438 (size_t)((count + 1) * sizeof (long)));
440 *mret[count++] = atol(aaa);
448 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
449 struct ctdlipcmessage **mret, char *cret)
455 int multipart_hunting = 0;
456 char multipart_prefix[SIZ];
458 if (!cret) return -1;
459 if (!mret) return -1;
460 if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
461 if (!*mret) return -1;
462 if (!msgnum) return -1;
464 strcpy(mret[0]->content_type, "");
465 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
466 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
467 if (ret / 100 == 1) {
469 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
470 while (strlen(bbb) > 4 && bbb[4] == '=') {
471 extract_token(aaa, bbb, 0, '\n');
472 remove_token(bbb, 0, '\n');
474 if (!strncasecmp(aaa, "nhdr=yes", 8))
476 else if (!strncasecmp(aaa, "from=", 5))
477 strcpy(mret[0]->author, &aaa[5]);
478 else if (!strncasecmp(aaa, "type=", 5))
479 mret[0]->type = atoi(&aaa[5]);
480 else if (!strncasecmp(aaa, "msgn=", 5))
481 strcpy(mret[0]->msgid, &aaa[5]);
482 else if (!strncasecmp(aaa, "subj=", 5))
483 strcpy(mret[0]->subject, &aaa[5]);
484 else if (!strncasecmp(aaa, "rfca=", 5))
485 strcpy(mret[0]->email, &aaa[5]);
486 else if (!strncasecmp(aaa, "hnod=", 5))
487 strcpy(mret[0]->hnod, &aaa[5]);
488 else if (!strncasecmp(aaa, "room=", 5))
489 strcpy(mret[0]->room, &aaa[5]);
490 else if (!strncasecmp(aaa, "node=", 5))
491 strcpy(mret[0]->node, &aaa[5]);
492 else if (!strncasecmp(aaa, "rcpt=", 5))
493 strcpy(mret[0]->recipient, &aaa[5]);
494 else if (!strncasecmp(aaa, "time=", 5))
495 mret[0]->time = atol(&aaa[5]);
497 /* Multipart/alternative prefix & suffix strings help
498 * us to determine which part we want to download.
500 else if (!strncasecmp(aaa, "pref=", 5)) {
501 extract(multipart_prefix, &aaa[5], 1);
502 if (!strcasecmp(multipart_prefix,
503 "multipart/alternative")) {
507 else if (!strncasecmp(aaa, "suff=", 5)) {
508 extract(multipart_prefix, &aaa[5], 1);
509 if (!strcasecmp(multipart_prefix,
510 "multipart/alternative")) {
515 else if (!strncasecmp(aaa, "part=", 5)) {
516 struct parts *ptr, *chain;
518 ptr = (struct parts *)calloc(1, sizeof (struct parts));
521 /* Fill the buffers for the caller */
522 extract(ptr->name, &aaa[5], 0);
523 extract(ptr->filename, &aaa[5], 1);
524 extract(ptr->number, &aaa[5], 2);
525 extract(ptr->disposition, &aaa[5], 3);
526 extract(ptr->mimetype, &aaa[5], 4);
527 ptr->length = extract_long(&aaa[5], 5);
528 if (!mret[0]->attachments)
529 mret[0]->attachments = ptr;
531 chain = mret[0]->attachments;
537 /* Now handle multipart/alternative */
538 if (multipart_hunting > 0) {
539 if ( (!strcasecmp(ptr->mimetype,
541 || (!strcasecmp(ptr->mimetype,
543 strcpy(mret[0]->mime_chosen,
551 /* Eliminate "text\n" */
552 remove_token(bbb, 0, '\n');
554 /* If doing a MIME thing, pull out the extra headers */
557 if (!strncasecmp(bbb, "Content-type: ", 14)) {
558 extract_token(mret[0]->content_type, bbb, 0, '\n');
559 strcpy(mret[0]->content_type,
560 &mret[0]->content_type[14]);
561 striplt(mret[0]->content_type);
563 remove_token(bbb, 0, '\n');
564 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
570 /* Strip trailing whitespace */
571 bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
573 bbb = (char *)realloc(bbb, 1);
583 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
588 if (!cret) return -2;
589 if (!listing) return -2;
590 if (*listing) return -2;
592 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
598 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
602 char *listing = NULL;
605 if (!cret) return -2;
606 if (!ServInfo) return -2;
608 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
609 if (ret / 100 == 1) {
612 while (*listing && strlen(listing)) {
613 extract_token(buf, listing, 0, '\n');
614 remove_token(listing, 0, '\n');
616 case 0: ServInfo->serv_pid = atoi(buf);
618 case 1: strcpy(ServInfo->serv_nodename,buf);
620 case 2: strcpy(ServInfo->serv_humannode,buf);
622 case 3: strcpy(ServInfo->serv_fqdn,buf);
624 case 4: strcpy(ServInfo->serv_software,buf);
626 case 5: ServInfo->serv_rev_level = atoi(buf);
628 case 6: strcpy(ServInfo->serv_bbs_city,buf);
630 case 7: strcpy(ServInfo->serv_sysadm,buf);
632 case 9: strcpy(ServInfo->serv_moreprompt,buf);
634 case 10: ServInfo->serv_ok_floors = atoi(buf);
636 case 11: ServInfo->serv_paging_level = atoi(buf);
638 case 13: ServInfo->serv_supports_qnop = atoi(buf);
649 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
654 if (!cret) return -2;
655 if (!listing) return -2;
656 if (*listing) return -2;
658 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
664 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
666 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
671 if (!cret) return -2;
674 sprintf(aaa, "SLRP %ld", msgnum);
676 sprintf(aaa, "SLRP HIGHEST");
677 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
683 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
688 if (!cret) return -2;
689 if (!username) return -2;
691 aaa = (char *)malloc(strlen(username) + 6);
694 sprintf(aaa, "INVT %s", username);
695 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
702 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
707 if (!cret) return -1;
708 if (!username) return -1;
710 aaa = (char *)malloc(strlen(username) + 6);
712 sprintf(aaa, "KICK %s", username);
713 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
720 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct quickroom **qret, char *cret)
724 if (!cret) return -2;
725 if (!qret) return -2;
726 if (!*qret) *qret = (struct quickroom *)calloc(1, sizeof (struct quickroom));
727 if (!*qret) return -1;
729 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
730 if (ret / 100 == 2) {
731 extract(qret[0]->QRname, cret, 0);
732 extract(qret[0]->QRpasswd, cret, 1);
733 extract(qret[0]->QRdirname, cret, 2);
734 qret[0]->QRflags = extract_int(cret, 3);
735 qret[0]->QRfloor = extract_int(cret, 4);
736 qret[0]->QRorder = extract_int(cret, 5);
737 qret[0]->QRdefaultview = extract_int(cret, 6);
738 qret[0]->QRflags2 = extract_int(cret, 7);
745 /* set forget to kick all users out of room */
746 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct quickroom *qret, char *cret)
751 if (!cret) return -2;
752 if (!qret) return -2;
754 aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
755 strlen(qret->QRdirname) + 52);
758 sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
759 qret->QRname, qret->QRpasswd, qret->QRdirname,
760 qret->QRflags, forget, qret->QRfloor, qret->QRorder);
761 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
768 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
770 if (!cret) return -1;
772 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
777 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
782 if (!cret) return -2;
783 if (!username) return -2;
785 aaa = (char *)malloc(strlen(username) + 6);
788 sprintf(aaa, "SETA %s", username);
789 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
796 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
801 if (!cret) return -2;
804 aaa = (char *)malloc(strlen(mr->recipient) + strlen(mr->author) + 40);
807 sprintf(aaa, "ENT0 %d|%s|%d|%d|%s", flag, mr->recipient, mr->anonymous,
808 mr->type, mr->author);
809 ret = CtdlIPCGenericCommand(ipc, aaa, mr->text, strlen(mr->text), NULL,
817 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
821 if (!cret) return -2;
822 if (!iret) return -2;
823 if (*iret) return -2;
825 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
830 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
834 if (!cret) return -2;
835 if (!msgnum) return -2;
837 sprintf(aaa, "DELE %ld", msgnum);
838 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
843 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
848 if (!cret) return -2;
849 if (!destroom) return -2;
850 if (!msgnum) return -2;
852 aaa = (char *)malloc(strlen(destroom) + 28);
855 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
856 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
863 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
867 if (!cret) return -2;
869 sprintf(aaa, "KILL %d", for_real);
870 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
875 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
876 const char *password, int floor, char *cret)
881 if (!cret) return -2;
882 if (!roomname) return -2;
885 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
887 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
890 aaa = (char *)malloc(strlen(roomname) + 40);
892 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
895 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
902 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
904 if (!cret) return -2;
906 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
911 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
917 if (!cret) return -2;
918 if (!mret) return -2;
919 if (*mret) return -2;
920 if (!message) return -2;
922 aaa = (char *)malloc(strlen(message) + 6);
925 sprintf(aaa, "MESG %s", message);
926 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
933 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
935 if (!cret) return -2;
937 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
942 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
948 if (!cret) return -2;
949 if (!rret) return -2;
950 if (*rret) return -2;
953 aaa = (char *)malloc(strlen(username) + 6);
955 aaa = (char *)malloc(12);
959 sprintf(aaa, "GREG %s", username);
961 sprintf(aaa, "GREG _SELF_");
962 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
969 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
974 if (!cret) return -2;
975 if (!username) return -2;
976 if (axlevel < 0 || axlevel > 7) return -2;
978 aaa = (char *)malloc(strlen(username) + 17);
981 sprintf(aaa, "VALI %s|%d", username, axlevel);
982 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
989 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
993 if (!cret) return -1;
994 if (!info) return -1;
996 sprintf(aaa, "EINF %d", for_real);
997 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1002 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
1006 if (!cret) return -1;
1007 if (!listing) return -1;
1008 if (*listing) return -1;
1010 return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
1015 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
1017 if (!cret) return -1;
1018 if (!info) return -1;
1020 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
1026 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1030 if (!cret) return -1;
1031 if (!chek) return -1;
1033 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1034 if (ret / 100 == 2) {
1035 chek->newmail = extract_long(cret, 0);
1036 chek->needregis = extract_int(cret, 1);
1037 chek->needvalid = extract_int(cret, 2);
1044 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1049 if (!cret) return -2;
1050 if (!filename) return -2;
1052 aaa = (char *)malloc(strlen(filename) + 6);
1053 if (!aaa) return -1;
1055 sprintf(aaa, "DELF %s", filename);
1056 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1063 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1068 if (!cret) return -2;
1069 if (!filename) return -2;
1070 if (!destroom) return -2;
1072 aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1073 if (!aaa) return -1;
1075 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1076 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1083 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1088 if (!cret) return -2;
1089 if (!filename) return -2;
1090 if (!destnode) return -2;
1092 aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1093 if (!aaa) return -1;
1095 sprintf(aaa, "NETF %s|%s", filename, destnode);
1096 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1103 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1108 if (!cret) return -1;
1109 if (!listing) return -1;
1110 if (*listing) return -1;
1112 *stamp = CtdlIPCServerTime(ipc, cret);
1114 *stamp = time(NULL);
1115 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1121 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1122 size_t resume, void (*progress_gauge_callback)(long, long),
1131 if (!cret) return -2;
1132 if (!filename) return -2;
1133 if (!buf) return -2;
1134 if (*buf) return -2;
1135 if (ipc->downloading) return -2;
1137 aaa = (char *)malloc(strlen(filename) + 6);
1138 if (!aaa) return -1;
1140 sprintf(aaa, "OPEN %s", filename);
1141 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1143 if (ret / 100 == 2) {
1144 ipc->downloading = 1;
1145 bytes = extract_long(cret, 0);
1146 last_mod = extract_int(cret, 1);
1147 extract(mimetype, cret, 2);
1148 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret);
1149 /* ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, resume, progress_gauge_callback, cret); */
1150 ret = CtdlIPCEndDownload(ipc, cret);
1152 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1153 filename, mimetype);
1160 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part, void **buf,
1161 void (*progress_gauge_callback)(long, long), char *cret)
1170 if (!cret) return -2;
1171 if (!buf) return -2;
1172 if (*buf) return -2;
1173 if (!part) return -2;
1174 if (!msgnum) return -2;
1175 if (ipc->downloading) return -2;
1177 aaa = (char *)malloc(strlen(part) + 17);
1178 if (!aaa) return -1;
1180 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1181 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1183 if (ret / 100 == 2) {
1184 ipc->downloading = 1;
1185 bytes = extract_long(cret, 0);
1186 last_mod = extract_int(cret, 1);
1187 extract(mimetype, cret, 2);
1188 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1189 ret = CtdlIPCEndDownload(ipc, cret);
1191 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1192 filename, mimetype);
1199 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1200 void (*progress_gauge_callback)(long, long), char *cret)
1208 if (!cret) return -1;
1209 if (!buf) return -1;
1210 if (*buf) return -1;
1211 if (!filename) return -1;
1212 if (ipc->downloading) return -1;
1214 aaa = (char *)malloc(strlen(filename) + 6);
1215 if (!aaa) return -1;
1217 sprintf(aaa, "OIMG %s", filename);
1218 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1220 if (ret / 100 == 2) {
1221 ipc->downloading = 1;
1222 bytes = extract_long(cret, 0);
1223 last_mod = extract_int(cret, 1);
1224 extract(mimetype, cret, 2);
1225 ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1226 ret = CtdlIPCEndDownload(ipc, cret);
1228 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1229 filename, mimetype);
1236 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1237 const char *path, void (*progress_gauge_callback)(long, long),
1243 if (!cret) return -1;
1244 if (!save_as) return -1;
1245 if (!comment) return -1;
1246 if (!path) return -1;
1247 if (!*path) return -1;
1248 if (ipc->uploading) return -1;
1250 aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1251 if (!aaa) return -1;
1253 sprintf(aaa, "UOPN %s|%s", save_as, comment);
1254 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1256 if (ret / 100 == 2) {
1258 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1259 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1267 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1268 const char *save_as,
1269 void (*progress_gauge_callback)(long, long), char *cret)
1274 if (!cret) return -1;
1275 if (!save_as) return -1;
1276 if (!path && for_real) return -1;
1277 if (!*path && for_real) return -1;
1278 if (ipc->uploading) return -1;
1280 aaa = (char *)malloc(strlen(save_as) + 17);
1281 if (!aaa) return -1;
1283 sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1284 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1286 if (ret / 100 == 2 && for_real) {
1288 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1289 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1297 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1302 if (!cret) return -2;
1303 if (!username) return -2;
1305 aaa = (char *)malloc(strlen(username) + 6);
1306 if (!aaa) return -1;
1308 sprintf(aaa, "QUSR %s", username);
1309 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1316 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1320 if (!cret) return -2;
1321 if (!listing) return -2;
1322 if (*listing) return -2;
1324 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1329 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1334 if (!cret) return -2;
1335 if (!name) return -2;
1337 aaa = (char *)malloc(strlen(name) + 17);
1338 if (!aaa) return -1;
1340 sprintf(aaa, "CFLR %s|%d", name, for_real);
1341 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1348 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1352 if (!cret) return -1;
1353 if (floornum < 0) return -1;
1355 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1356 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1361 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1366 if (!cret) return -2;
1367 if (!floorname) return -2;
1368 if (floornum < 0) return -2;
1370 aaa = (char *)malloc(strlen(floorname) + 17);
1371 if (!aaa) return -1;
1373 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1374 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1381 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid, int revision,
1382 const char *software_name, const char *hostname, char *cret)
1387 if (developerid < 0) return -2;
1388 if (clientid < 0) return -2;
1389 if (revision < 0) return -2;
1390 if (!software_name) return -2;
1391 if (!hostname) return -2;
1393 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1394 if (!aaa) return -1;
1396 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1397 revision, software_name, hostname);
1398 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1405 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1411 if (!cret) return -2;
1412 if (!username) return -2;
1414 aaa = (char *)malloc(strlen(username) + 8);
1415 if (!aaa) return -1;
1418 sprintf(aaa, "SEXP %s|-", username);
1419 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1422 sprintf(aaa, "SEXP %s||", username);
1423 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1431 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1435 if (!cret) return -2;
1436 if (!listing) return -2;
1437 if (*listing) return -2;
1439 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1444 /* mode is 0 = enable, 1 = disable, 2 = status */
1445 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1449 if (!cret) return -2;
1451 sprintf(aaa, "DEXP %d", mode);
1452 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1457 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1459 if (!cret) return -2;
1460 if (!bio) return -2;
1462 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1468 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1474 if (!cret) return -2;
1475 if (!username) return -2;
1476 if (!listing) return -2;
1477 if (*listing) return -2;
1479 aaa = (char *)malloc(strlen(username) + 6);
1480 if (!aaa) return -1;
1482 sprintf(aaa, "RBIO %s", username);
1483 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1490 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1494 if (!cret) return -2;
1495 if (!listing) return -2;
1496 if (*listing) return -2;
1498 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1503 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1507 if (!cret) return -1;
1509 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1510 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1515 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1519 if (!cret) return -1;
1521 sprintf(aaa, "TERM %d", sid);
1522 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1527 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1529 if (!cret) return -1;
1531 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1536 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1540 if (!cret) return -1;
1542 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1543 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1548 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1554 if (!cret) return -2;
1555 if (!text) return -2;
1556 if (!filename) return -2;
1558 aaa = (char *)malloc(strlen(filename) + 6);
1559 if (!aaa) return -1;
1561 sprintf(aaa, "EMSG %s", filename);
1562 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1569 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1574 if (!cret) return -2;
1575 if (!hostname) return -2;
1577 aaa = (char *)malloc(strlen(hostname) + 6);
1578 if (!aaa) return -1;
1580 sprintf(aaa, "HCHG %s", hostname);
1581 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1588 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1593 if (!cret) return -2;
1594 if (!roomname) return -2;
1596 aaa = (char *)malloc(strlen(roomname) + 6);
1597 if (!aaa) return -1;
1599 sprintf(aaa, "RCHG %s", roomname);
1600 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1607 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1612 if (!cret) return -2;
1613 if (!username) return -2;
1615 aaa = (char *)malloc(strlen(username) + 6);
1616 if (!aaa) return -1;
1618 sprintf(aaa, "UCHG %s", username);
1619 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1626 /* This function returns the actual server time reported, or 0 if error */
1627 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1629 register time_t tret;
1632 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1633 if (ret / 100 == 2) {
1634 tret = extract_long(cret, 0);
1643 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1644 struct usersupp **uret, char *cret)
1649 if (!cret) return -2;
1650 if (!uret) return -2;
1651 if (!*uret) *uret = (struct usersupp *)calloc(1, sizeof(struct usersupp));
1652 if (!*uret) return -1;
1654 sprintf(aaa, "AGUP %s", who);
1655 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1657 if (ret / 100 == 2) {
1658 extract(uret[0]->fullname, cret, 0);
1659 extract(uret[0]->password, cret, 1);
1660 uret[0]->flags = extract_int(cret, 2);
1661 uret[0]->timescalled = extract_long(cret, 3);
1662 uret[0]->posted = extract_long(cret, 4);
1663 uret[0]->axlevel = extract_int(cret, 5);
1664 uret[0]->usernum = extract_long(cret, 6);
1665 uret[0]->lastcall = extract_long(cret, 7);
1666 uret[0]->USuserpurge = extract_int(cret, 8);
1673 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct usersupp *uret, char *cret)
1678 if (!cret) return -2;
1679 if (!uret) return -2;
1681 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1682 if (!aaa) return -1;
1684 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1685 uret->fullname, uret->password, uret->flags,
1686 uret->timescalled, uret->posted, uret->axlevel,
1687 uret->usernum, uret->lastcall, uret->USuserpurge);
1688 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1695 /* which is 0 = room, 1 = floor, 2 = site */
1696 /* caller must free the struct ExpirePolicy */
1697 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1698 struct ExpirePolicy **policy, char *cret)
1700 static char *proto[] = {"room", "floor", "site"};
1704 if (!cret) return -2;
1705 if (!policy) return -2;
1706 if (!*policy) *policy = (struct ExpirePolicy *)calloc(1, sizeof(struct ExpirePolicy));
1707 if (!*policy) return -1;
1708 if (which < 0 || which > 2) return -2;
1710 sprintf(aaa, "GPEX %s", proto[which]);
1711 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1712 if (ret / 100 == 2) {
1713 policy[0]->expire_mode = extract_int(cret, 0);
1714 policy[0]->expire_value = extract_int(cret, 1);
1722 /* which is 0 = room, 1 = floor, 2 = site */
1723 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1724 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1725 struct ExpirePolicy *policy, char *cret)
1729 if (!cret) return -2;
1730 if (which < 0 || which > 2) return -2;
1731 if (!policy) return -2;
1732 if (policy->expire_mode < 0 || policy->expire_mode > 3) return -2;
1733 if (policy->expire_mode >= 2 && policy->expire_value < 1) return -2;
1735 sprintf(aaa, "SPEX %d|%d|%d", which,
1736 policy->expire_mode, policy->expire_value);
1737 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1742 int CtdlGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1746 if (!cret) return -2;
1747 if (!listing) return -2;
1748 if (*listing) return -2;
1750 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1751 listing, &bytes, cret);
1756 int CtdlSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1758 if (!cret) return -2;
1759 if (!listing) return -2;
1761 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1767 int CtdlGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1768 char **listing, char *cret)
1773 if (!cret) return -2;
1774 if (!mimetype) return -2;
1775 if (!listing) return -2;
1776 if (*listing) return -2;
1778 aaa = malloc(strlen(mimetype) + 13);
1779 if (!aaa) return -1;
1780 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1781 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1782 listing, &bytes, cret);
1787 int CtdlSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1788 const char *listing, char *cret)
1792 if (!cret) return -2;
1793 if (!mimetype) return -2;
1794 if (!listing) return -2;
1796 aaa = malloc(strlen(mimetype) + 13);
1797 if (!aaa) return -1;
1798 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1799 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1804 int CtdlIPCModerateMessage(CtdlIPC *ipc, long msgnum, int level, char *cret)
1808 if (!cret) return -2;
1809 if (!msgnum) return -2;
1811 sprintf(aaa, "MMOD %ld|%d", msgnum, level);
1812 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1817 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1821 if (!cret) return -2;
1822 if (session < 0) return -2;
1824 sprintf(aaa, "REQT %d", session);
1825 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1830 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1834 if (!cret) return -2;
1835 if (msgnum < 0) return -2;
1837 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1838 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1843 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1852 /* New SSL object */
1853 temp_ssl = SSL_new(ssl_ctx);
1855 error_printf("SSL_new failed: %s\n",
1856 ERR_reason_error_string(ERR_get_error()));
1859 /* Pointless flag waving */
1860 #if SSLEAY_VERSION_NUMBER >= 0x0922
1861 SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
1864 if (!access("/var/run/egd-pool", F_OK))
1865 RAND_egd("/var/run/egd-pool");
1867 if (!RAND_status()) {
1868 error_printf("PRNG not properly seeded\n");
1872 /* Associate network connection with SSL object */
1873 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1874 error_printf("SSL_set_fd failed: %s\n",
1875 ERR_reason_error_string(ERR_get_error()));
1879 if (status_hook != NULL)
1880 status_hook("Requesting encryption...\r");
1882 /* Ready to start SSL/TLS */
1884 CtdlIPC_putline(ipc, "STLS");
1885 CtdlIPC_getline(ipc, buf);
1886 if (buf[0] != '2') {
1887 error_printf("Server can't start TLS: %s\n", buf);
1891 r = CtdlIPCGenericCommand(ipc,
1892 "STLS", NULL, 0, NULL, NULL, cret);
1894 error_printf("Server can't start TLS: %s\n", buf);
1899 /* Do SSL/TLS handshake */
1900 if ((a = SSL_connect(temp_ssl)) < 1) {
1901 error_printf("SSL_connect failed: %s\n",
1902 ERR_reason_error_string(ERR_get_error()));
1906 ipc->ssl = temp_ssl;
1908 BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1912 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1913 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1914 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1915 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1921 #endif /* HAVE_OPENSSL */
1926 static void endtls(SSL *ssl)
1937 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1941 if (!address) return -2;
1942 if (!cret) return -2;
1944 aaa = (char *)malloc(strlen(address) + 6);
1945 if (!aaa) return -1;
1947 sprintf(aaa, "QDIR %s", address);
1948 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1953 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1957 if (!cret) return -2;
1958 sprintf(aaa, "IPGM %d", secret);
1959 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1980 /* ************************************************************************** */
1981 /* Stuff below this line is not for public consumption */
1982 /* ************************************************************************** */
1985 inline void CtdlIPC_lock(CtdlIPC *ipc)
1987 #ifdef THREADED_CLIENT
1988 pthread_mutex_lock(&(ipc->mutex));
1993 inline void CtdlIPC_unlock(CtdlIPC *ipc)
1995 #ifdef THREADED_CLIENT
1996 pthread_mutex_unlock(&(ipc->mutex));
2001 /* Read a listing from the server up to 000. Append to dest if it exists */
2002 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
2011 length = strlen(ret);
2018 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2019 linelength = strlen(aaa);
2020 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
2022 strcpy(&ret[length], aaa);
2023 length += linelength;
2024 strcpy(&ret[length++], "\n");
2032 /* Send a listing to the server; generate the ending 000. */
2033 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
2037 text = (char *)malloc(strlen(listing) + 6);
2039 strcpy(text, listing);
2040 while (text[strlen(text) - 1] == '\n')
2041 text[strlen(text) - 1] = '\0';
2042 strcat(text, "\n000");
2043 CtdlIPC_putline(ipc, text);
2047 /* Malloc failed but we are committed to send */
2048 /* This may result in extra blanks at the bottom */
2049 CtdlIPC_putline(ipc, text);
2050 CtdlIPC_putline(ipc, "000");
2056 /* Partial read of file from server */
2057 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2059 register size_t len = 0;
2062 if (!buf) return -1;
2063 if (!cret) return -1;
2064 if (bytes < 1) return -1;
2065 if (offset < 0) return -1;
2068 sprintf(aaa, "READ %d|%d", (int)offset, (int)bytes);
2069 CtdlIPC_putline(ipc, aaa);
2070 CtdlIPC_getline(ipc, aaa);
2072 strcpy(cret, &aaa[4]);
2074 len = extract_long(&aaa[4], 0);
2075 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2077 /* I know what I'm doing */
2078 serv_read(ipc, (*buf + offset), len);
2080 /* We have to read regardless */
2081 serv_read(ipc, aaa, len);
2085 CtdlIPC_unlock(ipc);
2091 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2095 if (!cret) return -2;
2096 if (!ipc->downloading) return -2;
2098 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2100 ipc->downloading = 0;
2106 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2110 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2111 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2118 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
2119 void (*progress_gauge_callback)(long, long), char *cret)
2121 register size_t len;
2123 if (!cret) return -1;
2124 if (!buf) return -1;
2125 if (*buf) return -1;
2126 if (!ipc->downloading) return -1;
2129 if (progress_gauge_callback)
2130 progress_gauge_callback(len, bytes);
2131 while (len < bytes) {
2132 register size_t block;
2134 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2140 if (progress_gauge_callback)
2141 progress_gauge_callback(len, bytes);
2147 /* READ - pipelined */
2148 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2149 size_t resume, void (*progress_gauge_callback)(long, long),
2152 register size_t len;
2153 register int calls; /* How many calls in the pipeline */
2154 register int i; /* iterator */
2157 if (!cret) return -1;
2158 if (!buf) return -1;
2159 if (*buf) return -1;
2160 if (!ipc->downloading) return -1;
2162 *buf = (void *)realloc(*buf, bytes - resume);
2163 if (!*buf) return -1;
2167 if (progress_gauge_callback)
2168 progress_gauge_callback(len, bytes);
2170 /* How many calls will be in the pipeline? */
2171 calls = (bytes - resume) / 4096;
2172 if ((bytes - resume) % 4096) calls++;
2174 /* Send all requests at once */
2175 for (i = 0; i < calls; i++) {
2176 sprintf(aaa, "READ %d|4096", (int)(i * 4096 + resume) );
2177 CtdlIPC_putline(ipc, aaa);
2180 /* Receive all responses at once */
2181 for (i = 0; i < calls; i++) {
2182 CtdlIPC_getline(ipc, aaa);
2184 strcpy(cret, &aaa[4]);
2186 len = extract_long(&aaa[4], 0);
2187 /* I know what I'm doing */
2188 serv_read(ipc, ((*buf) + (i * 4096)), len);
2190 if (progress_gauge_callback)
2191 progress_gauge_callback(i * 4096 + len, bytes);
2193 CtdlIPC_unlock(ipc);
2199 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2204 if (!cret) return -1;
2205 if (!ipc->uploading) return -1;
2207 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2208 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2215 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2216 void (*progress_gauge_callback)(long, long), char *cret)
2218 register int ret = -1;
2219 register size_t offset = 0;
2225 if (!cret) return -1;
2226 if (!path) return -1;
2227 if (!*path) return -1;
2229 fd = fopen(path, "r");
2232 fseek(fd, 0L, SEEK_END);
2236 if (progress_gauge_callback)
2237 progress_gauge_callback(0, bytes);
2239 while (offset < bytes) {
2240 register size_t to_write;
2242 /* Read some data in */
2243 to_write = fread(buf, 1, 4096, fd);
2245 if (feof(fd) || ferror(fd)) break;
2247 sprintf(aaa, "WRIT %d", (int)to_write);
2248 CtdlIPC_putline(ipc, aaa);
2249 CtdlIPC_getline(ipc, aaa);
2250 strcpy(cret, &aaa[4]);
2252 if (aaa[0] == '7') {
2253 to_write = extract_long(&aaa[4], 0);
2255 serv_write(ipc, buf, to_write);
2257 if (progress_gauge_callback)
2258 progress_gauge_callback(offset, bytes);
2259 /* Detect short reads and back up if needed */
2260 fseek(fd, offset, SEEK_SET);
2265 if (progress_gauge_callback)
2266 progress_gauge_callback(1, 1);
2267 return (!ferror(fd) ? ret : -2);
2272 * Generic command method. This method should handle any server command
2273 * except for CHAT. It takes the following arguments:
2275 * ipc The server to speak with
2276 * command Preformatted command to send to server
2277 * to_send A text or binary file to send to server
2278 * (only sent if server requests it)
2279 * bytes_to_send The number of bytes in to_send (required if
2280 * sending binary, optional if sending listing)
2281 * to_receive Pointer to a NULL pointer, if the server
2282 * sends text or binary we will allocate memory
2283 * for the file and stuff it here
2284 * bytes_to_receive If a file is received, we will store its
2286 * proto_response The protocol response. Caller must provide
2287 * this buffer and ensure that it is at least
2288 * 128 bytes in length.
2290 * This function returns a number equal to the protocol response number,
2291 * -1 if an internal error occurred, -2 if caller provided bad values,
2292 * or 0 - the protocol response number if bad values were found during
2293 * the protocol exchange.
2294 * It stores the protocol response string (minus the number) in
2295 * protocol_response as described above. Some commands send additional
2296 * data in this string.
2298 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2299 const char *command, const char *to_send,
2300 size_t bytes_to_send, char **to_receive,
2301 size_t *bytes_to_receive, char *proto_response)
2307 if (!command) return -2;
2308 if (!proto_response) return -2;
2311 if (ipc->ssl) watch_ssl = 1;
2315 CtdlIPC_putline(ipc, command);
2317 CtdlIPC_getline(ipc, proto_response);
2318 if (proto_response[3] == '*')
2320 ret = atoi(proto_response);
2321 strcpy(proto_response, &proto_response[4]);
2322 switch (ret / 100) {
2323 default: /* Unknown, punt */
2325 case 3: /* MORE_DATA */
2327 /* Don't need to do anything */
2329 case 1: /* LISTING_FOLLOWS */
2330 if (to_receive && !*to_receive && bytes_to_receive) {
2331 *to_receive = CtdlIPCReadListing(ipc, NULL);
2332 } else { /* Drain */
2333 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2337 case 4: /* SEND_LISTING */
2339 CtdlIPCSendListing(ipc, to_send);
2341 /* No listing given, fake it */
2342 CtdlIPC_putline(ipc, "000");
2346 case 6: /* BINARY_FOLLOWS */
2347 if (to_receive && !*to_receive && bytes_to_receive) {
2349 extract_long(proto_response, 0);
2350 *to_receive = (char *)
2351 malloc((size_t)*bytes_to_receive);
2355 serv_read(ipc, *to_receive,
2362 drain = extract_long(proto_response, 0);
2363 while (drain > SIZ) {
2364 serv_read(ipc, buf, SIZ);
2367 serv_read(ipc, buf, drain);
2371 case 7: /* SEND_BINARY */
2372 if (to_send && bytes_to_send) {
2373 serv_write(ipc, to_send, bytes_to_send);
2374 } else if (bytes_to_send) {
2375 /* Fake it, send nulls */
2378 fake = bytes_to_send;
2379 memset(buf, '\0', SIZ);
2380 while (fake > SIZ) {
2381 serv_write(ipc, buf, SIZ);
2384 serv_write(ipc, buf, fake);
2386 } /* else who knows? DANGER WILL ROBINSON */
2388 case 8: /* START_CHAT_MODE */
2389 if (!strncasecmp(command, "CHAT", 4)) {
2390 /* Don't call chatmode with generic! */
2391 CtdlIPC_putline(ipc, "/quit");
2394 /* In this mode we send then receive listing */
2396 CtdlIPCSendListing(ipc, to_send);
2398 /* No listing given, fake it */
2399 CtdlIPC_putline(ipc, "000");
2402 if (to_receive && !*to_receive
2403 && bytes_to_receive) {
2404 *to_receive = CtdlIPCReadListing(ipc, NULL);
2405 } else { /* Drain */
2406 while (CtdlIPC_getline(ipc, buf),
2407 strcmp(buf, "000")) ;
2412 case 9: /* ASYNC_MSG */
2413 /* CtdlIPCDoAsync(ret, proto_response); */
2414 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2420 CtdlIPC_unlock(ipc);
2425 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2427 struct hostent *phe;
2428 struct servent *pse;
2429 struct protoent *ppe;
2430 struct sockaddr_in sin;
2433 memset(&sin, 0, sizeof(sin));
2434 sin.sin_family = AF_INET;
2436 pse = getservbyname(service, protocol);
2438 sin.sin_port = pse->s_port;
2440 else if (atoi(service) > 0) {
2441 sin.sin_port = htons(atoi(service));
2444 sin.sin_port = htons(defaultPort);
2446 phe = gethostbyname(host);
2448 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2449 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2452 if ((ppe = getprotobyname(protocol)) == 0) {
2455 if (!strcmp(protocol, "udp")) {
2461 s = socket(PF_INET, type, ppe->p_proto);
2466 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2473 static int uds_connectsock(int *isLocal, char *sockpath)
2475 struct sockaddr_un addr;
2478 memset(&addr, 0, sizeof(addr));
2479 addr.sun_family = AF_UNIX;
2480 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2482 s = socket(AF_UNIX, SOCK_STREAM, 0);
2487 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2497 * input binary data from socket
2499 static void serv_read(CtdlIPC *ipc, char *buf, int bytes)
2503 #if defined(HAVE_OPENSSL)
2505 serv_read_ssl(ipc, buf, bytes);
2510 while (len < bytes) {
2511 rlen = read(ipc->sock, &buf[len], bytes - len);
2513 connection_died(ipc);
2522 * send binary to server
2524 static void serv_write(CtdlIPC *ipc, const char *buf, int nbytes)
2526 int bytes_written = 0;
2529 #if defined(HAVE_OPENSSL)
2531 serv_write_ssl(ipc, buf, nbytes);
2535 while (bytes_written < nbytes) {
2536 retval = write(ipc->sock, &buf[bytes_written],
2537 nbytes - bytes_written);
2539 connection_died(ipc);
2542 bytes_written += retval;
2549 * input binary data from encrypted connection
2551 static void serv_read_ssl(CtdlIPC* ipc, char *buf, int bytes)
2557 while (len < bytes) {
2558 if (SSL_want_read(ipc->ssl)) {
2559 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2560 error_printf("SSL_write in serv_read:\n");
2561 ERR_print_errors_fp(stderr);
2564 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2568 errval = SSL_get_error(ipc->ssl, rlen);
2569 if (errval == SSL_ERROR_WANT_READ ||
2570 errval == SSL_ERROR_WANT_WRITE) {
2574 if (errval == SSL_ERROR_ZERO_RETURN ||
2575 errval == SSL_ERROR_SSL) {
2576 serv_read(ipc, &buf[len], bytes - len);
2579 error_printf("SSL_read in serv_read:\n");
2580 ERR_print_errors_fp(stderr);
2581 connection_died(NULL);
2590 * send binary to server encrypted
2592 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, int nbytes)
2594 int bytes_written = 0;
2598 while (bytes_written < nbytes) {
2599 if (SSL_want_write(ipc->ssl)) {
2600 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2601 error_printf("SSL_read in serv_write:\n");
2602 ERR_print_errors_fp(stderr);
2605 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2606 nbytes - bytes_written);
2610 errval = SSL_get_error(ipc->ssl, retval);
2611 if (errval == SSL_ERROR_WANT_READ ||
2612 errval == SSL_ERROR_WANT_WRITE) {
2616 if (errval == SSL_ERROR_ZERO_RETURN ||
2617 errval == SSL_ERROR_SSL) {
2618 serv_write(ipc, &buf[bytes_written],
2619 nbytes - bytes_written);
2622 error_printf("SSL_write in serv_write:\n");
2623 ERR_print_errors_fp(stderr);
2624 connection_died(NULL);
2627 bytes_written += retval;
2632 static void CtdlIPC_init_OpenSSL(void)
2635 SSL_METHOD *ssl_method;
2638 /* already done init */
2646 SSL_load_error_strings();
2647 SSLeay_add_ssl_algorithms();
2649 /* Set up the SSL context in which we will oeprate */
2650 ssl_method = SSLv23_client_method();
2651 ssl_ctx = SSL_CTX_new(ssl_method);
2653 error_printf("SSL_CTX_new failed: %s\n",
2654 ERR_reason_error_string(ERR_get_error()));
2657 /* Any reasonable cipher we can get */
2658 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2659 error_printf("No ciphers available for encryption\n");
2662 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2664 /* Load DH parameters into the context */
2667 error_printf("Can't allocate a DH object: %s\n",
2668 ERR_reason_error_string(ERR_get_error()));
2671 if (!(BN_hex2bn(&(dh->p), DH_P))) {
2672 error_printf("Can't assign DH_P: %s\n",
2673 ERR_reason_error_string(ERR_get_error()));
2677 if (!(BN_hex2bn(&(dh->g), DH_G))) {
2678 error_printf("Can't assign DH_G: %s\n",
2679 ERR_reason_error_string(ERR_get_error()));
2684 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2687 #ifdef THREADED_CLIENT
2688 /* OpenSSL requires callbacks for threaded clients */
2689 CRYPTO_set_locking_callback(ssl_lock);
2690 CRYPTO_set_id_callback(id_callback);
2692 /* OpenSSL requires us to do semaphores for threaded clients */
2693 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2695 perror("malloc failed");
2698 for (a = 0; a < CRYPTO_num_locks(); a++) {
2699 Critters[a] = malloc(sizeof (pthread_mutex_t));
2701 perror("malloc failed");
2704 pthread_mutex_init(Critters[a], NULL);
2707 #endif /* THREADED_CLIENT */
2711 static void ssl_lock(int mode, int n, const char *file, int line)
2713 #ifdef THREADED_CLIENT
2714 if (mode & CRYPTO_LOCK)
2715 pthread_mutex_lock(Critters[n]);
2717 pthread_mutex_unlock(Critters[n]);
2718 #endif /* THREADED_CLIENT */
2721 #ifdef THREADED_CLIENT
2722 static unsigned long id_callback(void) {
2723 return (unsigned long)pthread_self();
2725 #endif /* THREADED_CLIENT */
2726 #endif /* HAVE_OPENSSL */
2730 * input string from socket - implemented in terms of serv_read()
2732 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2736 /* Read one character at a time. */
2738 serv_read(ipc, &buf[i], 1);
2739 if (buf[i] == '\n' || i == (SIZ-1))
2743 /* If we got a long line, discard characters until the newline. */
2745 while (buf[i] != '\n')
2746 serv_read(ipc, &buf[i], 1);
2748 /* Strip the trailing newline.
2755 * send line to server - implemented in terms of serv_write()
2757 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2759 /* error_printf("< %s\n", buf); */
2760 serv_write(ipc, buf, strlen(buf));
2761 serv_write(ipc, "\n", 1);
2763 ipc->last_command_sent = time(NULL);
2770 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2777 CtdlIPC *ipc = ialloc(CtdlIPC);
2781 #if defined(HAVE_OPENSSL)
2783 CtdlIPC_init_OpenSSL();
2785 #if defined(HAVE_PTHREAD_H)
2786 pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2788 ipc->sock = -1; /* Not connected */
2789 ipc->isLocal = 0; /* Not local, of course! */
2790 ipc->downloading = 0;
2792 ipc->last_command_sent = 0L;
2794 strcpy(cithost, DEFAULT_HOST); /* default host */
2795 strcpy(citport, DEFAULT_PORT); /* default port */
2797 for (a = 0; a < argc; ++a) {
2800 } else if (a == 1) {
2801 strcpy(cithost, argv[a]);
2802 } else if (a == 2) {
2803 strcpy(citport, argv[a]);
2805 error_printf("%s: usage: ",argv[0]);
2806 error_printf("%s [host] [port] ",argv[0]);
2813 if ((!strcmp(cithost, "localhost"))
2814 || (!strcmp(cithost, "127.0.0.1"))) {
2818 /* If we're using a unix domain socket we can do a bunch of stuff */
2819 if (!strcmp(cithost, UDS)) {
2820 snprintf(sockpath, sizeof sockpath, BBSDIR "/citadel.socket");
2821 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2822 if (ipc->sock == -1) {
2826 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2827 if (portbuf != NULL) strcpy(portbuf, sockpath);
2831 ipc->sock = connectsock(cithost, citport, "tcp", 504);
2832 if (ipc->sock == -1) {
2836 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2837 if (portbuf != NULL) strcpy(portbuf, citport);
2842 * return the file descriptor of the server socket so we can select() on it.
2844 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2847 int CtdlIPC_getsockfd(CtdlIPC* ipc)
2854 * return one character
2856 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2859 char CtdlIPC_get(CtdlIPC* ipc)
2864 serv_read(ipc, buf, 1);