16 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
29 #ifdef THREADED_CLIENT
33 #include "citadel_ipc.h"
34 #include "citadel_decls.h"
37 #ifdef THREADED_CLIENT
38 pthread_mutex_t rwlock;
42 static SSL_CTX *ssl_ctx;
45 #ifdef THREADED_CLIENT
46 pthread_mutex_t **Critters; /* Things that need locking */
47 #endif /* THREADED_CLIENT */
49 #endif /* HAVE_OPENSSL */
52 #define INADDR_NONE 0xffffffff
55 static void (*status_hook)(char *s) = NULL;
57 void setCryptoStatusHook(void (*hook)(char *s)) {
61 void CtdlIPC_SetNetworkStatusCallback(CtdlIPC *ipc, void (*hook)(int state)) {
62 ipc->network_status_cb = hook;
66 char express_msgs = 0;
69 static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes);
70 static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes);
72 static void serv_read_ssl(CtdlIPC *ipc, char *buf, unsigned int bytes);
73 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned 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);
302 mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
303 mptr->march_access = (char) extract_int(aaa, 5);
310 while (mptr2->next != NULL)
323 /* Caller must free the struct ctdluser; caller may pass an existing one */
324 int CtdlIPCGetConfig(CtdlIPC *ipc, struct ctdluser **uret, char *cret)
328 if (!cret) return -2;
329 if (!uret) return -2;
330 if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof (struct ctdluser));
331 if (!*uret) return -1;
333 ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
334 if (ret / 100 == 2) {
335 uret[0]->USscreenwidth = extract_int(cret, 0);
336 uret[0]->USscreenheight = extract_int(cret, 1);
337 uret[0]->flags = extract_int(cret, 2);
344 int CtdlIPCSetConfig(CtdlIPC *ipc, struct ctdluser *uret, char *cret)
348 if (!uret) return -2;
349 if (!cret) return -2;
351 sprintf(aaa, "SETU %d|%d|%d",
352 uret->USscreenwidth, uret->USscreenheight,
354 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
359 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
360 struct ctdlipcroom **rret, char *cret)
365 if (!cret) return -2;
366 if (!rret) return -2;
367 if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
368 if (!*rret) return -1;
371 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
376 sprintf(aaa, "GOTO %s|%s", room, passwd);
378 aaa = (char *)malloc(strlen(room) + 6);
383 sprintf(aaa, "GOTO %s", room);
385 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
386 if (ret / 100 == 2) {
387 extract(rret[0]->RRname, cret, 0);
388 rret[0]->RRunread = extract_long(cret, 1);
389 rret[0]->RRtotal = extract_long(cret, 2);
390 rret[0]->RRinfoupdated = extract_int(cret, 3);
391 rret[0]->RRflags = extract_int(cret, 4);
392 rret[0]->RRhighest = extract_long(cret, 5);
393 rret[0]->RRlastread = extract_long(cret, 6);
394 rret[0]->RRismailbox = extract_int(cret, 7);
395 rret[0]->RRaide = extract_int(cret, 8);
396 rret[0]->RRnewmail = extract_long(cret, 9);
397 rret[0]->RRfloor = extract_int(cret, 10);
406 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
407 /* whicharg is number of messages, applies to last, first, gt, lt */
408 int CtdlIPCGetMessages(CtdlIPC *ipc, enum MessageList which, int whicharg,
409 const char *mtemplate, unsigned long **mret, char *cret)
412 register unsigned long count = 0;
413 static char *proto[] =
414 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
419 if (!cret) return -2;
420 if (!mret) return -2;
421 if (*mret) return -2;
422 if (which < 0 || which > 6) return -2;
425 sprintf(aaa, "MSGS %s||%d", proto[which],
426 (mtemplate) ? 1 : 0);
428 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
429 (mtemplate) ? 1 : 0);
430 if (mtemplate) count = strlen(mtemplate);
431 ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbbsize, cret);
435 *mret = (unsigned long *)calloc(1, sizeof(unsigned long));
438 while (bbb && strlen(bbb)) {
439 extract_token(aaa, bbb, 0, '\n');
440 remove_token(bbb, 0, '\n');
441 *mret = (unsigned long *)realloc(*mret, (size_t)((count + 2) *
442 sizeof (unsigned long)));
444 (*mret)[count++] = atol(aaa);
456 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
457 struct ctdlipcmessage **mret, char *cret)
463 int multipart_hunting = 0;
464 char multipart_prefix[SIZ];
466 if (!cret) return -1;
467 if (!mret) return -1;
468 if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
469 if (!*mret) return -1;
470 if (!msgnum) return -1;
472 strcpy(mret[0]->content_type, "");
473 sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
474 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbbsize, cret);
475 if (ret / 100 == 1) {
477 strcpy(mret[0]->mime_chosen, "1"); /* Default chosen-part is "1" */
478 while (strlen(bbb) > 4 && bbb[4] == '=') {
479 extract_token(aaa, bbb, 0, '\n');
480 remove_token(bbb, 0, '\n');
482 if (!strncasecmp(aaa, "nhdr=yes", 8))
484 else if (!strncasecmp(aaa, "from=", 5))
485 safestrncpy(mret[0]->author, &aaa[5], SIZ);
486 else if (!strncasecmp(aaa, "type=", 5))
487 mret[0]->type = atoi(&aaa[5]);
488 else if (!strncasecmp(aaa, "msgn=", 5))
489 safestrncpy(mret[0]->msgid, &aaa[5], SIZ);
490 else if (!strncasecmp(aaa, "subj=", 5))
491 safestrncpy(mret[0]->subject, &aaa[5], SIZ);
492 else if (!strncasecmp(aaa, "rfca=", 5))
493 safestrncpy(mret[0]->email, &aaa[5], SIZ);
494 else if (!strncasecmp(aaa, "hnod=", 5))
495 safestrncpy(mret[0]->hnod, &aaa[5], SIZ);
496 else if (!strncasecmp(aaa, "room=", 5))
497 safestrncpy(mret[0]->room, &aaa[5], SIZ);
498 else if (!strncasecmp(aaa, "node=", 5))
499 safestrncpy(mret[0]->node, &aaa[5], SIZ);
500 else if (!strncasecmp(aaa, "rcpt=", 5))
501 safestrncpy(mret[0]->recipient, &aaa[5], SIZ);
502 else if (!strncasecmp(aaa, "time=", 5))
503 mret[0]->time = atol(&aaa[5]);
505 /* Multipart/alternative prefix & suffix strings help
506 * us to determine which part we want to download.
508 else if (!strncasecmp(aaa, "pref=", 5)) {
509 extract(multipart_prefix, &aaa[5], 1);
510 if (!strcasecmp(multipart_prefix,
511 "multipart/alternative")) {
515 else if (!strncasecmp(aaa, "suff=", 5)) {
516 extract(multipart_prefix, &aaa[5], 1);
517 if (!strcasecmp(multipart_prefix,
518 "multipart/alternative")) {
523 else if (!strncasecmp(aaa, "part=", 5)) {
524 struct parts *ptr, *chain;
526 ptr = (struct parts *)calloc(1, sizeof (struct parts));
529 /* Fill the buffers for the caller */
530 extract(ptr->name, &aaa[5], 0);
531 extract(ptr->filename, &aaa[5], 1);
532 extract(ptr->number, &aaa[5], 2);
533 extract(ptr->disposition, &aaa[5], 3);
534 extract(ptr->mimetype, &aaa[5], 4);
535 ptr->length = extract_long(&aaa[5], 5);
536 if (!mret[0]->attachments)
537 mret[0]->attachments = ptr;
539 chain = mret[0]->attachments;
545 /* Now handle multipart/alternative */
546 if (multipart_hunting > 0) {
547 if ( (!strcasecmp(ptr->mimetype,
549 || (!strcasecmp(ptr->mimetype,
551 strcpy(mret[0]->mime_chosen,
559 /* Eliminate "text\n" */
560 remove_token(bbb, 0, '\n');
562 /* If doing a MIME thing, pull out the extra headers */
565 if (!strncasecmp(bbb, "Content-type: ", 14)) {
566 extract_token(mret[0]->content_type, bbb, 0, '\n');
567 strcpy(mret[0]->content_type,
568 &mret[0]->content_type[14]);
569 striplt(mret[0]->content_type);
571 remove_token(bbb, 0, '\n');
572 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
573 remove_token(bbb, 0, '\n');
579 /* FIXME: Strip trailing whitespace */
580 bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
582 bbb = (char *)realloc(bbb, 1);
592 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
597 if (!cret) return -2;
598 if (!listing) return -2;
599 if (*listing) return -2;
601 ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
607 int CtdlIPCServerInfo(CtdlIPC *ipc, struct CtdlServInfo *ServInfo, char *cret)
611 char *listing = NULL;
614 if (!cret) return -2;
615 if (!ServInfo) return -2;
617 ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
618 if (ret / 100 == 1) {
621 while (*listing && strlen(listing)) {
622 extract_token(buf, listing, 0, '\n');
623 remove_token(listing, 0, '\n');
625 case 0: ServInfo->serv_pid = atoi(buf);
627 case 1: strcpy(ServInfo->serv_nodename,buf);
629 case 2: strcpy(ServInfo->serv_humannode,buf);
631 case 3: strcpy(ServInfo->serv_fqdn,buf);
633 case 4: strcpy(ServInfo->serv_software,buf);
635 case 5: ServInfo->serv_rev_level = atoi(buf);
637 case 6: strcpy(ServInfo->serv_bbs_city,buf);
639 case 7: strcpy(ServInfo->serv_sysadm,buf);
641 case 9: strcpy(ServInfo->serv_moreprompt,buf);
643 case 10: ServInfo->serv_ok_floors = atoi(buf);
645 case 11: ServInfo->serv_paging_level = atoi(buf);
647 case 13: ServInfo->serv_supports_qnop = atoi(buf);
648 case 14: ServInfo->serv_supports_ldap = atoi(buf);
659 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
664 if (!cret) return -2;
665 if (!listing) return -2;
666 if (*listing) return -2;
668 ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
674 * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
676 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
681 if (!cret) return -2;
684 sprintf(aaa, "SLRP %ld", msgnum);
687 sprintf(aaa, "SLRP HIGHEST");
689 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
695 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
700 if (!cret) return -2;
701 if (!username) return -2;
703 aaa = (char *)malloc(strlen(username) + 6);
706 sprintf(aaa, "INVT %s", username);
707 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
714 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
719 if (!cret) return -1;
720 if (!username) return -1;
722 aaa = (char *)malloc(strlen(username) + 6);
724 sprintf(aaa, "KICK %s", username);
725 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
732 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct ctdlroom **qret, char *cret)
736 if (!cret) return -2;
737 if (!qret) return -2;
738 if (!*qret) *qret = (struct ctdlroom *)calloc(1, sizeof (struct ctdlroom));
739 if (!*qret) return -1;
741 ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
742 if (ret / 100 == 2) {
743 extract(qret[0]->QRname, cret, 0);
744 extract(qret[0]->QRpasswd, cret, 1);
745 extract(qret[0]->QRdirname, cret, 2);
746 qret[0]->QRflags = extract_int(cret, 3);
747 qret[0]->QRfloor = extract_int(cret, 4);
748 qret[0]->QRorder = extract_int(cret, 5);
749 qret[0]->QRdefaultview = extract_int(cret, 6);
750 qret[0]->QRflags2 = extract_int(cret, 7);
757 /* set forget to kick all users out of room */
758 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct ctdlroom *qret, char *cret)
763 if (!cret) return -2;
764 if (!qret) return -2;
766 aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
767 strlen(qret->QRdirname) + 52);
770 sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d",
771 qret->QRname, qret->QRpasswd, qret->QRdirname,
772 qret->QRflags, forget, qret->QRfloor, qret->QRorder);
773 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
780 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
782 if (!cret) return -1;
784 return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
789 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
794 if (!cret) return -2;
795 if (!username) return -2;
797 aaa = (char *)malloc(strlen(username) + 6);
800 sprintf(aaa, "SETA %s", username);
801 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
808 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
813 if (!cret) return -2;
816 snprintf(cmd, sizeof cmd,
817 "ENT0 %d|%s|%d|%d|%s|%s", flag, mr->recipient,
818 mr->anonymous, mr->type, mr->subject, mr->author);
819 ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL,
826 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
830 if (!cret) return -2;
831 if (!iret) return -2;
832 if (*iret) return -2;
834 return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
839 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
843 if (!cret) return -2;
844 if (!msgnum) return -2;
846 sprintf(aaa, "DELE %ld", msgnum);
847 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
852 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
857 if (!cret) return -2;
858 if (!destroom) return -2;
859 if (!msgnum) return -2;
861 aaa = (char *)malloc(strlen(destroom) + 28);
864 sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
865 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
872 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
876 if (!cret) return -2;
878 sprintf(aaa, "KILL %d", for_real);
879 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
884 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
885 const char *password, int floor, char *cret)
890 if (!cret) return -2;
891 if (!roomname) return -2;
894 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
896 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
899 aaa = (char *)malloc(strlen(roomname) + 40);
901 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
904 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
911 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
913 if (!cret) return -2;
915 return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
920 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
926 if (!cret) return -2;
927 if (!mret) return -2;
928 if (*mret) return -2;
929 if (!message) return -2;
931 aaa = (char *)malloc(strlen(message) + 6);
934 sprintf(aaa, "MESG %s", message);
935 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
942 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
944 if (!cret) return -2;
946 return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
951 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
957 if (!cret) return -2;
958 if (!rret) return -2;
959 if (*rret) return -2;
962 aaa = (char *)malloc(strlen(username) + 6);
964 aaa = (char *)malloc(12);
968 sprintf(aaa, "GREG %s", username);
970 sprintf(aaa, "GREG _SELF_");
971 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
978 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
983 if (!cret) return -2;
984 if (!username) return -2;
985 if (axlevel < 0 || axlevel > 7) return -2;
987 aaa = (char *)malloc(strlen(username) + 17);
990 sprintf(aaa, "VALI %s|%d", username, axlevel);
991 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
998 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
1002 if (!cret) return -1;
1003 if (!info) return -1;
1005 sprintf(aaa, "EINF %d", for_real);
1006 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1011 int CtdlIPCUserListing(CtdlIPC *ipc, char **listing, char *cret)
1015 if (!cret) return -1;
1016 if (!listing) return -1;
1017 if (*listing) return -1;
1019 return CtdlIPCGenericCommand(ipc, "LIST", NULL, 0, listing, &bytes, cret);
1024 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
1026 if (!cret) return -1;
1027 if (!info) return -1;
1029 return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
1035 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1039 if (!cret) return -1;
1040 if (!chek) return -1;
1042 ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1043 if (ret / 100 == 2) {
1044 chek->newmail = extract_long(cret, 0);
1045 chek->needregis = extract_int(cret, 1);
1046 chek->needvalid = extract_int(cret, 2);
1053 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1058 if (!cret) return -2;
1059 if (!filename) return -2;
1061 aaa = (char *)malloc(strlen(filename) + 6);
1062 if (!aaa) return -1;
1064 sprintf(aaa, "DELF %s", filename);
1065 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1072 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1077 if (!cret) return -2;
1078 if (!filename) return -2;
1079 if (!destroom) return -2;
1081 aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1082 if (!aaa) return -1;
1084 sprintf(aaa, "MOVF %s|%s", filename, destroom);
1085 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1092 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1097 if (!cret) return -2;
1098 if (!filename) return -2;
1099 if (!destnode) return -2;
1101 aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1102 if (!aaa) return -1;
1104 sprintf(aaa, "NETF %s|%s", filename, destnode);
1105 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1112 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1117 if (!cret) return -1;
1118 if (!listing) return -1;
1119 if (*listing) return -1;
1121 *stamp = CtdlIPCServerTime(ipc, cret);
1123 *stamp = time(NULL);
1124 ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1130 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1132 void (*progress_gauge_callback)(unsigned long, unsigned long),
1141 if (!cret) return -2;
1142 if (!filename) return -2;
1143 if (!buf) return -2;
1144 if (*buf) return -2;
1145 if (ipc->downloading) return -2;
1147 aaa = (char *)malloc(strlen(filename) + 6);
1148 if (!aaa) return -1;
1150 sprintf(aaa, "OPEN %s", filename);
1151 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1153 if (ret / 100 == 2) {
1154 ipc->downloading = 1;
1155 bytes = extract_long(cret, 0);
1156 last_mod = extract_int(cret, 1);
1157 extract(mimetype, cret, 2);
1159 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume,
1160 progress_gauge_callback, cret);
1162 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, resume,
1163 progress_gauge_callback, cret);
1166 ret = CtdlIPCEndDownload(ipc, cret);
1168 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1169 filename, mimetype);
1176 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part,
1178 void (*progress_gauge_callback)(unsigned long, unsigned long),
1188 if (!cret) return -2;
1189 if (!buf) return -2;
1190 if (*buf) return -2;
1191 if (!part) return -2;
1192 if (!msgnum) return -2;
1193 if (ipc->downloading) return -2;
1195 sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1196 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1197 if (ret / 100 == 2) {
1198 ipc->downloading = 1;
1199 bytes = extract_long(cret, 0);
1200 last_mod = extract_int(cret, 1);
1201 extract(filename, cret, 2);
1202 extract(mimetype, cret, 3);
1203 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1204 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1205 ret = CtdlIPCEndDownload(ipc, cret);
1207 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1208 filename, mimetype);
1215 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1216 void (*progress_gauge_callback)(unsigned long, unsigned long),
1225 if (!cret) return -1;
1226 if (!buf) return -1;
1227 if (*buf) return -1;
1228 if (!filename) return -1;
1229 if (ipc->downloading) return -1;
1231 aaa = (char *)malloc(strlen(filename) + 6);
1232 if (!aaa) return -1;
1234 sprintf(aaa, "OIMG %s", filename);
1235 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1237 if (ret / 100 == 2) {
1238 ipc->downloading = 1;
1239 bytes = extract_long(cret, 0);
1240 last_mod = extract_int(cret, 1);
1241 extract(mimetype, cret, 2);
1242 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1243 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1244 ret = CtdlIPCEndDownload(ipc, cret);
1246 sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1247 filename, mimetype);
1254 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1256 void (*progress_gauge_callback)(unsigned long, unsigned long),
1262 if (!cret) return -1;
1263 if (!save_as) return -1;
1264 if (!comment) return -1;
1265 if (!path) return -1;
1266 if (!*path) return -1;
1267 if (ipc->uploading) return -1;
1269 aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1270 if (!aaa) return -1;
1272 sprintf(aaa, "UOPN %s|%s", save_as, comment);
1273 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1275 if (ret / 100 == 2) {
1277 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1278 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1286 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1287 const char *save_as,
1288 void (*progress_gauge_callback)(unsigned long, unsigned long),
1294 if (!cret) return -1;
1295 if (!save_as) return -1;
1296 if (!path && for_real) return -1;
1297 if (!*path && for_real) return -1;
1298 if (ipc->uploading) return -1;
1300 aaa = (char *)malloc(strlen(save_as) + 17);
1301 if (!aaa) return -1;
1303 sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1304 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1306 if (ret / 100 == 2 && for_real) {
1308 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1309 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1317 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1322 if (!cret) return -2;
1323 if (!username) return -2;
1325 aaa = (char *)malloc(strlen(username) + 6);
1326 if (!aaa) return -1;
1328 sprintf(aaa, "QUSR %s", username);
1329 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1336 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1340 if (!cret) return -2;
1341 if (!listing) return -2;
1342 if (*listing) return -2;
1344 return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1349 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1354 if (!cret) return -2;
1355 if (!name) return -2;
1357 sprintf(aaa, "CFLR %s|%d", name, for_real);
1358 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1364 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1368 if (!cret) return -1;
1369 if (floornum < 0) return -1;
1371 sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1372 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1377 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1382 if (!cret) return -2;
1383 if (!floorname) return -2;
1384 if (floornum < 0) return -2;
1386 sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1387 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1395 * You only need to fill out hostname, the defaults will be used if any of the
1396 * other fields are not set properly.
1398 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid,
1399 int revision, const char *software_name, const char *hostname,
1405 if (developerid < 0 || clientid < 0 || revision < 0 ||
1409 revision = REV_LEVEL - 600;
1410 software_name = "Citadel/UX (libcitadel)";
1412 if (!hostname) return -2;
1414 aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1415 if (!aaa) return -1;
1417 sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1418 revision, software_name, hostname);
1419 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1426 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1432 if (!cret) return -2;
1433 if (!username) return -2;
1435 aaa = (char *)malloc(strlen(username) + 8);
1436 if (!aaa) return -1;
1439 sprintf(aaa, "SEXP %s|-", username);
1440 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1443 sprintf(aaa, "SEXP %s||", username);
1444 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1452 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1456 if (!cret) return -2;
1457 if (!listing) return -2;
1458 if (*listing) return -2;
1460 return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1465 /* mode is 0 = enable, 1 = disable, 2 = status */
1466 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1470 if (!cret) return -2;
1472 sprintf(aaa, "DEXP %d", mode);
1473 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1478 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1480 if (!cret) return -2;
1481 if (!bio) return -2;
1483 return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1489 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1495 if (!cret) return -2;
1496 if (!username) return -2;
1497 if (!listing) return -2;
1498 if (*listing) return -2;
1500 aaa = (char *)malloc(strlen(username) + 6);
1501 if (!aaa) return -1;
1503 sprintf(aaa, "RBIO %s", username);
1504 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1511 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1515 if (!cret) return -2;
1516 if (!listing) return -2;
1517 if (*listing) return -2;
1519 return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1524 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1528 if (!cret) return -1;
1530 sprintf(aaa, "STEL %d", mode ? 1 : 0);
1531 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1536 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1540 if (!cret) return -1;
1542 sprintf(aaa, "TERM %d", sid);
1543 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1548 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1550 if (!cret) return -1;
1552 return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1557 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1561 if (!cret) return -1;
1563 sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1564 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1569 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1575 if (!cret) return -2;
1576 if (!text) return -2;
1577 if (!filename) return -2;
1579 aaa = (char *)malloc(strlen(filename) + 6);
1580 if (!aaa) return -1;
1582 sprintf(aaa, "EMSG %s", filename);
1583 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1590 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1595 if (!cret) return -2;
1596 if (!hostname) return -2;
1598 aaa = (char *)malloc(strlen(hostname) + 6);
1599 if (!aaa) return -1;
1601 sprintf(aaa, "HCHG %s", hostname);
1602 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1609 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1614 if (!cret) return -2;
1615 if (!roomname) return -2;
1617 aaa = (char *)malloc(strlen(roomname) + 6);
1618 if (!aaa) return -1;
1620 sprintf(aaa, "RCHG %s", roomname);
1621 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1628 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1633 if (!cret) return -2;
1634 if (!username) return -2;
1636 aaa = (char *)malloc(strlen(username) + 6);
1637 if (!aaa) return -1;
1639 sprintf(aaa, "UCHG %s", username);
1640 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1647 /* This function returns the actual server time reported, or 0 if error */
1648 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1650 register time_t tret;
1653 ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1654 if (ret / 100 == 2) {
1655 tret = extract_long(cret, 0);
1664 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1665 struct ctdluser **uret, char *cret)
1670 if (!cret) return -2;
1671 if (!uret) return -2;
1672 if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof(struct ctdluser));
1673 if (!*uret) return -1;
1675 sprintf(aaa, "AGUP %s", who);
1676 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1678 if (ret / 100 == 2) {
1679 extract(uret[0]->fullname, cret, 0);
1680 extract(uret[0]->password, cret, 1);
1681 uret[0]->flags = extract_int(cret, 2);
1682 uret[0]->timescalled = extract_long(cret, 3);
1683 uret[0]->posted = extract_long(cret, 4);
1684 uret[0]->axlevel = extract_int(cret, 5);
1685 uret[0]->usernum = extract_long(cret, 6);
1686 uret[0]->lastcall = extract_long(cret, 7);
1687 uret[0]->USuserpurge = extract_int(cret, 8);
1694 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct ctdluser *uret, char *cret)
1699 if (!cret) return -2;
1700 if (!uret) return -2;
1702 aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1703 if (!aaa) return -1;
1705 sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1706 uret->fullname, uret->password, uret->flags,
1707 uret->timescalled, uret->posted, uret->axlevel,
1708 uret->usernum, uret->lastcall, uret->USuserpurge);
1709 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1716 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1717 /* caller must free the struct ExpirePolicy */
1718 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1719 struct ExpirePolicy **policy, char *cret)
1721 static char *proto[] = {"room", "floor", "site", "mailboxes" };
1725 if (!cret) return -2;
1726 if (!policy) return -2;
1727 if (!*policy) *policy = (struct ExpirePolicy *)calloc(1, sizeof(struct ExpirePolicy));
1728 if (!*policy) return -1;
1729 if (which < 0 || which > 3) return -2;
1731 sprintf(aaa, "GPEX %s", proto[which]);
1732 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1733 if (ret / 100 == 2) {
1734 policy[0]->expire_mode = extract_int(cret, 0);
1735 policy[0]->expire_value = extract_int(cret, 1);
1743 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1744 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1745 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1746 struct ExpirePolicy *policy, char *cret)
1749 char *whichvals[] = { "room", "floor", "site", "mailboxes" };
1751 if (!cret) return -2;
1752 if (which < 0 || which > 3) return -2;
1753 if (!policy) return -2;
1754 if (policy->expire_mode < 0 || policy->expire_mode > 3) return -2;
1755 if (policy->expire_mode >= 2 && policy->expire_value < 1) return -2;
1757 sprintf(aaa, "SPEX %s|%d|%d", whichvals[which],
1758 policy->expire_mode, policy->expire_value);
1759 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1764 int CtdlIPCGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1768 if (!cret) return -2;
1769 if (!listing) return -2;
1770 if (*listing) return -2;
1772 return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1773 listing, &bytes, cret);
1778 int CtdlIPCSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1780 if (!cret) return -2;
1781 if (!listing) return -2;
1783 return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1789 int CtdlIPCGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1790 char **listing, char *cret)
1795 if (!cret) return -2;
1796 if (!mimetype) return -2;
1797 if (!listing) return -2;
1798 if (*listing) return -2;
1800 aaa = malloc(strlen(mimetype) + 13);
1801 if (!aaa) return -1;
1802 sprintf(aaa, "CONF GETSYS|%s", mimetype);
1803 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1804 listing, &bytes, cret);
1809 int CtdlIPCSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1810 const char *listing, char *cret)
1814 if (!cret) return -2;
1815 if (!mimetype) return -2;
1816 if (!listing) return -2;
1818 aaa = malloc(strlen(mimetype) + 13);
1819 if (!aaa) return -1;
1820 sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1821 return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1827 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1831 if (!cret) return -2;
1832 if (session < 0) return -2;
1834 sprintf(aaa, "REQT %d", session);
1835 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1840 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1844 if (!cret) return -2;
1845 if (msgnum < 0) return -2;
1847 sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1848 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1853 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1862 /* New SSL object */
1863 temp_ssl = SSL_new(ssl_ctx);
1865 error_printf("SSL_new failed: %s\n",
1866 ERR_reason_error_string(ERR_get_error()));
1869 /* Pointless flag waving */
1870 #if SSLEAY_VERSION_NUMBER >= 0x0922
1871 SSL_set_session_id_context(temp_ssl, "Citadel/UX SID", 14);
1874 if (!access("/var/run/egd-pool", F_OK))
1875 RAND_egd("/var/run/egd-pool");
1877 if (!RAND_status()) {
1878 error_printf("PRNG not properly seeded\n");
1882 /* Associate network connection with SSL object */
1883 if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1884 error_printf("SSL_set_fd failed: %s\n",
1885 ERR_reason_error_string(ERR_get_error()));
1889 if (status_hook != NULL)
1890 status_hook("Requesting encryption...\r");
1892 /* Ready to start SSL/TLS */
1894 CtdlIPC_putline(ipc, "STLS");
1895 CtdlIPC_getline(ipc, buf);
1896 if (buf[0] != '2') {
1897 error_printf("Server can't start TLS: %s\n", buf);
1901 r = CtdlIPCGenericCommand(ipc,
1902 "STLS", NULL, 0, NULL, NULL, cret);
1904 error_printf("Server can't start TLS: %s\n", buf);
1909 /* Do SSL/TLS handshake */
1910 if ((a = SSL_connect(temp_ssl)) < 1) {
1911 error_printf("SSL_connect failed: %s\n",
1912 ERR_reason_error_string(ERR_get_error()));
1916 ipc->ssl = temp_ssl;
1918 BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1922 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1923 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1924 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1925 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1931 #endif /* HAVE_OPENSSL */
1936 static void endtls(SSL *ssl)
1947 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1951 if (!address) return -2;
1952 if (!cret) return -2;
1954 aaa = (char *)malloc(strlen(address) + 6);
1955 if (!aaa) return -1;
1957 sprintf(aaa, "QDIR %s", address);
1958 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1963 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
1967 if (!cret) return -2;
1968 sprintf(aaa, "IPGM %d", secret);
1969 return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1974 int CtdlIPCMessageBaseCheck(CtdlIPC *ipc, char **mret, char *cret)
1978 if (!cret) return -2;
1979 if (!mret) return -2;
1980 if (*mret) return -2;
1982 return CtdlIPCGenericCommand(ipc, "FSCK", NULL, 0, mret, &size, cret);
2003 /* ************************************************************************** */
2004 /* Stuff below this line is not for public consumption */
2005 /* ************************************************************************** */
2008 INLINE void CtdlIPC_lock(CtdlIPC *ipc)
2010 if (ipc->network_status_cb) ipc->network_status_cb(1);
2011 #ifdef THREADED_CLIENT
2012 pthread_mutex_lock(&(ipc->mutex));
2017 INLINE void CtdlIPC_unlock(CtdlIPC *ipc)
2019 #ifdef THREADED_CLIENT
2020 pthread_mutex_unlock(&(ipc->mutex));
2022 if (ipc->network_status_cb) ipc->network_status_cb(0);
2026 /* Read a listing from the server up to 000. Append to dest if it exists */
2027 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
2036 length = strlen(ret);
2041 while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2042 linelength = strlen(aaa);
2043 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
2045 strcpy(&ret[length], aaa);
2046 length += linelength;
2047 strcpy(&ret[length++], "\n");
2055 /* Send a listing to the server; generate the ending 000. */
2056 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
2060 text = (char *)malloc(strlen(listing) + 6);
2062 strcpy(text, listing);
2063 while (text[strlen(text) - 1] == '\n')
2064 text[strlen(text) - 1] = '\0';
2065 strcat(text, "\n000");
2066 CtdlIPC_putline(ipc, text);
2070 /* Malloc failed but we are committed to send */
2071 /* This may result in extra blanks at the bottom */
2072 CtdlIPC_putline(ipc, text);
2073 CtdlIPC_putline(ipc, "000");
2079 /* Partial read of file from server */
2080 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2082 register size_t len = 0;
2086 if (!cret) return 0;
2087 if (bytes < 1) return 0;
2088 if (offset < 0) return 0;
2091 sprintf(aaa, "READ %d|%d", (int)offset, (int)bytes);
2092 CtdlIPC_putline(ipc, aaa);
2093 CtdlIPC_getline(ipc, aaa);
2095 strcpy(cret, &aaa[4]);
2097 len = extract_long(&aaa[4], 0);
2098 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2100 /* I know what I'm doing */
2101 serv_read(ipc, (*buf + offset), len);
2103 /* We have to read regardless */
2104 serv_read(ipc, aaa, len);
2108 CtdlIPC_unlock(ipc);
2114 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2118 if (!cret) return -2;
2119 if (!ipc->downloading) return -2;
2121 ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2123 ipc->downloading = 0;
2129 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2133 snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2134 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2141 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
2142 void (*progress_gauge_callback)(unsigned long, unsigned long),
2145 register size_t len;
2147 if (!cret) return -1;
2148 if (!buf) return -1;
2149 if (*buf) return -1;
2150 if (!ipc->downloading) return -1;
2153 if (progress_gauge_callback)
2154 progress_gauge_callback(len, bytes);
2155 while (len < bytes) {
2156 register size_t block;
2158 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2164 if (progress_gauge_callback)
2165 progress_gauge_callback(len, bytes);
2170 /* READ - pipelined */
2171 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2173 void (*progress_gauge_callback)(unsigned long, unsigned long),
2176 register size_t len;
2177 register int calls; /* How many calls in the pipeline */
2178 register int i; /* iterator */
2181 if (!cret) return -1;
2182 if (!buf) return -1;
2183 if (*buf) return -1;
2184 if (!ipc->downloading) return -1;
2186 *buf = (void *)realloc(*buf, bytes - resume);
2187 if (!*buf) return -1;
2191 if (progress_gauge_callback)
2192 progress_gauge_callback(len, bytes);
2194 /* How many calls will be in the pipeline? */
2195 calls = (bytes - resume) / 4096;
2196 if ((bytes - resume) % 4096) calls++;
2198 /* Send all requests at once */
2199 for (i = 0; i < calls; i++) {
2200 sprintf(aaa, "READ %d|4096", (int)(i * 4096 + resume) );
2201 CtdlIPC_putline(ipc, aaa);
2204 /* Receive all responses at once */
2205 for (i = 0; i < calls; i++) {
2206 CtdlIPC_getline(ipc, aaa);
2208 strcpy(cret, &aaa[4]);
2210 len = extract_long(&aaa[4], 0);
2211 /* I know what I'm doing */
2212 serv_read(ipc, ((*buf) + (i * 4096)), len);
2214 if (progress_gauge_callback)
2215 progress_gauge_callback(i * 4096 + len, bytes);
2217 CtdlIPC_unlock(ipc);
2223 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2228 if (!cret) return -1;
2229 if (!ipc->uploading) return -1;
2231 sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2232 ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2239 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2240 void (*progress_gauge_callback)(unsigned long, unsigned long),
2243 register int ret = -1;
2244 register size_t offset = 0;
2250 if (!cret) return -1;
2251 if (!path) return -1;
2252 if (!*path) return -1;
2254 fd = fopen(path, "r");
2257 fseek(fd, 0L, SEEK_END);
2261 if (progress_gauge_callback)
2262 progress_gauge_callback(0, bytes);
2264 while (offset < bytes) {
2265 register size_t to_write;
2267 /* Read some data in */
2268 to_write = fread(buf, 1, 4096, fd);
2270 if (feof(fd) || ferror(fd)) break;
2272 sprintf(aaa, "WRIT %d", (int)to_write);
2273 CtdlIPC_putline(ipc, aaa);
2274 CtdlIPC_getline(ipc, aaa);
2275 strcpy(cret, &aaa[4]);
2277 if (aaa[0] == '7') {
2278 to_write = extract_long(&aaa[4], 0);
2280 serv_write(ipc, buf, to_write);
2282 if (progress_gauge_callback)
2283 progress_gauge_callback(offset, bytes);
2284 /* Detect short reads and back up if needed */
2285 /* offset will never be negative anyway */
2286 fseek(fd, (signed)offset, SEEK_SET);
2291 if (progress_gauge_callback)
2292 progress_gauge_callback(1, 1);
2293 return (!ferror(fd) ? ret : -2);
2298 * Generic command method. This method should handle any server command
2299 * except for CHAT. It takes the following arguments:
2301 * ipc The server to speak with
2302 * command Preformatted command to send to server
2303 * to_send A text or binary file to send to server
2304 * (only sent if server requests it)
2305 * bytes_to_send The number of bytes in to_send (required if
2306 * sending binary, optional if sending listing)
2307 * to_receive Pointer to a NULL pointer, if the server
2308 * sends text or binary we will allocate memory
2309 * for the file and stuff it here
2310 * bytes_to_receive If a file is received, we will store its
2312 * proto_response The protocol response. Caller must provide
2313 * this buffer and ensure that it is at least
2314 * 128 bytes in length.
2316 * This function returns a number equal to the protocol response number,
2317 * -1 if an internal error occurred, -2 if caller provided bad values,
2318 * or 0 - the protocol response number if bad values were found during
2319 * the protocol exchange.
2320 * It stores the protocol response string (minus the number) in
2321 * protocol_response as described above. Some commands send additional
2322 * data in this string.
2324 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2325 const char *command, const char *to_send,
2326 size_t bytes_to_send, char **to_receive,
2327 size_t *bytes_to_receive, char *proto_response)
2333 if (!command) return -2;
2334 if (!proto_response) return -2;
2337 if (ipc->ssl) watch_ssl = 1;
2341 CtdlIPC_putline(ipc, command);
2343 CtdlIPC_getline(ipc, proto_response);
2344 if (proto_response[3] == '*')
2346 ret = atoi(proto_response);
2347 strcpy(proto_response, &proto_response[4]);
2348 switch (ret / 100) {
2349 default: /* Unknown, punt */
2351 case 3: /* MORE_DATA */
2353 /* Don't need to do anything */
2355 case 1: /* LISTING_FOLLOWS */
2356 if (to_receive && !*to_receive && bytes_to_receive) {
2357 *to_receive = CtdlIPCReadListing(ipc, NULL);
2358 } else { /* Drain */
2359 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2363 case 4: /* SEND_LISTING */
2365 CtdlIPCSendListing(ipc, to_send);
2367 /* No listing given, fake it */
2368 CtdlIPC_putline(ipc, "000");
2372 case 6: /* BINARY_FOLLOWS */
2373 if (to_receive && !*to_receive && bytes_to_receive) {
2375 extract_long(proto_response, 0);
2376 *to_receive = (char *)
2377 malloc((size_t)*bytes_to_receive);
2381 serv_read(ipc, *to_receive,
2388 drain = extract_long(proto_response, 0);
2389 while (drain > SIZ) {
2390 serv_read(ipc, buf, SIZ);
2393 serv_read(ipc, buf, drain);
2397 case 7: /* SEND_BINARY */
2398 if (to_send && bytes_to_send) {
2399 serv_write(ipc, to_send, bytes_to_send);
2400 } else if (bytes_to_send) {
2401 /* Fake it, send nulls */
2404 fake = bytes_to_send;
2405 memset(buf, '\0', SIZ);
2406 while (fake > SIZ) {
2407 serv_write(ipc, buf, SIZ);
2410 serv_write(ipc, buf, fake);
2412 } /* else who knows? DANGER WILL ROBINSON */
2414 case 8: /* START_CHAT_MODE */
2415 if (!strncasecmp(command, "CHAT", 4)) {
2416 /* Don't call chatmode with generic! */
2417 CtdlIPC_putline(ipc, "/quit");
2420 /* In this mode we send then receive listing */
2422 CtdlIPCSendListing(ipc, to_send);
2424 /* No listing given, fake it */
2425 CtdlIPC_putline(ipc, "000");
2428 if (to_receive && !*to_receive
2429 && bytes_to_receive) {
2430 *to_receive = CtdlIPCReadListing(ipc, NULL);
2431 } else { /* Drain */
2432 while (CtdlIPC_getline(ipc, buf),
2433 strcmp(buf, "000")) ;
2438 case 9: /* ASYNC_MSG */
2439 /* CtdlIPCDoAsync(ret, proto_response); */
2440 free(CtdlIPCReadListing(ipc, NULL)); /* STUB FIXME */
2446 CtdlIPC_unlock(ipc);
2451 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2453 struct hostent *phe;
2454 struct servent *pse;
2455 struct protoent *ppe;
2456 struct sockaddr_in sin;
2459 memset(&sin, 0, sizeof(sin));
2460 sin.sin_family = AF_INET;
2462 pse = getservbyname(service, protocol);
2464 sin.sin_port = pse->s_port;
2466 else if (atoi(service) > 0) {
2467 sin.sin_port = htons(atoi(service));
2470 sin.sin_port = htons(defaultPort);
2472 phe = gethostbyname(host);
2474 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2475 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2478 if ((ppe = getprotobyname(protocol)) == 0) {
2481 if (!strcmp(protocol, "udp")) {
2487 s = socket(PF_INET, type, ppe->p_proto);
2492 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2500 static int uds_connectsock(int *isLocal, char *sockpath)
2502 struct sockaddr_un addr;
2505 memset(&addr, 0, sizeof(addr));
2506 addr.sun_family = AF_UNIX;
2507 safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2509 s = socket(AF_UNIX, SOCK_STREAM, 0);
2514 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2525 * input binary data from socket
2527 static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes)
2529 unsigned int len, rlen;
2531 #if defined(HAVE_OPENSSL)
2533 serv_read_ssl(ipc, buf, bytes);
2538 while (len < bytes) {
2539 rlen = read(ipc->sock, &buf[len], bytes - len);
2541 connection_died(ipc);
2550 * send binary to server
2552 static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
2554 unsigned int bytes_written = 0;
2557 #if defined(HAVE_OPENSSL)
2559 serv_write_ssl(ipc, buf, nbytes);
2563 while (bytes_written < nbytes) {
2564 retval = write(ipc->sock, &buf[bytes_written],
2565 nbytes - bytes_written);
2567 connection_died(ipc);
2570 bytes_written += retval;
2577 * input binary data from encrypted connection
2579 static void serv_read_ssl(CtdlIPC* ipc, char *buf, unsigned int bytes)
2585 while (len < bytes) {
2586 if (SSL_want_read(ipc->ssl)) {
2587 if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2588 error_printf("SSL_write in serv_read:\n");
2589 ERR_print_errors_fp(stderr);
2592 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2596 errval = SSL_get_error(ipc->ssl, rlen);
2597 if (errval == SSL_ERROR_WANT_READ ||
2598 errval == SSL_ERROR_WANT_WRITE) {
2602 if (errval == SSL_ERROR_ZERO_RETURN ||
2603 errval == SSL_ERROR_SSL) {
2604 serv_read(ipc, &buf[len], bytes - len);
2607 error_printf("SSL_read in serv_read:\n");
2608 ERR_print_errors_fp(stderr);
2609 connection_died(ipc);
2618 * send binary to server encrypted
2620 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
2622 unsigned int bytes_written = 0;
2626 while (bytes_written < nbytes) {
2627 if (SSL_want_write(ipc->ssl)) {
2628 if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2629 error_printf("SSL_read in serv_write:\n");
2630 ERR_print_errors_fp(stderr);
2633 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2634 nbytes - bytes_written);
2638 errval = SSL_get_error(ipc->ssl, retval);
2639 if (errval == SSL_ERROR_WANT_READ ||
2640 errval == SSL_ERROR_WANT_WRITE) {
2644 if (errval == SSL_ERROR_ZERO_RETURN ||
2645 errval == SSL_ERROR_SSL) {
2646 serv_write(ipc, &buf[bytes_written],
2647 nbytes - bytes_written);
2650 error_printf("SSL_write in serv_write:\n");
2651 ERR_print_errors_fp(stderr);
2652 connection_died(ipc);
2655 bytes_written += retval;
2660 static void CtdlIPC_init_OpenSSL(void)
2663 SSL_METHOD *ssl_method;
2666 /* already done init */
2674 SSL_load_error_strings();
2675 SSLeay_add_ssl_algorithms();
2677 /* Set up the SSL context in which we will oeprate */
2678 ssl_method = SSLv23_client_method();
2679 ssl_ctx = SSL_CTX_new(ssl_method);
2681 error_printf("SSL_CTX_new failed: %s\n",
2682 ERR_reason_error_string(ERR_get_error()));
2685 /* Any reasonable cipher we can get */
2686 if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2687 error_printf("No ciphers available for encryption\n");
2690 SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2692 /* Load DH parameters into the context */
2695 error_printf("Can't allocate a DH object: %s\n",
2696 ERR_reason_error_string(ERR_get_error()));
2699 if (!(BN_hex2bn(&(dh->p), DH_P))) {
2700 error_printf("Can't assign DH_P: %s\n",
2701 ERR_reason_error_string(ERR_get_error()));
2705 if (!(BN_hex2bn(&(dh->g), DH_G))) {
2706 error_printf("Can't assign DH_G: %s\n",
2707 ERR_reason_error_string(ERR_get_error()));
2712 SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2715 #ifdef THREADED_CLIENT
2716 /* OpenSSL requires callbacks for threaded clients */
2717 CRYPTO_set_locking_callback(ssl_lock);
2718 CRYPTO_set_id_callback(id_callback);
2720 /* OpenSSL requires us to do semaphores for threaded clients */
2721 Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2723 perror("malloc failed");
2726 for (a = 0; a < CRYPTO_num_locks(); a++) {
2727 Critters[a] = malloc(sizeof (pthread_mutex_t));
2729 perror("malloc failed");
2732 pthread_mutex_init(Critters[a], NULL);
2735 #endif /* THREADED_CLIENT */
2739 static void ssl_lock(int mode, int n, const char *file, int line)
2741 #ifdef THREADED_CLIENT
2742 if (mode & CRYPTO_LOCK)
2743 pthread_mutex_lock(Critters[n]);
2745 pthread_mutex_unlock(Critters[n]);
2746 #endif /* THREADED_CLIENT */
2749 #ifdef THREADED_CLIENT
2750 static unsigned long id_callback(void) {
2751 return (unsigned long)pthread_self();
2753 #endif /* THREADED_CLIENT */
2754 #endif /* HAVE_OPENSSL */
2758 * input string from socket - implemented in terms of serv_read()
2760 void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2764 /* Read one character at a time. */
2766 serv_read(ipc, &buf[i], 1);
2767 if (buf[i] == '\n' || i == (SIZ-1))
2771 /* If we got a long line, discard characters until the newline. */
2773 while (buf[i] != '\n')
2774 serv_read(ipc, &buf[i], 1);
2776 /* Strip the trailing newline (and carriage return, if present) */
2777 if (buf[i] == 10) buf[i--] = 0;
2778 if (buf[i] == 13) buf[i--] = 0;
2783 * send line to server - implemented in terms of serv_write()
2785 void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2787 /* error_printf("< %s\n", buf); */
2788 serv_write(ipc, buf, strlen(buf));
2789 serv_write(ipc, "\n", 1);
2791 ipc->last_command_sent = time(NULL);
2798 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2805 CtdlIPC *ipc = ialloc(CtdlIPC);
2809 #if defined(HAVE_OPENSSL)
2811 CtdlIPC_init_OpenSSL();
2813 #if defined(HAVE_PTHREAD_H)
2814 pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2816 ipc->sock = -1; /* Not connected */
2817 ipc->isLocal = 0; /* Not local, of course! */
2818 ipc->downloading = 0;
2820 ipc->last_command_sent = 0L;
2821 ipc->network_status_cb = NULL;
2823 strcpy(cithost, DEFAULT_HOST); /* default host */
2824 strcpy(citport, DEFAULT_PORT); /* default port */
2826 /* Allow caller to supply our values (Windows) */
2827 if (hostbuf && strlen(hostbuf) > 0)
2828 strcpy(cithost, hostbuf);
2829 if (portbuf && strlen(portbuf) > 0)
2830 strcpy(citport, portbuf);
2832 /* Read host/port from command line if present */
2833 for (a = 0; a < argc; ++a) {
2836 } else if (a == 1) {
2837 strcpy(cithost, argv[a]);
2838 } else if (a == 2) {
2839 strcpy(citport, argv[a]);
2841 error_printf("%s: usage: ",argv[0]);
2842 error_printf("%s [host] [port] ",argv[0]);
2849 if ((!strcmp(cithost, "localhost"))
2850 || (!strcmp(cithost, "127.0.0.1"))) {
2854 /* If we're using a unix domain socket we can do a bunch of stuff */
2855 if (!strcmp(cithost, UDS)) {
2856 if (!strcasecmp(citport, DEFAULT_PORT)) {
2857 snprintf(sockpath, sizeof sockpath, "%s%s",
2858 BBSDIR, "/citadel.socket");
2861 snprintf(sockpath, sizeof sockpath, "%s%s",
2862 citport, "/citadel.socket");
2864 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2865 if (ipc->sock == -1) {
2869 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2870 if (portbuf != NULL) strcpy(portbuf, sockpath);
2874 ipc->sock = connectsock(cithost, citport, "tcp", 504);
2875 if (ipc->sock == -1) {
2879 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2880 if (portbuf != NULL) strcpy(portbuf, citport);
2885 * return the file descriptor of the server socket so we can select() on it.
2887 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2890 int CtdlIPC_getsockfd(CtdlIPC* ipc)
2897 * return one character
2899 * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2902 char CtdlIPC_get(CtdlIPC* ipc)
2907 serv_read(ipc, buf, 1);