* <.R>ead <U>ser-list now takes advantage of the server-side string match
[citadel.git] / citadel / citadel_ipc.c
1 /* $Id$ */
2
3 #include "sysdep.h"
4 #if TIME_WITH_SYS_TIME
5 # include <sys/time.h>
6 # include <time.h>
7 #else
8 # if HAVE_SYS_TIME_H
9 #  include <sys/time.h>
10 # else
11 #  include <time.h>
12 # endif
13 #endif
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <sys/types.h>
17 #include <string.h>
18 #ifdef HAVE_MALLOC_H
19 #include <malloc.h>
20 #endif
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include <sys/socket.h>
24 #include <arpa/inet.h>
25 #include <netinet/in.h>
26 #include <netdb.h>
27 #include <sys/un.h>
28 #include <errno.h>
29 #ifdef THREADED_CLIENT
30 #include <pthread.h>
31 #endif
32 #include "citadel.h"
33 #include "citadel_ipc.h"
34 #include "citadel_decls.h"
35 #include "tools.h"
36
37 #ifdef THREADED_CLIENT
38 pthread_mutex_t rwlock;
39 #endif
40
41 #ifdef HAVE_OPENSSL
42 static SSL_CTX *ssl_ctx;
43 char arg_encrypt;
44 char rc_encrypt;
45 #ifdef THREADED_CLIENT
46 pthread_mutex_t **Critters;                     /* Things that need locking */
47 #endif /* THREADED_CLIENT */
48
49 #endif /* HAVE_OPENSSL */
50
51 #ifndef INADDR_NONE
52 #define INADDR_NONE 0xffffffff
53 #endif
54
55 static void (*status_hook)(char *s) = NULL;
56
57 void setCryptoStatusHook(void (*hook)(char *s)) {
58         status_hook = hook;
59 }
60
61 void CtdlIPC_SetNetworkStatusCallback(CtdlIPC *ipc, void (*hook)(int state)) {
62         ipc->network_status_cb = hook;
63 }
64
65
66 char instant_msgs = 0;
67
68
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);
71 #ifdef HAVE_OPENSSL
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 */
80 static void CtdlIPC_getline(CtdlIPC* ipc, char *buf);
81 static void CtdlIPC_putline(CtdlIPC *ipc, const char *buf);
82
83
84 /*
85  * Does nothing.  The server should always return 200.
86  */
87 int CtdlIPCNoop(CtdlIPC *ipc)
88 {
89         char aaa[128];
90
91         return CtdlIPCGenericCommand(ipc, "NOOP", NULL, 0, NULL, NULL, aaa);
92 }
93
94
95 /*
96  * Does nothing interesting.  The server should always return 200
97  * along with your string.
98  */
99 int CtdlIPCEcho(CtdlIPC *ipc, const char *arg, char *cret)
100 {
101         register int ret;
102         char *aaa;
103         
104         if (!arg) return -2;
105         if (!cret) return -2;
106
107         aaa = (char *)malloc((size_t)(strlen(arg) + 6));
108         if (!aaa) return -1;
109
110         sprintf(aaa, "ECHO %s", arg);
111         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
112         free(aaa);
113         return ret;
114 }
115
116
117 /*
118  * Asks the server to close the connecction.
119  * Should always return 200.
120  */
121 int CtdlIPCQuit(CtdlIPC *ipc)
122 {
123         register int ret = 221;         /* Default to successful quit */
124         char aaa[128];
125
126         CtdlIPC_lock(ipc);
127         if (ipc->sock > -1) {
128                 CtdlIPC_putline(ipc, "QUIT");
129                 CtdlIPC_getline(ipc, aaa);
130                 ret = atoi(aaa);
131         }
132 #ifdef HAVE_OPENSSL
133         if (ipc->ssl)
134                 SSL_shutdown(ipc->ssl);
135         ipc->ssl = NULL;
136 #endif
137         if (ipc->sock)
138                 shutdown(ipc->sock, 2); /* Close connection; we're dead */
139         ipc->sock = -1;
140         CtdlIPC_unlock(ipc);
141         return ret;
142 }
143
144
145 /*
146  * Asks the server to logout.  Should always return 200, even if no user
147  * was logged in.  The user will not be logged in after this!
148  */
149 int CtdlIPCLogout(CtdlIPC *ipc)
150 {
151         register int ret;
152         char aaa[128];
153
154         CtdlIPC_lock(ipc);
155         CtdlIPC_putline(ipc, "LOUT");
156         CtdlIPC_getline(ipc, aaa);
157         ret = atoi(aaa);
158         CtdlIPC_unlock(ipc);
159         return ret;
160 }
161
162
163 /*
164  * First stage of authentication - pass the username.  Returns 300 if the
165  * username is able to log in, with the username correctly spelled in cret.
166  * Returns various 500 error codes if the user doesn't exist, etc.
167  */
168 int CtdlIPCTryLogin(CtdlIPC *ipc, const char *username, char *cret)
169 {
170         register int ret;
171         char *aaa;
172
173         if (!username) return -2;
174         if (!cret) return -2;
175
176         aaa = (char *)malloc((size_t)(strlen(username) + 6));
177         if (!aaa) return -1;
178
179         sprintf(aaa, "USER %s", username);
180         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
181         free(aaa);
182         return ret;
183 }
184
185
186 /*
187  * Second stage of authentication - provide password.  The server returns
188  * 200 and several arguments in cret relating to the user's account.
189  */
190 int CtdlIPCTryPassword(CtdlIPC *ipc, const char *passwd, char *cret)
191 {
192         register int ret;
193         char *aaa;
194
195         if (!passwd) return -2;
196         if (!cret) return -2;
197
198         aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
199         if (!aaa) return -1;
200
201         sprintf(aaa, "PASS %s", passwd);
202         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
203         free(aaa);
204         return ret;
205 }
206
207
208 /*
209  * Second stage of authentication - provide password.  The server returns
210  * 200 and several arguments in cret relating to the user's account.
211  */
212 int CtdlIPCTryApopPassword(CtdlIPC *ipc, const char *response, char *cret)
213 {
214         register int ret;
215         char *aaa;
216
217         if (!response) return -2;
218         if (!cret) return -2;
219
220         aaa = (char *)malloc((size_t)(strlen(response) + 6));
221         if (!aaa) return -1;
222
223         sprintf(aaa, "PAS2 %s", response);
224         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
225         free(aaa);
226         return ret;
227 }
228
229
230 /*
231  * Create a new user.  This returns 200 plus the same arguments as TryPassword
232  * if selfservice is nonzero, unless there was a problem creating the account.
233  * If selfservice is zero, creates a new user but does not log out the existing
234  * user - intended for use by system administrators to create accounts on
235  * behalf of other users.
236  */
237 int CtdlIPCCreateUser(CtdlIPC *ipc, const char *username, int selfservice, char *cret)
238 {
239         register int ret;
240         char *aaa;
241
242         if (!username) return -2;
243         if (!cret) return -2;
244
245         aaa = (char *)malloc((size_t)(strlen(username) + 6));
246         if (!aaa) return -1;
247
248         sprintf(aaa, "%s %s", selfservice ? "NEWU" : "CREU",  username);
249         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
250         free(aaa);
251         return ret;
252 }
253
254
255 /*
256  * Changes the user's password.  Returns 200 if changed, errors otherwise.
257  */
258 int CtdlIPCChangePassword(CtdlIPC *ipc, const char *passwd, char *cret)
259 {
260         register int ret;
261         char *aaa;
262
263         if (!passwd) return -2;
264         if (!cret) return -2;
265
266         aaa = (char *)malloc((size_t)(strlen(passwd) + 6));
267         if (!aaa) return -1;
268
269         sprintf(aaa, "SETP %s", passwd);
270         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
271         free(aaa);
272         return ret;
273 }
274
275
276 /* LKRN */
277 /* Caller must free the march list */
278 /* Room types are defined in enum RoomList; keep these in sync! */
279 /* floor is -1 for all, or floornum */
280 int CtdlIPCKnownRooms(CtdlIPC *ipc, enum RoomList which, int floor, struct march **listing, char *cret)
281 {
282         register int ret;
283         struct march *march = NULL;
284         static char *proto[] =
285                 {"LKRA", "LKRN", "LKRO", "LZRM", "LRMS", "LPRM" };
286         char aaa[SIZ];
287         char *bbb = NULL;
288         size_t bbb_len;
289
290         if (!listing) return -2;
291         if (*listing) return -2;        /* Free the listing first */
292         if (!cret) return -2;
293         /* if (which < 0 || which > 4) return -2; */
294         if (floor < -1) return -2;      /* Can't validate upper bound, sorry */
295
296         sprintf(aaa, "%s %d", proto[which], floor);
297         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
298         if (ret / 100 == 1) {
299                 struct march *mptr;
300
301                 while (bbb && strlen(bbb)) {
302                         int a;
303
304                         extract_token(aaa, bbb, 0, '\n', sizeof aaa);
305                         a = strlen(aaa);
306                         memmove(bbb, bbb + a + 1, strlen(bbb) - a);
307                         mptr = (struct march *) malloc(sizeof (struct march));
308                         if (mptr) {
309                                 mptr->next = NULL;
310                                 extract_token(mptr->march_name, aaa, 0, '|', sizeof mptr->march_name);
311                                 mptr->march_flags = (unsigned int) extract_int(aaa, 1);
312                                 mptr->march_floor = (char) extract_int(aaa, 2);
313                                 mptr->march_order = (char) extract_int(aaa, 3);
314                                 mptr->march_flags2 = (unsigned int) extract_int(aaa, 4);
315                                 mptr->march_access = (char) extract_int(aaa, 5);
316                                 if (march == NULL)
317                                         march = mptr;
318                                 else {
319                                         struct march *mptr2;
320
321                                         mptr2 = march;
322                                         while (mptr2->next != NULL)
323                                                 mptr2 = mptr2->next;
324                                         mptr2->next = mptr;
325                                 }
326                         }
327                 }
328         }
329         *listing = march;
330         return ret;
331 }
332
333
334 /* GETU */
335 /* Caller must free the struct ctdluser; caller may pass an existing one */
336 int CtdlIPCGetConfig(CtdlIPC *ipc, struct ctdluser **uret, char *cret)
337 {
338         register int ret;
339
340         if (!cret) return -2;
341         if (!uret) return -2;
342         if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof (struct ctdluser));
343         if (!*uret) return -1;
344
345         ret = CtdlIPCGenericCommand(ipc, "GETU", NULL, 0, NULL, NULL, cret);
346         if (ret / 100 == 2) {
347                 uret[0]->USscreenwidth = extract_int(cret, 0);
348                 uret[0]->USscreenheight = extract_int(cret, 1);
349                 uret[0]->flags = extract_int(cret, 2);
350         }
351         return ret;
352 }
353
354
355 /* SETU */
356 int CtdlIPCSetConfig(CtdlIPC *ipc, struct ctdluser *uret, char *cret)
357 {
358         char aaa[48];
359
360         if (!uret) return -2;
361         if (!cret) return -2;
362
363         sprintf(aaa, "SETU %d|%d|%d",
364                         uret->USscreenwidth, uret->USscreenheight,
365                         uret->flags);
366         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
367 }
368
369
370 /* GOTO */
371 int CtdlIPCGotoRoom(CtdlIPC *ipc, const char *room, const char *passwd,
372                 struct ctdlipcroom **rret, char *cret)
373 {
374         register int ret;
375         char *aaa;
376
377         if (!cret) return -2;
378         if (!rret) return -2;
379         if (!*rret) *rret = (struct ctdlipcroom *)calloc(1, sizeof (struct ctdlipcroom));
380         if (!*rret) return -1;
381
382         if (passwd) {
383                 aaa = (char *)malloc(strlen(room) + strlen(passwd) + 7);
384                 if (!aaa) {
385                         free(*rret);
386                         return -1;
387                 }
388                 sprintf(aaa, "GOTO %s|%s", room, passwd);
389         } else {
390                 aaa = (char *)malloc(strlen(room) + 6);
391                 if (!aaa) {
392                         free(*rret);
393                         return -1;
394                 }
395                 sprintf(aaa, "GOTO %s", room);
396         }
397         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
398         if (ret / 100 == 2) {
399                 extract_token(rret[0]->RRname, cret, 0, '|', sizeof rret[0]->RRname);
400                 rret[0]->RRunread = extract_long(cret, 1);
401                 rret[0]->RRtotal = extract_long(cret, 2);
402                 rret[0]->RRinfoupdated = extract_int(cret, 3);
403                 rret[0]->RRflags = extract_int(cret, 4);
404                 rret[0]->RRhighest = extract_long(cret, 5);
405                 rret[0]->RRlastread = extract_long(cret, 6);
406                 rret[0]->RRismailbox = extract_int(cret, 7);
407                 rret[0]->RRaide = extract_int(cret, 8);
408                 rret[0]->RRnewmail = extract_long(cret, 9);
409                 rret[0]->RRfloor = extract_int(cret, 10);
410         } else {
411                 free(*rret);
412         }
413         return ret;
414 }
415
416
417 /* MSGS */
418 /* which is 0 = all, 1 = old, 2 = new, 3 = last, 4 = first, 5 = gt, 6 = lt */
419 /* whicharg is number of messages, applies to last, first, gt, lt */
420 int CtdlIPCGetMessages(CtdlIPC *ipc, enum MessageList which, int whicharg,
421                 const char *mtemplate, unsigned long **mret, char *cret)
422 {
423         register int ret;
424         register unsigned long count = 0;
425         static char *proto[] =
426                 { "ALL", "OLD", "NEW", "LAST", "FIRST", "GT", "LT" };
427         char aaa[33];
428         char *bbb = NULL;
429         size_t bbb_len;
430
431         if (!cret) return -2;
432         if (!mret) return -2;
433         if (*mret) return -2;
434         if (which < 0 || which > 6) return -2;
435
436         if (which <= 2)
437                 sprintf(aaa, "MSGS %s||%d", proto[which],
438                                 (mtemplate) ? 1 : 0);
439         else
440                 sprintf(aaa, "MSGS %s|%d|%d", proto[which], whicharg,
441                                 (mtemplate) ? 1 : 0);
442         if (mtemplate) count = strlen(mtemplate);
443         ret = CtdlIPCGenericCommand(ipc, aaa, mtemplate, count, &bbb, &bbb_len, cret);
444         if (ret / 100 != 1)
445                 return ret;
446         count = 0;
447         *mret = (unsigned long *)calloc(1, sizeof(unsigned long));
448         if (!*mret)
449                 return -1;
450         while (bbb && strlen(bbb)) {
451                 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
452                 remove_token(bbb, 0, '\n');
453                 *mret = (unsigned long *)realloc(*mret, (size_t)((count + 2) *
454                                         sizeof (unsigned long)));
455                 if (*mret) {
456                         (*mret)[count++] = atol(aaa);
457                         (*mret)[count] = 0L;
458                 } else {
459                         break;
460                 }
461         }
462         if (bbb) free(bbb);
463         return ret;
464 }
465
466
467 /* MSG0, MSG2 */
468 int CtdlIPCGetSingleMessage(CtdlIPC *ipc, long msgnum, int headers, int as_mime,
469                 struct ctdlipcmessage **mret, char *cret)
470 {
471         register int ret;
472         char aaa[SIZ];
473         char *bbb = NULL;
474         size_t bbb_len;
475         int multipart_hunting = 0;
476         char multipart_prefix[128];
477
478         if (!cret) return -1;
479         if (!mret) return -1;
480         if (!*mret) *mret = (struct ctdlipcmessage *)calloc(1, sizeof (struct ctdlipcmessage));
481         if (!*mret) return -1;
482         if (!msgnum) return -1;
483
484         strcpy(mret[0]->content_type, "");
485         sprintf(aaa, "MSG%d %ld|%d", as_mime, msgnum, headers);
486         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, &bbb, &bbb_len, cret);
487         if (ret / 100 == 1) {
488                 if (as_mime != 2) {
489                         strcpy(mret[0]->mime_chosen, "1");      /* Default chosen-part is "1" */
490                         while (strlen(bbb) > 4 && bbb[4] == '=') {
491                                 extract_token(aaa, bbb, 0, '\n', sizeof aaa);
492                                 remove_token(bbb, 0, '\n');
493
494                                 if (!strncasecmp(aaa, "nhdr=yes", 8))
495                                         mret[0]->nhdr = 1;
496                                 else if (!strncasecmp(aaa, "from=", 5))
497                                         safestrncpy(mret[0]->author, &aaa[5], SIZ);
498                                 else if (!strncasecmp(aaa, "type=", 5))
499                                         mret[0]->type = atoi(&aaa[5]);
500                                 else if (!strncasecmp(aaa, "msgn=", 5))
501                                         safestrncpy(mret[0]->msgid, &aaa[5], SIZ);
502                                 else if (!strncasecmp(aaa, "subj=", 5))
503                                         safestrncpy(mret[0]->subject, &aaa[5], SIZ);
504                                 else if (!strncasecmp(aaa, "rfca=", 5))
505                                         safestrncpy(mret[0]->email, &aaa[5], SIZ);
506                                 else if (!strncasecmp(aaa, "hnod=", 5))
507                                         safestrncpy(mret[0]->hnod, &aaa[5], SIZ);
508                                 else if (!strncasecmp(aaa, "room=", 5))
509                                         safestrncpy(mret[0]->room, &aaa[5], SIZ);
510                                 else if (!strncasecmp(aaa, "node=", 5))
511                                         safestrncpy(mret[0]->node, &aaa[5], SIZ);
512                                 else if (!strncasecmp(aaa, "rcpt=", 5))
513                                         safestrncpy(mret[0]->recipient, &aaa[5], SIZ);
514                                 else if (!strncasecmp(aaa, "time=", 5))
515                                         mret[0]->time = atol(&aaa[5]);
516
517                                 /* Multipart/alternative prefix & suffix strings help
518                                  * us to determine which part we want to download.
519                                  */
520                                 else if (!strncasecmp(aaa, "pref=", 5)) {
521                                         extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
522                                         if (!strcasecmp(multipart_prefix,
523                                            "multipart/alternative")) {
524                                                 ++multipart_hunting;
525                                         }
526                                 }
527                                 else if (!strncasecmp(aaa, "suff=", 5)) {
528                                         extract_token(multipart_prefix, &aaa[5], 1, '|', sizeof multipart_prefix);
529                                         if (!strcasecmp(multipart_prefix,
530                                            "multipart/alternative")) {
531                                                 ++multipart_hunting;
532                                         }
533                                 }
534
535                                 else if (!strncasecmp(aaa, "part=", 5)) {
536                                         struct parts *ptr, *chain;
537         
538                                         ptr = (struct parts *)calloc(1, sizeof (struct parts));
539                                         if (ptr) {
540
541                                                 /* Fill the buffers for the caller */
542                                                 extract_token(ptr->name, &aaa[5], 0, '|', sizeof ptr->name);
543                                                 extract_token(ptr->filename, &aaa[5], 1, '|', sizeof ptr->filename);
544                                                 extract_token(ptr->number, &aaa[5], 2, '|', sizeof ptr->number);
545                                                 extract_token(ptr->disposition, &aaa[5], 3, '|', sizeof ptr->disposition);
546                                                 extract_token(ptr->mimetype, &aaa[5], 4, '|', sizeof ptr->mimetype);
547                                                 ptr->length = extract_long(&aaa[5], 5);
548                                                 if (!mret[0]->attachments)
549                                                         mret[0]->attachments = ptr;
550                                                 else {
551                                                         chain = mret[0]->attachments;
552                                                         while (chain->next)
553                                                                 chain = chain->next;
554                                                         chain->next = ptr;
555                                                 }
556
557                                                 /* Now handle multipart/alternative */
558                                                 if (multipart_hunting > 0) {
559                                                         if ( (!strcasecmp(ptr->mimetype,
560                                                              "text/plain"))
561                                                            || (!strcasecmp(ptr->mimetype,
562                                                               "text/html")) ) {
563                                                                 strcpy(mret[0]->mime_chosen,
564                                                                         ptr->number);
565                                                         }
566                                                 }
567
568                                         }
569                                 }
570                         }
571                         /* Eliminate "text\n" */
572                         remove_token(bbb, 0, '\n');
573
574                         /* If doing a MIME thing, pull out the extra headers */
575                         if (as_mime == 4) {
576                                 do {
577                                         if (!strncasecmp(bbb, "Content-type: ", 14)) {
578                                                 extract_token(mret[0]->content_type, bbb, 0, '\n', sizeof mret[0]->content_type);
579                                                 strcpy(mret[0]->content_type,
580                                                         &mret[0]->content_type[14]);
581                                                 striplt(mret[0]->content_type);
582                                         }
583                                         remove_token(bbb, 0, '\n');
584                                 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
585                                 remove_token(bbb, 0, '\n');
586                         }
587
588
589                 }
590                 if (strlen(bbb)) {
591                         /* FIXME: Strip trailing whitespace */
592                         bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
593                 } else {
594                         bbb = (char *)realloc(bbb, 1);
595                         *bbb = '\0';
596                 }
597                 mret[0]->text = bbb;
598         }
599         return ret;
600 }
601
602
603 /* WHOK */
604 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
605 {
606         register int ret;
607         size_t bytes;
608
609         if (!cret) return -2;
610         if (!listing) return -2;
611         if (*listing) return -2;
612
613         ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
614         return ret;
615 }
616
617
618 /* INFO */
619 int CtdlIPCServerInfo(CtdlIPC *ipc, char *cret)
620 {
621         register int ret;
622         size_t bytes;
623         char *listing = NULL;
624         char buf[SIZ];
625
626         if (!cret) return -2;
627
628         ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
629         if (ret / 100 == 1) {
630                 int line = 0;
631
632                 while (*listing && strlen(listing)) {
633                         extract_token(buf, listing, 0, '\n', sizeof buf);
634                         remove_token(listing, 0, '\n');
635                         switch (line++) {
636                         case 0:         ipc->ServInfo.pid = atoi(buf);
637                                         break;
638                         case 1:         strcpy(ipc->ServInfo.nodename,buf);
639                                         break;
640                         case 2:         strcpy(ipc->ServInfo.humannode,buf);
641                                         break;
642                         case 3:         strcpy(ipc->ServInfo.fqdn,buf);
643                                         break;
644                         case 4:         strcpy(ipc->ServInfo.software,buf);
645                                         break;
646                         case 5:         ipc->ServInfo.rev_level = atoi(buf);
647                                         break;
648                         case 6:         strcpy(ipc->ServInfo.site_location,buf);
649                                         break;
650                         case 7:         strcpy(ipc->ServInfo.sysadm,buf);
651                                         break;
652                         case 9:         strcpy(ipc->ServInfo.moreprompt,buf);
653                                         break;
654                         case 10:        ipc->ServInfo.ok_floors = atoi(buf);
655                                         break;
656                         case 11:        ipc->ServInfo.paging_level = atoi(buf);
657                                         break;
658                         case 13:        ipc->ServInfo.supports_qnop = atoi(buf);
659                                         break;
660                         case 14:        ipc->ServInfo.supports_ldap = atoi(buf);
661                                         break;
662                         }
663                 }
664
665         }
666         return ret;
667 }
668
669
670 /* RDIR */
671 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
672 {
673         register int ret;
674         size_t bytes;
675
676         if (!cret) return -2;
677         if (!listing) return -2;
678         if (*listing) return -2;
679
680         ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
681         return ret;
682 }
683
684
685 /*
686  * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
687  */
688 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
689 {
690         register int ret;
691         char aaa[16];
692
693         if (!cret) return -2;
694
695         if (msgnum) {
696                 sprintf(aaa, "SLRP %ld", msgnum);
697         }
698         else {
699                 sprintf(aaa, "SLRP HIGHEST");
700         }
701         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
702         return ret;
703 }
704
705
706 /* INVT */
707 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
708 {
709         register int ret;
710         char *aaa;
711
712         if (!cret) return -2;
713         if (!username) return -2;
714
715         aaa = (char *)malloc(strlen(username) + 6);
716         if (!aaa) return -1;
717
718         sprintf(aaa, "INVT %s", username);
719         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
720         free(aaa);
721         return ret;
722 }
723
724
725 /* KICK */
726 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
727 {
728         register int ret;
729         char *aaa;
730
731         if (!cret) return -1;
732         if (!username) return -1;
733
734         aaa = (char *)malloc(strlen(username) + 6);
735
736         sprintf(aaa, "KICK %s", username);
737         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
738         free(aaa);
739         return ret;
740 }
741
742
743 /* GETR */
744 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct ctdlroom **qret, char *cret)
745 {
746         register int ret;
747
748         if (!cret) return -2;
749         if (!qret) return -2;
750         if (!*qret) *qret = (struct ctdlroom *)calloc(1, sizeof (struct ctdlroom));
751         if (!*qret) return -1;
752
753         ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
754         if (ret / 100 == 2) {
755                 extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
756                 extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
757                 extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
758                 qret[0]->QRflags = extract_int(cret, 3);
759                 qret[0]->QRfloor = extract_int(cret, 4);
760                 qret[0]->QRorder = extract_int(cret, 5);
761                 qret[0]->QRdefaultview = extract_int(cret, 6);
762                 qret[0]->QRflags2 = extract_int(cret, 7);
763         }
764         return ret;
765 }
766
767
768 /* SETR */
769 /* set forget to kick all users out of room */
770 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct ctdlroom *qret, char *cret)
771 {
772         register int ret;
773         char *aaa;
774
775         if (!cret) return -2;
776         if (!qret) return -2;
777
778         aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
779                         strlen(qret->QRdirname) + 64);
780         if (!aaa) return -1;
781
782         sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
783                         qret->QRname, qret->QRpasswd, qret->QRdirname,
784                         qret->QRflags, forget, qret->QRfloor, qret->QRorder,
785                         qret->QRdefaultview, qret->QRflags2);
786         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
787         free(aaa);
788         return ret;
789 }
790
791
792 /* GETA */
793 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
794 {
795         if (!cret) return -1;
796
797         return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
798 }
799
800
801 /* SETA */
802 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
803 {
804         register int ret;
805         char *aaa;
806
807         if (!cret) return -2;
808         if (!username) return -2;
809
810         aaa = (char *)malloc(strlen(username) + 6);
811         if (!aaa) return -1;
812
813         sprintf(aaa, "SETA %s", username);
814         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
815         free(aaa);
816         return ret;
817 }
818
819
820 /* ENT0 */
821 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
822 {
823         register int ret;
824         char cmd[SIZ];
825
826         if (!cret) return -2;
827         if (!mr) return -2;
828
829         snprintf(cmd, sizeof cmd,
830                         "ENT0 %d|%s|%d|%d|%s|%s", flag, mr->recipient,
831                         mr->anonymous, mr->type, mr->subject, mr->author);
832         ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL,
833                         NULL, cret);
834         return ret;
835 }
836
837
838 /* RINF */
839 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
840 {
841         size_t bytes;
842
843         if (!cret) return -2;
844         if (!iret) return -2;
845         if (*iret) return -2;
846
847         return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
848 }
849
850
851 /* DELE */
852 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
853 {
854         char aaa[16];
855
856         if (!cret) return -2;
857         if (!msgnum) return -2;
858
859         sprintf(aaa, "DELE %ld", msgnum);
860         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
861 }
862
863
864 /* MOVE */
865 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
866 {
867         register int ret;
868         char *aaa;
869
870         if (!cret) return -2;
871         if (!destroom) return -2;
872         if (!msgnum) return -2;
873
874         aaa = (char *)malloc(strlen(destroom) + 28);
875         if (!aaa) return -1;
876
877         sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
878         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
879         free(aaa);
880         return ret;
881 }
882
883
884 /* KILL */
885 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
886 {
887         char aaa[16];
888
889         if (!cret) return -2;
890
891         sprintf(aaa, "KILL %d", for_real);
892         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
893 }
894
895
896 /* CRE8 */
897 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
898                 const char *password, int floor, char *cret)
899 {
900         register int ret;
901         char *aaa;
902
903         if (!cret) return -2;
904         if (!roomname) return -2;
905
906         if (password) {
907                 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
908                 if (!aaa) return -1;
909                 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
910                                 password, floor);
911         } else {
912                 aaa = (char *)malloc(strlen(roomname) + 40);
913                 if (!aaa) return -1;
914                 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
915                                 floor);
916         }
917         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
918         free(aaa);
919         return ret;
920 }
921
922
923 /* FORG */
924 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
925 {
926         if (!cret) return -2;
927
928         return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
929 }
930
931
932 /* MESG */
933 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
934 {
935         register int ret;
936         char *aaa;
937         size_t bytes;
938
939         if (!cret) return -2;
940         if (!mret) return -2;
941         if (*mret) return -2;
942         if (!message) return -2;
943
944         aaa = (char *)malloc(strlen(message) + 6);
945         if (!aaa) return -1;
946
947         sprintf(aaa, "MESG %s", message);
948         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
949         free(aaa);
950         return ret;
951 }
952
953
954 /* GNUR */
955 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
956 {
957         if (!cret) return -2;
958
959         return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
960 }
961
962
963 /* GREG */
964 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
965 {
966         register int ret;
967         char *aaa;
968         size_t bytes;
969
970         if (!cret) return -2;
971         if (!rret) return -2;
972         if (*rret) return -2;
973
974         if (username)
975                 aaa = (char *)malloc(strlen(username) + 6);
976         else
977                 aaa = (char *)malloc(12);
978         if (!aaa) return -1;
979
980         if (username)
981                 sprintf(aaa, "GREG %s", username);
982         else
983                 sprintf(aaa, "GREG _SELF_");
984         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
985         free(aaa);
986         return ret;
987 }
988
989
990 /* VALI */
991 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
992 {
993         register int ret;
994         char *aaa;
995
996         if (!cret) return -2;
997         if (!username) return -2;
998         if (axlevel < 0 || axlevel > 7) return -2;
999
1000         aaa = (char *)malloc(strlen(username) + 17);
1001         if (!aaa) return -1;
1002
1003         sprintf(aaa, "VALI %s|%d", username, axlevel);
1004         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1005         free(aaa);
1006         return ret;
1007 }
1008
1009
1010 /* EINF */
1011 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
1012 {
1013         char aaa[16];
1014
1015         if (!cret) return -1;
1016         if (!info) return -1;
1017
1018         sprintf(aaa, "EINF %d", for_real);
1019         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1020 }
1021
1022
1023 /* LIST */
1024 int CtdlIPCUserListing(CtdlIPC *ipc, char *searchstring, char **listing, char *cret)
1025 {
1026         size_t bytes;
1027         char *cmd;
1028         int ret;
1029
1030         if (!cret) return -1;
1031         if (!listing) return -1;
1032         if (*listing) return -1;
1033         if (!searchstring) return -1;
1034
1035         cmd = malloc(strlen(searchstring) + 10);
1036         sprintf(cmd, "LIST %s", searchstring);
1037
1038         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, listing, &bytes, cret);
1039         free(cmd);
1040         return(ret);
1041 }
1042
1043
1044 /* REGI */
1045 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
1046 {
1047         if (!cret) return -1;
1048         if (!info) return -1;
1049
1050         return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
1051                         NULL, NULL, cret);
1052 }
1053
1054
1055 /* CHEK */
1056 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1057 {
1058         register int ret;
1059
1060         if (!cret) return -1;
1061         if (!chek) return -1;
1062
1063         ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1064         if (ret / 100 == 2) {
1065                 chek->newmail = extract_long(cret, 0);
1066                 chek->needregis = extract_int(cret, 1);
1067                 chek->needvalid = extract_int(cret, 2);
1068         }
1069         return ret;
1070 }
1071
1072
1073 /* DELF */
1074 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1075 {
1076         register int ret;
1077         char *aaa;
1078
1079         if (!cret) return -2;
1080         if (!filename) return -2;
1081         
1082         aaa = (char *)malloc(strlen(filename) + 6);
1083         if (!aaa) return -1;
1084
1085         sprintf(aaa, "DELF %s", filename);
1086         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1087         free(aaa);
1088         return ret;
1089 }
1090
1091
1092 /* MOVF */
1093 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1094 {
1095         register int ret;
1096         char *aaa;
1097
1098         if (!cret) return -2;
1099         if (!filename) return -2;
1100         if (!destroom) return -2;
1101
1102         aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1103         if (!aaa) return -1;
1104
1105         sprintf(aaa, "MOVF %s|%s", filename, destroom);
1106         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1107         free(aaa);
1108         return ret;
1109 }
1110
1111
1112 /* NETF */
1113 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1114 {
1115         register int ret;
1116         char *aaa;
1117
1118         if (!cret) return -2;
1119         if (!filename) return -2;
1120         if (!destnode) return -2;
1121
1122         aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1123         if (!aaa) return -1;
1124
1125         sprintf(aaa, "NETF %s|%s", filename, destnode);
1126         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1127         free(aaa);
1128         return ret;
1129 }
1130
1131
1132 /* RWHO */
1133 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1134 {
1135         register int ret;
1136         size_t bytes;
1137
1138         if (!cret) return -1;
1139         if (!listing) return -1;
1140         if (*listing) return -1;
1141
1142         *stamp = CtdlIPCServerTime(ipc, cret);
1143         if (!*stamp)
1144                 *stamp = time(NULL);
1145         ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1146         return ret;
1147 }
1148
1149
1150 /* OPEN */
1151 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1152                 size_t resume,
1153                 void (*progress_gauge_callback)
1154                         (CtdlIPC*, unsigned long, unsigned long),
1155                 char *cret)
1156 {
1157         register int ret;
1158         size_t bytes;
1159         time_t last_mod;
1160         char mimetype[SIZ];
1161         char *aaa;
1162
1163         if (!cret) return -2;
1164         if (!filename) return -2;
1165         if (!buf) return -2;
1166         if (*buf) return -2;
1167         if (ipc->downloading) return -2;
1168
1169         aaa = (char *)malloc(strlen(filename) + 6);
1170         if (!aaa) return -1;
1171
1172         sprintf(aaa, "OPEN %s", filename);
1173         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1174         free(aaa);
1175         if (ret / 100 == 2) {
1176                 ipc->downloading = 1;
1177                 bytes = extract_long(cret, 0);
1178                 last_mod = extract_int(cret, 1);
1179                 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1180
1181                 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume,
1182                                         progress_gauge_callback, cret);
1183                 /*
1184                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, resume,
1185                                         progress_gauge_callback, cret);
1186                 */
1187
1188                 ret = CtdlIPCEndDownload(ipc, cret);
1189                 if (ret / 100 == 2)
1190                         sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1191                                         filename, mimetype);
1192         }
1193         return ret;
1194 }
1195
1196
1197 /* OPNA */
1198 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part,
1199                 void **buf,
1200                 void (*progress_gauge_callback)
1201                         (CtdlIPC*, unsigned long, unsigned long),
1202                 char *cret)
1203 {
1204         register int ret;
1205         size_t bytes;
1206         time_t last_mod;
1207         char filename[SIZ];
1208         char mimetype[SIZ];
1209         char aaa[SIZ];
1210
1211         if (!cret) return -2;
1212         if (!buf) return -2;
1213         if (*buf) return -2;
1214         if (!part) return -2;
1215         if (!msgnum) return -2;
1216         if (ipc->downloading) return -2;
1217
1218         sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1219         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_token(filename, cret, 2, '|', sizeof filename);
1225                 extract_token(mimetype, cret, 3, '|', sizeof mimetype);
1226                 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1227                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1228                 ret = CtdlIPCEndDownload(ipc, cret);
1229                 if (ret / 100 == 2)
1230                         sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1231                                         filename, mimetype);
1232         }
1233         return ret;
1234 }
1235
1236
1237 /* OIMG */
1238 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1239                 void (*progress_gauge_callback)
1240                         (CtdlIPC*, unsigned long, unsigned long),
1241                 char *cret)
1242 {
1243         register int ret;
1244         size_t bytes;
1245         time_t last_mod;
1246         char mimetype[SIZ];
1247         char *aaa;
1248
1249         if (!cret) return -1;
1250         if (!buf) return -1;
1251         if (*buf) return -1;
1252         if (!filename) return -1;
1253         if (ipc->downloading) return -1;
1254
1255         aaa = (char *)malloc(strlen(filename) + 6);
1256         if (!aaa) return -1;
1257
1258         sprintf(aaa, "OIMG %s", filename);
1259         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1260         free(aaa);
1261         if (ret / 100 == 2) {
1262                 ipc->downloading = 1;
1263                 bytes = extract_long(cret, 0);
1264                 last_mod = extract_int(cret, 1);
1265                 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1266 /*              ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1267                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1268                 ret = CtdlIPCEndDownload(ipc, cret);
1269                 if (ret / 100 == 2)
1270                         sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1271                                         filename, mimetype);
1272         }
1273         return ret;
1274 }
1275
1276
1277 /* UOPN */
1278 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1279                 const char *path,
1280                 void (*progress_gauge_callback)
1281                         (CtdlIPC*, unsigned long, unsigned long),
1282                 char *cret)
1283 {
1284         register int ret;
1285         char *aaa;
1286
1287         if (!cret) return -1;
1288         if (!save_as) return -1;
1289         if (!comment) return -1;
1290         if (!path) return -1;
1291         if (!*path) return -1;
1292         if (ipc->uploading) return -1;
1293
1294         aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1295         if (!aaa) return -1;
1296
1297         sprintf(aaa, "UOPN %s|%s", save_as, comment);
1298         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1299         free(aaa);
1300         if (ret / 100 == 2) {
1301                 ipc->uploading = 1;
1302                 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1303                 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1304                 ipc->uploading = 0;
1305         }
1306         return ret;
1307 }
1308
1309
1310 /* UIMG */
1311 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1312                 const char *save_as,
1313                 void (*progress_gauge_callback)
1314                         (CtdlIPC*, unsigned long, unsigned long),
1315                 char *cret)
1316 {
1317         register int ret;
1318         char *aaa;
1319
1320         if (!cret) return -1;
1321         if (!save_as) return -1;
1322         if (!path && for_real) return -1;
1323         if (!*path && for_real) return -1;
1324         if (ipc->uploading) return -1;
1325
1326         aaa = (char *)malloc(strlen(save_as) + 17);
1327         if (!aaa) return -1;
1328
1329         sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1330         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1331         free(aaa);
1332         if (ret / 100 == 2 && for_real) {
1333                 ipc->uploading = 1;
1334                 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1335                 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1336                 ipc->uploading = 0;
1337         }
1338         return ret;
1339 }
1340
1341
1342 /* QUSR */
1343 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1344 {
1345         register int ret;
1346         char *aaa;
1347
1348         if (!cret) return -2;
1349         if (!username) return -2;
1350
1351         aaa = (char *)malloc(strlen(username) + 6);
1352         if (!aaa) return -1;
1353
1354         sprintf(aaa, "QUSR %s", username);
1355         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1356         free(aaa);
1357         return ret;
1358 }
1359
1360
1361 /* LFLR */
1362 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1363 {
1364         size_t bytes;
1365
1366         if (!cret) return -2;
1367         if (!listing) return -2;
1368         if (*listing) return -2;
1369
1370         return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1371 }
1372
1373
1374 /* CFLR */
1375 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1376 {
1377         register int ret;
1378         char aaa[SIZ];
1379
1380         if (!cret) return -2;
1381         if (!name) return -2;
1382
1383         sprintf(aaa, "CFLR %s|%d", name, for_real);
1384         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1385         return ret;
1386 }
1387
1388
1389 /* KFLR */
1390 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1391 {
1392         char aaa[SIZ];
1393
1394         if (!cret) return -1;
1395         if (floornum < 0) return -1;
1396
1397         sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1398         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1399 }
1400
1401
1402 /* EFLR */
1403 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1404 {
1405         register int ret;
1406         char aaa[SIZ];
1407
1408         if (!cret) return -2;
1409         if (!floorname) return -2;
1410         if (floornum < 0) return -2;
1411
1412         sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1413         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1414         return ret;
1415 }
1416
1417
1418 /*
1419  * IDEN 
1420  *
1421  * You only need to fill out hostname, the defaults will be used if any of the
1422  * other fields are not set properly.
1423  */
1424 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid,
1425                 int revision, const char *software_name, const char *hostname,
1426                 char *cret)
1427 {
1428         register int ret;
1429         char *aaa;
1430
1431         if (developerid < 0 || clientid < 0 || revision < 0 ||
1432             !software_name) {
1433                 developerid = 8;
1434                 clientid = 0;
1435                 revision = REV_LEVEL - 600;
1436                 software_name = "Citadel (libcitadel)";
1437         }
1438         if (!hostname) return -2;
1439
1440         aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1441         if (!aaa) return -1;
1442
1443         sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1444                         revision, software_name, hostname);
1445         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1446         free(aaa);
1447         return ret;
1448 }
1449
1450
1451 /* SEXP */
1452 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1453                 char *cret)
1454 {
1455         register int ret;
1456         char *aaa;
1457
1458         if (!cret) return -2;
1459         if (!username) return -2;
1460
1461         aaa = (char *)malloc(strlen(username) + 8);
1462         if (!aaa) return -1;
1463
1464         if (text) {
1465                 sprintf(aaa, "SEXP %s|-", username);
1466                 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1467                                 NULL, NULL, cret);
1468         } else {
1469                 sprintf(aaa, "SEXP %s||", username);
1470                 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1471         }
1472         free(aaa);
1473         return ret;
1474 }
1475
1476
1477 /* GEXP */
1478 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1479 {
1480         size_t bytes;
1481
1482         if (!cret) return -2;
1483         if (!listing) return -2;
1484         if (*listing) return -2;
1485
1486         return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1487 }
1488
1489
1490 /* DEXP */
1491 /* mode is 0 = enable, 1 = disable, 2 = status */
1492 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1493 {
1494         char aaa[16];
1495
1496         if (!cret) return -2;
1497
1498         sprintf(aaa, "DEXP %d", mode);
1499         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1500 }
1501
1502
1503 /* EBIO */
1504 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1505 {
1506         if (!cret) return -2;
1507         if (!bio) return -2;
1508
1509         return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1510                         NULL, NULL, cret);
1511 }
1512
1513
1514 /* RBIO */
1515 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1516 {
1517         register int ret;
1518         size_t bytes;
1519         char *aaa;
1520
1521         if (!cret) return -2;
1522         if (!username) return -2;
1523         if (!listing) return -2;
1524         if (*listing) return -2;
1525
1526         aaa = (char *)malloc(strlen(username) + 6);
1527         if (!aaa) return -1;
1528
1529         sprintf(aaa, "RBIO %s", username);
1530         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1531         free(aaa);
1532         return ret;
1533 }
1534
1535
1536 /* LBIO */
1537 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1538 {
1539         size_t bytes;
1540
1541         if (!cret) return -2;
1542         if (!listing) return -2;
1543         if (*listing) return -2;
1544
1545         return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1546 }
1547
1548
1549 /* STEL */
1550 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1551 {
1552         char aaa[16];
1553
1554         if (!cret) return -1;
1555
1556         sprintf(aaa, "STEL %d", mode ? 1 : 0);
1557         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1558 }
1559
1560
1561 /* TERM */
1562 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1563 {
1564         char aaa[16];
1565
1566         if (!cret) return -1;
1567
1568         sprintf(aaa, "TERM %d", sid);
1569         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1570 }
1571
1572
1573 /* DOWN */
1574 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1575 {
1576         if (!cret) return -1;
1577
1578         return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1579 }
1580
1581
1582 /* SCDN */
1583 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1584 {
1585         char aaa[16];
1586
1587         if (!cret) return -1;
1588
1589         sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1590         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1591 }
1592
1593
1594 /* EMSG */
1595 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1596                 char *cret)
1597 {
1598         register int ret;
1599         char *aaa;
1600
1601         if (!cret) return -2;
1602         if (!text) return -2;
1603         if (!filename) return -2;
1604
1605         aaa = (char *)malloc(strlen(filename) + 6);
1606         if (!aaa) return -1;
1607
1608         sprintf(aaa, "EMSG %s", filename);
1609         ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1610         free(aaa);
1611         return ret;
1612 }
1613
1614
1615 /* HCHG */
1616 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1617 {
1618         register int ret;
1619         char *aaa;
1620
1621         if (!cret) return -2;
1622         if (!hostname) return -2;
1623
1624         aaa = (char *)malloc(strlen(hostname) + 6);
1625         if (!aaa) return -1;
1626
1627         sprintf(aaa, "HCHG %s", hostname);
1628         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1629         free(aaa);
1630         return ret;
1631 }
1632
1633
1634 /* RCHG */
1635 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1636 {
1637         register int ret;
1638         char *aaa;
1639
1640         if (!cret) return -2;
1641         if (!roomname) return -2;
1642
1643         aaa = (char *)malloc(strlen(roomname) + 6);
1644         if (!aaa) return -1;
1645
1646         sprintf(aaa, "RCHG %s", roomname);
1647         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1648         free(aaa);
1649         return ret;
1650 }
1651
1652
1653 /* UCHG */
1654 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1655 {
1656         register int ret;
1657         char *aaa;
1658
1659         if (!cret) return -2;
1660         if (!username) return -2;
1661
1662         aaa = (char *)malloc(strlen(username) + 6);
1663         if (!aaa) return -1;
1664
1665         sprintf(aaa, "UCHG %s", username);
1666         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1667         free(aaa);
1668         return ret;
1669 }
1670
1671
1672 /* TIME */
1673 /* This function returns the actual server time reported, or 0 if error */
1674 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1675 {
1676         register time_t tret;
1677         register int ret;
1678
1679         ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1680         if (ret / 100 == 2) {
1681                 tret = extract_long(cret, 0);
1682         } else {
1683                 tret = 0L;
1684         }
1685         return tret;
1686 }
1687
1688
1689 /* AGUP */
1690 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1691                                  struct ctdluser **uret, char *cret)
1692 {
1693         register int ret;
1694         char aaa[SIZ];
1695
1696         if (!cret) return -2;
1697         if (!uret) return -2;
1698         if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof(struct ctdluser));
1699         if (!*uret) return -1;
1700
1701         sprintf(aaa, "AGUP %s", who);
1702         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1703
1704         if (ret / 100 == 2) {
1705                 extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
1706                 extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
1707                 uret[0]->flags = extract_int(cret, 2);
1708                 uret[0]->timescalled = extract_long(cret, 3);
1709                 uret[0]->posted = extract_long(cret, 4);
1710                 uret[0]->axlevel = extract_int(cret, 5);
1711                 uret[0]->usernum = extract_long(cret, 6);
1712                 uret[0]->lastcall = extract_long(cret, 7);
1713                 uret[0]->USuserpurge = extract_int(cret, 8);
1714         }
1715         return ret;
1716 }
1717
1718
1719 /* ASUP */
1720 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct ctdluser *uret, char *cret)
1721 {
1722         register int ret;
1723         char *aaa;
1724
1725         if (!cret) return -2;
1726         if (!uret) return -2;
1727
1728         aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1729         if (!aaa) return -1;
1730
1731         sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1732                         uret->fullname, uret->password, uret->flags,
1733                         uret->timescalled, uret->posted, uret->axlevel,
1734                         uret->usernum, uret->lastcall, uret->USuserpurge);
1735         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1736         free(aaa);
1737         return ret;
1738 }
1739
1740
1741 /* GPEX */
1742 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1743 /* caller must free the struct ExpirePolicy */
1744 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1745                 struct ExpirePolicy **policy, char *cret)
1746 {
1747         static char *proto[] = {"room", "floor", "site", "mailboxes" };
1748         char aaa[11];
1749         register int ret;
1750
1751         if (!cret) return -2;
1752         if (!policy) return -2;
1753         if (!*policy) *policy = (struct ExpirePolicy *)calloc(1, sizeof(struct ExpirePolicy));
1754         if (!*policy) return -1;
1755         if (which < 0 || which > 3) return -2;
1756         
1757         sprintf(aaa, "GPEX %s", proto[which]);
1758         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1759         if (ret / 100 == 2) {
1760                 policy[0]->expire_mode = extract_int(cret, 0);
1761                 policy[0]->expire_value = extract_int(cret, 1);
1762         }
1763         return ret;
1764
1765 }
1766
1767
1768 /* SPEX */
1769 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1770 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1771 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1772                 struct ExpirePolicy *policy, char *cret)
1773 {
1774         char aaa[38];
1775         char *whichvals[] = { "room", "floor", "site", "mailboxes" };
1776
1777         if (!cret) return -2;
1778         if (which < 0 || which > 3) return -2;
1779         if (!policy) return -2;
1780         if (policy->expire_mode < 0 || policy->expire_mode > 3) return -2;
1781         if (policy->expire_mode >= 2 && policy->expire_value < 1) return -2;
1782
1783         sprintf(aaa, "SPEX %s|%d|%d", whichvals[which],
1784                         policy->expire_mode, policy->expire_value);
1785         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1786 }
1787
1788
1789 /* CONF GET */
1790 int CtdlIPCGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1791 {
1792         size_t bytes;
1793
1794         if (!cret) return -2;
1795         if (!listing) return -2;
1796         if (*listing) return -2;
1797
1798         return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1799                         listing, &bytes, cret);
1800 }
1801
1802
1803 /* CONF SET */
1804 int CtdlIPCSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1805 {
1806         if (!cret) return -2;
1807         if (!listing) return -2;
1808
1809         return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1810                         NULL, NULL, cret);
1811 }
1812
1813
1814 /* CONF GETSYS */
1815 int CtdlIPCGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1816                 char **listing, char *cret)
1817 {
1818         char *aaa;
1819         size_t bytes;
1820
1821         if (!cret) return -2;
1822         if (!mimetype) return -2;
1823         if (!listing) return -2;
1824         if (*listing) return -2;
1825
1826         aaa = malloc(strlen(mimetype) + 13);
1827         if (!aaa) return -1;
1828         sprintf(aaa, "CONF GETSYS|%s", mimetype);
1829         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1830                         listing, &bytes, cret);
1831 }
1832
1833
1834 /* CONF PUTSYS */
1835 int CtdlIPCSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1836                const char *listing, char *cret)
1837 {
1838         char *aaa;
1839
1840         if (!cret) return -2;
1841         if (!mimetype) return -2;
1842         if (!listing) return -2;
1843
1844         aaa = malloc(strlen(mimetype) + 13);
1845         if (!aaa) return -1;
1846         sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1847         return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1848                         NULL, NULL, cret);
1849 }
1850
1851
1852 /* GNET */
1853 int CtdlIPCGetRoomNetworkConfig(CtdlIPC *ipc, char **listing, char *cret)
1854 {
1855         size_t bytes;
1856
1857         if (!cret) return -2;
1858         if (!listing) return -2;
1859         if (*listing) return -2;
1860
1861         return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0,
1862                         listing, &bytes, cret);
1863 }
1864
1865
1866 /* SNET */
1867 int CtdlIPCSetRoomNetworkConfig(CtdlIPC *ipc, const char *listing, char *cret)
1868 {
1869         if (!cret) return -2;
1870         if (!listing) return -2;
1871
1872         return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing),
1873                         NULL, NULL, cret);
1874 }
1875
1876
1877 /* REQT */
1878 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1879 {
1880         char aaa[16];
1881
1882         if (!cret) return -2;
1883         if (session < 0) return -2;
1884
1885         sprintf(aaa, "REQT %d", session);
1886         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1887 }
1888
1889
1890 /* SEEN */
1891 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1892 {
1893         char aaa[27];
1894
1895         if (!cret) return -2;
1896         if (msgnum < 0) return -2;
1897
1898         sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1899         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1900 }
1901
1902
1903 /* STLS */
1904 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1905 {
1906         int a;
1907         int r;
1908         char buf[SIZ];
1909
1910 #ifdef HAVE_OPENSSL
1911         SSL *temp_ssl;
1912
1913         /* New SSL object */
1914         temp_ssl = SSL_new(ssl_ctx);
1915         if (!temp_ssl) {
1916                 error_printf("SSL_new failed: %s\n",
1917                                 ERR_reason_error_string(ERR_get_error()));
1918                 return -2;
1919         }
1920         /* Pointless flag waving */
1921 #if SSLEAY_VERSION_NUMBER >= 0x0922
1922         SSL_set_session_id_context(temp_ssl, "Citadel SID", 14);
1923 #endif
1924
1925         if (!access("/var/run/egd-pool", F_OK))
1926                 RAND_egd("/var/run/egd-pool");
1927
1928         if (!RAND_status()) {
1929                 error_printf("PRNG not properly seeded\n");
1930                 return -2;
1931         }
1932
1933         /* Associate network connection with SSL object */
1934         if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1935                 error_printf("SSL_set_fd failed: %s\n",
1936                                 ERR_reason_error_string(ERR_get_error()));
1937                 return -2;
1938         }
1939
1940         if (status_hook != NULL)
1941                 status_hook("Requesting encryption...\r");
1942
1943         /* Ready to start SSL/TLS */
1944         /* Old code
1945         CtdlIPC_putline(ipc, "STLS");
1946         CtdlIPC_getline(ipc, buf);
1947         if (buf[0] != '2') {
1948                 error_printf("Server can't start TLS: %s\n", buf);
1949                 return 0;
1950         }
1951         */
1952         r = CtdlIPCGenericCommand(ipc,
1953                                   "STLS", NULL, 0, NULL, NULL, cret);
1954         if (r / 100 != 2) {
1955                 error_printf("Server can't start TLS: %s\n", buf);
1956                 endtls(temp_ssl);
1957                 return r;
1958         }
1959
1960         /* Do SSL/TLS handshake */
1961         if ((a = SSL_connect(temp_ssl)) < 1) {
1962                 error_printf("SSL_connect failed: %s\n",
1963                                 ERR_reason_error_string(ERR_get_error()));
1964                 endtls(temp_ssl);
1965                 return -2;
1966         }
1967         ipc->ssl = temp_ssl;
1968
1969         BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1970         {
1971                 int bits, alg_bits;
1972
1973                 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1974                 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1975                                 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1976                                 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1977                                 bits, alg_bits);
1978         }
1979         return r;
1980 #else
1981         return 0;
1982 #endif /* HAVE_OPENSSL */
1983 }
1984
1985
1986 #ifdef HAVE_OPENSSL
1987 static void endtls(SSL *ssl)
1988 {
1989         if (ssl) {
1990                 SSL_shutdown(ssl);
1991                 SSL_free(ssl);
1992         }
1993 }
1994 #endif
1995
1996
1997 /* QDIR */
1998 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
1999 {
2000         char *aaa;
2001
2002         if (!address) return -2;
2003         if (!cret) return -2;
2004
2005         aaa = (char *)malloc(strlen(address) + 6);
2006         if (!aaa) return -1;
2007
2008         sprintf(aaa, "QDIR %s", address);
2009         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2010 }
2011
2012
2013 /* IPGM */
2014 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
2015 {
2016         char aaa[30];
2017
2018         if (!cret) return -2;
2019         sprintf(aaa, "IPGM %d", secret);
2020         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2021 }
2022
2023
2024 /* FSCK */
2025 int CtdlIPCMessageBaseCheck(CtdlIPC *ipc, char **mret, char *cret)
2026 {
2027         size_t size = 0;
2028
2029         if (!cret) return -2;
2030         if (!mret) return -2;
2031         if (*mret) return -2;
2032
2033         return CtdlIPCGenericCommand(ipc, "FSCK", NULL, 0, mret, &size, cret);
2034 }
2035
2036
2037 /*
2038  * Not implemented:
2039  * 
2040  * CHAT
2041  * ETLS
2042  * EXPI
2043  * GTLS
2044  * IGAB
2045  * MSG3
2046  * MSG4
2047  * NDOP
2048  * NETP
2049  * NUOP
2050  * SMTP
2051  */
2052
2053
2054 /* ************************************************************************** */
2055 /*             Stuff below this line is not for public consumption            */
2056 /* ************************************************************************** */
2057
2058
2059 INLINE void CtdlIPC_lock(CtdlIPC *ipc)
2060 {
2061         if (ipc->network_status_cb) ipc->network_status_cb(1);
2062 #ifdef THREADED_CLIENT
2063         pthread_mutex_lock(&(ipc->mutex));
2064 #endif
2065 }
2066
2067
2068 INLINE void CtdlIPC_unlock(CtdlIPC *ipc)
2069 {
2070 #ifdef THREADED_CLIENT
2071         pthread_mutex_unlock(&(ipc->mutex));
2072 #endif
2073         if (ipc->network_status_cb) ipc->network_status_cb(0);
2074 }
2075
2076
2077 /* Read a listing from the server up to 000.  Append to dest if it exists */
2078 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
2079 {
2080         size_t length = 0;
2081         size_t linelength;
2082         char *ret = NULL;
2083         char aaa[SIZ];
2084
2085         ret = dest;
2086         if (ret != NULL) {
2087                 length = strlen(ret);
2088         } else {
2089                 length = 0;
2090         }
2091
2092         while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2093                 linelength = strlen(aaa);
2094                 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
2095                 if (ret) {
2096                         strcpy(&ret[length], aaa);
2097                         length += linelength;
2098                         strcpy(&ret[length++], "\n");
2099                 }
2100         }
2101
2102         return(ret);
2103 }
2104
2105
2106 /* Send a listing to the server; generate the ending 000. */
2107 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
2108 {
2109         char *text;
2110
2111         text = (char *)malloc(strlen(listing) + 6);
2112         if (text) {
2113                 strcpy(text, listing);
2114                 while (text[strlen(text) - 1] == '\n')
2115                         text[strlen(text) - 1] = '\0';
2116                 strcat(text, "\n000");
2117                 CtdlIPC_putline(ipc, text);
2118                 free(text);
2119                 text = NULL;
2120         } else {
2121                 /* Malloc failed but we are committed to send */
2122                 /* This may result in extra blanks at the bottom */
2123                 CtdlIPC_putline(ipc, text);
2124                 CtdlIPC_putline(ipc, "000");
2125         }
2126         return 0;
2127 }
2128
2129
2130 /* Partial read of file from server */
2131 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2132 {
2133         register size_t len = 0;
2134         char aaa[SIZ];
2135
2136         if (!buf) return 0;
2137         if (!cret) return 0;
2138         if (bytes < 1) return 0;
2139
2140         CtdlIPC_lock(ipc);
2141         sprintf(aaa, "READ %d|%d", (int)offset, (int)bytes);
2142         CtdlIPC_putline(ipc, aaa);
2143         CtdlIPC_getline(ipc, aaa);
2144         if (aaa[0] != '6')
2145                 strcpy(cret, &aaa[4]);
2146         else {
2147                 len = extract_long(&aaa[4], 0);
2148                 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2149                 if (*buf) {
2150                         /* I know what I'm doing */
2151                         serv_read(ipc, ((char *)(*buf) + offset), len);
2152                 } else {
2153                         /* We have to read regardless */
2154                         serv_read(ipc, aaa, len);
2155                         len = 0;
2156                 }
2157         }
2158         CtdlIPC_unlock(ipc);
2159         return len;
2160 }
2161
2162
2163 /* CLOS */
2164 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2165 {
2166         register int ret;
2167
2168         if (!cret) return -2;
2169         if (!ipc->downloading) return -2;
2170
2171         ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2172         if (ret / 100 == 2)
2173                 ipc->downloading = 0;
2174         return ret;
2175 }
2176
2177
2178 /* MSGP */
2179 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2180         register int ret;
2181         char cmd[SIZ];
2182         
2183         snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2184         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2185         return ret;
2186 }
2187
2188
2189
2190 /* READ */
2191 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
2192                 void (*progress_gauge_callback)
2193                         (CtdlIPC*, unsigned long, unsigned long),
2194                char *cret)
2195 {
2196         register size_t len;
2197
2198         if (!cret) return -1;
2199         if (!buf) return -1;
2200         if (*buf) return -1;
2201         if (!ipc->downloading) return -1;
2202
2203         len = resume;
2204         if (progress_gauge_callback)
2205                 progress_gauge_callback(ipc, len, bytes);
2206         while (len < bytes) {
2207                 register size_t block;
2208
2209                 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2210                 if (block == 0) {
2211                         free(*buf);
2212                         return 0;
2213                 }
2214                 len += block;
2215                 if (progress_gauge_callback)
2216                         progress_gauge_callback(ipc, len, bytes);
2217         }
2218         return len;
2219 }
2220
2221 /* READ - pipelined */
2222 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2223                size_t resume,
2224                 void (*progress_gauge_callback)
2225                         (CtdlIPC*, unsigned long, unsigned long),
2226                char *cret)
2227 {
2228         register size_t len;
2229         register int calls;     /* How many calls in the pipeline */
2230         register int i;         /* iterator */
2231         char aaa[4096];
2232
2233         if (!cret) return -1;
2234         if (!buf) return -1;
2235         if (*buf) return -1;
2236         if (!ipc->downloading) return -1;
2237
2238         *buf = (void *)realloc(*buf, bytes - resume);
2239         if (!*buf) return -1;
2240
2241         len = 0;
2242         CtdlIPC_lock(ipc);
2243         if (progress_gauge_callback)
2244                 progress_gauge_callback(ipc, len, bytes);
2245
2246         /* How many calls will be in the pipeline? */
2247         calls = (bytes - resume) / 4096;
2248         if ((bytes - resume) % 4096) calls++;
2249
2250         /* Send all requests at once */
2251         for (i = 0; i < calls; i++) {
2252                 sprintf(aaa, "READ %d|4096", (int)(i * 4096 + resume) );
2253                 CtdlIPC_putline(ipc, aaa);
2254         }
2255
2256         /* Receive all responses at once */
2257         for (i = 0; i < calls; i++) {
2258                 CtdlIPC_getline(ipc, aaa);
2259                 if (aaa[0] != '6')
2260                         strcpy(cret, &aaa[4]);
2261                 else {
2262                         len = extract_long(&aaa[4], 0);
2263                         /* I know what I'm doing */
2264                         serv_read(ipc, ((char *)(*buf) + (i * 4096)), len);
2265                 }
2266                 if (progress_gauge_callback)
2267                         progress_gauge_callback(ipc, i * 4096 + len, bytes);
2268         }
2269         CtdlIPC_unlock(ipc);
2270         return len;
2271 }
2272
2273
2274 /* UCLS */
2275 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2276 {
2277         register int ret;
2278         char cmd[8];
2279
2280         if (!cret) return -1;
2281         if (!ipc->uploading) return -1;
2282
2283         sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2284         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2285         ipc->uploading = 0;
2286         return ret;
2287 }
2288
2289
2290 /* WRIT */
2291 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2292                 void (*progress_gauge_callback)
2293                         (CtdlIPC*, unsigned long, unsigned long),
2294                 char *cret)
2295 {
2296         register int ret = -1;
2297         register size_t offset = 0;
2298         size_t bytes;
2299         char aaa[SIZ];
2300         char buf[4096];
2301         FILE *fd;
2302
2303         if (!cret) return -1;
2304         if (!path) return -1;
2305         if (!*path) return -1;
2306
2307         fd = fopen(path, "r");
2308         if (!fd) return -2;
2309
2310         fseek(fd, 0L, SEEK_END);
2311         bytes = ftell(fd);
2312         rewind(fd);
2313
2314         if (progress_gauge_callback)
2315                 progress_gauge_callback(ipc, 0, bytes);
2316
2317         while (offset < bytes) {
2318                 register size_t to_write;
2319
2320                 /* Read some data in */
2321                 to_write = fread(buf, 1, 4096, fd);
2322                 if (!to_write) {
2323                         if (feof(fd) || ferror(fd)) break;
2324                 }
2325                 sprintf(aaa, "WRIT %d", (int)to_write);
2326                 CtdlIPC_putline(ipc, aaa);
2327                 CtdlIPC_getline(ipc, aaa);
2328                 strcpy(cret, &aaa[4]);
2329                 ret = atoi(aaa);
2330                 if (aaa[0] == '7') {
2331                         to_write = extract_long(&aaa[4], 0);
2332                         
2333                         serv_write(ipc, buf, to_write);
2334                         offset += to_write;
2335                         if (progress_gauge_callback)
2336                                 progress_gauge_callback(ipc, offset, bytes);
2337                         /* Detect short reads and back up if needed */
2338                         /* offset will never be negative anyway */
2339                         fseek(fd, (signed)offset, SEEK_SET);
2340                 } else {
2341                         break;
2342                 }
2343         }
2344         if (progress_gauge_callback)
2345                 progress_gauge_callback(ipc, 1, 1);
2346         return (!ferror(fd) ? ret : -2);
2347 }
2348
2349
2350 /*
2351  * Generic command method.  This method should handle any server command
2352  * except for CHAT.  It takes the following arguments:
2353  *
2354  * ipc                  The server to speak with
2355  * command              Preformatted command to send to server
2356  * to_send              A text or binary file to send to server
2357  *                      (only sent if server requests it)
2358  * bytes_to_send        The number of bytes in to_send (required if
2359  *                      sending binary, optional if sending listing)
2360  * to_receive           Pointer to a NULL pointer, if the server
2361  *                      sends text or binary we will allocate memory
2362  *                      for the file and stuff it here
2363  * bytes_to_receive     If a file is received, we will store its
2364  *                      byte count here
2365  * proto_response       The protocol response.  Caller must provide
2366  *                      this buffer and ensure that it is at least
2367  *                      128 bytes in length.
2368  *
2369  * This function returns a number equal to the protocol response number,
2370  * -1 if an internal error occurred, -2 if caller provided bad values,
2371  * or 0 - the protocol response number if bad values were found during
2372  * the protocol exchange.
2373  * It stores the protocol response string (minus the number) in 
2374  * protocol_response as described above.  Some commands send additional
2375  * data in this string.
2376  */
2377 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2378                 const char *command, const char *to_send,
2379                 size_t bytes_to_send, char **to_receive, 
2380                 size_t *bytes_to_receive, char *proto_response)
2381 {
2382         char buf[SIZ];
2383         register int ret;
2384         int watch_ssl = 0;
2385
2386         if (!command) return -2;
2387         if (!proto_response) return -2;
2388
2389 #ifdef HAVE_OPENSSL
2390         if (ipc->ssl) watch_ssl = 1;
2391 #endif
2392
2393         CtdlIPC_lock(ipc);
2394         CtdlIPC_putline(ipc, command);
2395         while (1) {
2396                 CtdlIPC_getline(ipc, proto_response);
2397                 if (proto_response[3] == '*')
2398                         instant_msgs = 1;
2399                 ret = atoi(proto_response);
2400                 strcpy(proto_response, &proto_response[4]);
2401                 switch (ret / 100) {
2402                 default:                        /* Unknown, punt */
2403                 case 2:                         /* OK */
2404                 case 3:                         /* MORE_DATA */
2405                 case 5:                         /* ERROR */
2406                         /* Don't need to do anything */
2407                         break;
2408                 case 1:                         /* LISTING_FOLLOWS */
2409                         if (to_receive && !*to_receive && bytes_to_receive) {
2410                                 *to_receive = CtdlIPCReadListing(ipc, NULL);
2411                         } else { /* Drain */
2412                                 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2413                                 ret = -ret;
2414                         }
2415                         break;
2416                 case 4:                         /* SEND_LISTING */
2417                         if (to_send) {
2418                                 CtdlIPCSendListing(ipc, to_send);
2419                         } else {
2420                                 /* No listing given, fake it */
2421                                 CtdlIPC_putline(ipc, "000");
2422                                 ret = -ret;
2423                         }
2424                         break;
2425                 case 6:                         /* BINARY_FOLLOWS */
2426                         if (to_receive && !*to_receive && bytes_to_receive) {
2427                                 *bytes_to_receive =
2428                                         extract_long(proto_response, 0);
2429                                 *to_receive = (char *)
2430                                         malloc((size_t)*bytes_to_receive);
2431                                 if (!*to_receive) {
2432                                         ret = -1;
2433                                 } else {
2434                                         serv_read(ipc, *to_receive,
2435                                                         *bytes_to_receive);
2436                                 }
2437                         } else {
2438                                 /* Drain */
2439                                 size_t drain;
2440
2441                                 drain = extract_long(proto_response, 0);
2442                                 while (drain > SIZ) {
2443                                         serv_read(ipc, buf, SIZ);
2444                                         drain -= SIZ;
2445                                 }
2446                                 serv_read(ipc, buf, drain);
2447                                 ret = -ret;
2448                         }
2449                         break;
2450                 case 7:                         /* SEND_BINARY */
2451                         if (to_send && bytes_to_send) {
2452                                 serv_write(ipc, to_send, bytes_to_send);
2453                         } else if (bytes_to_send) {
2454                                 /* Fake it, send nulls */
2455                                 size_t fake;
2456
2457                                 fake = bytes_to_send;
2458                                 memset(buf, '\0', SIZ);
2459                                 while (fake > SIZ) {
2460                                         serv_write(ipc, buf, SIZ);
2461                                         fake -= SIZ;
2462                                 }
2463                                 serv_write(ipc, buf, fake);
2464                                 ret = -ret;
2465                         } /* else who knows?  DANGER WILL ROBINSON */
2466                         break;
2467                 case 8:                         /* START_CHAT_MODE */
2468                         if (!strncasecmp(command, "CHAT", 4)) {
2469                                 /* Don't call chatmode with generic! */
2470                                 CtdlIPC_putline(ipc, "/quit");
2471                                 ret = -ret;
2472                         } else {
2473                                 /* In this mode we send then receive listing */
2474                                 if (to_send) {
2475                                         CtdlIPCSendListing(ipc, to_send);
2476                                 } else {
2477                                         /* No listing given, fake it */
2478                                         CtdlIPC_putline(ipc, "000");
2479                                         ret = -ret;
2480                                 }
2481                                 if (to_receive && !*to_receive
2482                                                 && bytes_to_receive) {
2483                                         *to_receive = CtdlIPCReadListing(ipc, NULL);
2484                                 } else { /* Drain */
2485                                         while (CtdlIPC_getline(ipc, buf),
2486                                                         strcmp(buf, "000")) ;
2487                                         ret = -ret;
2488                                 }
2489                         }
2490                         break;
2491                 case 9:                         /* ASYNC_MSG */
2492                         /* CtdlIPCDoAsync(ret, proto_response); */
2493                         free(CtdlIPCReadListing(ipc, NULL));    /* STUB FIXME */
2494                         break;
2495                 }
2496                 if (ret / 100 != 9)
2497                         break;
2498         }
2499         CtdlIPC_unlock(ipc);
2500         return ret;
2501 }
2502
2503
2504 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2505 {
2506         struct hostent *phe;
2507         struct servent *pse;
2508         struct protoent *ppe;
2509         struct sockaddr_in sin;
2510         int s, type;
2511
2512         memset(&sin, 0, sizeof(sin));
2513         sin.sin_family = AF_INET;
2514
2515         pse = getservbyname(service, protocol);
2516         if (pse != NULL) {
2517                 sin.sin_port = pse->s_port;
2518         }
2519         else if (atoi(service) > 0) {
2520                 sin.sin_port = htons(atoi(service));
2521         }
2522         else {
2523                 sin.sin_port = htons(defaultPort);
2524         }
2525         phe = gethostbyname(host);
2526         if (phe) {
2527                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2528         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2529                 return -1;
2530         }
2531         if ((ppe = getprotobyname(protocol)) == 0) {
2532                 return -1;
2533         }
2534         if (!strcmp(protocol, "udp")) {
2535                 type = SOCK_DGRAM;
2536         } else {
2537                 type = SOCK_STREAM;
2538         }
2539
2540         s = socket(PF_INET, type, ppe->p_proto);
2541         if (s < 0) {
2542                 return -1;
2543         }
2544
2545         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2546                 close(s);
2547                 return -1;
2548         }
2549
2550         return (s);
2551 }
2552
2553 static int uds_connectsock(int *isLocal, char *sockpath)
2554 {
2555         struct sockaddr_un addr;
2556         int s;
2557
2558         memset(&addr, 0, sizeof(addr));
2559         addr.sun_family = AF_UNIX;
2560         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2561
2562         s = socket(AF_UNIX, SOCK_STREAM, 0);
2563         if (s < 0) {
2564                 return -1;
2565         }
2566
2567         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2568                 close(s);
2569                 return -1;
2570         }
2571
2572         *isLocal = 1;
2573         return s;
2574 }
2575
2576
2577 /*
2578  * input binary data from socket
2579  */
2580 static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes)
2581 {
2582         unsigned int len, rlen;
2583
2584 #if defined(HAVE_OPENSSL)
2585         if (ipc->ssl) {
2586                 serv_read_ssl(ipc, buf, bytes);
2587                 return;
2588         }
2589 #endif
2590         len = 0;
2591         while (len < bytes) {
2592                 rlen = read(ipc->sock, &buf[len], bytes - len);
2593                 if (rlen < 1) {
2594                         connection_died(ipc, 0);
2595                         return;
2596                 }
2597                 len += rlen;
2598         }
2599 }
2600
2601
2602 /*
2603  * send binary to server
2604  */
2605 static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
2606 {
2607         unsigned int bytes_written = 0;
2608         int retval;
2609
2610 #if defined(HAVE_OPENSSL)
2611         if (ipc->ssl) {
2612                 serv_write_ssl(ipc, buf, nbytes);
2613                 return;
2614         }
2615 #endif
2616         while (bytes_written < nbytes) {
2617                 retval = write(ipc->sock, &buf[bytes_written],
2618                                nbytes - bytes_written);
2619                 if (retval < 1) {
2620                         connection_died(ipc, 0);
2621                         return;
2622                 }
2623                 bytes_written += retval;
2624         }
2625 }
2626
2627
2628 #ifdef HAVE_OPENSSL
2629 /*
2630  * input binary data from encrypted connection
2631  */
2632 static void serv_read_ssl(CtdlIPC* ipc, char *buf, unsigned int bytes)
2633 {
2634         int len, rlen;
2635         char junk[1];
2636
2637         len = 0;
2638         while (len < bytes) {
2639                 if (SSL_want_read(ipc->ssl)) {
2640                         if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2641                                 error_printf("SSL_write in serv_read:\n");
2642                                 ERR_print_errors_fp(stderr);
2643                         }
2644                 }
2645                 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2646                 if (rlen < 1) {
2647                         long errval;
2648
2649                         errval = SSL_get_error(ipc->ssl, rlen);
2650                         if (errval == SSL_ERROR_WANT_READ ||
2651                                         errval == SSL_ERROR_WANT_WRITE) {
2652                                 sleep(1);
2653                                 continue;
2654                         }
2655                         if (errval == SSL_ERROR_ZERO_RETURN ||
2656                                         errval == SSL_ERROR_SSL) {
2657                                 serv_read(ipc, &buf[len], bytes - len);
2658                                 return;
2659                         }
2660                         error_printf("SSL_read in serv_read: %s\n",
2661                                         ERR_reason_error_string(ERR_peek_error()));
2662                         connection_died(ipc, 1);
2663                         return;
2664                 }
2665                 len += rlen;
2666         }
2667 }
2668
2669
2670 /*
2671  * send binary to server encrypted
2672  */
2673 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
2674 {
2675         unsigned int bytes_written = 0;
2676         int retval;
2677         char junk[1];
2678
2679         while (bytes_written < nbytes) {
2680                 if (SSL_want_write(ipc->ssl)) {
2681                         if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2682                                 error_printf("SSL_read in serv_write:\n");
2683                                 ERR_print_errors_fp(stderr);
2684                         }
2685                 }
2686                 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2687                                 nbytes - bytes_written);
2688                 if (retval < 1) {
2689                         long errval;
2690
2691                         errval = SSL_get_error(ipc->ssl, retval);
2692                         if (errval == SSL_ERROR_WANT_READ ||
2693                                         errval == SSL_ERROR_WANT_WRITE) {
2694                                 sleep(1);
2695                                 continue;
2696                         }
2697                         if (errval == SSL_ERROR_ZERO_RETURN ||
2698                                         errval == SSL_ERROR_SSL) {
2699                                 serv_write(ipc, &buf[bytes_written],
2700                                                 nbytes - bytes_written);
2701                                 return;
2702                         }
2703                         error_printf("SSL_write in serv_write: %s\n",
2704                                         ERR_reason_error_string(ERR_peek_error()));
2705                         connection_died(ipc, 1);
2706                         return;
2707                 }
2708                 bytes_written += retval;
2709         }
2710 }
2711
2712
2713 static void CtdlIPC_init_OpenSSL(void)
2714 {
2715         int a;
2716         SSL_METHOD *ssl_method;
2717         DH *dh;
2718         
2719         /* already done init */
2720         if (ssl_ctx) {
2721                 return;
2722         }
2723
2724         /* Get started */
2725         a = 0;
2726         ssl_ctx = NULL;
2727         dh = NULL;
2728         SSL_load_error_strings();
2729         SSLeay_add_ssl_algorithms();
2730
2731         /* Set up the SSL context in which we will oeprate */
2732         ssl_method = SSLv23_client_method();
2733         ssl_ctx = SSL_CTX_new(ssl_method);
2734         if (!ssl_ctx) {
2735                 error_printf("SSL_CTX_new failed: %s\n",
2736                                 ERR_reason_error_string(ERR_get_error()));
2737                 return;
2738         }
2739         /* Any reasonable cipher we can get */
2740         if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2741                 error_printf("No ciphers available for encryption\n");
2742                 return;
2743         }
2744         SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2745         
2746         /* Load DH parameters into the context */
2747         dh = DH_new();
2748         if (!dh) {
2749                 error_printf("Can't allocate a DH object: %s\n",
2750                                 ERR_reason_error_string(ERR_get_error()));
2751                 return;
2752         }
2753         if (!(BN_hex2bn(&(dh->p), DH_P))) {
2754                 error_printf("Can't assign DH_P: %s\n",
2755                                 ERR_reason_error_string(ERR_get_error()));
2756                 DH_free(dh);
2757                 return;
2758         }
2759         if (!(BN_hex2bn(&(dh->g), DH_G))) {
2760                 error_printf("Can't assign DH_G: %s\n",
2761                                 ERR_reason_error_string(ERR_get_error()));
2762                 DH_free(dh);
2763                 return;
2764         }
2765         dh->length = DH_L;
2766         SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2767         DH_free(dh);
2768
2769 #ifdef THREADED_CLIENT
2770         /* OpenSSL requires callbacks for threaded clients */
2771         CRYPTO_set_locking_callback(ssl_lock);
2772         CRYPTO_set_id_callback(id_callback);
2773
2774         /* OpenSSL requires us to do semaphores for threaded clients */
2775         Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2776         if (!Critters) {
2777                 perror("malloc failed");
2778                 exit(1);
2779         } else {
2780                 for (a = 0; a < CRYPTO_num_locks(); a++) {
2781                         Critters[a] = malloc(sizeof (pthread_mutex_t));
2782                         if (!Critters[a]) {
2783                                 perror("malloc failed");
2784                                 exit(1);
2785                         }
2786                         pthread_mutex_init(Critters[a], NULL);
2787                 }
2788         }
2789 #endif /* THREADED_CLIENT */       
2790 }
2791
2792
2793 static void ssl_lock(int mode, int n, const char *file, int line)
2794 {
2795 #ifdef THREADED_CLIENT
2796         if (mode & CRYPTO_LOCK)
2797                 pthread_mutex_lock(Critters[n]);
2798         else
2799                 pthread_mutex_unlock(Critters[n]);
2800 #endif /* THREADED_CLIENT */
2801 }
2802
2803 #ifdef THREADED_CLIENT
2804 static unsigned long id_callback(void) {
2805         return (unsigned long)pthread_self();
2806 }
2807 #endif /* THREADED_CLIENT */
2808 #endif /* HAVE_OPENSSL */
2809
2810
2811 /*
2812  * input string from socket - implemented in terms of serv_read()
2813  */
2814 static void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2815 {
2816         int i;
2817
2818         /* Read one character at a time. */
2819         for (i = 0;; i++) {
2820                 serv_read(ipc, &buf[i], 1);
2821                 if (buf[i] == '\n' || i == (SIZ-1))
2822                         break;
2823         }
2824
2825         /* If we got a long line, discard characters until the newline. */
2826         if (i == (SIZ-1))
2827                 while (buf[i] != '\n')
2828                         serv_read(ipc, &buf[i], 1);
2829
2830         /* Strip the trailing newline (and carriage return, if present) */
2831         if (buf[i] == 10) buf[i--] = 0;
2832         if (buf[i] == 13) buf[i--] = 0;
2833 }
2834
2835 void CtdlIPC_chat_recv(CtdlIPC* ipc, char* buf)
2836 {
2837         CtdlIPC_getline(ipc, buf);
2838 }
2839
2840 /*
2841  * send line to server - implemented in terms of serv_write()
2842  */
2843 static void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2844 {
2845         char *cmd = NULL;
2846         int len;
2847
2848         len = strlen(buf);
2849         cmd = malloc(len + 2);
2850         if (!cmd) {
2851                 /* This requires no extra memory */
2852                 serv_write(ipc, buf, len);
2853                 serv_write(ipc, "\n", 1);
2854         } else {
2855                 /* This is network-optimized */
2856                 strncpy(cmd, buf, len);
2857                 strcpy(cmd + len, "\n");
2858                 serv_write(ipc, cmd, len + 1);
2859                 free(cmd);
2860         }
2861
2862         ipc->last_command_sent = time(NULL);
2863 }
2864
2865 void CtdlIPC_chat_send(CtdlIPC* ipc, const char* buf)
2866 {
2867         CtdlIPC_putline(ipc, buf);
2868 }
2869
2870
2871 /*
2872  * attach to server
2873  */
2874 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2875 {
2876         int a;
2877         char cithost[SIZ];
2878         char citport[SIZ];
2879         char sockpath[SIZ];
2880         CtdlIPC* ipc;
2881
2882         ipc = ialloc(CtdlIPC);
2883         if (!ipc) {
2884                 return 0;
2885         }
2886 #if defined(HAVE_OPENSSL)
2887         ipc->ssl = NULL;
2888         CtdlIPC_init_OpenSSL();
2889 #endif
2890 #if defined(HAVE_PTHREAD_H)
2891         pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2892 #endif
2893         ipc->sock = -1;                 /* Not connected */
2894         ipc->isLocal = 0;               /* Not local, of course! */
2895         ipc->downloading = 0;
2896         ipc->uploading = 0;
2897         ipc->last_command_sent = 0L;
2898         ipc->network_status_cb = NULL;
2899
2900         strcpy(cithost, DEFAULT_HOST);  /* default host */
2901         strcpy(citport, DEFAULT_PORT);  /* default port */
2902
2903         /* Allow caller to supply our values (Windows) */
2904         if (hostbuf && strlen(hostbuf) > 0)
2905                 strcpy(cithost, hostbuf);
2906         if (portbuf && strlen(portbuf) > 0)
2907                 strcpy(citport, portbuf);
2908
2909         /* Read host/port from command line if present */
2910         for (a = 0; a < argc; ++a) {
2911                 if (a == 0) {
2912                         /* do nothing */
2913                 } else if (a == 1) {
2914                         strcpy(cithost, argv[a]);
2915                 } else if (a == 2) {
2916                         strcpy(citport, argv[a]);
2917                 } else {
2918                         error_printf("%s: usage: ",argv[0]);
2919                         error_printf("%s [host] [port] ",argv[0]);
2920                         ifree(ipc);
2921                         errno = EINVAL;
2922                         return 0;
2923                 }
2924         }
2925
2926         if ((!strcmp(cithost, "localhost"))
2927            || (!strcmp(cithost, "127.0.0.1"))) {
2928                 ipc->isLocal = 1;
2929         }
2930
2931         /* If we're using a unix domain socket we can do a bunch of stuff */
2932         if (!strcmp(cithost, UDS)) {
2933                 if (!strcasecmp(citport, DEFAULT_PORT)) {
2934                         snprintf(sockpath, sizeof sockpath, "%s%s",
2935                                 CTDLDIR, "/citadel.socket");
2936                 }
2937                 else {
2938                         snprintf(sockpath, sizeof sockpath, "%s%s",
2939                                 citport, "/citadel.socket");
2940                 }
2941                 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2942                 if (ipc->sock == -1) {
2943                         ifree(ipc);
2944                         return 0;
2945                 }
2946                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2947                 if (portbuf != NULL) strcpy(portbuf, sockpath);
2948                 return ipc;
2949         }
2950
2951         ipc->sock = connectsock(cithost, citport, "tcp", 504);
2952         if (ipc->sock == -1) {
2953                 ifree(ipc);
2954                 return 0;
2955         }
2956         if (hostbuf != NULL) strcpy(hostbuf, cithost);
2957         if (portbuf != NULL) strcpy(portbuf, citport);
2958         return ipc;
2959 }
2960
2961
2962 /*
2963  * Disconnect and delete the IPC class (destructor)
2964  */
2965 void CtdlIPC_delete(CtdlIPC* ipc)
2966 {
2967 #ifdef HAVE_OPENSSL
2968         if (ipc->ssl) {
2969                 SSL_shutdown(ipc->ssl);
2970                 SSL_free(ipc->ssl);
2971                 ipc->ssl = NULL;
2972         }
2973 #endif
2974         if (ipc->sock > -1) {
2975                 shutdown(ipc->sock, 2); /* Close it up */
2976                 ipc->sock = -1;
2977         }
2978         ifree(ipc);
2979 }
2980
2981
2982 /*
2983  * Disconnect and delete the IPC class (destructor)
2984  * Also NULLs out the pointer
2985  */
2986 void CtdlIPC_delete_ptr(CtdlIPC** pipc)
2987 {
2988         CtdlIPC_delete(*pipc);
2989         *pipc = NULL;
2990 }
2991
2992
2993 /*
2994  * return the file descriptor of the server socket so we can select() on it.
2995  *
2996  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
2997  * rewritten...
2998  */
2999 int CtdlIPC_getsockfd(CtdlIPC* ipc)
3000 {
3001         return ipc->sock;
3002 }
3003
3004
3005 /*
3006  * return one character
3007  *
3008  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
3009  * rewritten...
3010  */
3011 char CtdlIPC_get(CtdlIPC* ipc)
3012 {
3013         char buf[2];
3014         char ch;
3015
3016         serv_read(ipc, buf, 1);
3017         ch = (int) buf[0];
3018
3019         return (ch);
3020 }