script.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * dpkg - main program for package management
  3. * script.c - maintainer script routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2007-2014 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <stdlib.h>
  30. #ifdef WITH_LIBSELINUX
  31. #include <selinux/selinux.h>
  32. #endif
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/debug.h>
  35. #include <dpkg/dpkg.h>
  36. #include <dpkg/dpkg-db.h>
  37. #include <dpkg/pkg.h>
  38. #include <dpkg/subproc.h>
  39. #include <dpkg/command.h>
  40. #include <dpkg/triglib.h>
  41. #include "filesdb.h"
  42. #include "infodb.h"
  43. #include "main.h"
  44. void
  45. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  46. {
  47. if (new_status < PKG_STAT_TRIGGERSAWAITED)
  48. pkg_set_status(pkg, new_status);
  49. else if (pkg->trigaw.head)
  50. pkg_set_status(pkg, PKG_STAT_TRIGGERSAWAITED);
  51. else if (pkg->trigpend_head)
  52. pkg_set_status(pkg, PKG_STAT_TRIGGERSPENDING);
  53. else
  54. pkg_set_status(pkg, PKG_STAT_INSTALLED);
  55. modstatdb_note(pkg);
  56. debug(dbg_triggersdetail, "post_postinst_tasks - trig_incorporate");
  57. trig_incorporate(modstatdb_get_status());
  58. }
  59. static void
  60. post_script_tasks(void)
  61. {
  62. debug(dbg_triggersdetail, "post_script_tasks - ensure_diversions");
  63. ensure_diversions();
  64. debug(dbg_triggersdetail, "post_script_tasks - trig_incorporate");
  65. trig_incorporate(modstatdb_get_status());
  66. }
  67. static void
  68. cu_post_script_tasks(int argc, void **argv)
  69. {
  70. post_script_tasks();
  71. }
  72. static void
  73. setexecute(const char *path, struct stat *stab)
  74. {
  75. if ((stab->st_mode & 0555) == 0555)
  76. return;
  77. if (!chmod(path, 0755))
  78. return;
  79. ohshite(_("unable to set execute permissions on '%.250s'"), path);
  80. }
  81. /**
  82. * Returns the path to the script inside the chroot.
  83. */
  84. static const char *
  85. maintscript_pre_exec(struct command *cmd)
  86. {
  87. const char *admindir = dpkg_db_get_dir();
  88. const char *changedir;
  89. size_t instdirlen = strlen(instdir);
  90. if (instdirlen > 0 && fc_script_chrootless)
  91. changedir = instdir;
  92. else
  93. changedir = "/";
  94. if (instdirlen > 0 && !fc_script_chrootless) {
  95. if (strncmp(admindir, instdir, instdirlen) != 0)
  96. ohshit(_("admindir must be inside instdir for dpkg to work properly"));
  97. if (setenv("DPKG_ADMINDIR", admindir + instdirlen, 1) < 0)
  98. ohshite(_("unable to setenv for subprocesses"));
  99. if (setenv("DPKG_ROOT", "", 1) < 0)
  100. ohshite(_("unable to setenv for subprocesses"));
  101. if (chroot(instdir))
  102. ohshite(_("failed to chroot to '%.250s'"), instdir);
  103. }
  104. /* Switch to a known good directory to give the maintainer script
  105. * a saner environment, also needed after the chroot(). */
  106. if (chdir(changedir))
  107. ohshite(_("failed to chdir to '%.255s'"), changedir);
  108. if (debug_has_flag(dbg_scripts)) {
  109. struct varbuf args = VARBUF_INIT;
  110. const char **argv = cmd->argv;
  111. while (*++argv) {
  112. varbuf_add_char(&args, ' ');
  113. varbuf_add_str(&args, *argv);
  114. }
  115. varbuf_end_str(&args);
  116. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename,
  117. args.buf);
  118. varbuf_destroy(&args);
  119. }
  120. if (instdirlen == 0 || fc_script_chrootless)
  121. return cmd->filename;
  122. assert(strlen(cmd->filename) >= instdirlen);
  123. return cmd->filename + instdirlen;
  124. }
  125. /**
  126. * Set a new security execution context for the maintainer script.
  127. *
  128. * Try to create a new execution context based on the current one and the
  129. * specific maintainer script filename. If it's the same as the current
  130. * one, use the given fallback.
  131. */
  132. static int
  133. maintscript_set_exec_context(struct command *cmd, const char *fallback)
  134. {
  135. int rc = 0;
  136. #ifdef WITH_LIBSELINUX
  137. rc = setexecfilecon(cmd->filename, fallback);
  138. #endif
  139. return rc < 0 ? rc : 0;
  140. }
  141. static int
  142. maintscript_exec(struct pkginfo *pkg, struct pkgbin *pkgbin,
  143. struct command *cmd, struct stat *stab, int warn)
  144. {
  145. pid_t pid;
  146. int rc;
  147. setexecute(cmd->filename, stab);
  148. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  149. pid = subproc_fork();
  150. if (pid == 0) {
  151. char *pkg_count;
  152. const char *maintscript_debug;
  153. pkg_count = str_fmt("%d", pkgset_installed_instances(pkg->set));
  154. maintscript_debug = debug_has_flag(dbg_scripts) ? "1" : "0";
  155. if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->set->name, 1) ||
  156. setenv("DPKG_MAINTSCRIPT_PACKAGE_REFCOUNT", pkg_count, 1) ||
  157. setenv("DPKG_MAINTSCRIPT_ARCH", pkgbin->arch->name, 1) ||
  158. setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) ||
  159. setenv("DPKG_MAINTSCRIPT_DEBUG", maintscript_debug, 1) ||
  160. setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1))
  161. ohshite(_("unable to setenv for maintainer script"));
  162. cmd->filename = cmd->argv[0] = maintscript_pre_exec(cmd);
  163. if (maintscript_set_exec_context(cmd, "dpkg_script_t") < 0)
  164. ohshite(_("cannot set security execution context for "
  165. "maintainer script"));
  166. runcmd(cmd);
  167. }
  168. subproc_signals_ignore(cmd->name);
  169. rc = subproc_reap(pid, cmd->name, warn);
  170. subproc_signals_restore();
  171. pop_cleanup(ehflag_normaltidy);
  172. return rc;
  173. }
  174. static int
  175. vmaintscript_installed(struct pkginfo *pkg, const char *scriptname,
  176. const char *desc, va_list args)
  177. {
  178. struct command cmd;
  179. const char *scriptpath;
  180. struct stat stab;
  181. char buf[100];
  182. scriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  183. sprintf(buf, _("installed %s script"), desc);
  184. command_init(&cmd, scriptpath, buf);
  185. command_add_arg(&cmd, scriptname);
  186. command_add_argv(&cmd, args);
  187. if (stat(scriptpath, &stab)) {
  188. command_destroy(&cmd);
  189. if (errno == ENOENT) {
  190. debug(dbg_scripts,
  191. "vmaintscript_installed nonexistent %s",
  192. scriptname);
  193. return 0;
  194. }
  195. ohshite(_("unable to stat %s '%.250s'"), buf, scriptpath);
  196. }
  197. maintscript_exec(pkg, &pkg->installed, &cmd, &stab, 0);
  198. command_destroy(&cmd);
  199. return 1;
  200. }
  201. /*
  202. * All ...'s in maintscript_* are const char *'s.
  203. */
  204. int
  205. maintscript_installed(struct pkginfo *pkg, const char *scriptname,
  206. const char *desc, ...)
  207. {
  208. va_list args;
  209. int rc;
  210. va_start(args, desc);
  211. rc = vmaintscript_installed(pkg, scriptname, desc, args);
  212. va_end(args);
  213. if (rc)
  214. post_script_tasks();
  215. return rc;
  216. }
  217. int
  218. maintscript_postinst(struct pkginfo *pkg, ...)
  219. {
  220. va_list args;
  221. int rc;
  222. va_start(args, pkg);
  223. rc = vmaintscript_installed(pkg, POSTINSTFILE, "post-installation", args);
  224. va_end(args);
  225. if (rc)
  226. ensure_diversions();
  227. return rc;
  228. }
  229. int
  230. maintscript_new(struct pkginfo *pkg, const char *scriptname,
  231. const char *desc, const char *cidir, char *cidirrest, ...)
  232. {
  233. struct command cmd;
  234. struct stat stab;
  235. va_list args;
  236. char buf[100];
  237. strcpy(cidirrest, scriptname);
  238. sprintf(buf, _("new %s script"), desc);
  239. va_start(args, cidirrest);
  240. command_init(&cmd, cidir, buf);
  241. command_add_arg(&cmd, scriptname);
  242. command_add_argv(&cmd, args);
  243. va_end(args);
  244. if (stat(cidir, &stab)) {
  245. command_destroy(&cmd);
  246. if (errno == ENOENT) {
  247. debug(dbg_scripts,
  248. "maintscript_new nonexistent %s '%s'",
  249. scriptname, cidir);
  250. return 0;
  251. }
  252. ohshite(_("unable to stat %s '%.250s'"), buf, cidir);
  253. }
  254. maintscript_exec(pkg, &pkg->available, &cmd, &stab, 0);
  255. command_destroy(&cmd);
  256. post_script_tasks();
  257. return 1;
  258. }
  259. int
  260. maintscript_fallback(struct pkginfo *pkg,
  261. const char *scriptname, const char *desc,
  262. const char *cidir, char *cidirrest,
  263. const char *ifok, const char *iffallback)
  264. {
  265. struct command cmd;
  266. const char *oldscriptpath;
  267. struct stat stab;
  268. char buf[100];
  269. oldscriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  270. sprintf(buf, _("old %s script"), desc);
  271. command_init(&cmd, oldscriptpath, buf);
  272. command_add_args(&cmd, scriptname, ifok,
  273. versiondescribe(&pkg->available.version, vdew_nonambig),
  274. NULL);
  275. if (stat(oldscriptpath, &stab)) {
  276. if (errno == ENOENT) {
  277. debug(dbg_scripts,
  278. "maintscript_fallback nonexistent %s '%s'",
  279. scriptname, oldscriptpath);
  280. command_destroy(&cmd);
  281. return 0;
  282. }
  283. warning(_("unable to stat %s '%.250s': %s"),
  284. cmd.name, oldscriptpath, strerror(errno));
  285. } else {
  286. if (!maintscript_exec(pkg, &pkg->installed, &cmd, &stab, SUBPROC_WARN)) {
  287. command_destroy(&cmd);
  288. post_script_tasks();
  289. return 1;
  290. }
  291. }
  292. notice(_("trying script from the new package instead ..."));
  293. strcpy(cidirrest, scriptname);
  294. sprintf(buf, _("new %s script"), desc);
  295. command_destroy(&cmd);
  296. command_init(&cmd, cidir, buf);
  297. command_add_args(&cmd, scriptname, iffallback,
  298. versiondescribe(&pkg->installed.version, vdew_nonambig),
  299. versiondescribe(&pkg->available.version, vdew_nonambig),
  300. NULL);
  301. if (stat(cidir, &stab)) {
  302. command_destroy(&cmd);
  303. if (errno == ENOENT)
  304. ohshit(_("there is no script in the new version of the package - giving up"));
  305. else
  306. ohshite(_("unable to stat %s '%.250s'"), buf, cidir);
  307. }
  308. maintscript_exec(pkg, &pkg->available, &cmd, &stab, 0);
  309. notice(_("... it looks like that went OK"));
  310. command_destroy(&cmd);
  311. post_script_tasks();
  312. return 1;
  313. }