help.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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,
  116. P_("%d expected program not found in PATH or not executable.\n"
  117. "NB: root's PATH should usually contain "
  118. "/usr/local/sbin, /usr/sbin and /sbin.",
  119. "%d expected programs not found in PATH or not executable.\n"
  120. "NB: root's PATH should usually contain "
  121. "/usr/local/sbin, /usr/sbin and /sbin.", warned), warned);
  122. }
  123. bool
  124. ignore_depends(struct pkginfo *pkg)
  125. {
  126. struct pkg_list *id;
  127. for (id= ignoredependss; id; id= id->next)
  128. if (id->pkg == pkg)
  129. return true;
  130. return false;
  131. }
  132. bool
  133. force_depends(struct deppossi *possi)
  134. {
  135. return fc_depends ||
  136. ignore_depends(possi->ed) ||
  137. ignore_depends(possi->up->up);
  138. }
  139. bool
  140. force_breaks(struct deppossi *possi)
  141. {
  142. return fc_breaks ||
  143. ignore_depends(possi->ed) ||
  144. ignore_depends(possi->up->up);
  145. }
  146. bool
  147. force_conflicts(struct deppossi *possi)
  148. {
  149. return fc_conflicts;
  150. }
  151. static const char *
  152. preexecscript(struct command *cmd)
  153. {
  154. /* returns the path to the script inside the chroot
  155. * FIXME: none of the stuff here will work if admindir isn't inside
  156. * instdir as expected.
  157. */
  158. size_t instdirl;
  159. if (*instdir) {
  160. if (chroot(instdir)) ohshite(_("failed to chroot to `%.250s'"),instdir);
  161. if (chdir("/"))
  162. ohshite(_("failed to chdir to `%.255s'"), "/");
  163. }
  164. if (f_debug & dbg_scripts) {
  165. struct varbuf args = VARBUF_INIT;
  166. const char **argv = cmd->argv;
  167. while (*++argv) {
  168. varbufaddc(&args, ' ');
  169. varbufaddstr(&args, *argv);
  170. }
  171. varbufaddc(&args, '\0');
  172. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename, args.buf);
  173. varbuf_destroy(&args);
  174. }
  175. instdirl= strlen(instdir);
  176. if (!instdirl)
  177. return cmd->filename;
  178. assert(strlen(cmd->filename) >= instdirl);
  179. return cmd->filename + instdirl;
  180. }
  181. void
  182. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  183. {
  184. pkg->trigpend_head = NULL;
  185. pkg->status = pkg->trigaw.head ? stat_triggersawaited : new_status;
  186. post_postinst_tasks_core(pkg);
  187. }
  188. void
  189. post_postinst_tasks_core(struct pkginfo *pkg)
  190. {
  191. modstatdb_note(pkg);
  192. if (!f_noact) {
  193. debug(dbg_triggersdetail, "post_postinst_tasks_core - trig_incorporate");
  194. trig_incorporate(msdbrw_write, admindir);
  195. }
  196. }
  197. static void
  198. post_script_tasks(void)
  199. {
  200. ensure_diversions();
  201. debug(dbg_triggersdetail,
  202. "post_script_tasks - ensure_diversions; trig_incorporate");
  203. trig_incorporate(msdbrw_write, admindir);
  204. }
  205. static void
  206. cu_post_script_tasks(int argc, void **argv)
  207. {
  208. post_script_tasks();
  209. }
  210. static void setexecute(const char *path, struct stat *stab) {
  211. if ((stab->st_mode & 0555) == 0555) return;
  212. if (!chmod(path,0755)) return;
  213. ohshite(_("unable to set execute permissions on `%.250s'"),path);
  214. }
  215. static int
  216. do_script(struct pkginfo *pkg, struct pkginfoperfile *pif,
  217. struct command *cmd, struct stat *stab, int warn)
  218. {
  219. int c1, r;
  220. setexecute(cmd->filename, stab);
  221. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  222. c1 = subproc_fork();
  223. if (!c1) {
  224. if (setenv(MAINTSCRIPTPKGENVVAR, pkg->name, 1) ||
  225. setenv(MAINTSCRIPTARCHENVVAR, pif->architecture, 1) ||
  226. setenv(MAINTSCRIPTNAMEENVVAR, cmd->argv[0], 1) ||
  227. setenv(MAINTSCRIPTDPKGENVVAR, PACKAGE_VERSION, 1))
  228. ohshite(_("unable to setenv for maintainer script"));
  229. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  230. command_exec(cmd);
  231. }
  232. subproc_signals_setup(cmd->name); /* This does a push_cleanup() */
  233. r = subproc_wait_check(c1, cmd->name, warn);
  234. pop_cleanup(ehflag_normaltidy);
  235. pop_cleanup(ehflag_normaltidy);
  236. return r;
  237. }
  238. static int
  239. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  240. const char *desc, va_list args)
  241. {
  242. struct command cmd;
  243. const char *scriptpath;
  244. struct stat stab;
  245. char buf[100];
  246. scriptpath= pkgadminfile(pkg,scriptname);
  247. sprintf(buf, _("installed %s script"), desc);
  248. command_init(&cmd, scriptpath, buf);
  249. command_add_arg(&cmd, scriptname);
  250. command_add_argv(&cmd, args);
  251. if (stat(scriptpath,&stab)) {
  252. command_destroy(&cmd);
  253. if (errno == ENOENT) {
  254. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  255. scriptname);
  256. return 0;
  257. }
  258. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  259. }
  260. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  261. command_destroy(&cmd);
  262. return 1;
  263. }
  264. /* All ...'s are const char*'s. */
  265. int
  266. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  267. const char *desc, ...)
  268. {
  269. int r;
  270. va_list args;
  271. va_start(args, desc);
  272. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  273. va_end(args);
  274. if (r)
  275. post_script_tasks();
  276. return r;
  277. }
  278. int
  279. maintainer_script_postinst(struct pkginfo *pkg, ...)
  280. {
  281. int r;
  282. va_list args;
  283. va_start(args, pkg);
  284. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", args);
  285. va_end(args);
  286. if (r)
  287. ensure_diversions();
  288. return r;
  289. }
  290. int
  291. maintainer_script_new(struct pkginfo *pkg,
  292. const char *scriptname, const char *desc,
  293. const char *cidir, char *cidirrest, ...)
  294. {
  295. struct command cmd;
  296. struct stat stab;
  297. va_list args;
  298. char buf[100];
  299. strcpy(cidirrest, scriptname);
  300. sprintf(buf, _("new %s script"), desc);
  301. va_start(args, cidirrest);
  302. command_init(&cmd, cidir, buf);
  303. command_add_arg(&cmd, scriptname);
  304. command_add_argv(&cmd, args);
  305. va_end(args);
  306. if (stat(cidir,&stab)) {
  307. command_destroy(&cmd);
  308. if (errno == ENOENT) {
  309. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  310. return 0;
  311. }
  312. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  313. }
  314. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  315. command_destroy(&cmd);
  316. post_script_tasks();
  317. return 1;
  318. }
  319. int maintainer_script_alternative(struct pkginfo *pkg,
  320. const char *scriptname, const char *desc,
  321. const char *cidir, char *cidirrest,
  322. const char *ifok, const char *iffallback) {
  323. struct command cmd;
  324. const char *oldscriptpath;
  325. struct stat stab;
  326. char buf[100];
  327. oldscriptpath= pkgadminfile(pkg,scriptname);
  328. sprintf(buf, _("old %s script"), desc);
  329. command_init(&cmd, oldscriptpath, buf);
  330. command_add_args(&cmd, scriptname, ifok,
  331. versiondescribe(&pkg->available.version, vdew_nonambig),
  332. NULL);
  333. if (stat(oldscriptpath,&stab)) {
  334. if (errno == ENOENT) {
  335. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  336. scriptname,oldscriptpath);
  337. command_destroy(&cmd);
  338. return 0;
  339. }
  340. warning(_("unable to stat %s '%.250s': %s"),
  341. cmd.name, oldscriptpath, strerror(errno));
  342. } else {
  343. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  344. command_destroy(&cmd);
  345. post_script_tasks();
  346. return 1;
  347. }
  348. }
  349. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  350. strcpy(cidirrest,scriptname);
  351. sprintf(buf, _("new %s script"), desc);
  352. command_destroy(&cmd);
  353. command_init(&cmd, cidir, buf);
  354. command_add_args(&cmd, scriptname, iffallback,
  355. versiondescribe(&pkg->installed.version, vdew_nonambig),
  356. NULL);
  357. if (stat(cidir,&stab)) {
  358. command_destroy(&cmd);
  359. if (errno == ENOENT)
  360. ohshit(_("there is no script in the new version of the package - giving up"));
  361. else
  362. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  363. }
  364. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  365. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  366. command_destroy(&cmd);
  367. post_script_tasks();
  368. return 1;
  369. }
  370. void clear_istobes(void) {
  371. struct pkgiterator *it;
  372. struct pkginfo *pkg;
  373. it= iterpkgstart();
  374. while ((pkg = iterpkgnext(it)) != NULL) {
  375. ensure_package_clientdata(pkg);
  376. pkg->clientdata->istobe= itb_normal;
  377. pkg->clientdata->replacingfilesandsaid= 0;
  378. }
  379. iterpkgend(it);
  380. }
  381. void debug(int which, const char *fmt, ...) {
  382. va_list args;
  383. if (!(f_debug & which)) return;
  384. fprintf(stderr,"D0%05o: ",which);
  385. va_start(args, fmt);
  386. vfprintf(stderr, fmt, args);
  387. va_end(args);
  388. putc('\n',stderr);
  389. }
  390. /*
  391. * Returns true if the directory contains conffiles belonging to pkg,
  392. * false otherwise.
  393. */
  394. bool
  395. hasdirectoryconffiles(struct filenamenode *file, struct pkginfo *pkg)
  396. {
  397. struct conffile *conff;
  398. size_t namelen;
  399. debug(dbg_veryverbose, "hasdirectoryconffiles `%s' (from %s)", file->name,
  400. pkg->name);
  401. namelen = strlen(file->name);
  402. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  403. if (!strncmp(file->name,conff->name,namelen)) {
  404. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  405. file->name, conff->name, pkg->name);
  406. return true;
  407. }
  408. }
  409. debug(dbg_veryverbose, "hasdirectoryconffiles no");
  410. return false;
  411. }
  412. /*
  413. * Returns true if the file is used by packages other than pkg,
  414. * false otherwise.
  415. */
  416. bool
  417. isdirectoryinuse(struct filenamenode *file, struct pkginfo *pkg)
  418. {
  419. struct filepackages_iterator *iter;
  420. struct pkginfo *other_pkg;
  421. debug(dbg_veryverbose, "isdirectoryinuse `%s' (except %s)", file->name,
  422. pkg ? pkg->name : "<none>");
  423. iter = filepackages_iter_new(file);
  424. while ((other_pkg = filepackages_iter_next(iter))) {
  425. debug(dbg_veryverbose, "isdirectoryinuse considering %s ...",
  426. other_pkg->name);
  427. if (other_pkg == pkg)
  428. continue;
  429. return true;
  430. }
  431. filepackages_iter_free(iter);
  432. debug(dbg_veryverbose, "isdirectoryinuse no");
  433. return false;
  434. }
  435. void oldconffsetflags(const struct conffile *searchconff) {
  436. struct filenamenode *namenode;
  437. while (searchconff) {
  438. namenode= findnamenode(searchconff->name, 0); /* XXX */
  439. namenode->flags |= fnnf_old_conff;
  440. if (!namenode->oldhash)
  441. namenode->oldhash= searchconff->hash;
  442. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  443. searchconff->name, namenode, namenode->flags);
  444. searchconff= searchconff->next;
  445. }
  446. }
  447. /*
  448. * If the pathname to remove is:
  449. *
  450. * 1. a sticky or set-id file, or
  451. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  452. *
  453. * we change its mode so that a malicious user cannot use it, even if it's
  454. * linked to another file.
  455. */
  456. int
  457. secure_unlink(const char *pathname)
  458. {
  459. struct stat stab;
  460. if (lstat(pathname,&stab)) return -1;
  461. return secure_unlink_statted(pathname, &stab);
  462. }
  463. int
  464. secure_unlink_statted(const char *pathname, const struct stat *stab)
  465. {
  466. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  467. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  468. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  469. if (chmod(pathname, 0600))
  470. return -1;
  471. }
  472. if (unlink(pathname)) return -1;
  473. return 0;
  474. }
  475. void ensure_pathname_nonexisting(const char *pathname) {
  476. int c1;
  477. const char *u;
  478. u = path_skip_slash_dotslash(pathname);
  479. assert(*u);
  480. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  481. if (!rmdir(pathname)) return; /* Deleted it OK, it was a directory. */
  482. if (errno == ENOENT || errno == ELOOP) return;
  483. if (errno == ENOTDIR) {
  484. /* Either it's a file, or one of the path components is. If one
  485. * of the path components is this will fail again ...
  486. */
  487. if (secure_unlink(pathname) == 0)
  488. return; /* OK, it was */
  489. if (errno == ENOTDIR) return;
  490. }
  491. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh ? */
  492. ohshite(_("unable to securely remove '%.255s'"), pathname);
  493. }
  494. c1 = subproc_fork();
  495. if (!c1) {
  496. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  497. ohshite(_("failed to exec rm for cleanup"));
  498. }
  499. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  500. subproc_wait_check(c1, "rm cleanup", 0);
  501. }
  502. void log_action(const char *action, struct pkginfo *pkg) {
  503. log_message("%s %s %s %s", action, pkg->name,
  504. versiondescribe(&pkg->installed.version, vdew_nonambig),
  505. versiondescribe(&pkg->available.version, vdew_nonambig));
  506. statusfd_send("processing: %s: %s", action, pkg->name);
  507. }