script.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * dpkg - main program for package management
  3. * script.c - maintainer script routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2007-2013 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 <http://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_SELINUX
  31. #include <selinux/selinux.h>
  32. #include <selinux/flask.h>
  33. #include <selinux/context.h>
  34. #endif
  35. #include <dpkg/i18n.h>
  36. #include <dpkg/dpkg.h>
  37. #include <dpkg/dpkg-db.h>
  38. #include <dpkg/pkg.h>
  39. #include <dpkg/subproc.h>
  40. #include <dpkg/command.h>
  41. #include <dpkg/triglib.h>
  42. #include "filesdb.h"
  43. #include "infodb.h"
  44. #include "main.h"
  45. void
  46. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  47. {
  48. if (new_status < stat_triggersawaited)
  49. pkg_set_status(pkg, new_status);
  50. else if (pkg->trigaw.head)
  51. pkg_set_status(pkg, stat_triggersawaited);
  52. else if (pkg->trigpend_head)
  53. pkg_set_status(pkg, stat_triggerspending);
  54. else
  55. pkg_set_status(pkg, stat_installed);
  56. modstatdb_note(pkg);
  57. post_postinst_tasks_core(pkg);
  58. }
  59. void
  60. post_postinst_tasks_core(struct pkginfo *pkg)
  61. {
  62. if (!f_noact) {
  63. debug(dbg_triggersdetail,
  64. "post_postinst_tasks_core - trig_incorporate");
  65. trig_incorporate(msdbrw_write);
  66. }
  67. }
  68. static void
  69. post_script_tasks(void)
  70. {
  71. ensure_diversions();
  72. debug(dbg_triggersdetail,
  73. "post_script_tasks - ensure_diversions; trig_incorporate");
  74. trig_incorporate(msdbrw_write);
  75. }
  76. static void
  77. cu_post_script_tasks(int argc, void **argv)
  78. {
  79. post_script_tasks();
  80. }
  81. static void
  82. setexecute(const char *path, struct stat *stab)
  83. {
  84. if ((stab->st_mode & 0555) == 0555)
  85. return;
  86. if (!chmod(path, 0755))
  87. return;
  88. ohshite(_("unable to set execute permissions on `%.250s'"), path);
  89. }
  90. /**
  91. * Returns the path to the script inside the chroot.
  92. */
  93. static const char *
  94. preexecscript(struct command *cmd)
  95. {
  96. const char *admindir = dpkg_db_get_dir();
  97. size_t instdirl = strlen(instdir);
  98. if (*instdir) {
  99. if (strncmp(admindir, instdir, instdirl) != 0)
  100. ohshit(_("admindir must be inside instdir for dpkg to work properly"));
  101. if (setenv("DPKG_ADMINDIR", admindir + instdirl, 1) < 0)
  102. ohshite(_("unable to setenv for subprocesses"));
  103. if (chroot(instdir))
  104. ohshite(_("failed to chroot to `%.250s'"), instdir);
  105. }
  106. /* Switch to a known good directory to give the maintainer script
  107. * a saner environment, also needed after the chroot(). */
  108. if (chdir("/"))
  109. ohshite(_("failed to chdir to `%.255s'"), "/");
  110. if (debug_has_flag(dbg_scripts)) {
  111. struct varbuf args = VARBUF_INIT;
  112. const char **argv = cmd->argv;
  113. while (*++argv) {
  114. varbuf_add_char(&args, ' ');
  115. varbuf_add_str(&args, *argv);
  116. }
  117. varbuf_end_str(&args);
  118. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename,
  119. args.buf);
  120. varbuf_destroy(&args);
  121. }
  122. if (!instdirl)
  123. return cmd->filename;
  124. assert(strlen(cmd->filename) >= instdirl);
  125. return cmd->filename + instdirl;
  126. }
  127. /**
  128. * Set a new security execution context for the maintainer script.
  129. *
  130. * Try to create a new execution context based on the current one and the
  131. * specific maintainer script filename. If it's the same as the current
  132. * one, use the given fallback.
  133. */
  134. static int
  135. maintscript_set_exec_context(struct command *cmd, const char *fallback)
  136. {
  137. int rc = 0;
  138. #ifdef WITH_SELINUX
  139. security_context_t curcon = NULL, newcon = NULL, filecon = NULL;
  140. context_t tmpcon = NULL;
  141. if (is_selinux_enabled() < 1)
  142. return 0;
  143. rc = getcon(&curcon);
  144. if (rc < 0)
  145. goto out;
  146. rc = getfilecon(cmd->filename, &filecon);
  147. if (rc < 0)
  148. goto out;
  149. rc = security_compute_create(curcon, filecon, SECCLASS_PROCESS, &newcon);
  150. if (rc < 0)
  151. goto out;
  152. if (strcmp(curcon, newcon) == 0) {
  153. /* No default transition, use fallback for now. */
  154. rc = -1;
  155. tmpcon = context_new(curcon);
  156. if (tmpcon == NULL)
  157. goto out;
  158. if (context_type_set(tmpcon, fallback))
  159. goto out;
  160. freecon(newcon);
  161. newcon = strdup(context_str(tmpcon));
  162. if (newcon == NULL)
  163. goto out;
  164. rc = 0;
  165. }
  166. rc = setexeccon(newcon);
  167. out:
  168. if (rc < 0 && security_getenforce() == 0)
  169. rc = 0;
  170. context_free(tmpcon);
  171. freecon(newcon);
  172. freecon(curcon);
  173. freecon(filecon);
  174. #endif
  175. return rc < 0 ? rc : 0;
  176. }
  177. static int
  178. do_script(struct pkginfo *pkg, struct pkgbin *pkgbin,
  179. struct command *cmd, struct stat *stab, int warn)
  180. {
  181. pid_t pid;
  182. int r;
  183. setexecute(cmd->filename, stab);
  184. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  185. pid = subproc_fork();
  186. if (pid == 0) {
  187. if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->set->name, 1) ||
  188. setenv("DPKG_MAINTSCRIPT_ARCH", pkgbin->arch->name, 1) ||
  189. setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) ||
  190. setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1))
  191. ohshite(_("unable to setenv for maintainer script"));
  192. cmd->filename = cmd->argv[0] = preexecscript(cmd);
  193. if (maintscript_set_exec_context(cmd, "dpkg_script_t") < 0)
  194. ohshite(_("cannot set security execution context for "
  195. "maintainer script"));
  196. command_exec(cmd);
  197. }
  198. subproc_signals_setup(cmd->name); /* This does a push_cleanup(). */
  199. r = subproc_wait_check(pid, cmd->name, warn);
  200. pop_cleanup(ehflag_normaltidy);
  201. pop_cleanup(ehflag_normaltidy);
  202. return r;
  203. }
  204. static int
  205. vmaintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  206. const char *desc, va_list args)
  207. {
  208. struct command cmd;
  209. const char *scriptpath;
  210. struct stat stab;
  211. char buf[100];
  212. scriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  213. sprintf(buf, _("installed %s script"), desc);
  214. command_init(&cmd, scriptpath, buf);
  215. command_add_arg(&cmd, scriptname);
  216. command_add_argv(&cmd, args);
  217. if (stat(scriptpath, &stab)) {
  218. command_destroy(&cmd);
  219. if (errno == ENOENT) {
  220. debug(dbg_scripts,
  221. "vmaintainer_script_installed nonexistent %s",
  222. scriptname);
  223. return 0;
  224. }
  225. ohshite(_("unable to stat %s `%.250s'"), buf, scriptpath);
  226. }
  227. do_script(pkg, &pkg->installed, &cmd, &stab, 0);
  228. command_destroy(&cmd);
  229. return 1;
  230. }
  231. /*
  232. * All ...'s in maintainer_script_* are const char *'s.
  233. */
  234. int
  235. maintainer_script_installed(struct pkginfo *pkg, const char *scriptname,
  236. const char *desc, ...)
  237. {
  238. va_list args;
  239. int r;
  240. va_start(args, desc);
  241. r = vmaintainer_script_installed(pkg, scriptname, desc, args);
  242. va_end(args);
  243. if (r)
  244. post_script_tasks();
  245. return r;
  246. }
  247. int
  248. maintainer_script_postinst(struct pkginfo *pkg, ...)
  249. {
  250. va_list args;
  251. int r;
  252. va_start(args, pkg);
  253. r = vmaintainer_script_installed(pkg, POSTINSTFILE, "post-installation",
  254. args);
  255. va_end(args);
  256. if (r)
  257. ensure_diversions();
  258. return r;
  259. }
  260. int
  261. maintainer_script_new(struct pkginfo *pkg,
  262. const char *scriptname, const char *desc,
  263. const char *cidir, char *cidirrest, ...)
  264. {
  265. struct command cmd;
  266. struct stat stab;
  267. va_list args;
  268. char buf[100];
  269. strcpy(cidirrest, scriptname);
  270. sprintf(buf, _("new %s script"), desc);
  271. va_start(args, cidirrest);
  272. command_init(&cmd, cidir, buf);
  273. command_add_arg(&cmd, scriptname);
  274. command_add_argv(&cmd, args);
  275. va_end(args);
  276. if (stat(cidir, &stab)) {
  277. command_destroy(&cmd);
  278. if (errno == ENOENT) {
  279. debug(dbg_scripts,
  280. "maintainer_script_new nonexistent %s '%s'",
  281. scriptname, cidir);
  282. return 0;
  283. }
  284. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  285. }
  286. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  287. command_destroy(&cmd);
  288. post_script_tasks();
  289. return 1;
  290. }
  291. int
  292. maintainer_script_alternative(struct pkginfo *pkg,
  293. const char *scriptname, const char *desc,
  294. const char *cidir, char *cidirrest,
  295. const char *ifok, const char *iffallback)
  296. {
  297. struct command cmd;
  298. const char *oldscriptpath;
  299. struct stat stab;
  300. char buf[100];
  301. oldscriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  302. sprintf(buf, _("old %s script"), desc);
  303. command_init(&cmd, oldscriptpath, buf);
  304. command_add_args(&cmd, scriptname, ifok,
  305. versiondescribe(&pkg->available.version, vdew_nonambig),
  306. NULL);
  307. if (stat(oldscriptpath, &stab)) {
  308. if (errno == ENOENT) {
  309. debug(dbg_scripts,
  310. "maintainer_script_alternative nonexistent %s '%s'",
  311. scriptname, oldscriptpath);
  312. command_destroy(&cmd);
  313. return 0;
  314. }
  315. warning(_("unable to stat %s '%.250s': %s"),
  316. cmd.name, oldscriptpath, strerror(errno));
  317. } else {
  318. if (!do_script(pkg, &pkg->installed, &cmd, &stab, PROCWARN)) {
  319. command_destroy(&cmd);
  320. post_script_tasks();
  321. return 1;
  322. }
  323. }
  324. notice(_("trying script from the new package instead ..."));
  325. strcpy(cidirrest, scriptname);
  326. sprintf(buf, _("new %s script"), desc);
  327. command_destroy(&cmd);
  328. command_init(&cmd, cidir, buf);
  329. command_add_args(&cmd, scriptname, iffallback,
  330. versiondescribe(&pkg->installed.version, vdew_nonambig),
  331. NULL);
  332. if (stat(cidir, &stab)) {
  333. command_destroy(&cmd);
  334. if (errno == ENOENT)
  335. ohshit(_("there is no script in the new version of the package - giving up"));
  336. else
  337. ohshite(_("unable to stat %s `%.250s'"), buf, cidir);
  338. }
  339. do_script(pkg, &pkg->available, &cmd, &stab, 0);
  340. notice(_("... it looks like that went OK"));
  341. command_destroy(&cmd);
  342. post_script_tasks();
  343. return 1;
  344. }