script.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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-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_SELINUX
  31. #include <selinux/selinux.h>
  32. #endif
  33. #include <dpkg/i18n.h>
  34. #include <dpkg/dpkg.h>
  35. #include <dpkg/dpkg-db.h>
  36. #include <dpkg/pkg.h>
  37. #include <dpkg/subproc.h>
  38. #include <dpkg/command.h>
  39. #include <dpkg/triglib.h>
  40. #include "filesdb.h"
  41. #include "infodb.h"
  42. #include "main.h"
  43. void
  44. post_postinst_tasks(struct pkginfo *pkg, enum pkgstatus new_status)
  45. {
  46. if (new_status < PKG_STAT_TRIGGERSAWAITED)
  47. pkg_set_status(pkg, new_status);
  48. else if (pkg->trigaw.head)
  49. pkg_set_status(pkg, PKG_STAT_TRIGGERSAWAITED);
  50. else if (pkg->trigpend_head)
  51. pkg_set_status(pkg, PKG_STAT_TRIGGERSPENDING);
  52. else
  53. pkg_set_status(pkg, PKG_STAT_INSTALLED);
  54. modstatdb_note(pkg);
  55. debug(dbg_triggersdetail, "post_postinst_tasks - trig_incorporate");
  56. trig_incorporate(modstatdb_get_status());
  57. }
  58. static void
  59. post_script_tasks(void)
  60. {
  61. debug(dbg_triggersdetail, "post_script_tasks - ensure_diversions");
  62. ensure_diversions();
  63. debug(dbg_triggersdetail, "post_script_tasks - trig_incorporate");
  64. trig_incorporate(modstatdb_get_status());
  65. }
  66. static void
  67. cu_post_script_tasks(int argc, void **argv)
  68. {
  69. post_script_tasks();
  70. }
  71. static void
  72. setexecute(const char *path, struct stat *stab)
  73. {
  74. if ((stab->st_mode & 0555) == 0555)
  75. return;
  76. if (!chmod(path, 0755))
  77. return;
  78. ohshite(_("unable to set execute permissions on '%.250s'"), path);
  79. }
  80. /**
  81. * Returns the path to the script inside the chroot.
  82. */
  83. static const char *
  84. maintscript_pre_exec(struct command *cmd)
  85. {
  86. const char *admindir = dpkg_db_get_dir();
  87. size_t instdirl = strlen(instdir);
  88. if (*instdir) {
  89. if (strncmp(admindir, instdir, instdirl) != 0)
  90. ohshit(_("admindir must be inside instdir for dpkg to work properly"));
  91. if (setenv("DPKG_ADMINDIR", admindir + instdirl, 1) < 0)
  92. ohshite(_("unable to setenv for subprocesses"));
  93. if (chroot(instdir))
  94. ohshite(_("failed to chroot to '%.250s'"), instdir);
  95. }
  96. /* Switch to a known good directory to give the maintainer script
  97. * a saner environment, also needed after the chroot(). */
  98. if (chdir("/"))
  99. ohshite(_("failed to chdir to '%.255s'"), "/");
  100. if (debug_has_flag(dbg_scripts)) {
  101. struct varbuf args = VARBUF_INIT;
  102. const char **argv = cmd->argv;
  103. while (*++argv) {
  104. varbuf_add_char(&args, ' ');
  105. varbuf_add_str(&args, *argv);
  106. }
  107. varbuf_end_str(&args);
  108. debug(dbg_scripts, "fork/exec %s (%s )", cmd->filename,
  109. args.buf);
  110. varbuf_destroy(&args);
  111. }
  112. if (!instdirl)
  113. return cmd->filename;
  114. assert(strlen(cmd->filename) >= instdirl);
  115. return cmd->filename + instdirl;
  116. }
  117. /**
  118. * Set a new security execution context for the maintainer script.
  119. *
  120. * Try to create a new execution context based on the current one and the
  121. * specific maintainer script filename. If it's the same as the current
  122. * one, use the given fallback.
  123. */
  124. static int
  125. maintscript_set_exec_context(struct command *cmd, const char *fallback)
  126. {
  127. int rc = 0;
  128. #ifdef WITH_SELINUX
  129. rc = setexecfilecon(cmd->filename, fallback);
  130. #endif
  131. return rc < 0 ? rc : 0;
  132. }
  133. static int
  134. maintscript_exec(struct pkginfo *pkg, struct pkgbin *pkgbin,
  135. struct command *cmd, struct stat *stab, int warn)
  136. {
  137. pid_t pid;
  138. int rc;
  139. setexecute(cmd->filename, stab);
  140. push_cleanup(cu_post_script_tasks, ehflag_bombout, NULL, 0, 0);
  141. pid = subproc_fork();
  142. if (pid == 0) {
  143. char *pkg_count;
  144. m_asprintf(&pkg_count, "%d", pkgset_installed_instances(pkg->set));
  145. if (setenv("DPKG_MAINTSCRIPT_PACKAGE", pkg->set->name, 1) ||
  146. setenv("DPKG_MAINTSCRIPT_PACKAGE_REFCOUNT", pkg_count, 1) ||
  147. setenv("DPKG_MAINTSCRIPT_ARCH", pkgbin->arch->name, 1) ||
  148. setenv("DPKG_MAINTSCRIPT_NAME", cmd->argv[0], 1) ||
  149. setenv("DPKG_RUNNING_VERSION", PACKAGE_VERSION, 1))
  150. ohshite(_("unable to setenv for maintainer script"));
  151. cmd->filename = cmd->argv[0] = maintscript_pre_exec(cmd);
  152. if (maintscript_set_exec_context(cmd, "dpkg_script_t") < 0)
  153. ohshite(_("cannot set security execution context for "
  154. "maintainer script"));
  155. command_exec(cmd);
  156. }
  157. subproc_signals_ignore(cmd->name);
  158. rc = subproc_reap(pid, cmd->name, warn);
  159. subproc_signals_restore();
  160. pop_cleanup(ehflag_normaltidy);
  161. return rc;
  162. }
  163. static int
  164. vmaintscript_installed(struct pkginfo *pkg, const char *scriptname,
  165. const char *desc, va_list args)
  166. {
  167. struct command cmd;
  168. const char *scriptpath;
  169. struct stat stab;
  170. char buf[100];
  171. scriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  172. sprintf(buf, _("installed %s script"), desc);
  173. command_init(&cmd, scriptpath, buf);
  174. command_add_arg(&cmd, scriptname);
  175. command_add_argv(&cmd, args);
  176. if (stat(scriptpath, &stab)) {
  177. command_destroy(&cmd);
  178. if (errno == ENOENT) {
  179. debug(dbg_scripts,
  180. "vmaintscript_installed nonexistent %s",
  181. scriptname);
  182. return 0;
  183. }
  184. ohshite(_("unable to stat %s '%.250s'"), buf, scriptpath);
  185. }
  186. maintscript_exec(pkg, &pkg->installed, &cmd, &stab, 0);
  187. command_destroy(&cmd);
  188. return 1;
  189. }
  190. /*
  191. * All ...'s in maintscript_* are const char *'s.
  192. */
  193. int
  194. maintscript_installed(struct pkginfo *pkg, const char *scriptname,
  195. const char *desc, ...)
  196. {
  197. va_list args;
  198. int rc;
  199. va_start(args, desc);
  200. rc = vmaintscript_installed(pkg, scriptname, desc, args);
  201. va_end(args);
  202. if (rc)
  203. post_script_tasks();
  204. return rc;
  205. }
  206. int
  207. maintscript_postinst(struct pkginfo *pkg, ...)
  208. {
  209. va_list args;
  210. int rc;
  211. va_start(args, pkg);
  212. rc = vmaintscript_installed(pkg, POSTINSTFILE, "post-installation", args);
  213. va_end(args);
  214. if (rc)
  215. ensure_diversions();
  216. return rc;
  217. }
  218. int
  219. maintscript_new(struct pkginfo *pkg, const char *scriptname,
  220. const char *desc, const char *cidir, char *cidirrest, ...)
  221. {
  222. struct command cmd;
  223. struct stat stab;
  224. va_list args;
  225. char buf[100];
  226. strcpy(cidirrest, scriptname);
  227. sprintf(buf, _("new %s script"), desc);
  228. va_start(args, cidirrest);
  229. command_init(&cmd, cidir, buf);
  230. command_add_arg(&cmd, scriptname);
  231. command_add_argv(&cmd, args);
  232. va_end(args);
  233. if (stat(cidir, &stab)) {
  234. command_destroy(&cmd);
  235. if (errno == ENOENT) {
  236. debug(dbg_scripts,
  237. "maintscript_new nonexistent %s '%s'",
  238. scriptname, cidir);
  239. return 0;
  240. }
  241. ohshite(_("unable to stat %s '%.250s'"), buf, cidir);
  242. }
  243. maintscript_exec(pkg, &pkg->available, &cmd, &stab, 0);
  244. command_destroy(&cmd);
  245. post_script_tasks();
  246. return 1;
  247. }
  248. int
  249. maintscript_fallback(struct pkginfo *pkg,
  250. const char *scriptname, const char *desc,
  251. const char *cidir, char *cidirrest,
  252. const char *ifok, const char *iffallback)
  253. {
  254. struct command cmd;
  255. const char *oldscriptpath;
  256. struct stat stab;
  257. char buf[100];
  258. oldscriptpath = pkg_infodb_get_file(pkg, &pkg->installed, scriptname);
  259. sprintf(buf, _("old %s script"), desc);
  260. command_init(&cmd, oldscriptpath, buf);
  261. command_add_args(&cmd, scriptname, ifok,
  262. versiondescribe(&pkg->available.version, vdew_nonambig),
  263. NULL);
  264. if (stat(oldscriptpath, &stab)) {
  265. if (errno == ENOENT) {
  266. debug(dbg_scripts,
  267. "maintscript_fallback nonexistent %s '%s'",
  268. scriptname, oldscriptpath);
  269. command_destroy(&cmd);
  270. return 0;
  271. }
  272. warning(_("unable to stat %s '%.250s': %s"),
  273. cmd.name, oldscriptpath, strerror(errno));
  274. } else {
  275. if (!maintscript_exec(pkg, &pkg->installed, &cmd, &stab, SUBPROC_WARN)) {
  276. command_destroy(&cmd);
  277. post_script_tasks();
  278. return 1;
  279. }
  280. }
  281. notice(_("trying script from the new package instead ..."));
  282. strcpy(cidirrest, scriptname);
  283. sprintf(buf, _("new %s script"), desc);
  284. command_destroy(&cmd);
  285. command_init(&cmd, cidir, buf);
  286. command_add_args(&cmd, scriptname, iffallback,
  287. versiondescribe(&pkg->installed.version, vdew_nonambig),
  288. NULL);
  289. if (stat(cidir, &stab)) {
  290. command_destroy(&cmd);
  291. if (errno == ENOENT)
  292. ohshit(_("there is no script in the new version of the package - giving up"));
  293. else
  294. ohshite(_("unable to stat %s '%.250s'"), buf, cidir);
  295. }
  296. maintscript_exec(pkg, &pkg->available, &cmd, &stab, 0);
  297. notice(_("... it looks like that went OK"));
  298. command_destroy(&cmd);
  299. post_script_tasks();
  300. return 1;
  301. }