help.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * dpkg - main program for package management
  3. * help.c - various helper routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <compat.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/wait.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <time.h>
  29. #include <dirent.h>
  30. #include <unistd.h>
  31. #include <stdlib.h>
  32. #include <dpkg/i18n.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/path.h>
  36. #include <dpkg/subproc.h>
  37. #include <dpkg/command.h>
  38. #include <dpkg/triglib.h>
  39. #include "filesdb.h"
  40. #include "main.h"
  41. const char *const statusstrings[]= {
  42. [stat_notinstalled] = N_("not installed"),
  43. [stat_configfiles] = N_("not installed but configs remain"),
  44. [stat_halfinstalled] = N_("broken due to failed removal or installation"),
  45. [stat_unpacked] = N_("unpacked but not configured"),
  46. [stat_halfconfigured] = N_("broken due to postinst failure"),
  47. [stat_triggersawaited] = N_("awaiting trigger processing by another package"),
  48. [stat_triggerspending] = N_("triggered"),
  49. [stat_installed] = N_("installed")
  50. };
  51. struct filenamenode *namenodetouse(struct filenamenode *namenode, struct pkginfo *pkg) {
  52. struct filenamenode *r;
  53. if (!namenode->divert) {
  54. r = namenode;
  55. return r;
  56. }
  57. debug(dbg_eachfile,"namenodetouse namenode=`%s' pkg=%s",
  58. namenode->name,pkg->name);
  59. r=
  60. (namenode->divert->useinstead && namenode->divert->pkg != pkg)
  61. ? namenode->divert->useinstead : namenode;
  62. debug(dbg_eachfile,
  63. "namenodetouse ... useinstead=%s camefrom=%s pkg=%s return %s",
  64. namenode->divert->useinstead ? namenode->divert->useinstead->name : "<none>",
  65. namenode->divert->camefrom ? namenode->divert->camefrom->name : "<none>",
  66. namenode->divert->pkg ? namenode->divert->pkg->name : "<none>",
  67. r->name);
  68. return r;
  69. }
  70. void checkpath(void) {
  71. /* Verify that some programs can be found in the PATH. */
  72. static const char *const prog_list[] = {
  73. DEFAULTSHELL,
  74. RM,
  75. TAR,
  76. FIND,
  77. BACKEND,
  78. "ldconfig",
  79. #if WITH_START_STOP_DAEMON
  80. "start-stop-daemon",
  81. #endif
  82. "update-rc.d",
  83. NULL
  84. };
  85. const char *const *prog;
  86. const char *path_list;
  87. struct varbuf filename = VARBUF_INIT;
  88. int warned= 0;
  89. path_list = getenv("PATH");
  90. if (!path_list)
  91. ohshit(_("error: PATH is not set."));
  92. for (prog = prog_list; *prog; prog++) {
  93. struct stat stab;
  94. const char *path, *path_end;
  95. size_t path_len;
  96. for (path = path_list; path; path = path_end ? path_end + 1 : NULL) {
  97. path_end = strchr(path, ':');
  98. path_len = path_end ? (size_t)(path_end - path) : strlen(path);
  99. varbufreset(&filename);
  100. varbufaddbuf(&filename, path, path_len);
  101. if (path_len)
  102. varbufaddc(&filename, '/');
  103. varbufaddstr(&filename, *prog);
  104. varbufaddc(&filename, '\0');
  105. if (stat(filename.buf, &stab) == 0 && (stab.st_mode & 0111))
  106. break;
  107. }
  108. if (!path) {
  109. warning(_("'%s' not found in PATH or not executable."), *prog);
  110. warned++;
  111. }
  112. }
  113. varbuf_destroy(&filename);
  114. if (warned)
  115. forcibleerr(fc_badpath, _("%d expected program(s) not found in PATH "
  116. "or not executable.\nNB: root's PATH should usually contain "
  117. "/usr/local/sbin, /usr/sbin and /sbin."),
  118. warned);
  119. }
  120. bool
  121. ignore_depends(struct pkginfo *pkg)
  122. {
  123. struct pkg_list *id;
  124. for (id= ignoredependss; id; id= id->next)
  125. if (id->pkg == pkg)
  126. return true;
  127. return false;
  128. }
  129. bool
  130. force_depends(struct deppossi *possi)
  131. {
  132. return fc_depends ||
  133. ignore_depends(possi->ed) ||
  134. ignore_depends(possi->up->up);
  135. }
  136. bool
  137. force_breaks(struct deppossi *possi)
  138. {
  139. return fc_breaks ||
  140. ignore_depends(possi->ed) ||
  141. ignore_depends(possi->up->up);
  142. }
  143. bool
  144. force_conflicts(struct deppossi *possi)
  145. {
  146. return fc_conflicts;
  147. }
  148. static const char *
  149. preexecscript(struct command *cmd)
  150. {
  151. /* returns the path to the script inside the chroot
  152. * FIXME: none of the stuff here will work if admindir isn't inside
  153. * instdir as expected.
  154. */
  155. size_t instdirl;
  156. if (*instdir) {
  157. if (chroot(instdir)) ohshite(_("failed to chroot to `%.250s'"),instdir);
  158. if (chdir("/"))
  159. ohshite(_("failed to chdir to `%.255s'"), "/");
  160. }
  161. if (f_debug & dbg_scripts) {
  162. struct varbuf args = VARBUF_INIT;
  163. const char **argv = cmd->argv;
  164. while (*++argv) {
  165. varbufaddc(&args, ' ');
  166. varbufaddstr(&args, *argv);
  167. }
  168. varbufaddc(&args, '\0');
  169. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename, args.buf);
  170. varbuf_destroy(&args);
  171. }
  172. instdirl= strlen(instdir);
  173. if (!instdirl)
  174. return cmd->filename;
  175. assert(strlen(cmd->filename) >= instdirl);
  176. return cmd->filename + instdirl;
  177. }
  178. void
  179. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  180. {
  181. pkg->trigpend_head = NULL;
  182. pkg->status = pkg->trigaw.head ? stat_triggersawaited : new_status;
  183. post_postinst_tasks_core(pkg);
  184. }
  185. void
  186. post_postinst_tasks_core(struct pkginfo *pkg)
  187. {
  188. modstatdb_note(pkg);
  189. if (!f_noact) {
  190. debug(dbg_triggersdetail, "post_postinst_tasks_core - trig_incorporate");
  191. trig_incorporate(msdbrw_write, admindir);
  192. }
  193. }
  194. static void
  195. post_script_tasks(void)
  196. {
  197. ensure_diversions();
  198. debug(dbg_triggersdetail,
  199. "post_script_tasks - ensure_diversions; trig_incorporate");
  200. trig_incorporate(msdbrw_write, admindir);
  201. }
  202. static void
  203. cu_post_script_tasks(int argc, void **argv)
  204. {
  205. post_script_tasks();
  206. }
  207. static void setexecute(const char *path, struct stat *stab) {
  208. if ((stab->st_mode & 0555) == 0555) return;
  209. if (!chmod(path,0755)) return;
  210. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  211. }
  212. static int
  213. do_script(struct pkginfo *pkg, struct pkginfoperfile *pif,
  214. struct command *cmd, struct stat *stab, int warn)
  215. {
  216. int c1, r;
  217. setexecute(cmd->filename, stab);
  218. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  219. c1 = subproc_fork();
  220. if (!c1) {
  221. if (setenv(MAINTSCRIPTPKGENVVAR, pkg->name, 1) ||
  222. setenv(MAINTSCRIPTARCHENVVAR, pif->architecture, 1) ||
  223. setenv(MAINTSCRIPTNAMEENVVAR, cmd->argv[0], 1) ||
  224. setenv(MAINTSCRIPTDPKGENVVAR, PACKAGE_VERSION, 1))
  225. ohshite(_("unable to setenv for maintainer script"));
  226. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  227. command_exec(cmd);
  228. }
  229. subproc_signals_setup(cmd->name); /* This does a push_cleanup() */
  230. r = subproc_wait_check(c1, cmd->name, warn);
  231. pop_cleanup(ehflag_normaltidy);
  232. pop_cleanup(ehflag_normaltidy);
  233. return r;
  234. }
  235. static int
  236. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  237. const char *desc, va_list args)
  238. {
  239. struct command cmd;
  240. const char *scriptpath;
  241. struct stat stab;
  242. char buf[100];
  243. scriptpath= pkgadminfile(pkg,scriptname);
  244. sprintf(buf, _("installed %s script"), desc);
  245. command_init(&cmd, scriptpath, buf);
  246. command_add_arg(&cmd, scriptname);
  247. command_add_argv(&cmd, args);
  248. if (stat(scriptpath,&stab)) {
  249. command_destroy(&cmd);
  250. if (errno == ENOENT) {
  251. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  252. scriptname);
  253. return 0;
  254. }
  255. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  256. }
  257. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  258. command_destroy(&cmd);
  259. return 1;
  260. }
  261. /* All ...'s are const char*'s. */
  262. int
  263. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  264. const char *desc, ...)
  265. {
  266. int r;
  267. va_list args;
  268. va_start(args, desc);
  269. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  270. va_end(args);
  271. if (r)
  272. post_script_tasks();
  273. return r;
  274. }
  275. int
  276. maintainer_script_postinst(struct pkginfo *pkg, ...)
  277. {
  278. int r;
  279. va_list args;
  280. va_start(args, pkg);
  281. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", args);
  282. va_end(args);
  283. if (r)
  284. ensure_diversions();
  285. return r;
  286. }
  287. int
  288. maintainer_script_new(struct pkginfo *pkg,
  289. const char *scriptname, const char *desc,
  290. const char *cidir, char *cidirrest, ...)
  291. {
  292. struct command cmd;
  293. struct stat stab;
  294. va_list args;
  295. char buf[100];
  296. strcpy(cidirrest, scriptname);
  297. sprintf(buf, _("new %s script"), desc);
  298. va_start(args, cidirrest);
  299. command_init(&cmd, cidir, buf);
  300. command_add_arg(&cmd, scriptname);
  301. command_add_argv(&cmd, args);
  302. va_end(args);
  303. if (stat(cidir,&stab)) {
  304. command_destroy(&cmd);
  305. if (errno == ENOENT) {
  306. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  307. return 0;
  308. }
  309. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  310. }
  311. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  312. command_destroy(&cmd);
  313. post_script_tasks();
  314. return 1;
  315. }
  316. int maintainer_script_alternative(struct pkginfo *pkg,
  317. const char *scriptname, const char *desc,
  318. const char *cidir, char *cidirrest,
  319. const char *ifok, const char *iffallback) {
  320. struct command cmd;
  321. const char *oldscriptpath;
  322. struct stat stab;
  323. char buf[100];
  324. oldscriptpath= pkgadminfile(pkg,scriptname);
  325. sprintf(buf, _("old %s script"), desc);
  326. command_init(&cmd, oldscriptpath, buf);
  327. command_add_args(&cmd, scriptname, ifok,
  328. versiondescribe(&pkg->available.version, vdew_nonambig),
  329. NULL);
  330. if (stat(oldscriptpath,&stab)) {
  331. if (errno == ENOENT) {
  332. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  333. scriptname,oldscriptpath);
  334. command_destroy(&cmd);
  335. return 0;
  336. }
  337. warning(_("unable to stat %s '%.250s': %s"),
  338. cmd.name, oldscriptpath, strerror(errno));
  339. } else {
  340. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  341. command_destroy(&cmd);
  342. post_script_tasks();
  343. return 1;
  344. }
  345. }
  346. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  347. strcpy(cidirrest,scriptname);
  348. sprintf(buf, _("new %s script"), desc);
  349. command_destroy(&cmd);
  350. command_init(&cmd, cidir, buf);
  351. command_add_args(&cmd, scriptname, iffallback,
  352. versiondescribe(&pkg->installed.version, vdew_nonambig),
  353. NULL);
  354. if (stat(cidir,&stab)) {
  355. command_destroy(&cmd);
  356. if (errno == ENOENT)
  357. ohshit(_("there is no script in the new version of the package - giving up"));
  358. else
  359. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  360. }
  361. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  362. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  363. command_destroy(&cmd);
  364. post_script_tasks();
  365. return 1;
  366. }
  367. void clear_istobes(void) {
  368. struct pkgiterator *it;
  369. struct pkginfo *pkg;
  370. it= iterpkgstart();
  371. while ((pkg = iterpkgnext(it)) != NULL) {
  372. ensure_package_clientdata(pkg);
  373. pkg->clientdata->istobe= itb_normal;
  374. pkg->clientdata->replacingfilesandsaid= 0;
  375. }
  376. iterpkgend(it);
  377. }
  378. void debug(int which, const char *fmt, ...) {
  379. va_list args;
  380. if (!(f_debug & which)) return;
  381. fprintf(stderr,"D0%05o: ",which);
  382. va_start(args, fmt);
  383. vfprintf(stderr, fmt, args);
  384. va_end(args);
  385. putc('\n',stderr);
  386. }
  387. /*
  388. * Returns true if the directory contains conffiles belonging to pkg,
  389. * false otherwise.
  390. */
  391. bool
  392. hasdirectoryconffiles(struct filenamenode *file, struct pkginfo *pkg)
  393. {
  394. struct conffile *conff;
  395. size_t namelen;
  396. debug(dbg_veryverbose, "hasdirectoryconffiles `%s' (from %s)", file->name,
  397. pkg->name);
  398. namelen = strlen(file->name);
  399. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  400. if (!strncmp(file->name,conff->name,namelen)) {
  401. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  402. file->name, conff->name, pkg->name);
  403. return true;
  404. }
  405. }
  406. debug(dbg_veryverbose, "hasdirectoryconffiles no");
  407. return false;
  408. }
  409. /*
  410. * Returns true if the file is used by packages other than pkg,
  411. * false otherwise.
  412. */
  413. bool
  414. isdirectoryinuse(struct filenamenode *file, struct pkginfo *pkg)
  415. {
  416. struct filepackages_iterator *iter;
  417. struct pkginfo *other_pkg;
  418. debug(dbg_veryverbose, "isdirectoryinuse `%s' (except %s)", file->name,
  419. pkg ? pkg->name : "<none>");
  420. iter = filepackages_iter_new(file);
  421. while ((other_pkg = filepackages_iter_next(iter))) {
  422. debug(dbg_veryverbose, "isdirectoryinuse considering %s ...",
  423. other_pkg->name);
  424. if (other_pkg == pkg)
  425. continue;
  426. return true;
  427. }
  428. filepackages_iter_free(iter);
  429. debug(dbg_veryverbose, "isdirectoryinuse no");
  430. return false;
  431. }
  432. void oldconffsetflags(const struct conffile *searchconff) {
  433. struct filenamenode *namenode;
  434. while (searchconff) {
  435. namenode= findnamenode(searchconff->name, 0); /* XXX */
  436. namenode->flags |= fnnf_old_conff;
  437. if (!namenode->oldhash)
  438. namenode->oldhash= searchconff->hash;
  439. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  440. searchconff->name, namenode, namenode->flags);
  441. searchconff= searchconff->next;
  442. }
  443. }
  444. /*
  445. * If the pathname to remove is:
  446. *
  447. * 1. a sticky or set-id file, or
  448. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  449. *
  450. * we change its mode so that a malicious user cannot use it, even if it's
  451. * linked to another file.
  452. */
  453. int
  454. secure_unlink(const char *pathname)
  455. {
  456. struct stat stab;
  457. if (lstat(pathname,&stab)) return -1;
  458. return secure_unlink_statted(pathname, &stab);
  459. }
  460. int
  461. secure_unlink_statted(const char *pathname, const struct stat *stab)
  462. {
  463. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  464. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  465. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  466. if (chmod(pathname, 0600))
  467. return -1;
  468. }
  469. if (unlink(pathname)) return -1;
  470. return 0;
  471. }
  472. void ensure_pathname_nonexisting(const char *pathname) {
  473. int c1;
  474. const char *u;
  475. u = path_skip_slash_dotslash(pathname);
  476. assert(*u);
  477. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  478. if (!rmdir(pathname)) return; /* Deleted it OK, it was a directory. */
  479. if (errno == ENOENT || errno == ELOOP) return;
  480. if (errno == ENOTDIR) {
  481. /* Either it's a file, or one of the path components is. If one
  482. * of the path components is this will fail again ...
  483. */
  484. if (secure_unlink(pathname) == 0)
  485. return; /* OK, it was */
  486. if (errno == ENOTDIR) return;
  487. }
  488. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh ? */
  489. ohshite(_("unable to securely remove '%.255s'"), pathname);
  490. }
  491. c1 = subproc_fork();
  492. if (!c1) {
  493. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  494. ohshite(_("failed to exec rm for cleanup"));
  495. }
  496. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  497. subproc_wait_check(c1, "rm cleanup", 0);
  498. }
  499. void log_action(const char *action, struct pkginfo *pkg) {
  500. log_message("%s %s %s %s", action, pkg->name,
  501. versiondescribe(&pkg->installed.version, vdew_nonambig),
  502. versiondescribe(&pkg->available.version, vdew_nonambig));
  503. statusfd_send("processing: %s: %s", action, pkg->name);
  504. }