Imported Upstream version 2.5.1
[debian/amanda] / server-src / amlabel.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998, 2000 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: amlabel.c,v 1.53 2006/07/25 18:27:57 martinea Exp $
28  *
29  * write an Amanda label on a tape
30  */
31 #include "amanda.h"
32 #include "conffile.h"
33 #include "tapefile.h"
34 #include "tapeio.h"
35 #include "changer.h"
36
37 #ifdef HAVE_LIBVTBLC
38 #include <vtblc.h>
39 #endif /* HAVE_LIBVTBLC */
40
41 /* local functions */
42
43 int main(int argc, char **argv);
44 void usage(void);
45
46 void
47 usage(void)
48 {
49     fprintf(stderr, "Usage: %s [-f] <conf> <label> [slot <slot-number>] [-o configoption]*\n",
50             get_pname());
51     exit(1);
52 }
53
54 int
55 main(
56     int         argc,
57     char **     argv)
58 {
59     char *conffile;
60     char *conf_tapelist;
61     char *outslot = NULL;
62     char *errstr = NULL, *label, *oldlabel=NULL, *tapename = NULL;
63     char *labelstr, *slotstr;
64     char *olddatestamp=NULL;
65     char *conf_tapelist_old;
66     unsigned long malloc_hist_1, malloc_size_1;
67     unsigned long malloc_hist_2, malloc_size_2;
68 #ifdef HAVE_LINUX_ZFTAPE_H
69     int fd = -1;
70     int isa_zftape;
71 #endif /* HAVE_LINUX_ZFTAPE_H */
72     int have_changer;
73     int force, tape_ok;
74     tapetype_t *tape;
75     size_t tt_blocksize_kb;
76     int slotcommand;
77     uid_t uid_me;
78     uid_t uid_dumpuser;
79     char *dumpuser;
80     struct passwd *pw;
81     int    new_argc;
82     char **new_argv;
83
84 #ifdef HAVE_LIBVTBLC
85     int vtbl_no      = -1;
86     char *datestr    = NULL;
87     char *rawtapedev = NULL;
88     int first_seg, last_seg;
89 #endif /* HAVE_LIBVTBLC */
90
91     safe_fd(-1, 0);
92     safe_cd();
93
94     set_pname("amlabel");
95
96     dbopen(DBG_SUBDIR_SERVER);
97
98     /* Don't die when child closes pipe */
99     signal(SIGPIPE, SIG_IGN);
100
101     malloc_size_1 = malloc_inuse(&malloc_hist_1);
102
103     erroutput_type = ERR_INTERACTIVE;
104
105     parse_server_conf(argc, argv, &new_argc, &new_argv);
106
107     if(new_argc > 1 && strcmp(new_argv[1],"-f") == 0)
108          force=1;
109     else force=0;
110
111     if(new_argc != 3+force && new_argc != 5+force)
112         usage();
113
114     config_name = new_argv[1+force];
115     label = new_argv[2+force];
116
117     if(new_argc == 5+force) {
118         if(strcmp(new_argv[3+force], "slot"))
119             usage();
120         slotstr = new_argv[4+force];
121         slotcommand = 1;
122     } else {
123         slotstr = "current";
124         slotcommand = 0;
125     }
126
127     config_dir = vstralloc(CONFIG_DIR, "/", config_name, "/", NULL);
128     conffile = stralloc2(config_dir, CONFFILE_NAME);
129     if (read_conffile(conffile)) {
130         error("errors processing config file \"%s\"", conffile);
131         /*NOTREACHED*/
132     }
133
134     dbrename(config_name, DBG_SUBDIR_SERVER);
135
136     report_bad_conf_arg();
137
138     conf_tapelist = getconf_str(CNF_TAPELIST);
139     if (*conf_tapelist == '/') {
140         conf_tapelist = stralloc(conf_tapelist);
141     } else {
142         conf_tapelist = stralloc2(config_dir, conf_tapelist);
143     }
144     if (read_tapelist(conf_tapelist)) {
145         error("could not load tapelist \"%s\"", conf_tapelist);
146         /*NOTREACHED*/
147     }
148
149     uid_me = getuid();
150     uid_dumpuser = uid_me;
151     dumpuser = getconf_str(CNF_DUMPUSER);
152
153     if ((pw = getpwnam(dumpuser)) == NULL) {
154         error("cannot look up dump user \"%s\"", dumpuser);
155         /*NOTREACHED*/
156     }
157     uid_dumpuser = pw->pw_uid;
158     if ((pw = getpwuid(uid_me)) == NULL) {
159         error("cannot look up my own uid %ld", (long)uid_me);
160         /*NOTREACHED*/
161     }
162     if (uid_me != uid_dumpuser) {
163         error("running as user \"%s\" instead of \"%s\"",
164               pw->pw_name, dumpuser);
165         /*NOTREACHED*/
166     }
167
168     labelstr = getconf_str(CNF_LABELSTR);
169
170     if(!match(labelstr, label)) {
171         error("label %s doesn't match labelstr \"%s\"", label, labelstr);
172         /*NOTREACHED*/
173     }
174
175     if((lookup_tapelabel(label))!=NULL) {
176         if(!force) {
177             error("label %s already on a tape\n",label);
178             /*NOTREACHED*/
179         }
180     }
181     tape = lookup_tapetype(getconf_str(CNF_TAPETYPE));
182     tt_blocksize_kb = (size_t)tapetype_get_blocksize(tape);
183
184     if((have_changer = changer_init()) == 0) {
185         if(slotcommand) {
186             fprintf(stderr,
187              "%s: no tpchanger specified in \"%s\", so slot command invalid\n",
188                     new_argv[0], conffile);
189             usage();
190         }
191         tapename = stralloc(getconf_str(CNF_TAPEDEV));
192 #ifdef HAVE_LIBVTBLC
193         rawtapedev = stralloc(getconf_str(CNF_RAWTAPEDEV));
194 #endif /* HAVE_LIBVTBLC */
195     } else if(have_changer != 1) {
196         error("changer initialization failed: %s", strerror(errno));
197         /*NOTREACHED*/
198     } else {
199         if(changer_loadslot(slotstr, &outslot, &tapename)) {
200             error("could not load slot \"%s\": %s", slotstr, changer_resultstr);
201             /*NOTREACHED*/
202         }
203
204         printf("labeling tape in slot %s (%s):\n", outslot, tapename);
205     }
206
207 #ifdef HAVE_LINUX_ZFTAPE_H
208     isa_zftape = is_zftape(tapename);
209     if (isa_zftape) {
210         if((fd = tape_open(tapename, O_WRONLY)) == -1) {
211             errstr = newstralloc2(errstr, "amlabel: ",
212                                   (errno == EACCES) ? "tape is write-protected"
213                                   : strerror(errno));
214             error(errstr);
215             /*NOTREACHED*/
216         }
217     }
218 #endif /* HAVE_LINUX_ZFTAPE_H */
219
220     printf("rewinding"); fflush(stdout);
221
222 #ifdef HAVE_LINUX_ZFTAPE_H
223     if (isa_zftape) {
224         if(tapefd_rewind(fd) == -1) {
225             putchar('\n');
226             error(strerror(errno));
227             /*NOTREACHED*/
228         }
229     }
230     else
231 #endif /* HAVE_LINUX_ZFTAPE_H */
232     if((errstr = tape_rewind(tapename)) != NULL) {
233         putchar('\n');
234         error(errstr);
235         /*NOTREACHED*/
236     }
237
238     tape_ok=1;
239     printf(", reading label");fflush(stdout);
240     if((errstr = tape_rdlabel(tapename, &olddatestamp, &oldlabel)) != NULL) {
241         printf(", %s\n",errstr);
242         tape_ok=1;
243     }
244     else {
245         /* got an amanda tape */
246         printf(" %s",oldlabel);
247         if(strcmp(oldlabel, FAKE_LABEL) != 0
248            && match(labelstr, oldlabel) == 0) {
249             printf(", tape is in another amanda configuration");
250             if(!force)
251                 tape_ok=0;
252         }
253         else {
254             if((lookup_tapelabel(oldlabel)) != NULL) {
255                 printf(", tape is active");
256                 if(!force)
257                     tape_ok=0;
258             }
259         }
260         printf("\n");
261     }
262     amfree(oldlabel);
263     amfree(olddatestamp);
264         
265     printf("rewinding"); fflush(stdout);
266
267 #ifdef HAVE_LINUX_ZFTAPE_H
268     if (isa_zftape) {
269         if(tapefd_rewind(fd) == -1) {
270             putchar('\n');
271             error(strerror(errno));
272             /*NOTREACHED*/
273         }
274     }
275     else
276 #endif /* HAVE_LINUX_ZFTAPE_H */
277     if((errstr = tape_rewind(tapename)) != NULL) {
278         putchar('\n');
279         error(errstr);
280         /*NOTREACHED*/
281     }
282
283     if(tape_ok) {
284         printf(", writing label %s", label); fflush(stdout);
285
286 #ifdef HAVE_LINUX_ZFTAPE_H
287         if (isa_zftape) {
288             errstr = tapefd_wrlabel(fd, "X", label,
289                                     (tt_blocksize_kb * 1024));
290             if(errstr != NULL) {
291                 putchar('\n');
292                 error(errstr);
293                 /*NOTREACHED*/
294             }
295         }
296         else
297 #endif /* HAVE_LINUX_ZFTAPE_H */
298         errstr = tape_wrlabel(tapename, "X", label,
299                               (tt_blocksize_kb * 1024));
300         if(errstr != NULL) {
301             putchar('\n');
302             error(errstr);
303             /*NOTREACHED*/
304         }
305
306 #ifdef HAVE_LINUX_ZFTAPE_H
307         if (isa_zftape) {
308             tapefd_weof(fd, (off_t)1);
309         }
310 #endif /* HAVE_LINUX_ZFTAPE_H */
311
312 #ifdef HAVE_LINUX_ZFTAPE_H
313         if (isa_zftape) {
314             errstr = tapefd_wrendmark(fd, "X",
315                                       (tt_blocksize_kb * 1024));
316             if(errstr != NULL) {
317                 putchar('\n');
318                 error(errstr);
319                 /*NOTREACHED*/
320             }
321         }
322         else
323 #endif /* HAVE_LINUX_ZFTAPE_H */
324         errstr = tape_wrendmark(tapename, "X",
325                                 (tt_blocksize_kb * 1024));
326         if(errstr != NULL) {
327             putchar('\n');
328             error(errstr);
329             /*NOTREACHED*/
330         }
331
332 #ifdef HAVE_LINUX_ZFTAPE_H
333         if (isa_zftape) {
334             tapefd_weof(fd, (off_t)1);
335
336             printf(",\nrewinding"); fflush(stdout); 
337      
338             if(tapefd_rewind(fd) == -1) { 
339                 putchar('\n'); 
340                 error(strerror(errno)); 
341                 /*NOTREACHED*/
342             } 
343             close(fd);
344 #ifdef HAVE_LIBVTBLC
345             /* update volume table */
346             printf(", updating volume table"); fflush(stdout);
347     
348             if ((fd = raw_tape_open(rawtapedev, O_RDWR)) == -1) {
349                 if(errno == EACCES) {
350                     errstr = newstralloc(errstr,
351                                          "updating volume table: raw tape device is write protected");
352                 } else {
353                     errstr = newstralloc2(errstr,
354                                           "updating volume table: ", strerror(errno));
355                 }
356                 putchar('\n');
357                 error(errstr);
358                 /*NOTREACHED*/
359             }
360             /* read volume table */
361             if ((num_volumes = read_vtbl(fd, volumes, vtbl_buffer,
362                                          &first_seg, &last_seg)) == -1 ) {
363                 errstr = newstralloc2(errstr,
364                                       "reading volume table: ", strerror(errno));
365                 putchar('\n');
366                 error(errstr);
367                 /*NOTREACHED*/
368             }
369             /* set date and volume label for first entry */
370             vtbl_no = 0;
371             datestr = NULL; 
372             if (set_date(datestr, volumes, num_volumes, vtbl_no)){
373                 errstr = newstralloc2(errstr,
374                                       "setting date for entry 1: ", strerror(errno));
375                 putchar('\n');
376                 error(errstr);
377                 /*NOTREACHED*/
378             }
379             if(set_label(label, volumes, num_volumes, vtbl_no)){
380                 errstr = newstralloc2(errstr,
381                                       "setting label for entry 1: ", strerror(errno));
382                 putchar('\n');
383                 error(errstr);
384                 /*NOTREACHED*/
385             }
386             /* set date and volume label for last entry */
387             vtbl_no = 1;
388             datestr = NULL; 
389             if (set_date(datestr, volumes, num_volumes, vtbl_no)){
390                 errstr = newstralloc2(errstr,
391                                       "setting date for entry 2: ", strerror(errno));
392                 putchar('\n');
393                 error(errstr);
394                 /*NOTREACHED*/
395             }
396             if(set_label("AMANDA Tape End", volumes, num_volumes, vtbl_no)){
397                 errstr = newstralloc2(errstr,
398                                       "setting label for entry 2: ", strerror(errno));
399                 putchar('\n');
400                 error(errstr);
401                 /*NOTREACHED*/
402             }
403             /* write volume table back */
404             if (write_vtbl(fd, volumes, vtbl_buffer, num_volumes, first_seg,
405                            op_mode == trunc)) {
406                 errstr = newstralloc2(errstr,
407                                       "writing volume table: ", strerror(errno));
408                 putchar('\n');
409                 error(errstr);
410                 /*NOTREACHED*/
411             }  
412             close(fd);
413 #endif /* HAVE_LIBVTBLC */
414         }
415 #endif /* HAVE_LINUX_ZFTAPE_H */
416
417         if (tape_ok) {
418             printf(", checking label"); fflush(stdout);
419
420             if((errstr = tape_rdlabel(tapename, &olddatestamp, &oldlabel)) != NULL) {
421                 putchar('\n');
422                 if (strcmp(errstr, "not an amanda tape") != 0) {
423                     error(errstr);
424                     /*NOTREACHED*/
425                 }
426                 error("no label found, are you sure %s is non-rewinding?",
427                       tapename);
428                 /*NOTREACHED*/
429             }
430
431             if (strcmp("X", olddatestamp) != 0 ||
432                 (strcmp(oldlabel, FAKE_LABEL) != 0
433                  && strcmp(label, oldlabel) != 0)) {
434                 putchar('\n');
435                 error("read label %s back, timestamp %s (expected X), what now?",
436                       oldlabel, olddatestamp);
437                 /*NOTREACHED*/
438             }
439             amfree(oldlabel);
440             amfree(olddatestamp);
441
442             /* write tape list */
443
444             /* make a copy */
445             conf_tapelist_old = stralloc2(conf_tapelist, ".amlabel");
446             if(write_tapelist(conf_tapelist_old)) {
447                 error("couldn't write tapelist: %s", strerror(errno));
448                 /*NOTREACHED*/
449             }
450             amfree(conf_tapelist_old);
451
452             /* XXX add cur_tape number to tape list structure */
453             remove_tapelabel(label);
454             add_tapelabel("0", label);
455             if(write_tapelist(conf_tapelist)) {
456                 error("couldn't write tapelist: %s", strerror(errno));
457                 /*NOTREACHED*/
458             }
459         } /* write tape list */
460         printf(", done.\n");
461     } else {
462         printf("\ntape not labeled\n");
463     }
464
465     clear_tapelist();
466     free_new_argv(new_argc, new_argv);
467     free_server_config();
468     amfree(outslot);
469     amfree(tapename);
470     amfree(conffile);
471     amfree(conf_tapelist);
472     amfree(config_dir);
473     config_name=NULL;
474     dbclose();
475
476     malloc_size_2 = malloc_inuse(&malloc_hist_2);
477
478     if(malloc_size_1 != malloc_size_2) {
479         malloc_list(fileno(stderr), malloc_hist_1, malloc_hist_2);
480     }
481
482     return 0;
483 }