Imported Upstream version 2.6.0p1
[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 "token.h"
35 #include "logfile.h"
36 #include "util.h"
37 #include "conffile.h"
38 #include "diskfile.h"
39
40 const char *cmdstr[] = {
41     "BOGUS", "QUIT", "QUITTING", "DONE", "PARTIAL", 
42     "START", "FILE-DUMP", "PORT-DUMP", "CONTINUE", "ABORT",/* dumper cmds */
43     "FAILED", "TRY-AGAIN", "NO-ROOM", "RQ-MORE-DISK",   /* dumper results */
44     "ABORT-FINISHED", "BAD-COMMAND",                    /* dumper results */
45     "START-TAPER", "FILE-WRITE", "NEW-TAPE", "NO-NEW-TAPE",
46      
47     "PARTDONE", "PORT-WRITE", "DUMPER-STATUS",              /* taper cmds */
48     "PORT", "TAPE-ERROR", "TAPER-OK",                    /* taper results */
49     "REQUEST-NEW-TAPE",
50     "LAST_TOK",
51     NULL
52 };
53
54
55 cmd_t
56 getcmd(
57     struct cmdargs *    cmdargs)
58 {
59     char *line;
60     cmd_t cmd_i;
61
62     assert(cmdargs != NULL);
63
64     if (isatty(0)) {
65         g_printf("%s> ", get_pname());
66         fflush(stdout);
67         line = agets(stdin);
68     } else {
69         line = agets(stdin);
70     }
71     if (line == NULL) {
72         line = stralloc("QUIT");
73     }
74
75     cmdargs->argc = split(line, cmdargs->argv,
76         (int)(sizeof(cmdargs->argv) / sizeof(cmdargs->argv[0])), " ");
77     dbprintf(_("getcmd: %s\n"), line);
78     amfree(line);
79
80 #if DEBUG
81     {
82         int i;
83         g_fprintf(stderr,_("argc = %d\n"), cmdargs->argc);
84         for (i = 0; i < cmdargs->argc+1; i++)
85             g_fprintf(stderr,_("argv[%d] = \"%s\"\n"), i, cmdargs->argv[i]);
86     }
87 #endif
88
89     if (cmdargs->argc < 1)
90         return (BOGUS);
91
92     for(cmd_i=BOGUS; cmdstr[cmd_i] != NULL; cmd_i++)
93         if(strcmp(cmdargs->argv[1], cmdstr[cmd_i]) == 0)
94             return (cmd_i);
95     return (BOGUS);
96 }
97
98
99 printf_arglist_function1(void putresult, cmd_t, result, const char *, format)
100 {
101     va_list argp;
102
103     arglist_start(argp, format);
104     dbprintf(_("putresult: %d %s\n"), result, cmdstr[result]);
105     g_printf("%s ", cmdstr[result]);
106     g_vprintf(format, argp);
107     fflush(stdout);
108     arglist_end(argp);
109 }
110
111 char *
112 amhost_get_security_conf(
113     char *      string,
114     void *      arg)
115 {
116     if(!string || !*string)
117         return(NULL);
118
119     if(strcmp(string, "krb5principal")==0)
120         return(getconf_str(CNF_KRB5PRINCIPAL));
121     else if(strcmp(string, "krb5keytab")==0)
122         return(getconf_str(CNF_KRB5KEYTAB));
123
124     if(!arg || !((am_host_t *)arg)->disks) return(NULL);
125
126     if(strcmp(string, "amandad_path")==0)
127         return ((am_host_t *)arg)->disks->amandad_path;
128     else if(strcmp(string, "client_username")==0)
129         return ((am_host_t *)arg)->disks->client_username;
130     else if(strcmp(string, "ssh_keys")==0)
131         return ((am_host_t *)arg)->disks->ssh_keys;
132
133     return(NULL);
134 }
135
136 int check_infofile(
137     char        *infodir,
138     disklist_t  *dl,
139     char       **errmsg)
140 {
141     disk_t      *dp, *diskp;
142     char        *hostinfodir, *old_hostinfodir, *Xhostinfodir;
143     char        *diskdir,     *old_diskdir,     *Xdiskdir;
144     char        *infofile,    *old_infofile,    *Xinfofile;
145     struct stat  statbuf;
146     int other_dle_match;
147
148     if (stat(infodir, &statbuf) != 0) {
149         return 0;
150     }
151
152     for (dp = dl->head; dp != NULL; dp = dp->next) {
153         hostinfodir = sanitise_filename(dp->host->hostname);
154         diskdir     = sanitise_filename(dp->name);
155         infofile = vstralloc(infodir, "/", hostinfodir, "/", diskdir,
156                              "/info", NULL);
157         if (stat(infofile, &statbuf) == -1 && errno == ENOENT) {
158             old_hostinfodir = old_sanitise_filename(dp->host->hostname);
159             old_diskdir     = old_sanitise_filename(dp->name);
160             old_infofile    = vstralloc(infodir, old_hostinfodir, "/",
161                                         old_diskdir, "/info", NULL);
162             if (stat(old_infofile, &statbuf) == 0) {
163                 other_dle_match = 0;
164                 diskp = dl->head;
165                 while (diskp != NULL) {
166                     Xhostinfodir = sanitise_filename(diskp->host->hostname);
167                     Xdiskdir     = sanitise_filename(diskp->name);
168                     Xinfofile = vstralloc(infodir, "/", Xhostinfodir, "/",
169                                           Xdiskdir, "/info", NULL);
170                     if (strcmp(old_infofile, Xinfofile) == 0) {
171                         other_dle_match = 1;
172                         diskp = NULL;
173                     }
174                     else {
175                         diskp = diskp->next;
176                     }
177                 }
178                 if (other_dle_match == 0) {
179                     if(mkpdir(infofile, (mode_t)0755, (uid_t)-1,
180                               (gid_t)-1) == -1)  {
181                         *errmsg = vstralloc("Can't create directory for ",
182                                             infofile, NULL);
183                         return -1;
184                     }
185                     if(copy_file(infofile, old_infofile, errmsg) == -1) 
186                         return -1;
187                 }
188             }
189         }
190         amfree(diskdir);
191         amfree(hostinfodir);
192         amfree(infofile);
193     }
194     return 0;
195 }