Imported Upstream version 3.2.1
[debian/amanda] / server-src / server_util.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1999 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: server_util.c,v 1.17 2006/05/25 01:47:20 johnfranks Exp $
28  *
29  */
30
31 #include "amanda.h"
32 #include "server_util.h"
33 #include "arglist.h"
34 #include "logfile.h"
35 #include "util.h"
36 #include "conffile.h"
37 #include "diskfile.h"
38 #include "pipespawn.h"
39 #include "conffile.h"
40 #include "infofile.h"
41 #include "sys/wait.h"
42
43 const char *cmdstr[] = {
44     "BOGUS", "QUIT", "QUITTING", "DONE", "PARTIAL", 
45     "START", "FILE-DUMP", "PORT-DUMP", "CONTINUE", "ABORT",/* dumper cmds */
46     "FAILED", "TRY-AGAIN", "NO-ROOM", "RQ-MORE-DISK",   /* dumper results */
47     "ABORT-FINISHED", "BAD-COMMAND",                    /* dumper results */
48     "START-TAPER", "FILE-WRITE", "NEW-TAPE", "NO-NEW-TAPE",
49      
50     "PARTDONE", "PORT-WRITE", "DUMPER-STATUS",              /* taper cmds */
51     "PORT", "TAPE-ERROR", "TAPER-OK",                    /* taper results */
52     "REQUEST-NEW-TAPE", "DIRECTTCP-PORT", "TAKE-SCRIBE-FROM",
53     "START-SCAN", "LAST_TOK",
54     NULL
55 };
56
57
58 struct cmdargs *
59 getcmd(void)
60 {
61     char *line;
62     cmd_t cmd_i;
63     struct cmdargs *cmdargs = g_new0(struct cmdargs, 1);
64
65     if (isatty(0)) {
66         g_printf("%s> ", get_pname());
67         fflush(stdout);
68         line = agets(stdin);
69     } else {
70         line = agets(stdin);
71     }
72     if (line == NULL) {
73         line = stralloc("QUIT");
74     }
75
76     dbprintf(_("getcmd: %s\n"), line);
77
78     cmdargs->argv = split_quoted_strings(line);
79     cmdargs->argc = g_strv_length(cmdargs->argv);
80     cmdargs->cmd = BOGUS;
81
82     amfree(line);
83
84     if (cmdargs->argc < 1) {
85         return cmdargs;
86     }
87
88     for(cmd_i=BOGUS; cmdstr[cmd_i] != NULL; cmd_i++)
89         if(strcmp(cmdargs->argv[0], cmdstr[cmd_i]) == 0) {
90             cmdargs->cmd = cmd_i;
91             return cmdargs;
92         }
93     return cmdargs;
94 }
95
96 struct cmdargs *
97 get_pending_cmd(void)
98 {
99     SELECT_ARG_TYPE ready;
100     struct timeval  to;
101     int             nfound;
102
103     FD_ZERO(&ready);
104     FD_SET(0, &ready);
105     to.tv_sec = 0;
106     to.tv_usec = 0;
107
108     nfound = select(1, &ready, NULL, NULL, &to);
109     if (nfound && FD_ISSET(0, &ready)) {
110         return getcmd();
111     } else {
112         return NULL;
113     }
114 }
115
116 void
117 free_cmdargs(
118     struct cmdargs *cmdargs)
119 {
120     if (!cmdargs)
121         return;
122     if (cmdargs->argv)
123         g_strfreev(cmdargs->argv);
124     g_free(cmdargs);
125 }
126
127 printf_arglist_function1(void putresult, cmd_t, result, const char *, format)
128 {
129     va_list argp;
130
131     arglist_start(argp, format);
132     dbprintf(_("putresult: %d %s\n"), result, cmdstr[result]);
133     g_printf("%s ", cmdstr[result]);
134     g_vprintf(format, argp);
135     fflush(stdout);
136     arglist_end(argp);
137 }
138
139 char *
140 amhost_get_security_conf(
141     char *      string,
142     void *      arg)
143 {
144     if(!string || !*string)
145         return(NULL);
146
147     if(strcmp(string, "krb5principal")==0)
148         return(getconf_str(CNF_KRB5PRINCIPAL));
149     else if(strcmp(string, "krb5keytab")==0)
150         return(getconf_str(CNF_KRB5KEYTAB));
151
152     if(!arg || !((am_host_t *)arg)->disks) return(NULL);
153
154     if(strcmp(string, "amandad_path")==0)
155         return ((am_host_t *)arg)->disks->amandad_path;
156     else if(strcmp(string, "client_username")==0)
157         return ((am_host_t *)arg)->disks->client_username;
158     else if(strcmp(string, "client_port")==0)
159         return ((am_host_t *)arg)->disks->client_port;
160     else if(strcmp(string, "ssh_keys")==0)
161         return ((am_host_t *)arg)->disks->ssh_keys;
162
163     return(NULL);
164 }
165
166 int check_infofile(
167     char        *infodir,
168     disklist_t  *dl,
169     char       **errmsg)
170 {
171     disk_t      *dp, *diskp;
172     char        *hostinfodir, *old_hostinfodir, *Xhostinfodir;
173     char        *diskdir,     *old_diskdir,     *Xdiskdir;
174     char        *infofile,    *old_infofile,    *Xinfofile;
175     struct stat  statbuf;
176     int other_dle_match;
177
178     if (stat(infodir, &statbuf) != 0) {
179         return 0;
180     }
181
182     for (dp = dl->head; dp != NULL; dp = dp->next) {
183         hostinfodir = sanitise_filename(dp->host->hostname);
184         diskdir     = sanitise_filename(dp->name);
185         infofile = vstralloc(infodir, "/", hostinfodir, "/", diskdir,
186                              "/info", NULL);
187         if (stat(infofile, &statbuf) == -1 && errno == ENOENT) {
188             old_hostinfodir = old_sanitise_filename(dp->host->hostname);
189             old_diskdir     = old_sanitise_filename(dp->name);
190             old_infofile    = vstralloc(infodir, old_hostinfodir, "/",
191                                         old_diskdir, "/info", NULL);
192             if (stat(old_infofile, &statbuf) == 0) {
193                 other_dle_match = 0;
194                 diskp = dl->head;
195                 while (diskp != NULL) {
196                     Xhostinfodir = sanitise_filename(diskp->host->hostname);
197                     Xdiskdir     = sanitise_filename(diskp->name);
198                     Xinfofile = vstralloc(infodir, "/", Xhostinfodir, "/",
199                                           Xdiskdir, "/info", NULL);
200                     if (strcmp(old_infofile, Xinfofile) == 0) {
201                         other_dle_match = 1;
202                         diskp = NULL;
203                     }
204                     else {
205                         diskp = diskp->next;
206                     }
207                 }
208                 if (other_dle_match == 0) {
209                     if(mkpdir(infofile, (mode_t)0755, (uid_t)-1,
210                               (gid_t)-1) == -1) {
211                         *errmsg = vstralloc("Can't create directory for ",
212                                             infofile, NULL);
213                         return -1;
214                     }
215                     if(copy_file(infofile, old_infofile, errmsg) == -1) 
216                         return -1;
217                 }
218             }
219             amfree(old_hostinfodir);
220             amfree(old_diskdir);
221             amfree(old_infofile);
222         }
223         amfree(diskdir);
224         amfree(hostinfodir);
225         amfree(infofile);
226     }
227     return 0;
228 }
229
230 void
231 run_server_script(
232     pp_script_t  *pp_script,
233     execute_on_t  execute_on,
234     char         *config,
235     disk_t       *dp,
236     int           level)
237 {
238     pid_t      scriptpid;
239     int        scriptin, scriptout, scripterr;
240     char      *cmd;
241     char      *command = NULL;
242     GPtrArray *argv_ptr = g_ptr_array_new();
243     FILE      *streamout;
244     char      *line;
245     char      *plugin;
246     char       level_number[NUM_STR_SIZE];
247
248     if ((pp_script_get_execute_on(pp_script) & execute_on) == 0)
249         return;
250     if (pp_script_get_execute_where(pp_script) != ES_SERVER)
251         return;
252
253     plugin = pp_script_get_plugin(pp_script);
254     cmd = vstralloc(APPLICATION_DIR, "/", plugin, NULL);
255     g_ptr_array_add(argv_ptr, stralloc(plugin));
256
257     switch (execute_on) {
258     case EXECUTE_ON_PRE_DLE_AMCHECK:
259         command = "PRE-DLE-AMCHECK";
260         break;
261     case EXECUTE_ON_PRE_HOST_AMCHECK:
262         command = "PRE-HOST-AMCHECK";
263         break;
264     case EXECUTE_ON_POST_DLE_AMCHECK:
265         command = "POST-DLE-AMCHECK";
266         break;
267     case EXECUTE_ON_POST_HOST_AMCHECK:
268         command = "POST-HOST-AMCHECK";
269         break;
270     case EXECUTE_ON_PRE_DLE_ESTIMATE:
271         command = "PRE-DLE-ESTIMATE";
272         break;
273     case EXECUTE_ON_PRE_HOST_ESTIMATE:
274         command = "PRE-HOST-ESTIMATE";
275         break;
276     case EXECUTE_ON_POST_DLE_ESTIMATE:
277         command = "POST-DLE-ESTIMATE";
278         break;
279     case EXECUTE_ON_POST_HOST_ESTIMATE:
280         command = "POST-HOST-ESTIMATE";
281         break;
282     case EXECUTE_ON_PRE_DLE_BACKUP:
283         command = "PRE-DLE-BACKUP";
284         break;
285     case EXECUTE_ON_PRE_HOST_BACKUP:
286         command = "PRE-HOST-BACKUP";
287         break;
288     case EXECUTE_ON_POST_DLE_BACKUP:
289         command = "POST-DLE-BACKUP";
290         break;
291     case EXECUTE_ON_POST_HOST_BACKUP:
292         command = "POST-HOST-BACKUP";
293         break;
294     case EXECUTE_ON_PRE_RECOVER:
295     case EXECUTE_ON_POST_RECOVER:
296     case EXECUTE_ON_PRE_LEVEL_RECOVER:
297     case EXECUTE_ON_POST_LEVEL_RECOVER:
298     case EXECUTE_ON_INTER_LEVEL_RECOVER:
299         {
300              // ERROR these script can't be executed on server.
301              return;
302         }
303     }
304
305     g_ptr_array_add(argv_ptr, stralloc(command));
306     g_ptr_array_add(argv_ptr, stralloc("--execute-where"));
307     g_ptr_array_add(argv_ptr, stralloc("server"));
308
309     if (config) {
310         g_ptr_array_add(argv_ptr, stralloc("--config"));
311         g_ptr_array_add(argv_ptr, stralloc(config));
312     }
313     if (dp->host->hostname) {
314         g_ptr_array_add(argv_ptr, stralloc("--host"));
315         g_ptr_array_add(argv_ptr, stralloc(dp->host->hostname));
316     }
317     if (dp->name) {
318         g_ptr_array_add(argv_ptr, stralloc("--disk"));
319         g_ptr_array_add(argv_ptr, stralloc(dp->name));
320     }
321     if (dp->device) {
322         g_ptr_array_add(argv_ptr, stralloc("--device"));
323         g_ptr_array_add(argv_ptr, stralloc(dp->device));
324     }
325     if (level >= 0) {
326         g_snprintf(level_number, SIZEOF(level_number), "%d", level);
327         g_ptr_array_add(argv_ptr, stralloc("--level"));
328         g_ptr_array_add(argv_ptr, stralloc(level_number));
329     }
330
331     property_add_to_argv(argv_ptr, pp_script_get_property(pp_script));
332     g_ptr_array_add(argv_ptr, NULL);
333
334     scripterr = fileno(stderr);
335     scriptpid = pipespawnv(cmd, STDIN_PIPE|STDOUT_PIPE, 0, &scriptin,
336                            &scriptout, &scripterr,
337                            (char **)argv_ptr->pdata);
338     close(scriptin);
339
340     streamout = fdopen(scriptout, "r");
341     if (streamout) {
342         while((line = agets(streamout)) != NULL) {
343             dbprintf("script: %s\n", line);
344             amfree(line);
345         }
346     }
347     fclose(streamout);
348     waitpid(scriptpid, NULL, 0);
349     g_ptr_array_free_full(argv_ptr);
350     amfree(cmd);
351 }
352
353
354 void
355 run_server_scripts(
356     execute_on_t  execute_on,
357     char         *config,
358     disk_t       *dp,
359     int           level)
360 {
361     identlist_t pp_scriptlist;
362
363     for (pp_scriptlist = dp->pp_scriptlist; pp_scriptlist != NULL;
364          pp_scriptlist = pp_scriptlist->next) {
365         pp_script_t *pp_script = lookup_pp_script((char *)pp_scriptlist->data);
366         g_assert(pp_script != NULL);
367         run_server_script(pp_script, execute_on, config, dp, level);
368     }
369 }
370
371 void
372 run_amcleanup(
373     char *config_name)
374 {
375     pid_t amcleanup_pid;
376     char *amcleanup_program;
377     char *amcleanup_options[4];
378
379     switch(amcleanup_pid = fork()) {
380         case -1:
381             return;
382             break;
383         case  0: /* child process */
384             amcleanup_program = vstralloc(sbindir, "/", "amcleanup", NULL);
385             amcleanup_options[0] = amcleanup_program;
386             amcleanup_options[1] = "-p";
387             amcleanup_options[2] = config_name;
388             amcleanup_options[3] = NULL;
389             execve(amcleanup_program, amcleanup_options, safe_env());
390             error("exec %s: %s", amcleanup_program, strerror(errno));
391             /*NOTREACHED*/
392         default:
393             break;
394     }
395     waitpid(amcleanup_pid, NULL, 0);
396 }
397
398 char *
399 get_master_process(
400     char *logfile)
401 {
402     FILE *log;
403     char line[1024];
404     char *s, ch;
405     char *process_name;
406
407     log = fopen(logfile, "r");
408     if (!log)
409         return stralloc("UNKNOWN");
410
411     while(fgets(line, 1024, log)) {
412         if (strncmp_const(line, "INFO ") == 0) {
413             s = line+5;
414             ch = *s++;
415             process_name = s-1;
416             skip_non_whitespace(s, ch);
417             s[-1] = '\0';
418             skip_whitespace(s, ch);
419             skip_non_whitespace(s, ch);
420             s[-1] = '\0';
421             skip_whitespace(s, ch);
422             if (strncmp_const(s-1, "pid ") == 0) {
423                 process_name = stralloc(process_name);
424                 fclose(log);
425                 return process_name;
426             }
427         }
428     }
429     fclose(log);
430     return stralloc("UNKNOWN");
431 }
432
433
434 gint64
435 internal_server_estimate(
436     disk_t *dp,
437     info_t *info,
438     int     level,
439     int    *stats)
440 {
441     int    j;
442     gint64 size = 0;
443
444     *stats = 0;
445
446     if (level == 0) { /* use latest level 0, should do extrapolation */
447         gint64 est_size = (gint64)0;
448         int nb_est = 0;
449
450         for (j=NB_HISTORY-2; j>=0; j--) {
451             if (info->history[j].level == 0) {
452                 if (info->history[j].size < (gint64)0) continue;
453                 est_size = info->history[j].size;
454                 nb_est++;
455             }
456         }
457         if (nb_est > 0) {
458             size = est_size;
459             *stats = 1;
460         } else if (info->inf[level].size > (gint64)1000) { /* stats */
461             size = info->inf[level].size;
462             *stats = 1;
463         } else {
464             char *conf_tapetype = getconf_str(CNF_TAPETYPE);
465             tapetype_t *tape = lookup_tapetype(conf_tapetype);
466             size = (gint64)1000000;
467             if (size > tapetype_get_length(tape)/2)
468                 size = tapetype_get_length(tape)/2;
469             *stats = 0;
470         }
471     } else if (level == info->last_level) {
472         /* means of all X day at the same level */
473         #define NB_DAY 30
474         int nb_day = 0;
475         gint64 est_size_day[NB_DAY];
476         int nb_est_day[NB_DAY];
477
478         for (j=0; j<NB_DAY; j++) {
479             est_size_day[j] = (gint64)0;
480             nb_est_day[j] = 0;
481         }
482
483         for (j=NB_HISTORY-2; j>=0; j--) {
484             if (info->history[j].level <= 0) continue;
485             if (info->history[j].size < (gint64)0) continue;
486             if (info->history[j].level == info->history[j+1].level) {
487                 if (nb_day <NB_DAY-1) nb_day++;
488                 est_size_day[nb_day] += info->history[j].size;
489                 nb_est_day[nb_day]++;
490             } else {
491                 nb_day=0;
492             }
493         }
494         nb_day = info->consecutive_runs + 1;
495         if (nb_day > NB_DAY-1) nb_day = NB_DAY-1;
496
497         while (nb_day > 0 && nb_est_day[nb_day] == 0) nb_day--;
498
499         if (nb_est_day[nb_day] > 0) {
500             size = est_size_day[nb_day] / (gint64)nb_est_day[nb_day];
501             *stats = 1;
502         }
503         else if (info->inf[level].size > (gint64)1000) { /* stats */
504             size = info->inf[level].size;
505             *stats = 1;
506         }
507         else {
508             int level0_stat;
509             gint64 level0_size;
510             char *conf_tapetype = getconf_str(CNF_TAPETYPE);
511             tapetype_t *tape = lookup_tapetype(conf_tapetype);
512
513             level0_size = internal_server_estimate(dp, info, 0, &level0_stat);
514             size = (gint64)10000;
515             if (size > tapetype_get_length(tape)/2)
516                 size = tapetype_get_length(tape)/2;
517             if (level0_size > 0 && dp->strategy != DS_NOFULL) {
518                 if (size > level0_size/2)
519                     size = level0_size/2;
520             }
521             *stats = 0;
522         }
523     }
524     else if (level == info->last_level + 1) {
525         /* means of all first day at a new level */
526         gint64 est_size = (gint64)0;
527         int nb_est = 0;
528
529         for (j=NB_HISTORY-2; j>=0; j--) {
530             if (info->history[j].level <= 0) continue;
531             if (info->history[j].size < (gint64)0) continue;
532             if (info->history[j].level == info->history[j+1].level + 1 ) {
533                 est_size += info->history[j].size;
534                 nb_est++;
535             }
536         }
537         if (nb_est > 0) {
538             size = est_size / (gint64)nb_est;
539             *stats = 1;
540         } else if (info->inf[level].size > (gint64)1000) { /* stats */
541             size = info->inf[level].size;
542             *stats = 1;
543         } else {
544             int level0_stat;
545             gint64 level0_size;
546             char *conf_tapetype = getconf_str(CNF_TAPETYPE);
547             tapetype_t *tape = lookup_tapetype(conf_tapetype);
548
549             level0_size = internal_server_estimate(dp, info, 0, &level0_stat);
550             size = (gint64)100000;
551             if (size > tapetype_get_length(tape)/2)
552                 size = tapetype_get_length(tape)/2;
553             if (level0_size > 0 && dp->strategy != DS_NOFULL) {
554                 if (size > level0_size/2)
555                     size = level0_size/2;
556             }
557             *stats = 0;
558         }
559     } else {
560         char *conf_tapetype = getconf_str(CNF_TAPETYPE);
561         tapetype_t *tape = lookup_tapetype(conf_tapetype);
562         size = (gint64)100000;
563         if (size > tapetype_get_length(tape)/2)
564             size = tapetype_get_length(tape)/2;
565     }
566
567     return size;
568 }
569
570 int
571 server_can_do_estimate(
572     disk_t *dp,
573     info_t *info,
574     int     level)
575 {
576     gint64  size;
577     int     stats;
578
579     size = internal_server_estimate(dp, info, level, &stats);
580     return stats;
581 }
582