remove.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * dpkg - main program for package management
  3. * remove.c - functionality for removing packages
  4. *
  5. * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.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
  9. * published by the Free Software Foundation; either version 2,
  10. * or (at your option) any later version.
  11. *
  12. * This is distributed in the hope that it will be useful, but
  13. * 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
  18. * License along with dpkg; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. #include <dirent.h>
  30. #include <ctype.h>
  31. #include <unistd.h>
  32. #include <string.h>
  33. #include <assert.h>
  34. #include <config.h>
  35. #include <dpkg.h>
  36. #include <dpkg-db.h>
  37. #include <myopt.h>
  38. #include "filesdb.h"
  39. #include "main.h"
  40. static void checkforremoval(struct pkginfo *pkgtoremove,
  41. struct pkginfo *pkgdepcheck, /* may be virtual pkg */
  42. int *rokp, struct varbuf *raemsgs) {
  43. struct deppossi *possi;
  44. struct pkginfo *depender;
  45. int before, ok;
  46. for (possi= pkgdepcheck->installed.depended; possi; possi= possi->nextrev) {
  47. if (possi->up->type != dep_depends && possi->up->type != dep_predepends) continue;
  48. depender= possi->up->up;
  49. debug(dbg_depcon,"checking depending package `%s'",depender->name);
  50. if (depender->status != stat_installed) continue;
  51. if (ignore_depends(depender)) {
  52. debug(dbg_depcon,"ignoring depending package `%s'\n",depender->name);
  53. continue;
  54. }
  55. if (dependtry > 1) { if (findbreakcycle(pkgtoremove,0)) sincenothing= 0; }
  56. before= raemsgs->used;
  57. ok= dependencies_ok(depender,pkgtoremove,raemsgs);
  58. if (ok == 0 && depender->clientdata->istobe == itb_remove) ok= 1;
  59. if (ok == 1) raemsgs->used= before; /* Don't burble about reasons for deferral */
  60. if (ok < *rokp) *rokp= ok;
  61. }
  62. }
  63. void deferred_remove(struct pkginfo *pkg) {
  64. struct varbuf raemsgs;
  65. int rok;
  66. struct dependency *dep;
  67. debug(dbg_general,"deferred_remove package %s",pkg->name);
  68. if (pkg->status == stat_notinstalled) {
  69. fprintf(stderr,
  70. _("dpkg - warning: ignoring request to remove %.250s which isn't installed.\n"),
  71. pkg->name);
  72. pkg->clientdata->istobe= itb_normal;
  73. return;
  74. } else if (!f_pending &&
  75. pkg->status == stat_configfiles &&
  76. cipaction->arg != act_purge) {
  77. fprintf(stderr,
  78. _("dpkg - warning: ignoring request to remove %.250s, only the config\n"
  79. " files of which are on the system. Use --purge to remove them too.\n"),
  80. pkg->name);
  81. pkg->clientdata->istobe= itb_normal;
  82. return;
  83. }
  84. assert(pkg->installed.valid);
  85. if (pkg->installed.essential && pkg->status != stat_configfiles)
  86. forcibleerr(fc_removeessential, _("This is an essential package -"
  87. " it should not be removed."));
  88. if (!f_pending)
  89. pkg->want= (cipaction->arg == act_purge) ? want_purge : want_deinstall;
  90. if (!f_noact) modstatdb_note(pkg);
  91. debug(dbg_general,"checking dependencies for remove `%s'",pkg->name);
  92. varbufinit(&raemsgs);
  93. rok= 2;
  94. checkforremoval(pkg,pkg,&rok,&raemsgs);
  95. for (dep= pkg->installed.depends; dep; dep= dep->next) {
  96. if (dep->type != dep_provides) continue;
  97. debug(dbg_depcon,"checking virtual package `%s'",dep->list->ed->name);
  98. checkforremoval(pkg,dep->list->ed,&rok,&raemsgs);
  99. }
  100. if (rok == 1) {
  101. varbuffree(&raemsgs);
  102. pkg->clientdata->istobe= itb_remove;
  103. add_to_queue(pkg);
  104. return;
  105. } else if (rok == 0) {
  106. sincenothing= 0;
  107. varbufaddc(&raemsgs,0);
  108. fprintf(stderr,
  109. _("dpkg: dependency problems prevent removal of %s:\n%s"),
  110. pkg->name, raemsgs.buf);
  111. ohshit(_("dependency problems - not removing"));
  112. } else if (raemsgs.used) {
  113. varbufaddc(&raemsgs,0);
  114. fprintf(stderr,
  115. _("dpkg: %s: dependency problems, but removing anyway as you request:\n%s"),
  116. pkg->name, raemsgs.buf);
  117. }
  118. varbuffree(&raemsgs);
  119. sincenothing= 0;
  120. if (pkg->eflag & eflagf_reinstreq)
  121. forcibleerr(fc_removereinstreq,
  122. _("Package is in a very bad inconsistent state - you should\n"
  123. " reinstall it before attempting a removal."));
  124. ensure_allinstfiles_available();
  125. filesdbinit();
  126. if (f_noact) {
  127. printf(_("Would remove or purge %s ...\n"),pkg->name);
  128. pkg->status= stat_notinstalled;
  129. pkg->clientdata->istobe= itb_normal;
  130. return;
  131. }
  132. oldconffsetflags(pkg->installed.conffiles);
  133. printf(_("Removing %s ...\n"),pkg->name);
  134. if (pkg->status == stat_halfconfigured || pkg->status == stat_installed) {
  135. if (pkg->status == stat_installed || pkg->status == stat_halfconfigured) {
  136. pkg->status= stat_halfconfigured;
  137. modstatdb_note(pkg);
  138. push_cleanup(cu_prermremove,~ehflag_normaltidy, 0,0, 1,(void*)pkg);
  139. maintainer_script_installed(pkg, PRERMFILE, "pre-removal",
  140. "remove", (char*)0);
  141. }
  142. pkg->status= stat_unpacked; /* Will turn into halfinstalled soon ... */
  143. }
  144. removal_bulk(pkg);
  145. }
  146. static void push_leftover(struct fileinlist **leftoverp,
  147. struct filenamenode *namenode) {
  148. struct fileinlist *newentry;
  149. newentry= nfmalloc(sizeof(struct fileinlist));
  150. newentry->next= *leftoverp;
  151. newentry->namenode= namenode;
  152. *leftoverp= newentry;
  153. }
  154. void removal_bulk(struct pkginfo *pkg) {
  155. /* This is used both by deferred_remove in this file, and at
  156. * the end of process_archive in archives.c if it needs to finish
  157. * removing a conflicting package.
  158. */
  159. static const char *const removeconffexts[]= { REMOVECONFFEXTS, 0 };
  160. static struct varbuf fnvb, removevb;
  161. int before, r, foundpostrm, removevbbase;
  162. int infodirbaseused, conffnameused, conffbasenamelen, pkgnameused;
  163. char *conffbasename;
  164. struct reversefilelistiter rlistit;
  165. struct conffile *conff, **lconffp;
  166. struct fileinlist *searchfile, *leftover;
  167. struct stat stab;
  168. DIR *dsd;
  169. struct dirent *de;
  170. char *p;
  171. const char *const *ext;
  172. const char *postrmfilename;
  173. struct filenamenode *namenode;
  174. debug(dbg_general,"removal_bulk package %s",pkg->name);
  175. if (pkg->want == want_purge) {
  176. printf(_("Purging configuration files for %s ...\n"),pkg->name);
  177. ensure_packagefiles_available(pkg); /* We may have modified this above. */
  178. /* We're about to remove the configuration, so remove the note
  179. * about which version it was ...
  180. */
  181. blankversion(&pkg->configversion);
  182. modstatdb_note(pkg);
  183. /* Remove from our list any conffiles that aren't ours any more or
  184. * are involved in diversions, except if we are the package doing the
  185. * diverting.
  186. */
  187. for (lconffp= &pkg->installed.conffiles;
  188. (conff= *lconffp) != 0;
  189. lconffp= &conff->next) {
  190. for (searchfile= pkg->clientdata->files;
  191. searchfile && strcmp(searchfile->namenode->name,conff->name);
  192. searchfile= searchfile->next);
  193. if (!searchfile) {
  194. debug(dbg_conff,"removal_bulk conffile not ours any more `%s'",conff->name);
  195. *lconffp= conff->next;
  196. } else if (searchfile->namenode->divert &&
  197. (searchfile->namenode->divert->camefrom ||
  198. (searchfile->namenode->divert->useinstead &&
  199. searchfile->namenode->divert->pkg != pkg))) {
  200. debug(dbg_conff,"removal_bulk conffile `%s' ignored due to diversion\n",
  201. conff->name);
  202. *lconffp= conff->next;
  203. } else {
  204. debug(dbg_conffdetail,"removal_bulk set to new conffile `%s'",conff->name);
  205. conff->hash= (char*)NEWCONFFILEFLAG; /* yes, cast away const */
  206. }
  207. }
  208. modstatdb_note(pkg);
  209. for (conff= pkg->installed.conffiles; conff; conff= conff->next) {
  210. varbufreset(&fnvb);
  211. r= conffderef(pkg, &fnvb, conff->name);
  212. debug(dbg_conffdetail, "removal_bulk conffile `%s' (= `%s')",
  213. conff->name, r == -1 ? "<r==-1>" : fnvb.buf);
  214. if (r == -1) continue;
  215. conffnameused= fnvb.used-1;
  216. if (unlink(fnvb.buf) && errno != ENOENT && errno != ENOTDIR)
  217. ohshite(_("cannot remove old config file `%.250s' (= `%.250s')"),
  218. conff->name, fnvb.buf);
  219. p= strrchr(fnvb.buf,'/'); if (!p) continue;
  220. *p= 0;
  221. varbufreset(&removevb);
  222. varbufaddstr(&removevb,fnvb.buf);
  223. varbufaddc(&removevb,'/');
  224. removevbbase= removevb.used;
  225. varbufaddc(&removevb,0);
  226. dsd= opendir(removevb.buf);
  227. if (!dsd) {
  228. int e=errno;
  229. debug(dbg_conffdetail, "removal_bulk conffile no dsd %s %s",
  230. fnvb.buf, strerror(e)); errno= e;
  231. if (errno == ENOENT || errno == ENOTDIR) continue;
  232. ohshite(_("cannot read config file dir `%.250s' (from `%.250s')"),
  233. fnvb.buf, conff->name);
  234. }
  235. debug(dbg_conffdetail, "removal_bulk conffile cleaning dsd %s", fnvb.buf);
  236. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  237. *p= '/';
  238. conffbasenamelen= strlen(++p);
  239. conffbasename= fnvb.buf+conffnameused-conffbasenamelen;
  240. while ((de= readdir(dsd)) != 0) {
  241. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry=`%s'"
  242. " conffbasename=`%s' conffnameused=%d conffbasenamelen=%d",
  243. de->d_name, conffbasename, conffnameused, conffbasenamelen);
  244. if (!strncmp(de->d_name,conffbasename,conffbasenamelen)) {
  245. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts right");
  246. for (ext= removeconffexts; *ext; ext++)
  247. if (!strcmp(*ext,de->d_name+conffbasenamelen)) goto yes_remove;
  248. p= de->d_name+conffbasenamelen;
  249. if (*p++ == '~') {
  250. while (*p && isdigit(*p)) p++;
  251. if (*p == '~' && !*++p) goto yes_remove;
  252. }
  253. }
  254. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry starts wrong");
  255. if (de->d_name[0] == '#' &&
  256. !strncmp(de->d_name+1,conffbasename,conffbasenamelen) &&
  257. !strcmp(de->d_name+1+conffbasenamelen,"#"))
  258. goto yes_remove;
  259. debug(dbg_stupidlyverbose, "removal_bulk conffile dsd entry not it");
  260. continue;
  261. yes_remove:
  262. removevb.used= removevbbase;
  263. varbufaddstr(&removevb,de->d_name); varbufaddc(&removevb,0);
  264. debug(dbg_conffdetail, "removal_bulk conffile dsd entry removing `%s'",
  265. removevb.buf);
  266. if (unlink(removevb.buf) && errno != ENOENT && errno != ENOTDIR)
  267. ohshite(_("cannot remove old backup config file `%.250s' (of `%.250s')"),
  268. removevb.buf, conff->name);
  269. }
  270. pop_cleanup(ehflag_normaltidy); /* closedir */
  271. }
  272. pkg->installed.conffiles= 0;
  273. modstatdb_note(pkg);
  274. }
  275. if (pkg->status == stat_halfinstalled || pkg->status == stat_unpacked ||
  276. pkg->status == stat_configfiles) {
  277. pkg->status= stat_halfinstalled;
  278. modstatdb_note(pkg);
  279. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  280. reversefilelist_init(&rlistit,pkg->clientdata->files);
  281. leftover= 0;
  282. while ((namenode= reversefilelist_next(&rlistit))) {
  283. debug(dbg_eachfile, "removal_bulk `%s' flags=%o",
  284. namenode->name, namenode->flags);
  285. if (namenode->flags & fnnf_old_conff) {
  286. push_leftover(&leftover,namenode);
  287. continue;
  288. }
  289. varbufreset(&fnvb);
  290. varbufaddstr(&fnvb,instdir);
  291. varbufaddstr(&fnvb,namenodetouse(namenode,pkg)->name);
  292. before= fnvb.used;
  293. varbufaddstr(&fnvb,DPKGTEMPEXT);
  294. varbufaddc(&fnvb,0);
  295. debug(dbg_eachfiledetail, "removal_bulk cleaning temp `%s'", fnvb.buf);
  296. ensure_pathname_nonexisting(fnvb.buf);
  297. fnvb.used= before;
  298. varbufaddstr(&fnvb,DPKGNEWEXT);
  299. varbufaddc(&fnvb,0);
  300. debug(dbg_eachfiledetail, "removal_bulk cleaning new `%s'", fnvb.buf);
  301. ensure_pathname_nonexisting(fnvb.buf);
  302. fnvb.used= before;
  303. varbufaddc(&fnvb,0);
  304. if (!stat(fnvb.buf,&stab) && S_ISDIR(stab.st_mode)) {
  305. debug(dbg_eachfiledetail, "removal_bulk is a directory");
  306. /* Only delete a directory or a link to one if we're the only
  307. * package which uses it. Other files should only be listed
  308. * in this package (but we don't check).
  309. */
  310. if (isdirectoryinuse(namenode,pkg)) continue;
  311. }
  312. debug(dbg_eachfiledetail, "removal_bulk removing `%s'", fnvb.buf);
  313. if (!rmdir(fnvb.buf) || errno == ENOENT || errno == ELOOP) continue;
  314. if (errno == ENOTEMPTY) {
  315. fprintf(stderr,
  316. _("dpkg - warning: while removing %.250s, directory `%.250s' not empty "
  317. "so not removed.\n"),
  318. pkg->name, namenode->name);
  319. push_leftover(&leftover,namenode);
  320. continue;
  321. } else if (errno == EBUSY || errno == EPERM) {
  322. fprintf(stderr, _("dpkg - warning: while removing %.250s,"
  323. " unable to remove directory `%.250s':"
  324. " %s - directory may be a mount point ?\n"),
  325. pkg->name, namenode->name, strerror(errno));
  326. push_leftover(&leftover,namenode);
  327. continue;
  328. }
  329. if (errno != ENOTDIR) ohshite(_("cannot remove `%.250s'"),fnvb.buf);
  330. debug(dbg_eachfiledetail, "removal_bulk unlinking `%s'", fnvb.buf);
  331. {
  332. /*
  333. * If file to remove is a device or s[gu]id, change its mode
  334. * so that a malicious user cannot use it even if it's linked
  335. * to another file
  336. */
  337. struct stat stat_buf;
  338. if (stat(fnvb.buf,&stat_buf)==0) {
  339. if (S_ISCHR(stat_buf.st_mode) || S_ISBLK(stat_buf.st_mode)) {
  340. chmod(fnvb.buf,0);
  341. }
  342. if (stat_buf.st_mode & (S_ISUID|S_ISGID)) {
  343. chmod(fnvb.buf,stat_buf.st_mode & ~(S_ISUID|S_ISGID));
  344. }
  345. }
  346. }
  347. if (unlink(fnvb.buf)) ohshite(_("cannot remove file `%.250s'"),fnvb.buf);
  348. }
  349. write_filelist_except(pkg,leftover,0);
  350. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  351. "remove", (char*)0);
  352. varbufreset(&fnvb);
  353. varbufaddstr(&fnvb,admindir);
  354. varbufaddstr(&fnvb,"/" INFODIR);
  355. infodirbaseused= fnvb.used;
  356. varbufaddc(&fnvb,0);
  357. dsd= opendir(fnvb.buf); if (!dsd) ohshite(_("cannot read info directory"));
  358. push_cleanup(cu_closedir,~0, 0,0, 1,(void*)dsd);
  359. foundpostrm= 0;
  360. debug(dbg_general, "removal_bulk cleaning info directory");
  361. while ((de= readdir(dsd)) != 0) {
  362. debug(dbg_veryverbose, "removal_bulk info file `%s'", de->d_name);
  363. if (de->d_name[0] == '.') continue;
  364. p= strrchr(de->d_name,'.'); if (!p) continue;
  365. if (strlen(pkg->name) != p-de->d_name ||
  366. strncmp(de->d_name,pkg->name,p-de->d_name)) continue;
  367. debug(dbg_stupidlyverbose, "removal_bulk info this pkg");
  368. /* We need the postrm and list files for --purge. */
  369. if (!strcmp(p+1,LISTFILE)) continue;
  370. if (!strcmp(p+1,POSTRMFILE)) { foundpostrm=1; continue; }
  371. debug(dbg_stupidlyverbose, "removal_bulk info not postrm or list");
  372. fnvb.used= infodirbaseused;
  373. varbufaddstr(&fnvb,de->d_name);
  374. varbufaddc(&fnvb,0);
  375. if (unlink(fnvb.buf))
  376. ohshite(_("unable to delete control info file `%.250s'"),fnvb.buf);
  377. debug(dbg_scripts, "removal_bulk info unlinked %s",fnvb.buf);
  378. }
  379. pop_cleanup(ehflag_normaltidy); /* closedir */
  380. pkg->status= stat_configfiles;
  381. pkg->installed.essential= 0;
  382. modstatdb_note(pkg);
  383. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  384. } else {
  385. postrmfilename= pkgadminfile(pkg,POSTRMFILE);
  386. if (!lstat(postrmfilename,&stab)) foundpostrm= 1;
  387. else if (errno == ENOENT) foundpostrm= 0;
  388. else ohshite(_("unable to check existence of `%.250s'"),postrmfilename);
  389. }
  390. debug(dbg_general, "removal_bulk purging? foundpostrm=%d",foundpostrm);
  391. if (!foundpostrm && !pkg->installed.conffiles) {
  392. /* If there are no config files and no postrm script then we
  393. * go straight into `purge'.
  394. */
  395. debug(dbg_general, "removal_bulk no postrm, no conffiles, purging");
  396. pkg->want= want_purge;
  397. } else if ( pkg->want == want_purge )
  398. maintainer_script_installed(pkg, POSTRMFILE, "post-removal",
  399. "purge", (char*)0);
  400. if (pkg->want == want_purge) {
  401. /* ie, either of the two branches above. */
  402. varbufreset(&fnvb);
  403. varbufaddstr(&fnvb,admindir);
  404. varbufaddstr(&fnvb,"/" INFODIR);
  405. varbufaddstr(&fnvb,pkg->name);
  406. pkgnameused= fnvb.used;
  407. varbufaddstr(&fnvb,"." LISTFILE);
  408. varbufaddc(&fnvb,0);
  409. debug(dbg_general, "removal_bulk purge done, removing list `%s'",fnvb.buf);
  410. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("cannot remove old files list"));
  411. fnvb.used= pkgnameused;
  412. varbufaddstr(&fnvb,"." POSTRMFILE);
  413. varbufaddc(&fnvb,0);
  414. debug(dbg_general, "removal_bulk purge done, removing postrm `%s'",fnvb.buf);
  415. if (unlink(fnvb.buf) && errno != ENOENT) ohshite(_("can't remove old postrm script"));
  416. pkg->status= stat_notinstalled;
  417. /* This will mess up reverse links, but if we follow them
  418. * we won't go back because pkg->status is stat_notinstalled.
  419. */
  420. pkg->installed.depends= 0;
  421. pkg->installed.essential= 0;
  422. pkg->installed.description= pkg->installed.maintainer= 0;
  423. pkg->installed.source= pkg->installed.installedsize= 0;
  424. blankversion(&pkg->installed.version);
  425. pkg->installed.arbs= 0;
  426. }
  427. pkg->eflag= eflagv_ok;
  428. modstatdb_note(pkg);
  429. debug(dbg_general, "removal done");
  430. }