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