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