script.c 9.8 KB

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