Imported Upstream version 3.3.1
[debian/amanda] / client-src / rundump.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 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: rundump.c,v 1.33 2006/07/25 18:27:56 martinea Exp $
28  *
29  * runs DUMP program as root
30  *
31  * argv[0] is the rundump program name
32  * argv[1] is the config name or NOCONFIG
33  * argv[2] will be argv[0] of the DUMP program
34  * ...
35  */
36 #include "amanda.h"
37 #include "util.h"
38 #include "conffile.h"
39
40 int main(int argc, char **argv);
41
42 #if defined(VDUMP) || defined(XFSDUMP)
43 #  undef USE_RUNDUMP
44 #  define USE_RUNDUMP
45 #endif
46
47 #if !defined(USE_RUNDUMP)
48 #  define ERRMSG _("rundump not enabled on this system.\n")
49 #else
50 #  if !defined(DUMP) && !defined(VXDUMP) && !defined(VDUMP) && !defined(XFSDUMP)
51 #    define ERRMSG _("DUMP not available on this system.\n")
52 #  else
53 #    undef ERRMSG
54 #  endif
55 #endif
56
57 int
58 main(
59     int         argc,
60     char **     argv)
61 {
62 #ifndef ERRMSG
63     char *dump_program;
64     int i;
65     char *e;
66     char *cmdline;
67 #endif /* ERRMSG */
68
69     if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
70         printf("rundump-%s\n", VERSION);
71         return (0);
72     }
73
74     /*
75      * Configure program for internationalization:
76      *   1) Only set the message locale for now.
77      *   2) Set textdomain for all amanda related programs to "amanda"
78      *      We don't want to be forced to support dozens of message catalogs.
79      */  
80     setlocale(LC_MESSAGES, "C");
81     textdomain("amanda"); 
82
83     safe_fd(-1, 0);
84     safe_cd();
85
86     set_pname("rundump");
87
88     /* Don't die when child closes pipe */
89     signal(SIGPIPE, SIG_IGN);
90
91     dbopen(DBG_SUBDIR_CLIENT);
92     config_init(CONFIG_INIT_CLIENT, NULL);
93
94     if (argc < 3) {
95         error(_("Need at least 3 arguments\n"));
96         /*NOTREACHED*/
97     }
98
99     dbprintf(_("version %s\n"), VERSION);
100
101 #ifdef ERRMSG                                                   /* { */
102
103     g_fprintf(stderr, ERRMSG);
104     dbprintf("%s: %s", argv[0], ERRMSG);
105     dbclose();
106     return 1;
107
108 #else                                                           /* } { */
109
110 #ifdef WANT_SETUID_CLIENT
111     check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
112     if (!become_root()) {
113         error(_("error [%s could not become root (is the setuid bit set?)]\n"), get_pname());
114         /*NOTREACHED*/
115     }
116 #else
117     check_running_as(RUNNING_AS_CLIENT_LOGIN);
118 #endif
119
120     /* skip argv[0] */
121     argc--;
122     argv++;
123
124     dbprintf(_("config: %s\n"), argv[0]);
125     if (strcmp(argv[0], "NOCONFIG") != 0)
126         dbrename(argv[0], DBG_SUBDIR_CLIENT);
127     argc--;
128     argv++;
129
130 #ifdef XFSDUMP
131
132     if (strcmp(argv[0], "xfsdump") == 0)
133         dump_program = XFSDUMP;
134     else /* strcmp(argv[0], "xfsdump") != 0 */
135
136 #endif
137
138 #ifdef VXDUMP
139
140     if (strcmp(argv[0], "vxdump") == 0)
141         dump_program = VXDUMP;
142     else /* strcmp(argv[0], "vxdump") != 0 */
143
144 #endif
145
146 #ifdef VDUMP
147
148     if (strcmp(argv[0], "vdump") == 0)
149         dump_program = VDUMP;
150     else /* strcmp(argv[0], "vdump") != 0 */
151
152 #endif
153
154 #if defined(DUMP)
155         dump_program = DUMP;
156 #else
157 # if defined(XFSDUMP)
158         dump_program = XFSDUMP;
159 # else
160 #  if defined(VXDUMP)
161         dump_program = VXDUMP;
162 #  else
163         dump_program = "dump";
164 #  endif
165 # endif
166 #endif
167
168     cmdline = stralloc(dump_program);
169     for (i = 1; argv[i]; i++) {
170         char *quoted;
171
172         quoted = quote_string(argv[i]);
173         cmdline = vstrextend(&cmdline, " ", quoted, NULL);
174         amfree(quoted);
175     }
176     dbprintf(_("running: %s\n"), cmdline);
177     amfree(cmdline);
178
179     execve(dump_program, argv, safe_env());
180
181     e = strerror(errno);
182     dbprintf(_("failed (%s)\n"), e);
183     dbclose();
184
185     g_fprintf(stderr, _("rundump: could not exec %s: %s\n"), dump_program, e);
186     return 1;
187 #endif                                                          /* } */
188 }