* Applied a patch sent in by Wilfried Goesgens which allows the various
[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                                                 /* strip out ";charset=" portion.  FIXME do something with
584                                                  * the charset (like... convert it) instead of just throwing
585                                                  * it away
586                                                  */
587                                                 if (strstr(mret[0]->content_type, ";") != NULL) {
588                                                         strcpy(strstr(mret[0]->content_type, ";"), "");
589                                                 }
590
591                                         }
592                                         remove_token(bbb, 0, '\n');
593                                 } while ((bbb[0] != 0) && (bbb[0] != '\n'));
594                                 remove_token(bbb, 0, '\n');
595                         }
596
597
598                 }
599                 if (strlen(bbb)) {
600                         /* FIXME: Strip trailing whitespace */
601                         bbb = (char *)realloc(bbb, (size_t)(strlen(bbb) + 1));
602                 } else {
603                         bbb = (char *)realloc(bbb, 1);
604                         *bbb = '\0';
605                 }
606                 mret[0]->text = bbb;
607         }
608         return ret;
609 }
610
611
612 /* WHOK */
613 int CtdlIPCWhoKnowsRoom(CtdlIPC *ipc, char **listing, char *cret)
614 {
615         register int ret;
616         size_t bytes;
617
618         if (!cret) return -2;
619         if (!listing) return -2;
620         if (*listing) return -2;
621
622         ret = CtdlIPCGenericCommand(ipc, "WHOK", NULL, 0, listing, &bytes, cret);
623         return ret;
624 }
625
626
627 /* INFO */
628 int CtdlIPCServerInfo(CtdlIPC *ipc, char *cret)
629 {
630         register int ret;
631         size_t bytes;
632         char *listing = NULL;
633         char buf[SIZ];
634
635         if (!cret) return -2;
636
637         ret = CtdlIPCGenericCommand(ipc, "INFO", NULL, 0, &listing, &bytes, cret);
638         if (ret / 100 == 1) {
639                 int line = 0;
640
641                 while (*listing && strlen(listing)) {
642                         extract_token(buf, listing, 0, '\n', sizeof buf);
643                         remove_token(listing, 0, '\n');
644                         switch (line++) {
645                         case 0:         ipc->ServInfo.pid = atoi(buf);
646                                         break;
647                         case 1:         strcpy(ipc->ServInfo.nodename,buf);
648                                         break;
649                         case 2:         strcpy(ipc->ServInfo.humannode,buf);
650                                         break;
651                         case 3:         strcpy(ipc->ServInfo.fqdn,buf);
652                                         break;
653                         case 4:         strcpy(ipc->ServInfo.software,buf);
654                                         break;
655                         case 5:         ipc->ServInfo.rev_level = atoi(buf);
656                                         break;
657                         case 6:         strcpy(ipc->ServInfo.site_location,buf);
658                                         break;
659                         case 7:         strcpy(ipc->ServInfo.sysadm,buf);
660                                         break;
661                         case 9:         strcpy(ipc->ServInfo.moreprompt,buf);
662                                         break;
663                         case 10:        ipc->ServInfo.ok_floors = atoi(buf);
664                                         break;
665                         case 11:        ipc->ServInfo.paging_level = atoi(buf);
666                                         break;
667                         case 13:        ipc->ServInfo.supports_qnop = atoi(buf);
668                                         break;
669                         case 14:        ipc->ServInfo.supports_ldap = atoi(buf);
670                                         break;
671                         }
672                 }
673
674         }
675         return ret;
676 }
677
678
679 /* RDIR */
680 int CtdlIPCReadDirectory(CtdlIPC *ipc, char **listing, char *cret)
681 {
682         register int ret;
683         size_t bytes;
684
685         if (!cret) return -2;
686         if (!listing) return -2;
687         if (*listing) return -2;
688
689         ret = CtdlIPCGenericCommand(ipc, "RDIR", NULL, 0, listing, &bytes, cret);
690         return ret;
691 }
692
693
694 /*
695  * Set last-read pointer in this room to msgnum, or 0 for HIGHEST.
696  */
697 int CtdlIPCSetLastRead(CtdlIPC *ipc, long msgnum, char *cret)
698 {
699         register int ret;
700         char aaa[16];
701
702         if (!cret) return -2;
703
704         if (msgnum) {
705                 sprintf(aaa, "SLRP %ld", msgnum);
706         }
707         else {
708                 sprintf(aaa, "SLRP HIGHEST");
709         }
710         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
711         return ret;
712 }
713
714
715 /* INVT */
716 int CtdlIPCInviteUserToRoom(CtdlIPC *ipc, const char *username, char *cret)
717 {
718         register int ret;
719         char *aaa;
720
721         if (!cret) return -2;
722         if (!username) return -2;
723
724         aaa = (char *)malloc(strlen(username) + 6);
725         if (!aaa) return -1;
726
727         sprintf(aaa, "INVT %s", username);
728         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
729         free(aaa);
730         return ret;
731 }
732
733
734 /* KICK */
735 int CtdlIPCKickoutUserFromRoom(CtdlIPC *ipc, const char *username, char *cret)
736 {
737         register int ret;
738         char *aaa;
739
740         if (!cret) return -1;
741         if (!username) return -1;
742
743         aaa = (char *)malloc(strlen(username) + 6);
744
745         sprintf(aaa, "KICK %s", username);
746         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
747         free(aaa);
748         return ret;
749 }
750
751
752 /* GETR */
753 int CtdlIPCGetRoomAttributes(CtdlIPC *ipc, struct ctdlroom **qret, char *cret)
754 {
755         register int ret;
756
757         if (!cret) return -2;
758         if (!qret) return -2;
759         if (!*qret) *qret = (struct ctdlroom *)calloc(1, sizeof (struct ctdlroom));
760         if (!*qret) return -1;
761
762         ret = CtdlIPCGenericCommand(ipc, "GETR", NULL, 0, NULL, NULL, cret);
763         if (ret / 100 == 2) {
764                 extract_token(qret[0]->QRname, cret, 0, '|', sizeof qret[0]->QRname);
765                 extract_token(qret[0]->QRpasswd, cret, 1, '|', sizeof qret[0]->QRpasswd);
766                 extract_token(qret[0]->QRdirname, cret, 2, '|', sizeof qret[0]->QRdirname);
767                 qret[0]->QRflags = extract_int(cret, 3);
768                 qret[0]->QRfloor = extract_int(cret, 4);
769                 qret[0]->QRorder = extract_int(cret, 5);
770                 qret[0]->QRdefaultview = extract_int(cret, 6);
771                 qret[0]->QRflags2 = extract_int(cret, 7);
772         }
773         return ret;
774 }
775
776
777 /* SETR */
778 /* set forget to kick all users out of room */
779 int CtdlIPCSetRoomAttributes(CtdlIPC *ipc, int forget, struct ctdlroom *qret, char *cret)
780 {
781         register int ret;
782         char *aaa;
783
784         if (!cret) return -2;
785         if (!qret) return -2;
786
787         aaa = (char *)malloc(strlen(qret->QRname) + strlen(qret->QRpasswd) +
788                         strlen(qret->QRdirname) + 64);
789         if (!aaa) return -1;
790
791         sprintf(aaa, "SETR %s|%s|%s|%d|%d|%d|%d|%d|%d",
792                         qret->QRname, qret->QRpasswd, qret->QRdirname,
793                         qret->QRflags, forget, qret->QRfloor, qret->QRorder,
794                         qret->QRdefaultview, qret->QRflags2);
795         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
796         free(aaa);
797         return ret;
798 }
799
800
801 /* GETA */
802 int CtdlIPCGetRoomAide(CtdlIPC *ipc, char *cret)
803 {
804         if (!cret) return -1;
805
806         return CtdlIPCGenericCommand(ipc, "GETA", NULL, 0, NULL, NULL, cret);
807 }
808
809
810 /* SETA */
811 int CtdlIPCSetRoomAide(CtdlIPC *ipc, const char *username, char *cret)
812 {
813         register int ret;
814         char *aaa;
815
816         if (!cret) return -2;
817         if (!username) return -2;
818
819         aaa = (char *)malloc(strlen(username) + 6);
820         if (!aaa) return -1;
821
822         sprintf(aaa, "SETA %s", username);
823         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
824         free(aaa);
825         return ret;
826 }
827
828
829 /* ENT0 */
830 int CtdlIPCPostMessage(CtdlIPC *ipc, int flag, const struct ctdlipcmessage *mr, char *cret)
831 {
832         register int ret;
833         char cmd[SIZ];
834
835         if (!cret) return -2;
836         if (!mr) return -2;
837
838         snprintf(cmd, sizeof cmd,
839                         "ENT0 %d|%s|%d|%d|%s|%s", flag, mr->recipient,
840                         mr->anonymous, mr->type, mr->subject, mr->author);
841         ret = CtdlIPCGenericCommand(ipc, cmd, mr->text, strlen(mr->text), NULL,
842                         NULL, cret);
843         return ret;
844 }
845
846
847 /* RINF */
848 int CtdlIPCRoomInfo(CtdlIPC *ipc, char **iret, char *cret)
849 {
850         size_t bytes;
851
852         if (!cret) return -2;
853         if (!iret) return -2;
854         if (*iret) return -2;
855
856         return CtdlIPCGenericCommand(ipc, "RINF", NULL, 0, iret, &bytes, cret);
857 }
858
859
860 /* DELE */
861 int CtdlIPCDeleteMessage(CtdlIPC *ipc, long msgnum, char *cret)
862 {
863         char aaa[16];
864
865         if (!cret) return -2;
866         if (!msgnum) return -2;
867
868         sprintf(aaa, "DELE %ld", msgnum);
869         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
870 }
871
872
873 /* MOVE */
874 int CtdlIPCMoveMessage(CtdlIPC *ipc, int copy, long msgnum, const char *destroom, char *cret)
875 {
876         register int ret;
877         char *aaa;
878
879         if (!cret) return -2;
880         if (!destroom) return -2;
881         if (!msgnum) return -2;
882
883         aaa = (char *)malloc(strlen(destroom) + 28);
884         if (!aaa) return -1;
885
886         sprintf(aaa, "MOVE %ld|%s|%d", msgnum, destroom, copy);
887         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
888         free(aaa);
889         return ret;
890 }
891
892
893 /* KILL */
894 int CtdlIPCDeleteRoom(CtdlIPC *ipc, int for_real, char *cret)
895 {
896         char aaa[16];
897
898         if (!cret) return -2;
899
900         sprintf(aaa, "KILL %d", for_real);
901         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
902 }
903
904
905 /* CRE8 */
906 int CtdlIPCCreateRoom(CtdlIPC *ipc, int for_real, const char *roomname, int type,
907                 const char *password, int floor, char *cret)
908 {
909         register int ret;
910         char *aaa;
911
912         if (!cret) return -2;
913         if (!roomname) return -2;
914
915         if (password) {
916                 aaa = (char *)malloc(strlen(roomname) + strlen(password) + 40);
917                 if (!aaa) return -1;
918                 sprintf(aaa, "CRE8 %d|%s|%d|%s|%d", for_real, roomname, type,
919                                 password, floor);
920         } else {
921                 aaa = (char *)malloc(strlen(roomname) + 40);
922                 if (!aaa) return -1;
923                 sprintf(aaa, "CRE8 %d|%s|%d||%d", for_real, roomname, type,
924                                 floor);
925         }
926         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
927         free(aaa);
928         return ret;
929 }
930
931
932 /* FORG */
933 int CtdlIPCForgetRoom(CtdlIPC *ipc, char *cret)
934 {
935         if (!cret) return -2;
936
937         return CtdlIPCGenericCommand(ipc, "FORG", NULL, 0, NULL, NULL, cret);
938 }
939
940
941 /* MESG */
942 int CtdlIPCSystemMessage(CtdlIPC *ipc, const char *message, char **mret, char *cret)
943 {
944         register int ret;
945         char *aaa;
946         size_t bytes;
947
948         if (!cret) return -2;
949         if (!mret) return -2;
950         if (*mret) return -2;
951         if (!message) return -2;
952
953         aaa = (char *)malloc(strlen(message) + 6);
954         if (!aaa) return -1;
955
956         sprintf(aaa, "MESG %s", message);
957         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, mret, &bytes, cret);
958         free(aaa);
959         return ret;
960 }
961
962
963 /* GNUR */
964 int CtdlIPCNextUnvalidatedUser(CtdlIPC *ipc, char *cret)
965 {
966         if (!cret) return -2;
967
968         return CtdlIPCGenericCommand(ipc, "GNUR", NULL, 0, NULL, NULL, cret);
969 }
970
971
972 /* GREG */
973 int CtdlIPCGetUserRegistration(CtdlIPC *ipc, const char *username, char **rret, char *cret)
974 {
975         register int ret;
976         char *aaa;
977         size_t bytes;
978
979         if (!cret) return -2;
980         if (!rret) return -2;
981         if (*rret) return -2;
982
983         if (username)
984                 aaa = (char *)malloc(strlen(username) + 6);
985         else
986                 aaa = (char *)malloc(12);
987         if (!aaa) return -1;
988
989         if (username)
990                 sprintf(aaa, "GREG %s", username);
991         else
992                 sprintf(aaa, "GREG _SELF_");
993         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, rret, &bytes, cret);
994         free(aaa);
995         return ret;
996 }
997
998
999 /* VALI */
1000 int CtdlIPCValidateUser(CtdlIPC *ipc, const char *username, int axlevel, char *cret)
1001 {
1002         register int ret;
1003         char *aaa;
1004
1005         if (!cret) return -2;
1006         if (!username) return -2;
1007         if (axlevel < 0 || axlevel > 7) return -2;
1008
1009         aaa = (char *)malloc(strlen(username) + 17);
1010         if (!aaa) return -1;
1011
1012         sprintf(aaa, "VALI %s|%d", username, axlevel);
1013         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1014         free(aaa);
1015         return ret;
1016 }
1017
1018
1019 /* EINF */
1020 int CtdlIPCSetRoomInfo(CtdlIPC *ipc, int for_real, const char *info, char *cret)
1021 {
1022         char aaa[16];
1023
1024         if (!cret) return -1;
1025         if (!info) return -1;
1026
1027         sprintf(aaa, "EINF %d", for_real);
1028         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1029 }
1030
1031
1032 /* LIST */
1033 int CtdlIPCUserListing(CtdlIPC *ipc, char *searchstring, char **listing, char *cret)
1034 {
1035         size_t bytes;
1036         char *cmd;
1037         int ret;
1038
1039         if (!cret) return -1;
1040         if (!listing) return -1;
1041         if (*listing) return -1;
1042         if (!searchstring) return -1;
1043
1044         cmd = malloc(strlen(searchstring) + 10);
1045         sprintf(cmd, "LIST %s", searchstring);
1046
1047         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, listing, &bytes, cret);
1048         free(cmd);
1049         return(ret);
1050 }
1051
1052
1053 /* REGI */
1054 int CtdlIPCSetRegistration(CtdlIPC *ipc, const char *info, char *cret)
1055 {
1056         if (!cret) return -1;
1057         if (!info) return -1;
1058
1059         return CtdlIPCGenericCommand(ipc, "REGI", info, strlen(info),
1060                         NULL, NULL, cret);
1061 }
1062
1063
1064 /* CHEK */
1065 int CtdlIPCMiscCheck(CtdlIPC *ipc, struct ctdlipcmisc *chek, char *cret)
1066 {
1067         register int ret;
1068
1069         if (!cret) return -1;
1070         if (!chek) return -1;
1071
1072         ret = CtdlIPCGenericCommand(ipc, "CHEK", NULL, 0, NULL, NULL, cret);
1073         if (ret / 100 == 2) {
1074                 chek->newmail = extract_long(cret, 0);
1075                 chek->needregis = extract_int(cret, 1);
1076                 chek->needvalid = extract_int(cret, 2);
1077         }
1078         return ret;
1079 }
1080
1081
1082 /* DELF */
1083 int CtdlIPCDeleteFile(CtdlIPC *ipc, const char *filename, char *cret)
1084 {
1085         register int ret;
1086         char *aaa;
1087
1088         if (!cret) return -2;
1089         if (!filename) return -2;
1090         
1091         aaa = (char *)malloc(strlen(filename) + 6);
1092         if (!aaa) return -1;
1093
1094         sprintf(aaa, "DELF %s", filename);
1095         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1096         free(aaa);
1097         return ret;
1098 }
1099
1100
1101 /* MOVF */
1102 int CtdlIPCMoveFile(CtdlIPC *ipc, const char *filename, const char *destroom, char *cret)
1103 {
1104         register int ret;
1105         char *aaa;
1106
1107         if (!cret) return -2;
1108         if (!filename) return -2;
1109         if (!destroom) return -2;
1110
1111         aaa = (char *)malloc(strlen(filename) + strlen(destroom) + 7);
1112         if (!aaa) return -1;
1113
1114         sprintf(aaa, "MOVF %s|%s", filename, destroom);
1115         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1116         free(aaa);
1117         return ret;
1118 }
1119
1120
1121 /* NETF */
1122 int CtdlIPCNetSendFile(CtdlIPC *ipc, const char *filename, const char *destnode, char *cret)
1123 {
1124         register int ret;
1125         char *aaa;
1126
1127         if (!cret) return -2;
1128         if (!filename) return -2;
1129         if (!destnode) return -2;
1130
1131         aaa = (char *)malloc(strlen(filename) + strlen(destnode) + 7);
1132         if (!aaa) return -1;
1133
1134         sprintf(aaa, "NETF %s|%s", filename, destnode);
1135         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1136         free(aaa);
1137         return ret;
1138 }
1139
1140
1141 /* RWHO */
1142 int CtdlIPCOnlineUsers(CtdlIPC *ipc, char **listing, time_t *stamp, char *cret)
1143 {
1144         register int ret;
1145         size_t bytes;
1146
1147         if (!cret) return -1;
1148         if (!listing) return -1;
1149         if (*listing) return -1;
1150
1151         *stamp = CtdlIPCServerTime(ipc, cret);
1152         if (!*stamp)
1153                 *stamp = time(NULL);
1154         ret = CtdlIPCGenericCommand(ipc, "RWHO", NULL, 0, listing, &bytes, cret);
1155         return ret;
1156 }
1157
1158
1159 /* OPEN */
1160 int CtdlIPCFileDownload(CtdlIPC *ipc, const char *filename, void **buf,
1161                 size_t resume,
1162                 void (*progress_gauge_callback)
1163                         (CtdlIPC*, unsigned long, unsigned long),
1164                 char *cret)
1165 {
1166         register int ret;
1167         size_t bytes;
1168         time_t last_mod;
1169         char mimetype[SIZ];
1170         char *aaa;
1171
1172         if (!cret) return -2;
1173         if (!filename) return -2;
1174         if (!buf) return -2;
1175         if (*buf) return -2;
1176         if (ipc->downloading) return -2;
1177
1178         aaa = (char *)malloc(strlen(filename) + 6);
1179         if (!aaa) return -1;
1180
1181         sprintf(aaa, "OPEN %s", filename);
1182         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1183         free(aaa);
1184         if (ret / 100 == 2) {
1185                 ipc->downloading = 1;
1186                 bytes = extract_long(cret, 0);
1187                 last_mod = extract_int(cret, 1);
1188                 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1189
1190                 ret = CtdlIPCReadDownload(ipc, buf, bytes, resume,
1191                                         progress_gauge_callback, cret);
1192                 /*
1193                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, resume,
1194                                         progress_gauge_callback, cret);
1195                 */
1196
1197                 ret = CtdlIPCEndDownload(ipc, cret);
1198                 if (ret / 100 == 2)
1199                         sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1200                                         filename, mimetype);
1201         }
1202         return ret;
1203 }
1204
1205
1206 /* OPNA */
1207 int CtdlIPCAttachmentDownload(CtdlIPC *ipc, long msgnum, const char *part,
1208                 void **buf,
1209                 void (*progress_gauge_callback)
1210                         (CtdlIPC*, unsigned long, unsigned long),
1211                 char *cret)
1212 {
1213         register int ret;
1214         size_t bytes;
1215         time_t last_mod;
1216         char filename[SIZ];
1217         char mimetype[SIZ];
1218         char aaa[SIZ];
1219
1220         if (!cret) return -2;
1221         if (!buf) return -2;
1222         if (*buf) return -2;
1223         if (!part) return -2;
1224         if (!msgnum) return -2;
1225         if (ipc->downloading) return -2;
1226
1227         sprintf(aaa, "OPNA %ld|%s", msgnum, part);
1228         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1229         if (ret / 100 == 2) {
1230                 ipc->downloading = 1;
1231                 bytes = extract_long(cret, 0);
1232                 last_mod = extract_int(cret, 1);
1233                 extract_token(filename, cret, 2, '|', sizeof filename);
1234                 extract_token(mimetype, cret, 3, '|', sizeof mimetype);
1235                 /* ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1236                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1237                 ret = CtdlIPCEndDownload(ipc, cret);
1238                 if (ret / 100 == 2)
1239                         sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1240                                         filename, mimetype);
1241         }
1242         return ret;
1243 }
1244
1245
1246 /* OIMG */
1247 int CtdlIPCImageDownload(CtdlIPC *ipc, const char *filename, void **buf,
1248                 void (*progress_gauge_callback)
1249                         (CtdlIPC*, unsigned long, unsigned long),
1250                 char *cret)
1251 {
1252         register int ret;
1253         size_t bytes;
1254         time_t last_mod;
1255         char mimetype[SIZ];
1256         char *aaa;
1257
1258         if (!cret) return -1;
1259         if (!buf) return -1;
1260         if (*buf) return -1;
1261         if (!filename) return -1;
1262         if (ipc->downloading) return -1;
1263
1264         aaa = (char *)malloc(strlen(filename) + 6);
1265         if (!aaa) return -1;
1266
1267         sprintf(aaa, "OIMG %s", filename);
1268         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1269         free(aaa);
1270         if (ret / 100 == 2) {
1271                 ipc->downloading = 1;
1272                 bytes = extract_long(cret, 0);
1273                 last_mod = extract_int(cret, 1);
1274                 extract_token(mimetype, cret, 2, '|', sizeof mimetype);
1275 /*              ret = CtdlIPCReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret); */
1276                 ret = CtdlIPCHighSpeedReadDownload(ipc, buf, bytes, 0, progress_gauge_callback, cret);
1277                 ret = CtdlIPCEndDownload(ipc, cret);
1278                 if (ret / 100 == 2)
1279                         sprintf(cret, "%d|%ld|%s|%s", (int)bytes, last_mod,
1280                                         filename, mimetype);
1281         }
1282         return ret;
1283 }
1284
1285
1286 /* UOPN */
1287 int CtdlIPCFileUpload(CtdlIPC *ipc, const char *save_as, const char *comment,
1288                 const char *path,
1289                 void (*progress_gauge_callback)
1290                         (CtdlIPC*, unsigned long, unsigned long),
1291                 char *cret)
1292 {
1293         register int ret;
1294         char *aaa;
1295
1296         if (!cret) return -1;
1297         if (!save_as) return -1;
1298         if (!comment) return -1;
1299         if (!path) return -1;
1300         if (!*path) return -1;
1301         if (ipc->uploading) return -1;
1302
1303         aaa = (char *)malloc(strlen(save_as) + strlen(comment) + 7);
1304         if (!aaa) return -1;
1305
1306         sprintf(aaa, "UOPN %s|%s", save_as, comment);
1307         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1308         free(aaa);
1309         if (ret / 100 == 2) {
1310                 ipc->uploading = 1;
1311                 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1312                 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1313                 ipc->uploading = 0;
1314         }
1315         return ret;
1316 }
1317
1318
1319 /* UIMG */
1320 int CtdlIPCImageUpload(CtdlIPC *ipc, int for_real, const char *path,
1321                 const char *save_as,
1322                 void (*progress_gauge_callback)
1323                         (CtdlIPC*, unsigned long, unsigned long),
1324                 char *cret)
1325 {
1326         register int ret;
1327         char *aaa;
1328
1329         if (!cret) return -1;
1330         if (!save_as) return -1;
1331         if (!path && for_real) return -1;
1332         if (!*path && for_real) return -1;
1333         if (ipc->uploading) return -1;
1334
1335         aaa = (char *)malloc(strlen(save_as) + 17);
1336         if (!aaa) return -1;
1337
1338         sprintf(aaa, "UIMG %d|%s", for_real, save_as);
1339         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1340         free(aaa);
1341         if (ret / 100 == 2 && for_real) {
1342                 ipc->uploading = 1;
1343                 ret = CtdlIPCWriteUpload(ipc, path, progress_gauge_callback, cret);
1344                 ret = CtdlIPCEndUpload(ipc, (ret == -2 ? 1 : 0), cret);
1345                 ipc->uploading = 0;
1346         }
1347         return ret;
1348 }
1349
1350
1351 /* QUSR */
1352 int CtdlIPCQueryUsername(CtdlIPC *ipc, const char *username, char *cret)
1353 {
1354         register int ret;
1355         char *aaa;
1356
1357         if (!cret) return -2;
1358         if (!username) return -2;
1359
1360         aaa = (char *)malloc(strlen(username) + 6);
1361         if (!aaa) return -1;
1362
1363         sprintf(aaa, "QUSR %s", username);
1364         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1365         free(aaa);
1366         return ret;
1367 }
1368
1369
1370 /* LFLR */
1371 int CtdlIPCFloorListing(CtdlIPC *ipc, char **listing, char *cret)
1372 {
1373         size_t bytes;
1374
1375         if (!cret) return -2;
1376         if (!listing) return -2;
1377         if (*listing) return -2;
1378
1379         return CtdlIPCGenericCommand(ipc, "LFLR", NULL, 0, listing, &bytes, cret);
1380 }
1381
1382
1383 /* CFLR */
1384 int CtdlIPCCreateFloor(CtdlIPC *ipc, int for_real, const char *name, char *cret)
1385 {
1386         register int ret;
1387         char aaa[SIZ];
1388
1389         if (!cret) return -2;
1390         if (!name) return -2;
1391
1392         sprintf(aaa, "CFLR %s|%d", name, for_real);
1393         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1394         return ret;
1395 }
1396
1397
1398 /* KFLR */
1399 int CtdlIPCDeleteFloor(CtdlIPC *ipc, int for_real, int floornum, char *cret)
1400 {
1401         char aaa[SIZ];
1402
1403         if (!cret) return -1;
1404         if (floornum < 0) return -1;
1405
1406         sprintf(aaa, "KFLR %d|%d", floornum, for_real);
1407         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1408 }
1409
1410
1411 /* EFLR */
1412 int CtdlIPCEditFloor(CtdlIPC *ipc, int floornum, const char *floorname, char *cret)
1413 {
1414         register int ret;
1415         char aaa[SIZ];
1416
1417         if (!cret) return -2;
1418         if (!floorname) return -2;
1419         if (floornum < 0) return -2;
1420
1421         sprintf(aaa, "EFLR %d|%s", floornum, floorname);
1422         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1423         return ret;
1424 }
1425
1426
1427 /*
1428  * IDEN 
1429  *
1430  * You only need to fill out hostname, the defaults will be used if any of the
1431  * other fields are not set properly.
1432  */
1433 int CtdlIPCIdentifySoftware(CtdlIPC *ipc, int developerid, int clientid,
1434                 int revision, const char *software_name, const char *hostname,
1435                 char *cret)
1436 {
1437         register int ret;
1438         char *aaa;
1439
1440         if (developerid < 0 || clientid < 0 || revision < 0 ||
1441             !software_name) {
1442                 developerid = 8;
1443                 clientid = 0;
1444                 revision = REV_LEVEL - 600;
1445                 software_name = "Citadel (libcitadel)";
1446         }
1447         if (!hostname) return -2;
1448
1449         aaa = (char *)malloc(strlen(software_name) + strlen(hostname) + 29);
1450         if (!aaa) return -1;
1451
1452         sprintf(aaa, "IDEN %d|%d|%d|%s|%s", developerid, clientid,
1453                         revision, software_name, hostname);
1454         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1455         free(aaa);
1456         return ret;
1457 }
1458
1459
1460 /* SEXP */
1461 int CtdlIPCSendInstantMessage(CtdlIPC *ipc, const char *username, const char *text,
1462                 char *cret)
1463 {
1464         register int ret;
1465         char *aaa;
1466
1467         if (!cret) return -2;
1468         if (!username) return -2;
1469
1470         aaa = (char *)malloc(strlen(username) + 8);
1471         if (!aaa) return -1;
1472
1473         if (text) {
1474                 sprintf(aaa, "SEXP %s|-", username);
1475                 ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text),
1476                                 NULL, NULL, cret);
1477         } else {
1478                 sprintf(aaa, "SEXP %s||", username);
1479                 ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1480         }
1481         free(aaa);
1482         return ret;
1483 }
1484
1485
1486 /* GEXP */
1487 int CtdlIPCGetInstantMessage(CtdlIPC *ipc, char **listing, char *cret)
1488 {
1489         size_t bytes;
1490
1491         if (!cret) return -2;
1492         if (!listing) return -2;
1493         if (*listing) return -2;
1494
1495         return CtdlIPCGenericCommand(ipc, "GEXP", NULL, 0, listing, &bytes, cret);
1496 }
1497
1498
1499 /* DEXP */
1500 /* mode is 0 = enable, 1 = disable, 2 = status */
1501 int CtdlIPCEnableInstantMessageReceipt(CtdlIPC *ipc, int mode, char *cret)
1502 {
1503         char aaa[16];
1504
1505         if (!cret) return -2;
1506
1507         sprintf(aaa, "DEXP %d", mode);
1508         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1509 }
1510
1511
1512 /* EBIO */
1513 int CtdlIPCSetBio(CtdlIPC *ipc, char *bio, char *cret)
1514 {
1515         if (!cret) return -2;
1516         if (!bio) return -2;
1517
1518         return CtdlIPCGenericCommand(ipc, "EBIO", bio, strlen(bio),
1519                         NULL, NULL, cret);
1520 }
1521
1522
1523 /* RBIO */
1524 int CtdlIPCGetBio(CtdlIPC *ipc, const char *username, char **listing, char *cret)
1525 {
1526         register int ret;
1527         size_t bytes;
1528         char *aaa;
1529
1530         if (!cret) return -2;
1531         if (!username) return -2;
1532         if (!listing) return -2;
1533         if (*listing) return -2;
1534
1535         aaa = (char *)malloc(strlen(username) + 6);
1536         if (!aaa) return -1;
1537
1538         sprintf(aaa, "RBIO %s", username);
1539         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, listing, &bytes, cret);
1540         free(aaa);
1541         return ret;
1542 }
1543
1544
1545 /* LBIO */
1546 int CtdlIPCListUsersWithBios(CtdlIPC *ipc, char **listing, char *cret)
1547 {
1548         size_t bytes;
1549
1550         if (!cret) return -2;
1551         if (!listing) return -2;
1552         if (*listing) return -2;
1553
1554         return CtdlIPCGenericCommand(ipc, "LBIO", NULL, 0, listing, &bytes, cret);
1555 }
1556
1557
1558 /* STEL */
1559 int CtdlIPCStealthMode(CtdlIPC *ipc, int mode, char *cret)
1560 {
1561         char aaa[16];
1562
1563         if (!cret) return -1;
1564
1565         sprintf(aaa, "STEL %d", mode ? 1 : 0);
1566         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1567 }
1568
1569
1570 /* TERM */
1571 int CtdlIPCTerminateSession(CtdlIPC *ipc, int sid, char *cret)
1572 {
1573         char aaa[16];
1574
1575         if (!cret) return -1;
1576
1577         sprintf(aaa, "TERM %d", sid);
1578         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1579 }
1580
1581
1582 /* DOWN */
1583 int CtdlIPCTerminateServerNow(CtdlIPC *ipc, char *cret)
1584 {
1585         if (!cret) return -1;
1586
1587         return CtdlIPCGenericCommand(ipc, "DOWN", NULL, 0, NULL, NULL, cret);
1588 }
1589
1590
1591 /* SCDN */
1592 int CtdlIPCTerminateServerScheduled(CtdlIPC *ipc, int mode, char *cret)
1593 {
1594         char aaa[16];
1595
1596         if (!cret) return -1;
1597
1598         sprintf(aaa, "SCDN %d", mode ? 1 : 0);
1599         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1600 }
1601
1602
1603 /* EMSG */
1604 int CtdlIPCEnterSystemMessage(CtdlIPC *ipc, const char *filename, const char *text,
1605                 char *cret)
1606 {
1607         register int ret;
1608         char *aaa;
1609
1610         if (!cret) return -2;
1611         if (!text) return -2;
1612         if (!filename) return -2;
1613
1614         aaa = (char *)malloc(strlen(filename) + 6);
1615         if (!aaa) return -1;
1616
1617         sprintf(aaa, "EMSG %s", filename);
1618         ret = CtdlIPCGenericCommand(ipc, aaa, text, strlen(text), NULL, NULL, cret);
1619         free(aaa);
1620         return ret;
1621 }
1622
1623
1624 /* HCHG */
1625 int CtdlIPCChangeHostname(CtdlIPC *ipc, const char *hostname, char *cret)
1626 {
1627         register int ret;
1628         char *aaa;
1629
1630         if (!cret) return -2;
1631         if (!hostname) return -2;
1632
1633         aaa = (char *)malloc(strlen(hostname) + 6);
1634         if (!aaa) return -1;
1635
1636         sprintf(aaa, "HCHG %s", hostname);
1637         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1638         free(aaa);
1639         return ret;
1640 }
1641
1642
1643 /* RCHG */
1644 int CtdlIPCChangeRoomname(CtdlIPC *ipc, const char *roomname, char *cret)
1645 {
1646         register int ret;
1647         char *aaa;
1648
1649         if (!cret) return -2;
1650         if (!roomname) return -2;
1651
1652         aaa = (char *)malloc(strlen(roomname) + 6);
1653         if (!aaa) return -1;
1654
1655         sprintf(aaa, "RCHG %s", roomname);
1656         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1657         free(aaa);
1658         return ret;
1659 }
1660
1661
1662 /* UCHG */
1663 int CtdlIPCChangeUsername(CtdlIPC *ipc, const char *username, char *cret)
1664 {
1665         register int ret;
1666         char *aaa;
1667
1668         if (!cret) return -2;
1669         if (!username) return -2;
1670
1671         aaa = (char *)malloc(strlen(username) + 6);
1672         if (!aaa) return -1;
1673
1674         sprintf(aaa, "UCHG %s", username);
1675         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1676         free(aaa);
1677         return ret;
1678 }
1679
1680
1681 /* TIME */
1682 /* This function returns the actual server time reported, or 0 if error */
1683 time_t CtdlIPCServerTime(CtdlIPC *ipc, char *cret)
1684 {
1685         register time_t tret;
1686         register int ret;
1687
1688         ret = CtdlIPCGenericCommand(ipc, "TIME", NULL, 0, NULL, NULL, cret);
1689         if (ret / 100 == 2) {
1690                 tret = extract_long(cret, 0);
1691         } else {
1692                 tret = 0L;
1693         }
1694         return tret;
1695 }
1696
1697
1698 /* AGUP */
1699 int CtdlIPCAideGetUserParameters(CtdlIPC *ipc, const char *who,
1700                                  struct ctdluser **uret, char *cret)
1701 {
1702         register int ret;
1703         char aaa[SIZ];
1704
1705         if (!cret) return -2;
1706         if (!uret) return -2;
1707         if (!*uret) *uret = (struct ctdluser *)calloc(1, sizeof(struct ctdluser));
1708         if (!*uret) return -1;
1709
1710         sprintf(aaa, "AGUP %s", who);
1711         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1712
1713         if (ret / 100 == 2) {
1714                 extract_token(uret[0]->fullname, cret, 0, '|', sizeof uret[0]->fullname);
1715                 extract_token(uret[0]->password, cret, 1, '|', sizeof uret[0]->password);
1716                 uret[0]->flags = extract_int(cret, 2);
1717                 uret[0]->timescalled = extract_long(cret, 3);
1718                 uret[0]->posted = extract_long(cret, 4);
1719                 uret[0]->axlevel = extract_int(cret, 5);
1720                 uret[0]->usernum = extract_long(cret, 6);
1721                 uret[0]->lastcall = extract_long(cret, 7);
1722                 uret[0]->USuserpurge = extract_int(cret, 8);
1723         }
1724         return ret;
1725 }
1726
1727
1728 /* ASUP */
1729 int CtdlIPCAideSetUserParameters(CtdlIPC *ipc, const struct ctdluser *uret, char *cret)
1730 {
1731         register int ret;
1732         char *aaa;
1733
1734         if (!cret) return -2;
1735         if (!uret) return -2;
1736
1737         aaa = (char *)malloc(strlen(uret->fullname) + strlen(uret->password) + 84);
1738         if (!aaa) return -1;
1739
1740         sprintf(aaa, "ASUP %s|%s|%d|%ld|%ld|%d|%ld|%ld|%d",
1741                         uret->fullname, uret->password, uret->flags,
1742                         uret->timescalled, uret->posted, uret->axlevel,
1743                         uret->usernum, uret->lastcall, uret->USuserpurge);
1744         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1745         free(aaa);
1746         return ret;
1747 }
1748
1749
1750 /* GPEX */
1751 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1752 /* caller must free the struct ExpirePolicy */
1753 int CtdlIPCGetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1754                 struct ExpirePolicy **policy, char *cret)
1755 {
1756         static char *proto[] = {"room", "floor", "site", "mailboxes" };
1757         char aaa[11];
1758         register int ret;
1759
1760         if (!cret) return -2;
1761         if (!policy) return -2;
1762         if (!*policy) *policy = (struct ExpirePolicy *)calloc(1, sizeof(struct ExpirePolicy));
1763         if (!*policy) return -1;
1764         if (which < 0 || which > 3) return -2;
1765         
1766         sprintf(aaa, "GPEX %s", proto[which]);
1767         ret = CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1768         if (ret / 100 == 2) {
1769                 policy[0]->expire_mode = extract_int(cret, 0);
1770                 policy[0]->expire_value = extract_int(cret, 1);
1771         }
1772         return ret;
1773
1774 }
1775
1776
1777 /* SPEX */
1778 /* which is 0 = room, 1 = floor, 2 = site, 3 = default for mailboxes */
1779 /* policy is 0 = inherit, 1 = no purge, 2 = by count, 3 = by age (days) */
1780 int CtdlIPCSetMessageExpirationPolicy(CtdlIPC *ipc, int which,
1781                 struct ExpirePolicy *policy, char *cret)
1782 {
1783         char aaa[38];
1784         char *whichvals[] = { "room", "floor", "site", "mailboxes" };
1785
1786         if (!cret) return -2;
1787         if (which < 0 || which > 3) return -2;
1788         if (!policy) return -2;
1789         if (policy->expire_mode < 0 || policy->expire_mode > 3) return -2;
1790         if (policy->expire_mode >= 2 && policy->expire_value < 1) return -2;
1791
1792         sprintf(aaa, "SPEX %s|%d|%d", whichvals[which],
1793                         policy->expire_mode, policy->expire_value);
1794         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1795 }
1796
1797
1798 /* CONF GET */
1799 int CtdlIPCGetSystemConfig(CtdlIPC *ipc, char **listing, char *cret)
1800 {
1801         size_t bytes;
1802
1803         if (!cret) return -2;
1804         if (!listing) return -2;
1805         if (*listing) return -2;
1806
1807         return CtdlIPCGenericCommand(ipc, "CONF GET", NULL, 0,
1808                         listing, &bytes, cret);
1809 }
1810
1811
1812 /* CONF SET */
1813 int CtdlIPCSetSystemConfig(CtdlIPC *ipc, const char *listing, char *cret)
1814 {
1815         if (!cret) return -2;
1816         if (!listing) return -2;
1817
1818         return CtdlIPCGenericCommand(ipc, "CONF SET", listing, strlen(listing),
1819                         NULL, NULL, cret);
1820 }
1821
1822
1823 /* CONF GETSYS */
1824 int CtdlIPCGetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1825                 char **listing, char *cret)
1826 {
1827         char *aaa;
1828         size_t bytes;
1829
1830         if (!cret) return -2;
1831         if (!mimetype) return -2;
1832         if (!listing) return -2;
1833         if (*listing) return -2;
1834
1835         aaa = malloc(strlen(mimetype) + 13);
1836         if (!aaa) return -1;
1837         sprintf(aaa, "CONF GETSYS|%s", mimetype);
1838         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0,
1839                         listing, &bytes, cret);
1840 }
1841
1842
1843 /* CONF PUTSYS */
1844 int CtdlIPCSetSystemConfigByType(CtdlIPC *ipc, const char *mimetype,
1845                const char *listing, char *cret)
1846 {
1847         char *aaa;
1848
1849         if (!cret) return -2;
1850         if (!mimetype) return -2;
1851         if (!listing) return -2;
1852
1853         aaa = malloc(strlen(mimetype) + 13);
1854         if (!aaa) return -1;
1855         sprintf(aaa, "CONF PUTSYS|%s", mimetype);
1856         return CtdlIPCGenericCommand(ipc, aaa, listing, strlen(listing),
1857                         NULL, NULL, cret);
1858 }
1859
1860
1861 /* GNET */
1862 int CtdlIPCGetRoomNetworkConfig(CtdlIPC *ipc, char **listing, char *cret)
1863 {
1864         size_t bytes;
1865
1866         if (!cret) return -2;
1867         if (!listing) return -2;
1868         if (*listing) return -2;
1869
1870         return CtdlIPCGenericCommand(ipc, "GNET", NULL, 0,
1871                         listing, &bytes, cret);
1872 }
1873
1874
1875 /* SNET */
1876 int CtdlIPCSetRoomNetworkConfig(CtdlIPC *ipc, const char *listing, char *cret)
1877 {
1878         if (!cret) return -2;
1879         if (!listing) return -2;
1880
1881         return CtdlIPCGenericCommand(ipc, "SNET", listing, strlen(listing),
1882                         NULL, NULL, cret);
1883 }
1884
1885
1886 /* REQT */
1887 int CtdlIPCRequestClientLogout(CtdlIPC *ipc, int session, char *cret)
1888 {
1889         char aaa[16];
1890
1891         if (!cret) return -2;
1892         if (session < 0) return -2;
1893
1894         sprintf(aaa, "REQT %d", session);
1895         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1896 }
1897
1898
1899 /* SEEN */
1900 int CtdlIPCSetMessageSeen(CtdlIPC *ipc, long msgnum, int seen, char *cret)
1901 {
1902         char aaa[27];
1903
1904         if (!cret) return -2;
1905         if (msgnum < 0) return -2;
1906
1907         sprintf(aaa, "SEEN %ld|%d", msgnum, seen ? 1 : 0);
1908         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
1909 }
1910
1911
1912 /* STLS */
1913 int CtdlIPCStartEncryption(CtdlIPC *ipc, char *cret)
1914 {
1915         int a;
1916         int r;
1917         char buf[SIZ];
1918
1919 #ifdef HAVE_OPENSSL
1920         SSL *temp_ssl;
1921
1922         /* New SSL object */
1923         temp_ssl = SSL_new(ssl_ctx);
1924         if (!temp_ssl) {
1925                 error_printf("SSL_new failed: %s\n",
1926                                 ERR_reason_error_string(ERR_get_error()));
1927                 return -2;
1928         }
1929         /* Pointless flag waving */
1930 #if SSLEAY_VERSION_NUMBER >= 0x0922
1931         SSL_set_session_id_context(temp_ssl, "Citadel SID", 14);
1932 #endif
1933
1934         if (!access(EGD_POOL, F_OK))
1935                 RAND_egd(EGD_POOL);
1936
1937         if (!RAND_status()) {
1938                 error_printf("PRNG not properly seeded\n");
1939                 return -2;
1940         }
1941
1942         /* Associate network connection with SSL object */
1943         if (SSL_set_fd(temp_ssl, ipc->sock) < 1) {
1944                 error_printf("SSL_set_fd failed: %s\n",
1945                                 ERR_reason_error_string(ERR_get_error()));
1946                 return -2;
1947         }
1948
1949         if (status_hook != NULL)
1950                 status_hook("Requesting encryption...\r");
1951
1952         /* Ready to start SSL/TLS */
1953         /* Old code
1954         CtdlIPC_putline(ipc, "STLS");
1955         CtdlIPC_getline(ipc, buf);
1956         if (buf[0] != '2') {
1957                 error_printf("Server can't start TLS: %s\n", buf);
1958                 return 0;
1959         }
1960         */
1961         r = CtdlIPCGenericCommand(ipc,
1962                                   "STLS", NULL, 0, NULL, NULL, cret);
1963         if (r / 100 != 2) {
1964                 error_printf("Server can't start TLS: %s\n", buf);
1965                 endtls(temp_ssl);
1966                 return r;
1967         }
1968
1969         /* Do SSL/TLS handshake */
1970         if ((a = SSL_connect(temp_ssl)) < 1) {
1971                 error_printf("SSL_connect failed: %s\n",
1972                                 ERR_reason_error_string(ERR_get_error()));
1973                 endtls(temp_ssl);
1974                 return -2;
1975         }
1976         ipc->ssl = temp_ssl;
1977
1978         BIO_set_close(ipc->ssl->rbio, BIO_NOCLOSE);
1979         {
1980                 int bits, alg_bits;
1981
1982                 bits = SSL_CIPHER_get_bits(SSL_get_current_cipher(ipc->ssl), &alg_bits);
1983                 error_printf("Encrypting with %s cipher %s (%d of %d bits)\n",
1984                                 SSL_CIPHER_get_version(SSL_get_current_cipher(ipc->ssl)),
1985                                 SSL_CIPHER_get_name(SSL_get_current_cipher(ipc->ssl)),
1986                                 bits, alg_bits);
1987         }
1988         return r;
1989 #else
1990         return 0;
1991 #endif /* HAVE_OPENSSL */
1992 }
1993
1994
1995 #ifdef HAVE_OPENSSL
1996 static void endtls(SSL *ssl)
1997 {
1998         if (ssl) {
1999                 SSL_shutdown(ssl);
2000                 SSL_free(ssl);
2001         }
2002 }
2003 #endif
2004
2005
2006 /* QDIR */
2007 int CtdlIPCDirectoryLookup(CtdlIPC *ipc, const char *address, char *cret)
2008 {
2009         char *aaa;
2010
2011         if (!address) return -2;
2012         if (!cret) return -2;
2013
2014         aaa = (char *)malloc(strlen(address) + 6);
2015         if (!aaa) return -1;
2016
2017         sprintf(aaa, "QDIR %s", address);
2018         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2019 }
2020
2021
2022 /* IPGM */
2023 int CtdlIPCInternalProgram(CtdlIPC *ipc, int secret, char *cret)
2024 {
2025         char aaa[30];
2026
2027         if (!cret) return -2;
2028         sprintf(aaa, "IPGM %d", secret);
2029         return CtdlIPCGenericCommand(ipc, aaa, NULL, 0, NULL, NULL, cret);
2030 }
2031
2032
2033 /* FSCK */
2034 int CtdlIPCMessageBaseCheck(CtdlIPC *ipc, char **mret, char *cret)
2035 {
2036         size_t size = 0;
2037
2038         if (!cret) return -2;
2039         if (!mret) return -2;
2040         if (*mret) return -2;
2041
2042         return CtdlIPCGenericCommand(ipc, "FSCK", NULL, 0, mret, &size, cret);
2043 }
2044
2045
2046 /*
2047  * Not implemented:
2048  * 
2049  * CHAT
2050  * ETLS
2051  * EXPI
2052  * GTLS
2053  * IGAB
2054  * MSG3
2055  * MSG4
2056  * NDOP
2057  * NETP
2058  * NUOP
2059  * SMTP
2060  */
2061
2062
2063 /* ************************************************************************** */
2064 /*             Stuff below this line is not for public consumption            */
2065 /* ************************************************************************** */
2066
2067
2068 INLINE void CtdlIPC_lock(CtdlIPC *ipc)
2069 {
2070         if (ipc->network_status_cb) ipc->network_status_cb(1);
2071 #ifdef THREADED_CLIENT
2072         pthread_mutex_lock(&(ipc->mutex));
2073 #endif
2074 }
2075
2076
2077 INLINE void CtdlIPC_unlock(CtdlIPC *ipc)
2078 {
2079 #ifdef THREADED_CLIENT
2080         pthread_mutex_unlock(&(ipc->mutex));
2081 #endif
2082         if (ipc->network_status_cb) ipc->network_status_cb(0);
2083 }
2084
2085
2086 /* Read a listing from the server up to 000.  Append to dest if it exists */
2087 char *CtdlIPCReadListing(CtdlIPC *ipc, char *dest)
2088 {
2089         size_t length = 0;
2090         size_t linelength;
2091         char *ret = NULL;
2092         char aaa[SIZ];
2093
2094         ret = dest;
2095         if (ret != NULL) {
2096                 length = strlen(ret);
2097         } else {
2098                 length = 0;
2099         }
2100
2101         while (CtdlIPC_getline(ipc, aaa), strcmp(aaa, "000")) {
2102                 linelength = strlen(aaa);
2103                 ret = (char *)realloc(ret, (size_t)(length + linelength + 2));
2104                 if (ret) {
2105                         strcpy(&ret[length], aaa);
2106                         length += linelength;
2107                         strcpy(&ret[length++], "\n");
2108                 }
2109         }
2110
2111         return(ret);
2112 }
2113
2114
2115 /* Send a listing to the server; generate the ending 000. */
2116 int CtdlIPCSendListing(CtdlIPC *ipc, const char *listing)
2117 {
2118         char *text;
2119
2120         text = (char *)malloc(strlen(listing) + 6);
2121         if (text) {
2122                 strcpy(text, listing);
2123                 while (text[strlen(text) - 1] == '\n')
2124                         text[strlen(text) - 1] = '\0';
2125                 strcat(text, "\n000");
2126                 CtdlIPC_putline(ipc, text);
2127                 free(text);
2128                 text = NULL;
2129         } else {
2130                 /* Malloc failed but we are committed to send */
2131                 /* This may result in extra blanks at the bottom */
2132                 CtdlIPC_putline(ipc, text);
2133                 CtdlIPC_putline(ipc, "000");
2134         }
2135         return 0;
2136 }
2137
2138
2139 /* Partial read of file from server */
2140 size_t CtdlIPCPartialRead(CtdlIPC *ipc, void **buf, size_t offset, size_t bytes, char *cret)
2141 {
2142         register size_t len = 0;
2143         char aaa[SIZ];
2144
2145         if (!buf) return 0;
2146         if (!cret) return 0;
2147         if (bytes < 1) return 0;
2148
2149         CtdlIPC_lock(ipc);
2150         sprintf(aaa, "READ %d|%d", (int)offset, (int)bytes);
2151         CtdlIPC_putline(ipc, aaa);
2152         CtdlIPC_getline(ipc, aaa);
2153         if (aaa[0] != '6')
2154                 strcpy(cret, &aaa[4]);
2155         else {
2156                 len = extract_long(&aaa[4], 0);
2157                 *buf = (void *)realloc(*buf, (size_t)(offset + len));
2158                 if (*buf) {
2159                         /* I know what I'm doing */
2160                         serv_read(ipc, ((char *)(*buf) + offset), len);
2161                 } else {
2162                         /* We have to read regardless */
2163                         serv_read(ipc, aaa, len);
2164                         len = 0;
2165                 }
2166         }
2167         CtdlIPC_unlock(ipc);
2168         return len;
2169 }
2170
2171
2172 /* CLOS */
2173 int CtdlIPCEndDownload(CtdlIPC *ipc, char *cret)
2174 {
2175         register int ret;
2176
2177         if (!cret) return -2;
2178         if (!ipc->downloading) return -2;
2179
2180         ret = CtdlIPCGenericCommand(ipc, "CLOS", NULL, 0, NULL, NULL, cret);
2181         if (ret / 100 == 2)
2182                 ipc->downloading = 0;
2183         return ret;
2184 }
2185
2186
2187 /* MSGP */
2188 int CtdlIPCSpecifyPreferredFormats(CtdlIPC *ipc, char *cret, char *formats) {
2189         register int ret;
2190         char cmd[SIZ];
2191         
2192         snprintf(cmd, sizeof cmd, "MSGP %s", formats);
2193         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2194         return ret;
2195 }
2196
2197
2198
2199 /* READ */
2200 int CtdlIPCReadDownload(CtdlIPC *ipc, void **buf, size_t bytes, size_t resume,
2201                 void (*progress_gauge_callback)
2202                         (CtdlIPC*, unsigned long, unsigned long),
2203                char *cret)
2204 {
2205         register size_t len;
2206
2207         if (!cret) return -1;
2208         if (!buf) return -1;
2209         if (*buf) return -1;
2210         if (!ipc->downloading) return -1;
2211
2212         len = resume;
2213         if (progress_gauge_callback)
2214                 progress_gauge_callback(ipc, len, bytes);
2215         while (len < bytes) {
2216                 register size_t block;
2217
2218                 block = CtdlIPCPartialRead(ipc, buf, len, 4096, cret);
2219                 if (block == 0) {
2220                         free(*buf);
2221                         return 0;
2222                 }
2223                 len += block;
2224                 if (progress_gauge_callback)
2225                         progress_gauge_callback(ipc, len, bytes);
2226         }
2227         return len;
2228 }
2229
2230 /* READ - pipelined */
2231 int CtdlIPCHighSpeedReadDownload(CtdlIPC *ipc, void **buf, size_t bytes,
2232                size_t resume,
2233                 void (*progress_gauge_callback)
2234                         (CtdlIPC*, unsigned long, unsigned long),
2235                char *cret)
2236 {
2237         register size_t len;
2238         register int calls;     /* How many calls in the pipeline */
2239         register int i;         /* iterator */
2240         char aaa[4096];
2241
2242         if (!cret) return -1;
2243         if (!buf) return -1;
2244         if (*buf) return -1;
2245         if (!ipc->downloading) return -1;
2246
2247         *buf = (void *)realloc(*buf, bytes - resume);
2248         if (!*buf) return -1;
2249
2250         len = 0;
2251         CtdlIPC_lock(ipc);
2252         if (progress_gauge_callback)
2253                 progress_gauge_callback(ipc, len, bytes);
2254
2255         /* How many calls will be in the pipeline? */
2256         calls = (bytes - resume) / 4096;
2257         if ((bytes - resume) % 4096) calls++;
2258
2259         /* Send all requests at once */
2260         for (i = 0; i < calls; i++) {
2261                 sprintf(aaa, "READ %d|4096", (int)(i * 4096 + resume) );
2262                 CtdlIPC_putline(ipc, aaa);
2263         }
2264
2265         /* Receive all responses at once */
2266         for (i = 0; i < calls; i++) {
2267                 CtdlIPC_getline(ipc, aaa);
2268                 if (aaa[0] != '6')
2269                         strcpy(cret, &aaa[4]);
2270                 else {
2271                         len = extract_long(&aaa[4], 0);
2272                         /* I know what I'm doing */
2273                         serv_read(ipc, ((char *)(*buf) + (i * 4096)), len);
2274                 }
2275                 if (progress_gauge_callback)
2276                         progress_gauge_callback(ipc, i * 4096 + len, bytes);
2277         }
2278         CtdlIPC_unlock(ipc);
2279         return len;
2280 }
2281
2282
2283 /* UCLS */
2284 int CtdlIPCEndUpload(CtdlIPC *ipc, int discard, char *cret)
2285 {
2286         register int ret;
2287         char cmd[8];
2288
2289         if (!cret) return -1;
2290         if (!ipc->uploading) return -1;
2291
2292         sprintf(cmd, "UCLS %d", discard ? 0 : 1);
2293         ret = CtdlIPCGenericCommand(ipc, cmd, NULL, 0, NULL, NULL, cret);
2294         ipc->uploading = 0;
2295         return ret;
2296 }
2297
2298
2299 /* WRIT */
2300 int CtdlIPCWriteUpload(CtdlIPC *ipc, const char *path,
2301                 void (*progress_gauge_callback)
2302                         (CtdlIPC*, unsigned long, unsigned long),
2303                 char *cret)
2304 {
2305         register int ret = -1;
2306         register size_t offset = 0;
2307         size_t bytes;
2308         char aaa[SIZ];
2309         char buf[4096];
2310         FILE *fd;
2311
2312         if (!cret) return -1;
2313         if (!path) return -1;
2314         if (!*path) return -1;
2315
2316         fd = fopen(path, "r");
2317         if (!fd) return -2;
2318
2319         fseek(fd, 0L, SEEK_END);
2320         bytes = ftell(fd);
2321         rewind(fd);
2322
2323         if (progress_gauge_callback)
2324                 progress_gauge_callback(ipc, 0, bytes);
2325
2326         while (offset < bytes) {
2327                 register size_t to_write;
2328
2329                 /* Read some data in */
2330                 to_write = fread(buf, 1, 4096, fd);
2331                 if (!to_write) {
2332                         if (feof(fd) || ferror(fd)) break;
2333                 }
2334                 sprintf(aaa, "WRIT %d", (int)to_write);
2335                 CtdlIPC_putline(ipc, aaa);
2336                 CtdlIPC_getline(ipc, aaa);
2337                 strcpy(cret, &aaa[4]);
2338                 ret = atoi(aaa);
2339                 if (aaa[0] == '7') {
2340                         to_write = extract_long(&aaa[4], 0);
2341                         
2342                         serv_write(ipc, buf, to_write);
2343                         offset += to_write;
2344                         if (progress_gauge_callback)
2345                                 progress_gauge_callback(ipc, offset, bytes);
2346                         /* Detect short reads and back up if needed */
2347                         /* offset will never be negative anyway */
2348                         fseek(fd, (signed)offset, SEEK_SET);
2349                 } else {
2350                         break;
2351                 }
2352         }
2353         if (progress_gauge_callback)
2354                 progress_gauge_callback(ipc, 1, 1);
2355         return (!ferror(fd) ? ret : -2);
2356 }
2357
2358
2359 /*
2360  * Generic command method.  This method should handle any server command
2361  * except for CHAT.  It takes the following arguments:
2362  *
2363  * ipc                  The server to speak with
2364  * command              Preformatted command to send to server
2365  * to_send              A text or binary file to send to server
2366  *                      (only sent if server requests it)
2367  * bytes_to_send        The number of bytes in to_send (required if
2368  *                      sending binary, optional if sending listing)
2369  * to_receive           Pointer to a NULL pointer, if the server
2370  *                      sends text or binary we will allocate memory
2371  *                      for the file and stuff it here
2372  * bytes_to_receive     If a file is received, we will store its
2373  *                      byte count here
2374  * proto_response       The protocol response.  Caller must provide
2375  *                      this buffer and ensure that it is at least
2376  *                      128 bytes in length.
2377  *
2378  * This function returns a number equal to the protocol response number,
2379  * -1 if an internal error occurred, -2 if caller provided bad values,
2380  * or 0 - the protocol response number if bad values were found during
2381  * the protocol exchange.
2382  * It stores the protocol response string (minus the number) in 
2383  * protocol_response as described above.  Some commands send additional
2384  * data in this string.
2385  */
2386 int CtdlIPCGenericCommand(CtdlIPC *ipc,
2387                 const char *command, const char *to_send,
2388                 size_t bytes_to_send, char **to_receive, 
2389                 size_t *bytes_to_receive, char *proto_response)
2390 {
2391         char buf[SIZ];
2392         register int ret;
2393         int watch_ssl = 0;
2394
2395         if (!command) return -2;
2396         if (!proto_response) return -2;
2397
2398 #ifdef HAVE_OPENSSL
2399         if (ipc->ssl) watch_ssl = 1;
2400 #endif
2401
2402         CtdlIPC_lock(ipc);
2403         CtdlIPC_putline(ipc, command);
2404         while (1) {
2405                 CtdlIPC_getline(ipc, proto_response);
2406                 if (proto_response[3] == '*')
2407                         instant_msgs = 1;
2408                 ret = atoi(proto_response);
2409                 strcpy(proto_response, &proto_response[4]);
2410                 switch (ret / 100) {
2411                 default:                        /* Unknown, punt */
2412                 case 2:                         /* OK */
2413                 case 3:                         /* MORE_DATA */
2414                 case 5:                         /* ERROR */
2415                         /* Don't need to do anything */
2416                         break;
2417                 case 1:                         /* LISTING_FOLLOWS */
2418                         if (to_receive && !*to_receive && bytes_to_receive) {
2419                                 *to_receive = CtdlIPCReadListing(ipc, NULL);
2420                         } else { /* Drain */
2421                                 while (CtdlIPC_getline(ipc, buf), strcmp(buf, "000")) ;
2422                                 ret = -ret;
2423                         }
2424                         break;
2425                 case 4:                         /* SEND_LISTING */
2426                         if (to_send) {
2427                                 CtdlIPCSendListing(ipc, to_send);
2428                         } else {
2429                                 /* No listing given, fake it */
2430                                 CtdlIPC_putline(ipc, "000");
2431                                 ret = -ret;
2432                         }
2433                         break;
2434                 case 6:                         /* BINARY_FOLLOWS */
2435                         if (to_receive && !*to_receive && bytes_to_receive) {
2436                                 *bytes_to_receive =
2437                                         extract_long(proto_response, 0);
2438                                 *to_receive = (char *)
2439                                         malloc((size_t)*bytes_to_receive);
2440                                 if (!*to_receive) {
2441                                         ret = -1;
2442                                 } else {
2443                                         serv_read(ipc, *to_receive,
2444                                                         *bytes_to_receive);
2445                                 }
2446                         } else {
2447                                 /* Drain */
2448                                 size_t drain;
2449
2450                                 drain = extract_long(proto_response, 0);
2451                                 while (drain > SIZ) {
2452                                         serv_read(ipc, buf, SIZ);
2453                                         drain -= SIZ;
2454                                 }
2455                                 serv_read(ipc, buf, drain);
2456                                 ret = -ret;
2457                         }
2458                         break;
2459                 case 7:                         /* SEND_BINARY */
2460                         if (to_send && bytes_to_send) {
2461                                 serv_write(ipc, to_send, bytes_to_send);
2462                         } else if (bytes_to_send) {
2463                                 /* Fake it, send nulls */
2464                                 size_t fake;
2465
2466                                 fake = bytes_to_send;
2467                                 memset(buf, '\0', SIZ);
2468                                 while (fake > SIZ) {
2469                                         serv_write(ipc, buf, SIZ);
2470                                         fake -= SIZ;
2471                                 }
2472                                 serv_write(ipc, buf, fake);
2473                                 ret = -ret;
2474                         } /* else who knows?  DANGER WILL ROBINSON */
2475                         break;
2476                 case 8:                         /* START_CHAT_MODE */
2477                         if (!strncasecmp(command, "CHAT", 4)) {
2478                                 /* Don't call chatmode with generic! */
2479                                 CtdlIPC_putline(ipc, "/quit");
2480                                 ret = -ret;
2481                         } else {
2482                                 /* In this mode we send then receive listing */
2483                                 if (to_send) {
2484                                         CtdlIPCSendListing(ipc, to_send);
2485                                 } else {
2486                                         /* No listing given, fake it */
2487                                         CtdlIPC_putline(ipc, "000");
2488                                         ret = -ret;
2489                                 }
2490                                 if (to_receive && !*to_receive
2491                                                 && bytes_to_receive) {
2492                                         *to_receive = CtdlIPCReadListing(ipc, NULL);
2493                                 } else { /* Drain */
2494                                         while (CtdlIPC_getline(ipc, buf),
2495                                                         strcmp(buf, "000")) ;
2496                                         ret = -ret;
2497                                 }
2498                         }
2499                         break;
2500                 case 9:                         /* ASYNC_MSG */
2501                         /* CtdlIPCDoAsync(ret, proto_response); */
2502                         free(CtdlIPCReadListing(ipc, NULL));    /* STUB FIXME */
2503                         break;
2504                 }
2505                 if (ret / 100 != 9)
2506                         break;
2507         }
2508         CtdlIPC_unlock(ipc);
2509         return ret;
2510 }
2511
2512
2513 static int connectsock(char *host, char *service, char *protocol, int defaultPort)
2514 {
2515         struct hostent *phe;
2516         struct servent *pse;
2517         struct protoent *ppe;
2518         struct sockaddr_in sin;
2519         int s, type;
2520
2521         memset(&sin, 0, sizeof(sin));
2522         sin.sin_family = AF_INET;
2523
2524         pse = getservbyname(service, protocol);
2525         if (pse != NULL) {
2526                 sin.sin_port = pse->s_port;
2527         }
2528         else if (atoi(service) > 0) {
2529                 sin.sin_port = htons(atoi(service));
2530         }
2531         else {
2532                 sin.sin_port = htons(defaultPort);
2533         }
2534         phe = gethostbyname(host);
2535         if (phe) {
2536                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
2537         } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
2538                 return -1;
2539         }
2540         if ((ppe = getprotobyname(protocol)) == 0) {
2541                 return -1;
2542         }
2543         if (!strcmp(protocol, "udp")) {
2544                 type = SOCK_DGRAM;
2545         } else {
2546                 type = SOCK_STREAM;
2547         }
2548
2549         s = socket(PF_INET, type, ppe->p_proto);
2550         if (s < 0) {
2551                 return -1;
2552         }
2553
2554         if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
2555                 close(s);
2556                 return -1;
2557         }
2558
2559         return (s);
2560 }
2561
2562 static int uds_connectsock(int *isLocal, char *sockpath)
2563 {
2564         struct sockaddr_un addr;
2565         int s;
2566
2567         memset(&addr, 0, sizeof(addr));
2568         addr.sun_family = AF_UNIX;
2569         safestrncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
2570
2571         s = socket(AF_UNIX, SOCK_STREAM, 0);
2572         if (s < 0) {
2573                 return -1;
2574         }
2575
2576         if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2577                 close(s);
2578                 return -1;
2579         }
2580
2581         *isLocal = 1;
2582         return s;
2583 }
2584
2585
2586 /*
2587  * input binary data from socket
2588  */
2589 static void serv_read(CtdlIPC *ipc, char *buf, unsigned int bytes)
2590 {
2591         unsigned int len, rlen;
2592
2593 #if defined(HAVE_OPENSSL)
2594         if (ipc->ssl) {
2595                 serv_read_ssl(ipc, buf, bytes);
2596                 return;
2597         }
2598 #endif
2599         len = 0;
2600         while (len < bytes) {
2601                 rlen = read(ipc->sock, &buf[len], bytes - len);
2602                 if (rlen < 1) {
2603                         connection_died(ipc, 0);
2604                         return;
2605                 }
2606                 len += rlen;
2607         }
2608 }
2609
2610
2611 /*
2612  * send binary to server
2613  */
2614 static void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
2615 {
2616         unsigned int bytes_written = 0;
2617         int retval;
2618
2619 #if defined(HAVE_OPENSSL)
2620         if (ipc->ssl) {
2621                 serv_write_ssl(ipc, buf, nbytes);
2622                 return;
2623         }
2624 #endif
2625         while (bytes_written < nbytes) {
2626                 retval = write(ipc->sock, &buf[bytes_written],
2627                                nbytes - bytes_written);
2628                 if (retval < 1) {
2629                         connection_died(ipc, 0);
2630                         return;
2631                 }
2632                 bytes_written += retval;
2633         }
2634 }
2635
2636
2637 #ifdef HAVE_OPENSSL
2638 /*
2639  * input binary data from encrypted connection
2640  */
2641 static void serv_read_ssl(CtdlIPC* ipc, char *buf, unsigned int bytes)
2642 {
2643         int len, rlen;
2644         char junk[1];
2645
2646         len = 0;
2647         while (len < bytes) {
2648                 if (SSL_want_read(ipc->ssl)) {
2649                         if ((SSL_write(ipc->ssl, junk, 0)) < 1) {
2650                                 error_printf("SSL_write in serv_read:\n");
2651                                 ERR_print_errors_fp(stderr);
2652                         }
2653                 }
2654                 rlen = SSL_read(ipc->ssl, &buf[len], bytes - len);
2655                 if (rlen < 1) {
2656                         long errval;
2657
2658                         errval = SSL_get_error(ipc->ssl, rlen);
2659                         if (errval == SSL_ERROR_WANT_READ ||
2660                                         errval == SSL_ERROR_WANT_WRITE) {
2661                                 sleep(1);
2662                                 continue;
2663                         }
2664                         if (errval == SSL_ERROR_ZERO_RETURN ||
2665                                         errval == SSL_ERROR_SSL) {
2666                                 serv_read(ipc, &buf[len], bytes - len);
2667                                 return;
2668                         }
2669                         error_printf("SSL_read in serv_read: %s\n",
2670                                         ERR_reason_error_string(ERR_peek_error()));
2671                         connection_died(ipc, 1);
2672                         return;
2673                 }
2674                 len += rlen;
2675         }
2676 }
2677
2678
2679 /*
2680  * send binary to server encrypted
2681  */
2682 static void serv_write_ssl(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
2683 {
2684         unsigned int bytes_written = 0;
2685         int retval;
2686         char junk[1];
2687
2688         while (bytes_written < nbytes) {
2689                 if (SSL_want_write(ipc->ssl)) {
2690                         if ((SSL_read(ipc->ssl, junk, 0)) < 1) {
2691                                 error_printf("SSL_read in serv_write:\n");
2692                                 ERR_print_errors_fp(stderr);
2693                         }
2694                 }
2695                 retval = SSL_write(ipc->ssl, &buf[bytes_written],
2696                                 nbytes - bytes_written);
2697                 if (retval < 1) {
2698                         long errval;
2699
2700                         errval = SSL_get_error(ipc->ssl, retval);
2701                         if (errval == SSL_ERROR_WANT_READ ||
2702                                         errval == SSL_ERROR_WANT_WRITE) {
2703                                 sleep(1);
2704                                 continue;
2705                         }
2706                         if (errval == SSL_ERROR_ZERO_RETURN ||
2707                                         errval == SSL_ERROR_SSL) {
2708                                 serv_write(ipc, &buf[bytes_written],
2709                                                 nbytes - bytes_written);
2710                                 return;
2711                         }
2712                         error_printf("SSL_write in serv_write: %s\n",
2713                                         ERR_reason_error_string(ERR_peek_error()));
2714                         connection_died(ipc, 1);
2715                         return;
2716                 }
2717                 bytes_written += retval;
2718         }
2719 }
2720
2721
2722 static void CtdlIPC_init_OpenSSL(void)
2723 {
2724         int a;
2725         SSL_METHOD *ssl_method;
2726         DH *dh;
2727         
2728         /* already done init */
2729         if (ssl_ctx) {
2730                 return;
2731         }
2732
2733         /* Get started */
2734         a = 0;
2735         ssl_ctx = NULL;
2736         dh = NULL;
2737         SSL_load_error_strings();
2738         SSLeay_add_ssl_algorithms();
2739
2740         /* Set up the SSL context in which we will oeprate */
2741         ssl_method = SSLv23_client_method();
2742         ssl_ctx = SSL_CTX_new(ssl_method);
2743         if (!ssl_ctx) {
2744                 error_printf("SSL_CTX_new failed: %s\n",
2745                                 ERR_reason_error_string(ERR_get_error()));
2746                 return;
2747         }
2748         /* Any reasonable cipher we can get */
2749         if (!(SSL_CTX_set_cipher_list(ssl_ctx, CIT_CIPHERS))) {
2750                 error_printf("No ciphers available for encryption\n");
2751                 return;
2752         }
2753         SSL_CTX_set_session_cache_mode(ssl_ctx, SSL_SESS_CACHE_BOTH);
2754         
2755         /* Load DH parameters into the context */
2756         dh = DH_new();
2757         if (!dh) {
2758                 error_printf("Can't allocate a DH object: %s\n",
2759                                 ERR_reason_error_string(ERR_get_error()));
2760                 return;
2761         }
2762         if (!(BN_hex2bn(&(dh->p), DH_P))) {
2763                 error_printf("Can't assign DH_P: %s\n",
2764                                 ERR_reason_error_string(ERR_get_error()));
2765                 DH_free(dh);
2766                 return;
2767         }
2768         if (!(BN_hex2bn(&(dh->g), DH_G))) {
2769                 error_printf("Can't assign DH_G: %s\n",
2770                                 ERR_reason_error_string(ERR_get_error()));
2771                 DH_free(dh);
2772                 return;
2773         }
2774         dh->length = DH_L;
2775         SSL_CTX_set_tmp_dh(ssl_ctx, dh);
2776         DH_free(dh);
2777
2778 #ifdef THREADED_CLIENT
2779         /* OpenSSL requires callbacks for threaded clients */
2780         CRYPTO_set_locking_callback(ssl_lock);
2781         CRYPTO_set_id_callback(id_callback);
2782
2783         /* OpenSSL requires us to do semaphores for threaded clients */
2784         Critters = malloc(CRYPTO_num_locks() * sizeof (pthread_mutex_t *));
2785         if (!Critters) {
2786                 perror("malloc failed");
2787                 exit(1);
2788         } else {
2789                 for (a = 0; a < CRYPTO_num_locks(); a++) {
2790                         Critters[a] = malloc(sizeof (pthread_mutex_t));
2791                         if (!Critters[a]) {
2792                                 perror("malloc failed");
2793                                 exit(1);
2794                         }
2795                         pthread_mutex_init(Critters[a], NULL);
2796                 }
2797         }
2798 #endif /* THREADED_CLIENT */       
2799 }
2800
2801
2802 static void ssl_lock(int mode, int n, const char *file, int line)
2803 {
2804 #ifdef THREADED_CLIENT
2805         if (mode & CRYPTO_LOCK)
2806                 pthread_mutex_lock(Critters[n]);
2807         else
2808                 pthread_mutex_unlock(Critters[n]);
2809 #endif /* THREADED_CLIENT */
2810 }
2811
2812 #ifdef THREADED_CLIENT
2813 static unsigned long id_callback(void) {
2814         return (unsigned long)pthread_self();
2815 }
2816 #endif /* THREADED_CLIENT */
2817 #endif /* HAVE_OPENSSL */
2818
2819
2820 /*
2821  * input string from socket - implemented in terms of serv_read()
2822  */
2823 static void CtdlIPC_getline(CtdlIPC* ipc, char *buf)
2824 {
2825         int i;
2826
2827         /* Read one character at a time. */
2828         for (i = 0;; i++) {
2829                 serv_read(ipc, &buf[i], 1);
2830                 if (buf[i] == '\n' || i == (SIZ-1))
2831                         break;
2832         }
2833
2834         /* If we got a long line, discard characters until the newline. */
2835         if (i == (SIZ-1))
2836                 while (buf[i] != '\n')
2837                         serv_read(ipc, &buf[i], 1);
2838
2839         /* Strip the trailing newline (and carriage return, if present) */
2840         if (buf[i] == 10) buf[i--] = 0;
2841         if (buf[i] == 13) buf[i--] = 0;
2842 }
2843
2844 void CtdlIPC_chat_recv(CtdlIPC* ipc, char* buf)
2845 {
2846         CtdlIPC_getline(ipc, buf);
2847 }
2848
2849 /*
2850  * send line to server - implemented in terms of serv_write()
2851  */
2852 static void CtdlIPC_putline(CtdlIPC *ipc, const char *buf)
2853 {
2854         char *cmd = NULL;
2855         int len;
2856
2857         len = strlen(buf);
2858         cmd = malloc(len + 2);
2859         if (!cmd) {
2860                 /* This requires no extra memory */
2861                 serv_write(ipc, buf, len);
2862                 serv_write(ipc, "\n", 1);
2863         } else {
2864                 /* This is network-optimized */
2865                 strncpy(cmd, buf, len);
2866                 strcpy(cmd + len, "\n");
2867                 serv_write(ipc, cmd, len + 1);
2868                 free(cmd);
2869         }
2870
2871         ipc->last_command_sent = time(NULL);
2872 }
2873
2874 void CtdlIPC_chat_send(CtdlIPC* ipc, const char* buf)
2875 {
2876         CtdlIPC_putline(ipc, buf);
2877 }
2878
2879
2880 /*
2881  * attach to server
2882  */
2883 CtdlIPC* CtdlIPC_new(int argc, char **argv, char *hostbuf, char *portbuf)
2884 {
2885         int a;
2886         char cithost[SIZ];
2887         char citport[SIZ];
2888         char sockpath[SIZ];
2889         CtdlIPC* ipc;
2890
2891         ipc = ialloc(CtdlIPC);
2892         if (!ipc) {
2893                 return 0;
2894         }
2895 #if defined(HAVE_OPENSSL)
2896         ipc->ssl = NULL;
2897         CtdlIPC_init_OpenSSL();
2898 #endif
2899 #if defined(HAVE_PTHREAD_H)
2900         pthread_mutex_init(&(ipc->mutex), NULL); /* Default fast mutex */
2901 #endif
2902         ipc->sock = -1;                 /* Not connected */
2903         ipc->isLocal = 0;               /* Not local, of course! */
2904         ipc->downloading = 0;
2905         ipc->uploading = 0;
2906         ipc->last_command_sent = 0L;
2907         ipc->network_status_cb = NULL;
2908
2909         strcpy(cithost, DEFAULT_HOST);  /* default host */
2910         strcpy(citport, DEFAULT_PORT);  /* default port */
2911
2912         /* Allow caller to supply our values (Windows) */
2913         if (hostbuf && strlen(hostbuf) > 0)
2914                 strcpy(cithost, hostbuf);
2915         if (portbuf && strlen(portbuf) > 0)
2916                 strcpy(citport, portbuf);
2917
2918         /* Read host/port from command line if present */
2919         for (a = 0; a < argc; ++a) {
2920                 if (a == 0) {
2921                         /* do nothing */
2922                 } else if (a == 1) {
2923                         strcpy(cithost, argv[a]);
2924                 } else if (a == 2) {
2925                         strcpy(citport, argv[a]);
2926                 } else {
2927                         error_printf("%s: usage: ",argv[0]);
2928                         error_printf("%s [host] [port] ",argv[0]);
2929                         ifree(ipc);
2930                         errno = EINVAL;
2931                         return 0;
2932                 }
2933         }
2934
2935         if ((!strcmp(cithost, "localhost"))
2936            || (!strcmp(cithost, "127.0.0.1"))) {
2937                 ipc->isLocal = 1;
2938         }
2939
2940         /* If we're using a unix domain socket we can do a bunch of stuff */
2941         if (!strcmp(cithost, UDS)) {
2942                 if (!strcasecmp(citport, DEFAULT_PORT)) {
2943                         snprintf(sockpath, sizeof sockpath, "%s%s",
2944 #ifndef HAVE_RUN_DIR
2945                                          CTDLDIR
2946 #else
2947                                          RUN_DIR
2948 #endif
2949                                          , "/citadel.socket");
2950                 }
2951                 else {
2952                         snprintf(sockpath, sizeof sockpath, "%s%s",
2953                                 citport, 
2954 #ifndef HAVE_RUN_DIR
2955                                          CTDLDIR
2956 #else
2957                                          RUN_DIR
2958 #endif
2959                                          "/citadel.socket");
2960                 }
2961                 ipc->sock = uds_connectsock(&(ipc->isLocal), sockpath);
2962                 if (ipc->sock == -1) {
2963                         ifree(ipc);
2964                         return 0;
2965                 }
2966                 if (hostbuf != NULL) strcpy(hostbuf, cithost);
2967                 if (portbuf != NULL) strcpy(portbuf, sockpath);
2968                 return ipc;
2969         }
2970
2971         ipc->sock = connectsock(cithost, citport, "tcp", 504);
2972         if (ipc->sock == -1) {
2973                 ifree(ipc);
2974                 return 0;
2975         }
2976         if (hostbuf != NULL) strcpy(hostbuf, cithost);
2977         if (portbuf != NULL) strcpy(portbuf, citport);
2978         return ipc;
2979 }
2980
2981
2982 /*
2983  * Disconnect and delete the IPC class (destructor)
2984  */
2985 void CtdlIPC_delete(CtdlIPC* ipc)
2986 {
2987 #ifdef HAVE_OPENSSL
2988         if (ipc->ssl) {
2989                 SSL_shutdown(ipc->ssl);
2990                 SSL_free(ipc->ssl);
2991                 ipc->ssl = NULL;
2992         }
2993 #endif
2994         if (ipc->sock > -1) {
2995                 shutdown(ipc->sock, 2); /* Close it up */
2996                 ipc->sock = -1;
2997         }
2998         ifree(ipc);
2999 }
3000
3001
3002 /*
3003  * Disconnect and delete the IPC class (destructor)
3004  * Also NULLs out the pointer
3005  */
3006 void CtdlIPC_delete_ptr(CtdlIPC** pipc)
3007 {
3008         CtdlIPC_delete(*pipc);
3009         *pipc = NULL;
3010 }
3011
3012
3013 /*
3014  * return the file descriptor of the server socket so we can select() on it.
3015  *
3016  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
3017  * rewritten...
3018  */
3019 int CtdlIPC_getsockfd(CtdlIPC* ipc)
3020 {
3021         return ipc->sock;
3022 }
3023
3024
3025 /*
3026  * return one character
3027  *
3028  * FIXME: This is only used in chat mode; eliminate it when chat mode gets
3029  * rewritten...
3030  */
3031 char CtdlIPC_get(CtdlIPC* ipc)
3032 {
3033         char buf[2];
3034         char ch;
3035
3036         serv_read(ipc, buf, 1);
3037         ch = (int) buf[0];
3038
3039         return (ch);
3040 }