help.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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(MAINTSCRIPTLIBDIRENVVAR, PKGLIBDIR, 1) ||
  225. setenv(MAINTSCRIPTDPKGENVVAR, PACKAGE_VERSION, 1))
  226. ohshite(_("unable to setenv for maintainer script"));
  227. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  228. command_exec(cmd);
  229. }
  230. subproc_signals_setup(cmd->name); /* This does a push_cleanup() */
  231. r = subproc_wait_check(c1, cmd->name, warn);
  232. pop_cleanup(ehflag_normaltidy);
  233. pop_cleanup(ehflag_normaltidy);
  234. return r;
  235. }
  236. static int
  237. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  238. const char *desc, va_list args)
  239. {
  240. struct command cmd;
  241. const char *scriptpath;
  242. struct stat stab;
  243. char buf[100];
  244. scriptpath= pkgadminfile(pkg,scriptname);
  245. sprintf(buf, _("installed %s script"), desc);
  246. command_init(&cmd, scriptpath, buf);
  247. command_add_arg(&cmd, scriptname);
  248. command_add_argv(&cmd, args);
  249. if (stat(scriptpath,&stab)) {
  250. command_destroy(&cmd);
  251. if (errno == ENOENT) {
  252. debug(dbg_scripts, "vmaintainer_script_installed nonexistent %s",
  253. scriptname);
  254. return 0;
  255. }
  256. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  257. }
  258. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  259. command_destroy(&cmd);
  260. return 1;
  261. }
  262. /* All ...'s are const char*'s. */
  263. int
  264. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  265. const char *desc, ...)
  266. {
  267. int r;
  268. va_list args;
  269. va_start(args, desc);
  270. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  271. va_end(args);
  272. if (r)
  273. post_script_tasks();
  274. return r;
  275. }
  276. int
  277. maintainer_script_postinst(struct pkginfo *pkg, ...)
  278. {
  279. int r;
  280. va_list args;
  281. va_start(args, pkg);
  282. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation", args);
  283. va_end(args);
  284. if (r)
  285. ensure_diversions();
  286. return r;
  287. }
  288. int
  289. maintainer_script_new(struct pkginfo *pkg,
  290. const char *scriptname, const char *desc,
  291. const char *cidir, char *cidirrest, ...)
  292. {
  293. struct command cmd;
  294. struct stat stab;
  295. va_list args;
  296. char buf[100];
  297. strcpy(cidirrest, scriptname);
  298. sprintf(buf, _("new %s script"), desc);
  299. va_start(args, cidirrest);
  300. command_init(&cmd, cidir, buf);
  301. command_add_arg(&cmd, scriptname);
  302. command_add_argv(&cmd, args);
  303. va_end(args);
  304. if (stat(cidir,&stab)) {
  305. command_destroy(&cmd);
  306. if (errno == ENOENT) {
  307. debug(dbg_scripts,"maintainer_script_new nonexistent %s `%s'",scriptname,cidir);
  308. return 0;
  309. }
  310. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  311. }
  312. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  313. command_destroy(&cmd);
  314. post_script_tasks();
  315. return 1;
  316. }
  317. int maintainer_script_alternative(struct pkginfo *pkg,
  318. const char *scriptname, const char *desc,
  319. const char *cidir, char *cidirrest,
  320. const char *ifok, const char *iffallback) {
  321. struct command cmd;
  322. const char *oldscriptpath;
  323. struct stat stab;
  324. char buf[100];
  325. oldscriptpath= pkgadminfile(pkg,scriptname);
  326. sprintf(buf, _("old %s script"), desc);
  327. command_init(&cmd, oldscriptpath, buf);
  328. command_add_args(&cmd, scriptname, ifok,
  329. versiondescribe(&pkg->available.version, vdew_nonambig),
  330. NULL);
  331. if (stat(oldscriptpath,&stab)) {
  332. if (errno == ENOENT) {
  333. debug(dbg_scripts,"maintainer_script_alternative nonexistent %s `%s'",
  334. scriptname,oldscriptpath);
  335. command_destroy(&cmd);
  336. return 0;
  337. }
  338. warning(_("unable to stat %s '%.250s': %s"),
  339. cmd.name, oldscriptpath, strerror(errno));
  340. } else {
  341. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  342. command_destroy(&cmd);
  343. post_script_tasks();
  344. return 1;
  345. }
  346. }
  347. fprintf(stderr, _("dpkg - trying script from the new package instead ...\n"));
  348. strcpy(cidirrest,scriptname);
  349. sprintf(buf, _("new %s script"), desc);
  350. command_destroy(&cmd);
  351. command_init(&cmd, cidir, buf);
  352. command_add_args(&cmd, scriptname, iffallback,
  353. versiondescribe(&pkg->installed.version, vdew_nonambig),
  354. NULL);
  355. if (stat(cidir,&stab)) {
  356. command_destroy(&cmd);
  357. if (errno == ENOENT)
  358. ohshit(_("there is no script in the new version of the package - giving up"));
  359. else
  360. ohshite(_("unable to stat %s `%.250s'"),buf,cidir);
  361. }
  362. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  363. fprintf(stderr, _("dpkg: ... it looks like that went OK.\n"));
  364. command_destroy(&cmd);
  365. post_script_tasks();
  366. return 1;
  367. }
  368. void clear_istobes(void) {
  369. struct pkgiterator *it;
  370. struct pkginfo *pkg;
  371. it= iterpkgstart();
  372. while ((pkg = iterpkgnext(it)) != NULL) {
  373. ensure_package_clientdata(pkg);
  374. pkg->clientdata->istobe= itb_normal;
  375. pkg->clientdata->replacingfilesandsaid= 0;
  376. }
  377. iterpkgend(it);
  378. }
  379. void debug(int which, const char *fmt, ...) {
  380. va_list args;
  381. if (!(f_debug & which)) return;
  382. fprintf(stderr,"D0%05o: ",which);
  383. va_start(args, fmt);
  384. vfprintf(stderr, fmt, args);
  385. va_end(args);
  386. putc('\n',stderr);
  387. }
  388. /*
  389. * Returns true if the directory contains conffiles belonging to pkg,
  390. * false otherwise.
  391. */
  392. bool
  393. hasdirectoryconffiles(struct filenamenode *file, struct pkginfo *pkg)
  394. {
  395. struct conffile *conff;
  396. size_t namelen;
  397. debug(dbg_veryverbose, "hasdirectoryconffiles `%s' (from %s)", file->name,
  398. pkg->name);
  399. namelen = strlen(file->name);
  400. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  401. if (!strncmp(file->name,conff->name,namelen)) {
  402. debug(dbg_veryverbose, "directory %s has conffile %s from %s",
  403. file->name, conff->name, pkg->name);
  404. return true;
  405. }
  406. }
  407. debug(dbg_veryverbose, "hasdirectoryconffiles no");
  408. return false;
  409. }
  410. /*
  411. * Returns true if the file is used by packages other than pkg,
  412. * false otherwise.
  413. */
  414. bool
  415. isdirectoryinuse(struct filenamenode *file, struct pkginfo *pkg)
  416. {
  417. struct filepackages_iterator *iter;
  418. struct pkginfo *other_pkg;
  419. debug(dbg_veryverbose, "isdirectoryinuse `%s' (except %s)", file->name,
  420. pkg ? pkg->name : "<none>");
  421. iter = filepackages_iter_new(file);
  422. while ((other_pkg = filepackages_iter_next(iter))) {
  423. debug(dbg_veryverbose, "isdirectoryinuse considering %s ...",
  424. other_pkg->name);
  425. if (other_pkg == pkg)
  426. continue;
  427. return true;
  428. }
  429. filepackages_iter_free(iter);
  430. debug(dbg_veryverbose, "isdirectoryinuse no");
  431. return false;
  432. }
  433. void oldconffsetflags(const struct conffile *searchconff) {
  434. struct filenamenode *namenode;
  435. while (searchconff) {
  436. namenode= findnamenode(searchconff->name, 0); /* XXX */
  437. namenode->flags |= fnnf_old_conff;
  438. if (!namenode->oldhash)
  439. namenode->oldhash= searchconff->hash;
  440. debug(dbg_conffdetail, "oldconffsetflags `%s' namenode %p flags %o",
  441. searchconff->name, namenode, namenode->flags);
  442. searchconff= searchconff->next;
  443. }
  444. }
  445. /*
  446. * If the pathname to remove is:
  447. *
  448. * 1. a sticky or set-id file, or
  449. * 2. an unknown object (i.e., not a file, link, directory, fifo or socket)
  450. *
  451. * we change its mode so that a malicious user cannot use it, even if it's
  452. * linked to another file.
  453. */
  454. int
  455. secure_unlink(const char *pathname)
  456. {
  457. struct stat stab;
  458. if (lstat(pathname,&stab)) return -1;
  459. return secure_unlink_statted(pathname, &stab);
  460. }
  461. int
  462. secure_unlink_statted(const char *pathname, const struct stat *stab)
  463. {
  464. if (S_ISREG(stab->st_mode) ? (stab->st_mode & 07000) :
  465. !(S_ISLNK(stab->st_mode) || S_ISDIR(stab->st_mode) ||
  466. S_ISFIFO(stab->st_mode) || S_ISSOCK(stab->st_mode))) {
  467. if (chmod(pathname, 0600))
  468. return -1;
  469. }
  470. if (unlink(pathname)) return -1;
  471. return 0;
  472. }
  473. void ensure_pathname_nonexisting(const char *pathname) {
  474. int c1;
  475. const char *u;
  476. u = path_skip_slash_dotslash(pathname);
  477. assert(*u);
  478. debug(dbg_eachfile,"ensure_pathname_nonexisting `%s'",pathname);
  479. if (!rmdir(pathname)) return; /* Deleted it OK, it was a directory. */
  480. if (errno == ENOENT || errno == ELOOP) return;
  481. if (errno == ENOTDIR) {
  482. /* Either it's a file, or one of the path components is. If one
  483. * of the path components is this will fail again ...
  484. */
  485. if (secure_unlink(pathname) == 0)
  486. return; /* OK, it was */
  487. if (errno == ENOTDIR) return;
  488. }
  489. if (errno != ENOTEMPTY && errno != EEXIST) { /* Huh ? */
  490. ohshite(_("unable to securely remove '%.255s'"), pathname);
  491. }
  492. c1 = subproc_fork();
  493. if (!c1) {
  494. execlp(RM, "rm", "-rf", "--", pathname, NULL);
  495. ohshite(_("failed to exec rm for cleanup"));
  496. }
  497. debug(dbg_eachfile,"ensure_pathname_nonexisting running rm -rf");
  498. subproc_wait_check(c1, "rm cleanup", 0);
  499. }
  500. void log_action(const char *action, struct pkginfo *pkg) {
  501. log_message("%s %s %s %s", action, pkg->name,
  502. versiondescribe(&pkg->installed.version, vdew_nonambig),
  503. versiondescribe(&pkg->available.version, vdew_nonambig));
  504. statusfd_send("processing: %s: %s", action, pkg->name);
  505. }