Imported Upstream version 3.3.3
[debian/amanda] / client-src / runtar.c
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-2012 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: runtar.c,v 1.24 2006/08/25 11:41:31 martinea Exp $
29  *
30  * runs GNUTAR program as root
31  *
32  * argv[0] is the runtar program name
33  * argv[1] is the config name or NOCONFIG
34  * argv[2] will be argv[0] of the gtar 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 int
44 main(
45     int         argc,
46     char **     argv)
47 {
48 #ifdef GNUTAR
49     int i;
50     char *e;
51     char *dbf;
52     char *cmdline;
53 #endif
54
55     if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
56         printf("runtar-%s\n", VERSION);
57         return (0);
58     }
59
60     /*
61      * Configure program for internationalization:
62      *   1) Only set the message locale for now.
63      *   2) Set textdomain for all amanda related programs to "amanda"
64      *      We don't want to be forced to support dozens of message catalogs.
65      */  
66     setlocale(LC_MESSAGES, "C");
67     textdomain("amanda"); 
68
69     safe_fd(-1, 0);
70     safe_cd();
71
72     set_pname("runtar");
73
74     /* Don't die when child closes pipe */
75     signal(SIGPIPE, SIG_IGN);
76
77     dbopen(DBG_SUBDIR_CLIENT);
78     config_init(CONFIG_INIT_CLIENT, NULL);
79
80     if (argc < 3) {
81         error(_("Need at least 3 arguments\n"));
82         /*NOTREACHED*/
83     }
84
85     dbprintf(_("version %s\n"), VERSION);
86
87     if (strcmp(argv[3], "--create") != 0) {
88         error(_("Can only be used to create tar archives\n"));
89         /*NOTREACHED*/
90     }
91
92 #ifndef GNUTAR
93
94     g_fprintf(stderr,_("gnutar not available on this system.\n"));
95     dbprintf(_("%s: gnutar not available on this system.\n"), argv[0]);
96     dbclose();
97     return 1;
98
99 #else
100
101     /*
102      * Print out version information for tar.
103      */
104     do {
105         FILE *  version_file;
106         char    version_buf[80];
107
108         if ((version_file = popen(GNUTAR " --version 2>&1", "r")) != NULL) {
109             if (fgets(version_buf, (int)sizeof(version_buf), version_file) != NULL) {
110                 dbprintf(_(GNUTAR " version: %s\n"), version_buf);
111             } else {
112                 if (ferror(version_file)) {
113                     dbprintf(_(GNUTAR " version: Read failure: %s\n"), strerror(errno));
114                 } else {
115                     dbprintf(_(GNUTAR " version: Read failure; EOF\n"));
116                 }
117             }
118         } else {
119             dbprintf(_(GNUTAR " version: unavailable: %s\n"), strerror(errno));
120         }
121     } while(0);
122
123 #ifdef WANT_SETUID_CLIENT
124     check_running_as(RUNNING_AS_CLIENT_LOGIN | RUNNING_AS_UID_ONLY);
125     if (!become_root()) {
126         error(_("error [%s could not become root (is the setuid bit set?)]\n"), get_pname());
127         /*NOTREACHED*/
128     }
129 #else
130     check_running_as(RUNNING_AS_CLIENT_LOGIN);
131 #endif
132
133     /* skip argv[0] */
134     argc--;
135     argv++;
136
137     dbprintf(_("config: %s\n"), argv[0]);
138     if (strcmp(argv[0], "NOCONFIG") != 0)
139         dbrename(argv[0], DBG_SUBDIR_CLIENT);
140     argc--;
141     argv++;
142
143     cmdline = stralloc(GNUTAR);
144     for (i = 1; argv[i]; i++) {
145         char *quoted;
146
147         quoted = quote_string(argv[i]);
148         cmdline = vstrextend(&cmdline, " ", quoted, NULL);
149         amfree(quoted);
150     }
151     dbprintf(_("running: %s\n"), cmdline);
152     amfree(cmdline);
153
154     dbf = dbfn();
155     if (dbf) {
156         dbf = stralloc(dbf);
157     }
158     dbclose();
159
160     execve(GNUTAR, argv, safe_env());
161
162     e = strerror(errno);
163     dbreopen(dbf, "more");
164     amfree(dbf);
165     dbprintf(_("execve of %s failed (%s)\n"), GNUTAR, e);
166     dbclose();
167
168     g_fprintf(stderr, _("runtar: could not exec %s: %s\n"), GNUTAR, e);
169     return 1;
170 #endif
171 }