0f103dc87ca87cba193cc226ae2d62b22037b955
[debian/amanda] / recover-src / amrecover.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998, 2000 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: the Amanda Development Team.  Its members are listed in a
24  * file named AUTHORS, in the root directory of this distribution.
25  */
26 /*
27  * $Id: amrecover.c,v 1.73 2006/07/25 18:27:57 martinea Exp $
28  *
29  * an interactive program for recovering backed-up files
30  */
31
32 #include "amanda.h"
33 #include "version.h"
34 #include "stream.h"
35 #include "amfeatures.h"
36 #include "amrecover.h"
37 #include "getfsent.h"
38 #include "dgram.h"
39 #include "util.h"
40 #include "conffile.h"
41 #include "protocol.h"
42 #include "event.h"
43 #include "security.h"
44
45 #define amrecover_debug(i, ...) do {    \
46         if ((i) <= debug_amrecover) {   \
47             dbprintf(__VA_ARGS__);      \
48         }                               \
49 } while (0)
50
51 extern int process_line(char *line);
52 int get_line(void);
53 int grab_reply(int show);
54 void sigint_handler(int signum);
55 int main(int argc, char **argv);
56
57 #define USAGE _("Usage: amrecover [[-C] <config>] [-s <index-server>] [-t <tape-server>] [-d <tape-device>] [-o <clientconfigoption>]*\n")
58
59 char *server_name = NULL;
60 int server_socket;
61 char *server_line = NULL;
62 char *dump_datestamp = NULL;            /* date we are restoring */
63 char *dump_hostname;                    /* which machine we are restoring */
64 char *disk_name = NULL;                 /* disk we are restoring */
65 char *mount_point = NULL;               /* where disk was mounted */
66 char *disk_path = NULL;                 /* path relative to mount point */
67 char dump_date[STR_SIZE];               /* date on which we are restoring */
68 int quit_prog;                          /* set when time to exit parser */
69 char *tape_server_name = NULL;
70 int tape_server_socket;
71 char *tape_device_name = NULL;
72 am_feature_t *our_features = NULL;
73 char *our_features_string = NULL;
74 am_feature_t *indexsrv_features = NULL;
75 am_feature_t *tapesrv_features = NULL;
76 static char *errstr = NULL;
77 char *authopt;
78 int amindexd_alive = 0;
79
80 static struct {
81     const char *name;
82     security_stream_t *fd;
83 } streams[] = {
84 #define MESGFD  0
85     { "MESG", NULL },
86 };
87 #define NSTREAMS        (int)(sizeof(streams) / sizeof(streams[0]))
88
89 static void amindexd_response(void *, pkt_t *, security_handle_t *);
90 void stop_amindexd(void);
91 char *amindexd_client_get_security_conf(char *, void *);
92
93 static char* mesg_buffer = NULL;
94 /* gets a "line" from server and put in server_line */
95 /* server_line is terminated with \0, \r\n is striped */
96 /* returns -1 if error */
97
98 int
99 get_line(void)
100 {
101     ssize_t size;
102     char *newbuf, *s;
103     void *buf;
104
105     if (!mesg_buffer)
106         mesg_buffer = stralloc("");
107  
108     while (!strstr(mesg_buffer,"\r\n")) {
109         buf = NULL;
110         size = security_stream_read_sync(streams[MESGFD].fd, &buf);
111         if(size < 0) {
112             amrecover_debug(1, "amrecover: get_line size < 0 (%zd)\n", size);
113             return -1;
114         }
115         else if(size == 0) {
116             amrecover_debug(1, "amrecover: get_line size == 0 (%zd)\n", size);
117             return -1;
118         }
119         else if (buf == NULL) {
120             amrecover_debug(1, "amrecover: get_line buf == NULL\n");
121             return -1;
122         }
123         amrecover_debug(1, "amrecover: get_line size = %zd\n", size);
124         newbuf = alloc(strlen(mesg_buffer)+size+1);
125         strncpy(newbuf, mesg_buffer, (size_t)(strlen(mesg_buffer) + size));
126         memcpy(newbuf+strlen(mesg_buffer), buf, (size_t)size);
127         newbuf[strlen(mesg_buffer)+size] = '\0';
128         amfree(mesg_buffer);
129         mesg_buffer = newbuf;
130     }
131
132     s = strstr(mesg_buffer,"\r\n");
133     *s = '\0';
134     newbuf = stralloc(s+2);
135     server_line = newstralloc(server_line, mesg_buffer);
136     amfree(mesg_buffer);
137     mesg_buffer = newbuf;
138     return 0;
139 }
140
141
142 /* get reply from server and print to screen */
143 /* handle multi-line reply */
144 /* return -1 if error */
145 /* return code returned by server always occupies first 3 bytes of global
146    variable server_line */
147 int
148 grab_reply(
149     int show)
150 {
151     do {
152         if (get_line() == -1) {
153             return -1;
154         }
155         if(show) puts(server_line);
156     } while (server_line[3] == '-');
157     if(show) fflush(stdout);
158
159     return 0;
160 }
161
162
163 /* get 1 line of reply */
164 /* returns -1 if error, 0 if last (or only) line, 1 if more to follow */
165 int
166 get_reply_line(void)
167 {
168     if (get_line() == -1)
169         return -1;
170     return server_line[3] == '-';
171 }
172
173
174 /* returns pointer to returned line */
175 char *
176 reply_line(void)
177 {
178     return server_line;
179 }
180
181
182
183 /* returns 0 if server returned an error code (ie code starting with 5)
184    and non-zero otherwise */
185 int
186 server_happy(void)
187 {
188     return server_line[0] != '5';
189 }
190
191
192 int
193 send_command(
194     char *      cmd)
195 {
196     /*
197      * NOTE: this routine is called from sigint_handler, so we must be
198      * **very** careful about what we do since there is no way to know
199      * our state at the time the interrupt happened.  For instance,
200      * do not use any stdio or malloc routines here.
201      */
202     char *buffer;
203
204     buffer = alloc(strlen(cmd)+3);
205     strncpy(buffer, cmd, strlen(cmd));
206     buffer[strlen(cmd)] = '\r';
207     buffer[strlen(cmd)+1] = '\n';
208     buffer[strlen(cmd)+2] = '\0';
209
210     if(security_stream_write(streams[MESGFD].fd, buffer, strlen(buffer)) < 0) {
211         return -1;
212     }
213     amfree(buffer);
214     return (0);
215 }
216
217
218 /* send a command to the server, get reply and print to screen */
219 int
220 converse(
221     char *      cmd)
222 {
223     if (send_command(cmd) == -1) return -1;
224     if (grab_reply(1) == -1) return -1;
225     return 0;
226 }
227
228
229 /* same as converse() but reply not echoed to stdout */
230 int
231 exchange(
232     char *      cmd)
233 {
234     if (send_command(cmd) == -1) return -1;
235     if (grab_reply(0) == -1) return -1;
236     return 0;
237 }
238
239
240 /* basic interrupt handler for when user presses ^C */
241 /* Bale out, letting server know before doing so */
242 void
243 sigint_handler(
244     int signum)
245 {
246     /*
247      * NOTE: we must be **very** careful about what we do here since there
248      * is no way to know our state at the time the interrupt happened.
249      * For instance, do not use any stdio routines here or in any called
250      * routines.  Also, use _exit() instead of exit() to make sure stdio
251      * buffer flushing is not attempted.
252      */
253     (void)signum;       /* Quiet unused parameter warning */
254
255     if (extract_restore_child_pid != -1)
256         (void)kill(extract_restore_child_pid, SIGKILL);
257     extract_restore_child_pid = -1;
258
259     if(amindexd_alive) 
260         (void)send_command("QUIT");
261
262     _exit(1);
263 }
264
265
266 void
267 clean_pathname(
268     char *      s)
269 {
270     size_t length;
271     length = strlen(s);
272
273     /* remove "/" at end of path */
274     if(length>1 && s[length-1]=='/')
275         s[length-1]='\0';
276
277     /* change "/." to "/" */
278     if(strcmp(s,"/.")==0)
279         s[1]='\0';
280
281     /* remove "/." at end of path */
282     if(strcmp(&(s[length-2]),"/.")==0)
283         s[length-2]='\0';
284 }
285
286
287 void
288 quit(void)
289 {
290     quit_prog = 1;
291     (void)converse("QUIT");
292     stop_amindexd();
293 }
294
295 char *localhost = NULL;
296
297 #ifdef DEFAULT_TAPE_SERVER
298 # define DEFAULT_TAPE_SERVER_FAILOVER (DEFAULT_TAPE_SERVER)
299 #else
300 # define DEFAULT_TAPE_SERVER_FAILOVER (NULL)
301 #endif
302
303 int
304 main(
305     int         argc,
306     char **     argv)
307 {
308     int i;
309     time_t timer;
310     char *lineread = NULL;
311     struct sigaction act, oact;
312     extern char *optarg;
313     extern int optind;
314     char *line = NULL;
315     const security_driver_t *secdrv;
316     char *req = NULL;
317     int response_error;
318     struct tm *tm;
319     config_overwrites_t *cfg_ovr;
320
321     /*
322      * Configure program for internationalization:
323      *   1) Only set the message locale for now.
324      *   2) Set textdomain for all amanda related programs to "amanda"
325      *      We don't want to be forced to support dozens of message catalogs.
326      */  
327     setlocale(LC_MESSAGES, "C");
328     textdomain("amanda"); 
329
330     safe_fd(-1, 0);
331
332     set_pname("amrecover");
333
334     /* Don't die when child closes pipe */
335     signal(SIGPIPE, SIG_IGN);
336
337     dbopen(DBG_SUBDIR_CLIENT);
338
339     localhost = alloc(MAX_HOSTNAME_LENGTH+1);
340     if (gethostname(localhost, MAX_HOSTNAME_LENGTH) != 0) {
341         error(_("cannot determine local host name\n"));
342         /*NOTREACHED*/
343     }
344     localhost[MAX_HOSTNAME_LENGTH] = '\0';
345
346     /* load the base client configuration */
347     config_init(CONFIG_INIT_CLIENT, NULL);
348
349     /* treat amrecover-specific command line options as the equivalent
350      * -o command-line options to set configuration values */
351     cfg_ovr = new_config_overwrites(argc/2);
352
353     /* If the first argument is not an option flag, then we assume
354      * it is a configuration name to match the syntax of the other
355      * Amanda utilities. */
356     if (argc > 1 && argv[1][0] != '-') {
357         add_config_overwrite(cfg_ovr, "conf", argv[1]);
358
359         /* remove that option from the command line */
360         argv[1] = argv[0];
361         argv++; argc--;
362     }
363
364     /* now parse regular command-line '-' options */
365     while ((i = getopt(argc, argv, "o:C:s:t:d:U")) != EOF) {
366         switch (i) {
367             case 'C':
368                 add_config_overwrite(cfg_ovr, "conf", optarg);
369                 break;
370
371             case 's':
372                 add_config_overwrite(cfg_ovr, "index_server", optarg);
373                 break;
374
375             case 't':
376                 add_config_overwrite(cfg_ovr, "tape_server", optarg);
377                 break;
378
379             case 'd':
380                 add_config_overwrite(cfg_ovr, "tapedev", optarg);
381                 break;
382
383             case 'o':
384                 add_config_overwrite_opt(cfg_ovr, optarg);
385                 break;
386
387             case 'U':
388             case '?':
389                 (void)g_printf(USAGE);
390                 return 0;
391         }
392     }
393     if (optind != argc) {
394         (void)g_fprintf(stderr, USAGE);
395         exit(1);
396     }
397
398     /* and now try to load the configuration named in that file */
399     apply_config_overwrites(cfg_ovr);
400     config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
401                 getconf_str(CNF_CONF));
402
403     check_running_as(RUNNING_AS_ROOT);
404
405     dbrename(config_name, DBG_SUBDIR_CLIENT);
406
407     our_features = am_init_feature_set();
408     our_features_string = am_feature_to_string(our_features);
409
410     server_name = NULL;
411     if (getconf_seen(CNF_INDEX_SERVER) == -2) { /* command line argument */
412         server_name = getconf_str(CNF_INDEX_SERVER);
413     }
414     if (!server_name) {
415         server_name = getenv("AMANDA_SERVER");
416         if (server_name) {
417             g_printf(_("Using index server from environment AMANDA_SERVER (%s)\n"), server_name);
418         }
419     }
420     if (!server_name) {
421         server_name = getconf_str(CNF_INDEX_SERVER);
422     }
423     if (!server_name) {
424         error(_("No index server set"));
425         /*NOTREACHED*/
426     }
427     server_name = stralloc(server_name);
428
429     tape_server_name = NULL;
430     if (getconf_seen(CNF_TAPE_SERVER) == -2) { /* command line argument */
431         tape_server_name = getconf_str(CNF_TAPE_SERVER);
432     }
433     if (!tape_server_name) {
434         tape_server_name = getenv("AMANDA_TAPE_SERVER");
435         if (!tape_server_name) {
436             tape_server_name = getenv("AMANDA_TAPESERVER");
437             if (tape_server_name) {
438                 g_printf(_("Using tape server from environment AMANDA_TAPESERVER (%s)\n"), tape_server_name);
439             }
440         } else {
441             g_printf(_("Using tape server from environment AMANDA_TAPE_SERVER (%s)\n"), tape_server_name);
442         }
443     }
444     if (!tape_server_name) {
445         tape_server_name = getconf_str(CNF_TAPE_SERVER);
446     }
447     if (!tape_server_name) {
448         error(_("No tape server set"));
449         /*NOTREACHED*/
450     }
451     tape_server_name = stralloc(tape_server_name);
452
453     amfree(tape_device_name);
454     tape_device_name = getconf_str(CNF_TAPEDEV);
455     if (!tape_device_name ||
456         strlen(tape_device_name) == 0 ||
457         !getconf_seen(CNF_TAPEDEV)) {
458         tape_device_name = NULL;
459     } else {
460         tape_device_name = stralloc(tape_device_name);
461     }
462
463     authopt = stralloc(getconf_str(CNF_AUTH));
464
465
466     amfree(disk_name);
467     amfree(mount_point);
468     amfree(disk_path);
469     dump_date[0] = '\0';
470
471     /* Don't die when child closes pipe */
472     signal(SIGPIPE, SIG_IGN);
473
474     /* set up signal handler */
475     act.sa_handler = sigint_handler;
476     sigemptyset(&act.sa_mask);
477     act.sa_flags = 0;
478     if (sigaction(SIGINT, &act, &oact) != 0) {
479         error(_("error setting signal handler: %s"), strerror(errno));
480         /*NOTREACHED*/
481     }
482
483     protocol_init();
484
485     /* We assume that amindexd support fe_amindexd_options_features */
486     /*                             and fe_amindexd_options_auth     */
487     /* We should send a noop to really know                         */
488     req = vstrallocf("SERVICE amindexd\n"
489                     "OPTIONS features=%s;auth=%s;\n",
490                     our_features_string, authopt);
491
492     secdrv = security_getdriver(authopt);
493     if (secdrv == NULL) {
494         error(_("no '%s' security driver available for host '%s'"),
495             authopt, server_name);
496         /*NOTREACHED*/
497     }
498
499     protocol_sendreq(server_name, secdrv, generic_client_get_security_conf,
500                      req, STARTUP_TIMEOUT, amindexd_response, &response_error);
501
502     amfree(req);
503     protocol_run();
504
505     g_printf(_("AMRECOVER Version %s. Contacting server on %s ...\n"),
506            version(), server_name);
507
508     if(response_error != 0) {
509         g_fprintf(stderr,"%s\n",errstr);
510         exit(1);
511     }
512
513     /* get server's banner */
514     if (grab_reply(1) == -1) {
515         aclose(server_socket);
516         exit(1);
517     }
518     if (!server_happy()) {
519         dbclose();
520         aclose(server_socket);
521         exit(1);
522     }
523
524     /* try to get the features from the server */
525     {
526         char *their_feature_string = NULL;
527
528         line = vstrallocf("FEATURES %s", our_features_string);
529         if(exchange(line) == 0) {
530             their_feature_string = stralloc(server_line+13);
531             indexsrv_features = am_string_to_feature(their_feature_string);
532         }
533         else {
534             indexsrv_features = am_set_default_feature_set();
535         }
536         amfree(their_feature_string);
537         amfree(line);
538     }
539
540     /* set the date of extraction to be today */
541     (void)time(&timer);
542     tm = localtime(&timer);
543     if (tm) 
544         strftime(dump_date, sizeof(dump_date), "%Y-%m-%d", tm);
545     else
546         error(_("BAD DATE"));
547
548     g_printf(_("Setting restore date to today (%s)\n"), dump_date);
549     line = vstrallocf("DATE %s", dump_date);
550     if (converse(line) == -1) {
551         aclose(server_socket);
552         exit(1);
553     }
554     amfree(line);
555
556     line = vstrallocf("SCNF %s", config_name);
557     if (converse(line) == -1) {
558         aclose(server_socket);
559         exit(1);
560     }
561     amfree(line);
562
563     if (server_happy()) {
564         /* set host we are restoring to this host by default */
565         amfree(dump_hostname);
566         set_host(localhost);
567         if (dump_hostname)
568             g_printf(_("Use the setdisk command to choose dump disk to recover\n"));
569         else
570             g_printf(_("Use the sethost command to choose a host to recover\n"));
571
572     }
573
574     quit_prog = 0;
575     do {
576         if ((lineread = readline("amrecover> ")) == NULL) {
577             clearerr(stdin);
578             putchar('\n');
579             break;
580         }
581         if (lineread[0] != '\0') 
582         {
583             add_history(lineread);
584             dbprintf(_("user command: '%s'\n"), lineread);
585             process_line(lineread);     /* act on line's content */
586         }
587         amfree(lineread);
588     } while (!quit_prog);
589
590     dbclose();
591
592     aclose(server_socket);
593     return 0;
594 }
595
596 static void
597 amindexd_response(
598     void *datap,
599     pkt_t *pkt,
600     security_handle_t *sech)
601 {
602     int ports[NSTREAMS], *response_error = datap, i;
603     char *p;
604     char *tok;
605     char *extra = NULL;
606
607     assert(response_error != NULL);
608     assert(sech != NULL);
609
610     if (pkt == NULL) {
611         errstr = newvstrallocf(errstr, _("[request failed: %s]"),
612                              security_geterror(sech));
613         *response_error = 1;
614         return;
615     }
616
617     if (pkt->type == P_NAK) {
618 #if defined(PACKET_DEBUG)
619         dbprintf(_("got nak response:\n----\n%s\n----\n\n"), pkt->body);
620 #endif
621
622         tok = strtok(pkt->body, " ");
623         if (tok == NULL || strcmp(tok, "ERROR") != 0)
624             goto bad_nak;
625
626         tok = strtok(NULL, "\n");
627         if (tok != NULL) {
628             errstr = newvstrallocf(errstr, "NAK: %s", tok);
629             *response_error = 1;
630         } else {
631 bad_nak:
632             errstr = newvstrallocf(errstr, _("request NAK"));
633             *response_error = 2;
634         }
635         return;
636     }
637
638     if (pkt->type != P_REP) {
639         errstr = newvstrallocf(errstr, _("received strange packet type %s: %s"),
640                               pkt_type2str(pkt->type), pkt->body);
641         *response_error = 1;
642         return;
643     }
644
645 #if defined(PACKET_DEBUG)
646     g_fprintf(stderr, _("got response:\n----\n%s\n----\n\n"), pkt->body);
647 #endif
648
649     for(i = 0; i < NSTREAMS; i++) {
650         ports[i] = -1;
651         streams[i].fd = NULL;
652     }
653
654     p = pkt->body;
655     while((tok = strtok(p, " \n")) != NULL) {
656         p = NULL;
657
658         /*
659          * Error response packets have "ERROR" followed by the error message
660          * followed by a newline.
661          */
662         if (strcmp(tok, "ERROR") == 0) {
663             tok = strtok(NULL, "\n");
664             if (tok == NULL) {
665                 errstr = newvstrallocf(errstr, _("[bogus error packet]"));
666             } else {
667                 errstr = newvstrallocf(errstr, "%s", tok);
668             }
669             *response_error = 2;
670             return;
671         }
672
673
674         /*
675          * Regular packets have CONNECT followed by three streams
676          */
677         if (strcmp(tok, "CONNECT") == 0) {
678
679             /*
680              * Parse the three stream specifiers out of the packet.
681              */
682             for (i = 0; i < NSTREAMS; i++) {
683                 tok = strtok(NULL, " ");
684                 if (tok == NULL || strcmp(tok, streams[i].name) != 0) {
685                     extra = vstrallocf(
686                            _("CONNECT token is \"%s\": expected \"%s\""),
687                            tok ? tok : _("(null)"), streams[i].name);
688                     goto parse_error;
689                 }
690                 tok = strtok(NULL, " \n");
691                 if (tok == NULL || sscanf(tok, "%d", &ports[i]) != 1) {
692                     extra = vstrallocf(
693                            _("CONNECT %s token is \"%s\" expected a port number"),
694                            streams[i].name, tok ? tok : _("(null)"));
695                     goto parse_error;
696                 }
697             }
698             continue;
699         }
700
701         /*
702          * OPTIONS [options string] '\n'
703          */
704         if (strcmp(tok, "OPTIONS") == 0) {
705             tok = strtok(NULL, "\n");
706             if (tok == NULL) {
707                 extra = vstrallocf(_("OPTIONS token is missing"));
708                 goto parse_error;
709             }
710 #if 0
711             tok_end = tok + strlen(tok);
712             while((p = strchr(tok, ';')) != NULL) {
713                 *p++ = '\0';
714                 if(strncmp_const(tok, "features=") == 0) {
715                     tok += SIZEOF("features=") - 1;
716                     am_release_feature_set(their_features);
717                     if((their_features = am_string_to_feature(tok)) == NULL) {
718                         errstr = newvstrallocf(errstr,
719                                       _("OPTIONS: bad features value: %s"),
720                                       tok);
721                         goto parse_error;
722                     }
723                 }
724                 tok = p;
725             }
726 #endif
727             continue;
728         }
729 #if 0
730         extra = vstrallocf(_("next token is \"%s\": expected \"CONNECT\", \"ERROR\" or \"OPTIONS\""), tok ? tok : _("(null)"));
731         goto parse_error;
732 #endif
733     }
734
735     /*
736      * Connect the streams to their remote ports
737      */
738     for (i = 0; i < NSTREAMS; i++) {
739 /*@i@*/ if (ports[i] == -1)
740             continue;
741         streams[i].fd = security_stream_client(sech, ports[i]);
742         if (streams[i].fd == NULL) {
743             errstr = newvstrallocf(errstr,
744                         _("[could not connect %s stream: %s]"),
745                         streams[i].name, security_geterror(sech));
746             goto connect_error;
747         }
748     }
749     /*
750      * Authenticate the streams
751      */
752     for (i = 0; i < NSTREAMS; i++) {
753         if (streams[i].fd == NULL)
754             continue;
755         if (security_stream_auth(streams[i].fd) < 0) {
756             errstr = newvstrallocf(errstr,
757                 _("[could not authenticate %s stream: %s]"),
758                 streams[i].name, security_stream_geterror(streams[i].fd));
759             goto connect_error;
760         }
761     }
762
763     /*
764      * The MESGFD and DATAFD streams are mandatory.  If we didn't get
765      * them, complain.
766      */
767     if (streams[MESGFD].fd == NULL) {
768         errstr = newvstrallocf(errstr, _("[couldn't open MESG streams]"));
769         goto connect_error;
770     }
771
772     /* everything worked */
773     *response_error = 0;
774     amindexd_alive = 1;
775     return;
776
777 parse_error:
778     errstr = newvstrallocf(errstr,
779                           _("[parse of reply message failed: %s]"),
780                           extra ? extra : _("(no additional information)"));
781     amfree(extra);
782     *response_error = 2;
783     return;
784
785 connect_error:
786     stop_amindexd();
787     *response_error = 1;
788 }
789
790 /*
791  * This is called when everything needs to shut down so event_loop()
792  * will exit.
793  */
794 void
795 stop_amindexd(void)
796 {
797     int i;
798
799     amindexd_alive = 0;
800     for (i = 0; i < NSTREAMS; i++) {
801         if (streams[i].fd != NULL) {
802             security_stream_close(streams[i].fd);
803             streams[i].fd = NULL;
804         }
805     }
806 }
807
808 char *
809 amindexd_client_get_security_conf(
810     char *      string,
811     void *      arg)
812 {
813     (void)arg;  /* Quiet unused parameter warning */
814
815     if(!string || !*string)
816         return(NULL);
817
818     if(strcmp(string, "auth")==0) {
819         return(getconf_str(CNF_AUTH));
820     }
821     else if(strcmp(string, "ssh_keys")==0) {
822         return(getconf_str(CNF_SSH_KEYS));
823     }
824     return(NULL);
825 }