1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * Copyright (c) 2007-2013 Zmanda, Inc.  All Rights Reserved.
5  * All Rights Reserved.
6  *
7  * Permission to use, copy, modify, distribute, and sell this software and its
8  * documentation for any purpose is hereby granted without fee, provided that
9  * the above copyright notice appear in all copies and that both that
10  * copyright notice and this permission notice appear in supporting
11  * documentation, and that the name of U.M. not be used in advertising or
12  * publicity pertaining to distribution of the software without specific,
13  * written prior permission.  U.M. makes no representations about the
14  * suitability of this software for any purpose.  It is provided "as is"
15  * without express or implied warranty.
16  *
17  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
19  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Authors: the Amanda Development Team.  Its members are listed in a
25  * file named AUTHORS, in the root directory of this distribution.
26  */
27 /*
28  * $Id: rundump.c,v 1.33 2006/07/25 18:27:56 martinea Exp $
29  *
30  * runs DUMP program as root
31  *
32  * argv[0] is the rundump program name
33  * argv[1] is the config name or NOCONFIG
34  * argv[2] will be argv[0] of the DUMP program
35  * ...
36  */
37 #include "amanda.h"
38 #include "util.h"
39 #include "conffile.h"
40 
41 int main(int argc, char **argv);
42 
43 #if defined(VDUMP) || defined(XFSDUMP)
44 #  undef USE_RUNDUMP
45 #  define USE_RUNDUMP
46 #endif
47 
48 #if !defined(USE_RUNDUMP)
49 #  define ERRMSG _("rundump not enabled on this system.\n")
50 #else
51 #  if !defined(DUMP) && !defined(VXDUMP) && !defined(VDUMP) && !defined(XFSDUMP)
52 #    define ERRMSG _("DUMP not available on this system.\n")
53 #  else
54 #    undef ERRMSG
55 #  endif
56 #endif
57 
58 int
main(int argc,char ** argv)59 main(
60     int		argc,
61     char **	argv)
62 {
63 #ifndef ERRMSG
64     char *dump_program;
65     int i;
66     char *e;
67     char *cmdline;
68 #endif /* ERRMSG */
69 
70     if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
71 	printf("rundump-%s\n", VERSION);
72 	return (0);
73     }
74 
75     /*
76      * Configure program for internationalization:
77      *   1) Only set the message locale for now.
78      *   2) Set textdomain for all amanda related programs to "amanda"
79      *      We don't want to be forced to support dozens of message catalogs.
80      */
81     setlocale(LC_MESSAGES, "C");
82     textdomain("amanda");
83 
84     safe_fd(-1, 0);
85     safe_cd();
86 
87     set_pname("rundump");
88 
89     /* Don't die when child closes pipe */
90     signal(SIGPIPE, SIG_IGN);
91 
92     dbopen(DBG_SUBDIR_CLIENT);
93     config_init(CONFIG_INIT_CLIENT, NULL);
94 
95     if (argc < 3) {
96 	error(_("Need at least 3 arguments\n"));
97 	/*NOTREACHED*/
98     }
99 
100     dbprintf(_("version %s\n"), VERSION);
101 
102 #ifdef ERRMSG							/* { */
103 
104     g_fprintf(stderr, ERRMSG);
105     dbprintf("%s: %s", argv[0], ERRMSG);
106     dbclose();
107     return 1;
108 
109 #else								/* } { */
110 
111 #ifdef WANT_SETUID_CLIENT
112     check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
113     if (!become_root()) {
114 	error(_("error [%s could not become root (is the setuid bit set?)]\n"), get_pname());
115 	/*NOTREACHED*/
116     }
117 #else
118     check_running_as(RUNNING_AS_CLIENT_LOGIN);
119 #endif
120 
121     /* skip argv[0] */
122     argc--;
123     argv++;
124 
125     dbprintf(_("config: %s\n"), argv[0]);
126     if (strcmp(argv[0], "NOCONFIG") != 0)
127 	dbrename(argv[0], DBG_SUBDIR_CLIENT);
128     argc--;
129     argv++;
130 
131 #ifdef XFSDUMP
132 
133     if (strcmp(argv[0], "xfsdump") == 0)
134         dump_program = XFSDUMP;
135     else /* strcmp(argv[0], "xfsdump") != 0 */
136 
137 #endif
138 
139 #ifdef VXDUMP
140 
141     if (strcmp(argv[0], "vxdump") == 0)
142         dump_program = VXDUMP;
143     else /* strcmp(argv[0], "vxdump") != 0 */
144 
145 #endif
146 
147 #ifdef VDUMP
148 
149     if (strcmp(argv[0], "vdump") == 0)
150 	dump_program = VDUMP;
151     else /* strcmp(argv[0], "vdump") != 0 */
152 
153 #endif
154 
155 #if defined(DUMP)
156         dump_program = DUMP;
157 #else
158 # if defined(XFSDUMP)
159         dump_program = XFSDUMP;
160 # else
161 #  if defined(VXDUMP)
162 	dump_program = VXDUMP;
163 #  else
164         dump_program = "dump";
165 #  endif
166 # endif
167 #endif
168 
169     cmdline = stralloc(dump_program);
170     for (i = 1; argv[i]; i++) {
171 	char *quoted;
172 
173 	quoted = quote_string(argv[i]);
174 	cmdline = vstrextend(&cmdline, " ", quoted, NULL);
175 	amfree(quoted);
176     }
177     dbprintf(_("running: %s\n"), cmdline);
178     amfree(cmdline);
179 
180     execve(dump_program, argv, safe_env());
181 
182     e = strerror(errno);
183     dbprintf(_("failed (%s)\n"), e);
184     dbclose();
185 
186     g_fprintf(stderr, _("rundump: could not exec %s: %s\n"), dump_program, e);
187     return 1;
188 #endif								/* } */
189 }
190