Imported Upstream version 2.5.1
[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 #endif
52
53     safe_fd(-1, 0);
54     safe_cd();
55
56     set_pname("runtar");
57
58     /* Don't die when child closes pipe */
59     signal(SIGPIPE, SIG_IGN);
60
61     dbopen(DBG_SUBDIR_CLIENT);
62     if (argc < 3) {
63         error("%s: Need at least 3 arguments\n", debug_prefix(NULL));
64         /*NOTREACHED*/
65     }
66
67     dbprintf(("%s: version %s\n", debug_prefix(NULL), version()));
68
69     if (strcmp(argv[3], "--create") != 0) {
70         error("%s: Can only be used to create tar archives\n",
71               debug_prefix(NULL));
72         /*NOTREACHED*/
73     }
74
75 #ifndef GNUTAR
76
77     fprintf(stderr,"gnutar not available on this system.\n");
78     dbprintf(("%s: gnutar not available on this system.\n", argv[0]));
79     dbclose();
80     return 1;
81
82 #else
83
84     /*
85      * Print out version information for tar.
86      */
87     do {
88         FILE *  version_file;
89         char    version_buf[80];
90
91         if ((version_file = popen(GNUTAR " --version 2>&1", "r")) != NULL) {
92             if (fgets(version_buf, (int)sizeof(version_buf), version_file) != NULL) {
93                 dbprintf((GNUTAR " version: %s\n", version_buf));
94             } else {
95                 if (ferror(version_file)) {
96                     dbprintf((GNUTAR " version: Read failure: %s\n", strerror(errno)));
97                 } else {
98                     dbprintf((GNUTAR " version: Read failure; EOF\n"));
99                 }
100             }
101         } else {
102             dbprintf((GNUTAR " version: unavailable: %s\n", strerror(errno)));
103         }
104     } while(0);
105
106     if(client_uid == (uid_t) -1) {
107         error("error [cannot find user %s in passwd file]\n", CLIENT_LOGIN);
108         /*NOTREACHED*/
109     }
110
111 #ifdef FORCE_USERID
112     if (getuid() != client_uid) {
113         error("error [must be invoked by %s]\n", CLIENT_LOGIN);
114         /*NOTREACHED*/
115     }
116
117     if (geteuid() != 0) {
118         error("error [must be setuid root]\n");
119         /*NOTREACHED*/
120     }
121 #endif
122
123 #if !defined (DONT_SUID_ROOT)
124     setuid(0);
125 #endif
126
127     /* skip argv[0] */
128     argc--;
129     argv++;
130
131     dbprintf(("config: %s\n", argv[0]));
132     if (strcmp(argv[0], "NOCONFIG") != 0)
133         dbrename(argv[0], DBG_SUBDIR_CLIENT);
134     argc--;
135     argv++;
136
137
138     dbprintf(("running: %s: ",GNUTAR));
139     for (i=0; argv[i]; i++) {
140         char *quoted;
141
142         quoted = quote_string(argv[i]);
143         dbprintf(("'%s' ", quoted));
144         amfree(quoted);
145     }
146     dbprintf(("\n"));
147     dbf = dbfn();
148     if (dbf) {
149         dbf = stralloc(dbf);
150     }
151     dbclose();
152
153     execve(GNUTAR, argv, safe_env());
154
155     e = strerror(errno);
156     dbreopen(dbf, "more");
157     amfree(dbf);
158     dbprintf(("execve of %s failed (%s)\n", GNUTAR, e));
159     dbclose();
160
161     fprintf(stderr, "runtar: could not exec %s: %s\n", GNUTAR, e);
162     return 1;
163 #endif
164 }