PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/amanda/tags/amanda252/recover-src/set_commands.c

#
C | 692 lines | 532 code | 69 blank | 91 comment | 155 complexity | 7c724694363b8f51d9053dc45de23a0a MD5 | raw file
  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: set_commands.c,v 1.26 2006/07/05 13:14:58 martinea Exp $
  28. *
  29. * implements the "set" commands in amrecover
  30. */
  31. #include "amanda.h"
  32. #include "util.h"
  33. #include "amrecover.h"
  34. #ifdef SAMBA_CLIENT
  35. extern unsigned short samba_extract_method;
  36. #endif /* SAMBA_CLIENT */
  37. /* sets a date, mapping given date into standard form if needed */
  38. int
  39. set_date(
  40. char * date)
  41. {
  42. char *cmd = NULL;
  43. char *qdisk_path;
  44. clear_dir_list();
  45. cmd = stralloc2("DATE ", date);
  46. if (converse(cmd) == -1)
  47. exit(1);
  48. /* if a host/disk/directory is set, then check if that directory
  49. is still valid at the new date, and if not set directory to
  50. mount_point */
  51. if (disk_path != NULL) {
  52. qdisk_path = quote_string(disk_path);
  53. cmd = newstralloc2(cmd, "OISD ", qdisk_path);
  54. amfree(qdisk_path);
  55. if (exchange(cmd) == -1)
  56. exit(1);
  57. if (server_happy())
  58. {
  59. suck_dir_list_from_server();
  60. }
  61. else
  62. {
  63. printf("No index records for cwd on new date\n");
  64. printf("Setting cwd to mount point\n");
  65. disk_path = newstralloc(disk_path, "/"); /* fake it */
  66. clear_dir_list();
  67. }
  68. }
  69. amfree(cmd);
  70. return 0;
  71. }
  72. void
  73. set_host(
  74. const char *host)
  75. {
  76. char *cmd = NULL;
  77. struct hostent *hp = NULL;
  78. char **hostp;
  79. int found_host = 0;
  80. char *uqhost = unquote_string(host);
  81. if (is_extract_list_nonempty())
  82. {
  83. printf("Must clear extract list before changing host\n");
  84. return;
  85. }
  86. /*
  87. * The idea here is to try as many permutations of the hostname
  88. * as we can imagine. The server will reject anything it doesn't
  89. * recognize.
  90. */
  91. cmd = stralloc2("HOST ", uqhost);
  92. if (converse(cmd) == -1)
  93. exit(1);
  94. if (server_happy())
  95. found_host = 1;
  96. /*
  97. * Try converting the given host to a fully qualified, canonical
  98. * name.
  99. */
  100. if (!found_host) {
  101. if ((hp = gethostbyname(uqhost)) != NULL) {
  102. host = hp->h_name;
  103. printf("Trying host %s ...\n", host);
  104. cmd = newstralloc2(cmd, "HOST ", host);
  105. if (converse(cmd) == -1)
  106. exit(1);
  107. if(server_happy())
  108. found_host = 1;
  109. }
  110. }
  111. /*
  112. * Since we have them, try any CNAMEs that were traversed from uqhost
  113. * to the canonical name (this assumes gethostbyname was called above)
  114. */
  115. if (!found_host) {
  116. if (hp) {
  117. for (hostp = hp->h_aliases; (host = *hostp) != NULL; hostp++)
  118. {
  119. printf(_("Trying host %s ...\n"), host);
  120. cmd = newstralloc2(cmd, "HOST ", host);
  121. if (converse(cmd) == -1)
  122. exit(1);
  123. if(server_happy())
  124. {
  125. found_host = 1;
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. /*
  132. * gethostbyname() will not return a canonical name for a host with no
  133. * IPv4 addresses, so use getaddrinfo() (if supported)
  134. */
  135. #ifdef WORKING_IPV6
  136. if (!found_host) {
  137. struct addrinfo hints;
  138. struct addrinfo *gaires = NULL;
  139. int res;
  140. hints.ai_flags = AI_CANONNAME;
  141. hints.ai_family = AF_UNSPEC;
  142. hints.ai_socktype = 0;
  143. hints.ai_protocol = 0;
  144. hints.ai_addrlen = 0;
  145. hints.ai_addr = NULL;
  146. hints.ai_canonname = NULL;
  147. hints.ai_next = NULL;
  148. if ((res = getaddrinfo(uqhost, NULL, &hints, &gaires)) == 0) {
  149. if (gaires && (host = gaires->ai_canonname)) {
  150. printf(_("Trying host %s ...\n"), host);
  151. cmd = newstralloc2(cmd, "HOST ", host);
  152. if (converse(cmd) == -1)
  153. exit(1);
  154. if(server_happy())
  155. found_host = 1;
  156. }
  157. }
  158. if (gaires) freeaddrinfo(gaires);
  159. }
  160. #endif
  161. if(found_host) {
  162. dump_hostname = newstralloc(dump_hostname, host);
  163. amfree(disk_name);
  164. amfree(mount_point);
  165. amfree(disk_path);
  166. clear_dir_list();
  167. }
  168. amfree(cmd);
  169. amfree(uqhost);
  170. }
  171. void
  172. list_host(void)
  173. {
  174. char *cmd = NULL;
  175. cmd = stralloc("LISTHOST");
  176. if (converse(cmd) == -1)
  177. exit(1);
  178. amfree(cmd);
  179. }
  180. void
  181. set_disk(
  182. char * dsk,
  183. char * mtpt)
  184. {
  185. char *cmd = NULL;
  186. char *qdsk;
  187. char *uqdsk;
  188. char *uqmtpt = NULL;
  189. if (is_extract_list_nonempty())
  190. {
  191. printf("Must clear extract list before changing disk\n");
  192. return;
  193. }
  194. /* if mount point specified, check it is valid */
  195. if (mtpt != NULL) {
  196. uqmtpt = unquote_string(mtpt);
  197. if (*mtpt != '/') {
  198. printf("Mount point \"%s\" invalid - must start with /\n", uqmtpt);
  199. amfree(uqmtpt);
  200. return;
  201. }
  202. }
  203. clear_dir_list();
  204. uqdsk = unquote_string(dsk);
  205. qdsk = quote_string(uqdsk);
  206. cmd = stralloc2("DISK ", qdsk);
  207. amfree(qdsk);
  208. if (converse(cmd) == -1)
  209. exit(1);
  210. amfree(cmd);
  211. if (!server_happy())
  212. return;
  213. disk_name = newstralloc(disk_name, uqdsk);
  214. if (mtpt == NULL)
  215. {
  216. /* mount point not specified */
  217. if (*uqdsk == '/')
  218. {
  219. /* disk specified by mount point, hence use it */
  220. mount_point = newstralloc(mount_point, uqdsk);
  221. }
  222. else
  223. {
  224. /* device name given, use '/' because nothing better */
  225. mount_point = newstralloc(mount_point, "/");
  226. }
  227. }
  228. else
  229. {
  230. /* mount point specified */
  231. mount_point = newstralloc(mount_point, uqmtpt);
  232. }
  233. /* set the working directory to the mount point */
  234. /* there is the possibility that there are no index records for the
  235. disk for the given date, hence setting the directory to the
  236. mount point will fail. Preempt this by checking first so we can write
  237. a more informative message. */
  238. if (exchange("OISD /") == -1)
  239. exit(1);
  240. if (server_happy())
  241. {
  242. disk_path = newstralloc(disk_path, "/");
  243. suck_dir_list_from_server(); /* get list of directory contents */
  244. }
  245. else
  246. {
  247. printf("No index records for disk for specified date\n");
  248. printf("If date correct, notify system administrator\n");
  249. disk_path = newstralloc(disk_path, "/"); /* fake it */
  250. clear_dir_list();
  251. }
  252. amfree(uqmtpt);
  253. amfree(uqdsk);
  254. }
  255. void
  256. list_disk(
  257. char * amdevice)
  258. {
  259. char *cmd = NULL;
  260. char *qamdevice, *uqamdevice;
  261. if(amdevice) {
  262. uqamdevice = unquote_string(amdevice);
  263. qamdevice = quote_string(uqamdevice);
  264. cmd = stralloc2("LISTDISK ", qamdevice);
  265. amfree(uqamdevice);
  266. amfree(qamdevice);
  267. if (converse(cmd) == -1)
  268. exit(1);
  269. amfree(cmd);
  270. }
  271. else {
  272. cmd = stralloc("LISTDISK");
  273. if (converse(cmd) == -1)
  274. exit(1);
  275. amfree(cmd);
  276. }
  277. }
  278. void
  279. local_cd(
  280. char *dir)
  281. {
  282. char *uqdir = unquote_string(dir);
  283. if (chdir(uqdir) == -1) {
  284. perror(uqdir);
  285. }
  286. amfree(uqdir);
  287. }
  288. void
  289. cd_glob(
  290. char * glob)
  291. {
  292. char *regex;
  293. char *regex_path;
  294. char *s;
  295. char *uqglob;
  296. char *path_on_disk = NULL;
  297. if (disk_name == NULL) {
  298. printf("Must select disk before changing directory\n");
  299. return;
  300. }
  301. uqglob = unquote_string(glob);
  302. regex = glob_to_regex(uqglob);
  303. dbprintf(("cd_glob (%s) -> %s\n", uqglob, regex));
  304. if ((s = validate_regexp(regex)) != NULL) {
  305. printf("\"%s\" is not a valid shell wildcard pattern: ", glob);
  306. puts(s);
  307. amfree(regex);
  308. return;
  309. }
  310. /*
  311. * glob_to_regex() anchors the beginning of the pattern with ^,
  312. * but we will be tacking it onto the end of the current directory
  313. * in add_file, so strip that off. Also, it anchors the end with
  314. * $, but we need to match a trailing /, add it if it is not there
  315. */
  316. regex_path = stralloc(regex + 1);
  317. amfree(regex);
  318. if(regex_path[strlen(regex_path) - 2] != '/' ) {
  319. regex_path[strlen(regex_path) - 1] = '\0';
  320. strappend(regex_path, "/$");
  321. }
  322. /* convert path (assumed in cwd) to one on disk */
  323. if (strcmp(disk_path, "/") == 0)
  324. path_on_disk = stralloc2("/", regex_path);
  325. else {
  326. char *clean_disk_path = clean_regex(disk_path);
  327. path_on_disk = vstralloc(clean_disk_path, "/", regex_path, NULL);
  328. amfree(clean_disk_path);
  329. }
  330. cd_dir(path_on_disk, uqglob);
  331. amfree(regex_path);
  332. amfree(path_on_disk);
  333. amfree(uqglob);
  334. }
  335. void
  336. cd_regex(
  337. char * regex)
  338. {
  339. char *s;
  340. char *uqregex;
  341. char *path_on_disk = NULL;
  342. if (disk_name == NULL) {
  343. printf("Must select disk before changing directory\n");
  344. return;
  345. }
  346. uqregex = unquote_string(regex);
  347. if ((s = validate_regexp(uqregex)) != NULL) {
  348. printf("\"%s\" is not a valid regular expression: ", uqregex);
  349. amfree(uqregex);
  350. puts(s);
  351. return;
  352. }
  353. /* convert path (assumed in cwd) to one on disk */
  354. if (strcmp(disk_path, "/") == 0)
  355. path_on_disk = stralloc2("/", regex);
  356. else {
  357. char *clean_disk_path = clean_regex(disk_path);
  358. path_on_disk = vstralloc(clean_disk_path, "/", regex, NULL);
  359. amfree(clean_disk_path);
  360. }
  361. cd_dir(path_on_disk, uqregex);
  362. amfree(path_on_disk);
  363. amfree(uqregex);
  364. }
  365. void
  366. cd_dir(
  367. char * path_on_disk,
  368. char * default_dir)
  369. {
  370. char *path_on_disk_slash = NULL;
  371. char *dir = NULL;
  372. int nb_found;
  373. size_t i;
  374. DIR_ITEM *ditem;
  375. path_on_disk_slash = stralloc2(path_on_disk, "/");
  376. nb_found = 0;
  377. for (ditem=get_dir_list(); ditem!=NULL && nb_found <= 1;
  378. ditem=get_next_dir_item(ditem))
  379. {
  380. if (match(path_on_disk, ditem->path)
  381. || match(path_on_disk_slash, ditem->path))
  382. {
  383. i = strlen(ditem->path);
  384. if((i > 0 && ditem->path[i-1] == '/')
  385. || (i > 1 && ditem->path[i-2] == '/' && ditem->path[i-1] == '.'))
  386. { /* It is a directory */
  387. char *dir1, *dir2;
  388. nb_found++;
  389. dir = newstralloc(dir,ditem->path);
  390. if(dir[strlen(dir)-1] == '/')
  391. dir[strlen(dir)-1] = '\0'; /* remove last / */
  392. /* remove everything before the last / */
  393. dir1 = rindex(dir,'/');
  394. if (dir1) {
  395. dir1++;
  396. dir2 = stralloc(dir1);
  397. amfree(dir);
  398. dir = dir2;
  399. }
  400. }
  401. }
  402. }
  403. amfree(path_on_disk_slash);
  404. if(nb_found==0) {
  405. set_directory(default_dir);
  406. }
  407. else if(nb_found==1) {
  408. set_directory(dir);
  409. }
  410. else {
  411. printf("Too many directory\n");
  412. }
  413. amfree(dir);
  414. }
  415. void
  416. set_directory(
  417. char * dir)
  418. {
  419. char *cmd = NULL;
  420. char *new_dir = NULL;
  421. char *qnew_dir;
  422. char *dp, *de;
  423. char *ldir = NULL;
  424. /* do nothing if "." */
  425. if(strcmp(dir,".")==0) {
  426. show_directory(); /* say where we are */
  427. return;
  428. /*NOTREACHED*/
  429. }
  430. if (disk_name == NULL) {
  431. printf("Must select disk before setting directory\n");
  432. return;
  433. /*NOTREACHED*/
  434. }
  435. ldir = stralloc(dir);
  436. clean_pathname(ldir);
  437. /* convert directory into absolute path relative to disk mount point */
  438. if (ldir[0] == '/')
  439. {
  440. /* absolute path specified, must start with mount point */
  441. if (strcmp(mount_point, "/") == 0)
  442. {
  443. new_dir = stralloc(ldir);
  444. }
  445. else
  446. {
  447. if (strncmp(mount_point, ldir, strlen(mount_point)) != 0)
  448. {
  449. printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
  450. mount_point);
  451. amfree(ldir);
  452. return;
  453. /*NOTREACHED*/
  454. }
  455. new_dir = stralloc(ldir+strlen(mount_point));
  456. if (strlen(new_dir) == 0) {
  457. new_dir = newstralloc(new_dir, "/");
  458. /* i.e. ldir == mount_point */
  459. }
  460. }
  461. }
  462. else
  463. {
  464. new_dir = stralloc(disk_path);
  465. dp = ldir;
  466. /* strip any leading ..s */
  467. while (strncmp(dp, "../", 3) == 0)
  468. {
  469. de = strrchr(new_dir, '/'); /* always at least 1 */
  470. if (de == new_dir)
  471. {
  472. /* at top of disk */
  473. *(de + 1) = '\0';
  474. dp = dp + 3;
  475. }
  476. else
  477. {
  478. *de = '\0';
  479. dp = dp + 3;
  480. }
  481. }
  482. if (strcmp(dp, "..") == 0) {
  483. if (strcmp(new_dir, "/") == 0) {
  484. /* at top of disk */
  485. printf("Invalid directory - Can't cd outside mount point \"%s\"\n",
  486. mount_point);
  487. /*@ignore@*/
  488. amfree(new_dir);
  489. /*@end@*/
  490. amfree(ldir);
  491. return;
  492. /*NOTREACHED*/
  493. }
  494. de = strrchr(new_dir, '/'); /* always at least 1 */
  495. if (de == new_dir)
  496. {
  497. /* at top of disk */
  498. *(de+1) = '\0';
  499. }
  500. else
  501. {
  502. *de = '\0';
  503. }
  504. } else {
  505. /*@ignore@*/
  506. if (strcmp(new_dir, "/") != 0) {
  507. strappend(new_dir, "/");
  508. }
  509. strappend(new_dir, ldir);
  510. /*@end@*/
  511. }
  512. }
  513. qnew_dir = quote_string(new_dir);
  514. cmd = stralloc2("OISD ", qnew_dir);
  515. amfree(qnew_dir);
  516. if (exchange(cmd) == -1) {
  517. exit(1);
  518. /*NOTREACHED*/
  519. }
  520. amfree(cmd);
  521. if (server_happy())
  522. {
  523. disk_path = newstralloc(disk_path, new_dir);
  524. suck_dir_list_from_server(); /* get list of directory contents */
  525. show_directory(); /* say where we moved to */
  526. }
  527. else
  528. {
  529. printf("Invalid directory - %s\n", dir);
  530. }
  531. /*@ignore@*/
  532. amfree(new_dir);
  533. amfree(ldir);
  534. /*@end@*/
  535. }
  536. /* prints the current working directory */
  537. void
  538. show_directory(void)
  539. {
  540. if (mount_point == NULL || disk_path == NULL)
  541. printf("Must select disk first\n");
  542. else if (strcmp(mount_point, "/") == 0)
  543. printf("%s\n", disk_path);
  544. else if (strcmp(disk_path, "/") == 0)
  545. printf("%s\n", mount_point);
  546. else
  547. printf("%s%s\n", mount_point, disk_path);
  548. }
  549. /* set the tape server and device */
  550. void
  551. set_tape(
  552. char * tape)
  553. {
  554. char *uqtape = unquote_string(tape);
  555. char *tapedev = strchr(uqtape, ':');
  556. if (tapedev)
  557. {
  558. if (tapedev != uqtape) {
  559. if((strchr(tapedev+1, ':') == NULL) &&
  560. (strncmp(uqtape, "null:", 5) == 0 ||
  561. strncmp(uqtape, "rait:", 5) == 0 ||
  562. strncmp(uqtape, "file:", 5) == 0 ||
  563. strncmp(uqtape, "tape:", 5) == 0)) {
  564. tapedev = uqtape;
  565. }
  566. else {
  567. *tapedev = '\0';
  568. tape_server_name = newstralloc(tape_server_name, uqtape);
  569. ++tapedev;
  570. }
  571. } else { /* reset server_name if start with : */
  572. amfree(tape_server_name);
  573. ++tapedev;
  574. }
  575. } else
  576. tapedev = uqtape;
  577. if (tapedev[0])
  578. {
  579. if (strcmp(tapedev, "default") == 0)
  580. amfree(tape_device_name);
  581. else
  582. tape_device_name = newstralloc(tape_device_name, tapedev);
  583. }
  584. if (tape_device_name)
  585. printf ("Using tape \"%s\"", tape_device_name);
  586. else
  587. printf ("Using default tape");
  588. if (tape_server_name)
  589. printf (" from server %s.\n", tape_server_name);
  590. else
  591. printf (".\nTape server unspecified, assumed to be %s.\n",
  592. server_name);
  593. }
  594. void
  595. set_mode(
  596. int mode)
  597. {
  598. #ifdef SAMBA_CLIENT
  599. if (mode == SAMBA_SMBCLIENT) {
  600. printf ("SAMBA dumps will be extracted using smbclient\n");
  601. samba_extract_method = SAMBA_SMBCLIENT;
  602. } else {
  603. if (mode == SAMBA_TAR) {
  604. printf ("SAMBA dumps will be extracted as TAR dumps\n");
  605. samba_extract_method = SAMBA_TAR;
  606. }
  607. }
  608. #else
  609. (void)mode; /* Quiet unused parameter warning */
  610. #endif /* SAMBA_CLIENT */
  611. }
  612. void
  613. show_mode(void)
  614. {
  615. #ifdef SAMBA_CLIENT
  616. printf ("SAMBA dumps are extracted ");
  617. if (samba_extract_method == SAMBA_TAR) {
  618. printf (" as TAR dumps\n");
  619. } else {
  620. printf ("using smbclient\n");
  621. }
  622. #endif /* SAMBA_CLIENT */
  623. }