help.c 16 KB

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