build.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. /*
  2. * dpkg-deb - construction and deconstruction of *.deb archives
  3. * build.c - building archives
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2000,2001 Wichert Akkerman <wakkerma@debian.org>
  7. * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <sys/wait.h>
  27. #include <errno.h>
  28. #include <limits.h>
  29. #include <string.h>
  30. #include <dirent.h>
  31. #include <fcntl.h>
  32. #include <unistd.h>
  33. #include <stdbool.h>
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <dpkg/i18n.h>
  38. #include <dpkg/c-ctype.h>
  39. #include <dpkg/dpkg.h>
  40. #include <dpkg/dpkg-db.h>
  41. #include <dpkg/path.h>
  42. #include <dpkg/treewalk.h>
  43. #include <dpkg/varbuf.h>
  44. #include <dpkg/fdio.h>
  45. #include <dpkg/buffer.h>
  46. #include <dpkg/subproc.h>
  47. #include <dpkg/compress.h>
  48. #include <dpkg/ar.h>
  49. #include <dpkg/options.h>
  50. #include "dpkg-deb.h"
  51. static void
  52. control_treewalk_feed(const char *dir, int fd_out)
  53. {
  54. struct treeroot *tree;
  55. struct treenode *node;
  56. tree = treewalk_open(dir, TREEWALK_NONE, NULL);
  57. for (node = treewalk_node(tree); node; node = treewalk_next(tree)) {
  58. char *nodename;
  59. nodename = str_fmt("./%s", treenode_get_virtname(node));
  60. if (fd_write(fd_out, nodename, strlen(nodename) + 1) < 0)
  61. ohshite(_("failed to write filename to tar pipe (%s)"),
  62. _("control member"));
  63. free(nodename);
  64. }
  65. treewalk_close(tree);
  66. }
  67. /**
  68. * Simple structure to store information about a file.
  69. */
  70. struct file_info {
  71. struct file_info *next;
  72. char *fn;
  73. };
  74. static struct file_info *
  75. file_info_new(const char *filename)
  76. {
  77. struct file_info *fi;
  78. fi = m_malloc(sizeof(*fi));
  79. fi->fn = m_strdup(filename);
  80. fi->next = NULL;
  81. return fi;
  82. }
  83. static void
  84. file_info_free(struct file_info *fi)
  85. {
  86. free(fi->fn);
  87. free(fi);
  88. }
  89. static struct file_info *
  90. file_info_find_name(struct file_info *list, const char *filename)
  91. {
  92. struct file_info *node;
  93. for (node = list; node; node = node->next)
  94. if (strcmp(node->fn, filename) == 0)
  95. return node;
  96. return NULL;
  97. }
  98. /**
  99. * Add a new file_info struct to a single linked list of file_info structs.
  100. *
  101. * We perform a slight optimization to work around a ‘feature’ in tar: tar
  102. * always recurses into subdirectories if you list a subdirectory. So if an
  103. * entry is added and the previous entry in the list is its subdirectory we
  104. * remove the subdirectory.
  105. *
  106. * After a file_info struct is added to a list it may no longer be freed, we
  107. * assume full responsibility for its memory.
  108. */
  109. static void
  110. file_info_list_append(struct file_info **head, struct file_info **tail,
  111. struct file_info *fi)
  112. {
  113. if (*head == NULL)
  114. *head = *tail = fi;
  115. else
  116. *tail = (*tail)->next =fi;
  117. }
  118. /**
  119. * Free the memory for all entries in a list of file_info structs.
  120. */
  121. static void
  122. file_info_list_free(struct file_info *fi)
  123. {
  124. while (fi) {
  125. struct file_info *fl;
  126. fl=fi; fi=fi->next;
  127. file_info_free(fl);
  128. }
  129. }
  130. static void
  131. file_treewalk_feed(const char *dir, int fd_out)
  132. {
  133. struct treeroot *tree;
  134. struct treenode *node;
  135. struct file_info *fi;
  136. struct file_info *symlist = NULL;
  137. struct file_info *symlist_end = NULL;
  138. tree = treewalk_open(dir, TREEWALK_NONE, NULL);
  139. for (node = treewalk_node(tree); node ; node = treewalk_next(tree)) {
  140. const char *virtname = treenode_get_virtname(node);
  141. char *nodename;
  142. if (strncmp(virtname, BUILDCONTROLDIR, strlen(BUILDCONTROLDIR)) == 0)
  143. continue;
  144. nodename = str_fmt("./%s", virtname);
  145. if (strchr(nodename, '\n'))
  146. ohshit(_("newline not allowed in pathname '%s'"), nodename);
  147. /* We need to reorder the files so we can make sure that symlinks
  148. * will not appear before their target. */
  149. if (S_ISLNK(treenode_get_mode(node))) {
  150. fi = file_info_new(nodename);
  151. file_info_list_append(&symlist, &symlist_end, fi);
  152. } else {
  153. if (fd_write(fd_out, nodename, strlen(nodename) + 1) < 0)
  154. ohshite(_("failed to write filename to tar pipe (%s)"),
  155. _("data member"));
  156. }
  157. free(nodename);
  158. }
  159. treewalk_close(tree);
  160. for (fi = symlist; fi; fi = fi->next)
  161. if (fd_write(fd_out, fi->fn, strlen(fi->fn) + 1) < 0)
  162. ohshite(_("failed to write filename to tar pipe (%s)"), _("data member"));
  163. file_info_list_free(symlist);
  164. }
  165. static const char *const maintainerscripts[] = {
  166. PREINSTFILE,
  167. POSTINSTFILE,
  168. PRERMFILE,
  169. POSTRMFILE,
  170. NULL,
  171. };
  172. /**
  173. * Check control directory and file permissions.
  174. */
  175. static void
  176. check_file_perms(const char *ctrldir)
  177. {
  178. struct varbuf path = VARBUF_INIT;
  179. const char *const *mscriptp;
  180. struct stat mscriptstab;
  181. varbuf_printf(&path, "%s/", ctrldir);
  182. if (lstat(path.buf, &mscriptstab))
  183. ohshite(_("unable to stat control directory"));
  184. if (!S_ISDIR(mscriptstab.st_mode))
  185. ohshit(_("control directory is not a directory"));
  186. if ((mscriptstab.st_mode & 07757) != 0755)
  187. ohshit(_("control directory has bad permissions %03lo "
  188. "(must be >=0755 and <=0775)"),
  189. (unsigned long)(mscriptstab.st_mode & 07777));
  190. for (mscriptp = maintainerscripts; *mscriptp; mscriptp++) {
  191. varbuf_reset(&path);
  192. varbuf_printf(&path, "%s/%s", ctrldir, *mscriptp);
  193. if (!lstat(path.buf, &mscriptstab)) {
  194. if (S_ISLNK(mscriptstab.st_mode))
  195. continue;
  196. if (!S_ISREG(mscriptstab.st_mode))
  197. ohshit(_("maintainer script '%.50s' is not a plain file or symlink"),
  198. *mscriptp);
  199. if ((mscriptstab.st_mode & 07557) != 0555)
  200. ohshit(_("maintainer script '%.50s' has bad permissions %03lo "
  201. "(must be >=0555 and <=0775)"),
  202. *mscriptp, (unsigned long)(mscriptstab.st_mode & 07777));
  203. } else if (errno != ENOENT) {
  204. ohshite(_("maintainer script '%.50s' is not stattable"), *mscriptp);
  205. }
  206. }
  207. varbuf_destroy(&path);
  208. }
  209. /**
  210. * Check if conffiles contains sane information.
  211. */
  212. static void
  213. check_conffiles(const char *ctrldir, const char *rootdir)
  214. {
  215. FILE *cf;
  216. struct varbuf controlfile = VARBUF_INIT;
  217. char conffilename[MAXCONFFILENAME + 1];
  218. struct file_info *conffiles_head = NULL;
  219. struct file_info *conffiles_tail = NULL;
  220. varbuf_printf(&controlfile, "%s/%s", ctrldir, CONFFILESFILE);
  221. cf = fopen(controlfile.buf, "r");
  222. if (cf == NULL) {
  223. if (errno == ENOENT)
  224. return;
  225. ohshite(_("error opening conffiles file"));
  226. }
  227. while (fgets(conffilename, MAXCONFFILENAME + 1, cf)) {
  228. struct stat controlstab;
  229. int n;
  230. n = strlen(conffilename);
  231. if (!n)
  232. ohshite(_("empty string from fgets reading conffiles"));
  233. if (conffilename[n - 1] != '\n')
  234. ohshit(_("conffile name '%s' is too long, or missing final newline"),
  235. conffilename);
  236. conffilename[n - 1] = '\0';
  237. varbuf_reset(&controlfile);
  238. varbuf_printf(&controlfile, "%s/%s", rootdir, conffilename);
  239. if (lstat(controlfile.buf, &controlstab)) {
  240. if (errno == ENOENT) {
  241. if ((n > 1) && c_isspace(conffilename[n - 2]))
  242. warning(_("conffile filename '%s' contains trailing white spaces"),
  243. conffilename);
  244. ohshit(_("conffile '%.250s' does not appear in package"), conffilename);
  245. } else
  246. ohshite(_("conffile '%.250s' is not stattable"), conffilename);
  247. } else if (!S_ISREG(controlstab.st_mode)) {
  248. warning(_("conffile '%s' is not a plain file"), conffilename);
  249. }
  250. if (file_info_find_name(conffiles_head, conffilename)) {
  251. warning(_("conffile name '%s' is duplicated"), conffilename);
  252. } else {
  253. struct file_info *conffile;
  254. conffile = file_info_new(conffilename);
  255. file_info_list_append(&conffiles_head, &conffiles_tail, conffile);
  256. }
  257. }
  258. file_info_list_free(conffiles_head);
  259. varbuf_destroy(&controlfile);
  260. if (ferror(cf))
  261. ohshite(_("error reading conffiles file"));
  262. fclose(cf);
  263. }
  264. /**
  265. * Check the control file.
  266. *
  267. * @param dir The directory from where to build the binary package.
  268. * @return The pkginfo struct from the parsed control file.
  269. */
  270. static struct pkginfo *
  271. check_control_file(const char *ctrldir)
  272. {
  273. struct pkginfo *pkg;
  274. char *controlfile;
  275. controlfile = str_fmt("%s/%s", ctrldir, CONTROLFILE);
  276. parsedb(controlfile, pdb_parse_binary, &pkg);
  277. if (strspn(pkg->set->name, "abcdefghijklmnopqrstuvwxyz0123456789+-.") !=
  278. strlen(pkg->set->name))
  279. ohshit(_("package name has characters that aren't lowercase alphanums or '-+.'"));
  280. if (pkg->available.arch->type == DPKG_ARCH_NONE ||
  281. pkg->available.arch->type == DPKG_ARCH_EMPTY)
  282. ohshit(_("package architecture is missing or empty"));
  283. if (pkg->priority == PKG_PRIO_OTHER)
  284. warning(_("'%s' contains user-defined Priority value '%s'"),
  285. controlfile, pkg->otherpriority);
  286. free(controlfile);
  287. return pkg;
  288. }
  289. /**
  290. * Perform some sanity checks on the to-be-built package control area.
  291. *
  292. * @param dir The directory from where to build the binary package.
  293. * @return The pkginfo struct from the parsed control file.
  294. */
  295. static struct pkginfo *
  296. check_control_area(const char *ctrldir, const char *rootdir)
  297. {
  298. struct pkginfo *pkg;
  299. int warns;
  300. /* Start by reading in the control file so we can check its contents. */
  301. pkg = check_control_file(ctrldir);
  302. check_file_perms(ctrldir);
  303. check_conffiles(ctrldir, rootdir);
  304. warns = warning_get_count();
  305. if (warns)
  306. warning(P_("ignoring %d warning about the control file(s)",
  307. "ignoring %d warnings about the control file(s)", warns),
  308. warns);
  309. return pkg;
  310. }
  311. /**
  312. * Generate the pathname for the destination binary package.
  313. *
  314. * If the pathname cannot be computed, because the destination is a directory,
  315. * then NULL will be returned.
  316. *
  317. * @param dir The directory from where to build the binary package.
  318. * @param dest The destination name, either a file or directory name.
  319. * @return The pathname for the package being built.
  320. */
  321. static char *
  322. gen_dest_pathname(const char *dir, const char *dest)
  323. {
  324. if (dest) {
  325. struct stat dest_stab;
  326. if (stat(dest, &dest_stab)) {
  327. if (errno != ENOENT)
  328. ohshite(_("unable to check for existence of archive '%.250s'"), dest);
  329. } else if (S_ISDIR(dest_stab.st_mode)) {
  330. /* Need to compute the destination name from the package control file. */
  331. return NULL;
  332. }
  333. return m_strdup(dest);
  334. } else {
  335. char *pathname;
  336. pathname = m_malloc(strlen(dir) + sizeof(DEBEXT));
  337. strcpy(pathname, dir);
  338. path_trim_slash_slashdot(pathname);
  339. strcat(pathname, DEBEXT);
  340. return pathname;
  341. }
  342. }
  343. /**
  344. * Generate the pathname for the destination binary package from control file.
  345. *
  346. * @return The pathname for the package being built.
  347. */
  348. static char *
  349. gen_dest_pathname_from_pkg(const char *dir, struct pkginfo *pkg)
  350. {
  351. return str_fmt("%s/%s_%s_%s%s", dir, pkg->set->name,
  352. versiondescribe(&pkg->available.version, vdew_never),
  353. pkg->available.arch->name, DEBEXT);
  354. }
  355. typedef void filenames_feed_func(const char *dir, int fd_out);
  356. /**
  357. * Pack the contents of a directory into a tarball.
  358. */
  359. static void
  360. tarball_pack(const char *dir, filenames_feed_func *tar_filenames_feeder,
  361. struct compress_params *tar_compress_params, int fd_out)
  362. {
  363. int pipe_filenames[2], pipe_tarball[2];
  364. pid_t pid_tar, pid_comp;
  365. /* Fork off a tar. We will feed it a list of filenames on stdin later. */
  366. m_pipe(pipe_filenames);
  367. m_pipe(pipe_tarball);
  368. pid_tar = subproc_fork();
  369. if (pid_tar == 0) {
  370. m_dup2(pipe_filenames[0], 0);
  371. close(pipe_filenames[0]);
  372. close(pipe_filenames[1]);
  373. m_dup2(pipe_tarball[1], 1);
  374. close(pipe_tarball[0]);
  375. close(pipe_tarball[1]);
  376. if (chdir(dir))
  377. ohshite(_("failed to chdir to '%.255s'"), dir);
  378. execlp(TAR, "tar", "-cf", "-", "--format=gnu", "--null", "--no-unquote",
  379. "--no-recursion", "-T", "-", NULL);
  380. ohshite(_("unable to execute %s (%s)"), "tar -cf", TAR);
  381. }
  382. close(pipe_filenames[0]);
  383. close(pipe_tarball[1]);
  384. /* Of course we should not forget to compress the archive as well. */
  385. pid_comp = subproc_fork();
  386. if (pid_comp == 0) {
  387. close(pipe_filenames[1]);
  388. compress_filter(tar_compress_params, pipe_tarball[0], fd_out,
  389. _("compressing tar member"));
  390. exit(0);
  391. }
  392. close(pipe_tarball[0]);
  393. /* All the pipes are set, now lets start feeding filenames to tar. */
  394. tar_filenames_feeder(dir, pipe_filenames[1]);
  395. /* All done, clean up wait for tar and <compress> to finish their job. */
  396. close(pipe_filenames[1]);
  397. subproc_reap(pid_comp, _("<compress> from tar -cf"), 0);
  398. subproc_reap(pid_tar, "tar -cf", 0);
  399. }
  400. /**
  401. * Overly complex function that builds a .deb file.
  402. */
  403. int
  404. do_build(const char *const *argv)
  405. {
  406. struct compress_params control_compress_params;
  407. struct dpkg_error err;
  408. struct dpkg_ar *ar;
  409. const char *dir, *dest;
  410. char *ctrldir;
  411. char *debar;
  412. char *tfbuf;
  413. int gzfd;
  414. /* Decode our arguments. */
  415. dir = *argv++;
  416. if (!dir)
  417. badusage(_("--%s needs a <directory> argument"), cipaction->olong);
  418. dest = *argv++;
  419. if (dest && *argv)
  420. badusage(_("--%s takes at most two arguments"), cipaction->olong);
  421. debar = gen_dest_pathname(dir, dest);
  422. ctrldir = str_fmt("%s/%s", dir, BUILDCONTROLDIR);
  423. /* Perform some sanity checks on the to-be-build package. */
  424. if (nocheckflag) {
  425. if (debar == NULL)
  426. ohshit(_("target is directory - cannot skip control file check"));
  427. warning(_("not checking contents of control area"));
  428. info(_("building an unknown package in '%s'."), debar);
  429. } else {
  430. struct pkginfo *pkg;
  431. pkg = check_control_area(ctrldir, dir);
  432. if (debar == NULL)
  433. debar = gen_dest_pathname_from_pkg(dest, pkg);
  434. info(_("building package '%s' in '%s'."), pkg->set->name, debar);
  435. }
  436. m_output(stdout, _("<standard output>"));
  437. /* Now that we have verified everything its time to actually
  438. * build something. Let's start by making the ar-wrapper. */
  439. ar = dpkg_ar_create(debar, 0644);
  440. unsetenv("TAR_OPTIONS");
  441. /* Create a temporary file to store the control data in. Immediately
  442. * unlink our temporary file so others can't mess with it. */
  443. tfbuf = path_make_temp_template("dpkg-deb");
  444. gzfd = mkstemp(tfbuf);
  445. if (gzfd == -1)
  446. ohshite(_("failed to make temporary file (%s)"), _("control member"));
  447. /* Make sure it's gone, the fd will remain until we close it. */
  448. if (unlink(tfbuf))
  449. ohshit(_("failed to unlink temporary file (%s), %s"), _("control member"),
  450. tfbuf);
  451. free(tfbuf);
  452. /* Select the compressor to use for our control archive. */
  453. if (opt_uniform_compression) {
  454. control_compress_params = compress_params;
  455. } else {
  456. control_compress_params.type = COMPRESSOR_TYPE_GZIP;
  457. control_compress_params.strategy = COMPRESSOR_STRATEGY_NONE;
  458. control_compress_params.level = -1;
  459. if (!compressor_check_params(&control_compress_params, &err))
  460. internerr("invalid control member compressor params: %s", err.str);
  461. }
  462. /* Fork a tar to package the control-section of the package. */
  463. tarball_pack(ctrldir, control_treewalk_feed, &control_compress_params, gzfd);
  464. free(ctrldir);
  465. if (lseek(gzfd, 0, SEEK_SET))
  466. ohshite(_("failed to rewind temporary file (%s)"), _("control member"));
  467. /* We have our first file for the ar-archive. Write a header for it
  468. * to the package and insert it. */
  469. if (deb_format.major == 0) {
  470. struct stat controlstab;
  471. char versionbuf[40];
  472. if (fstat(gzfd, &controlstab))
  473. ohshite(_("failed to stat temporary file (%s)"), _("control member"));
  474. sprintf(versionbuf, "%-8s\n%jd\n", OLDARCHIVEVERSION,
  475. (intmax_t)controlstab.st_size);
  476. if (fd_write(ar->fd, versionbuf, strlen(versionbuf)) < 0)
  477. ohshite(_("error writing '%s'"), debar);
  478. if (fd_fd_copy(gzfd, ar->fd, -1, &err) < 0)
  479. ohshit(_("cannot copy '%s' into archive '%s': %s"), _("control member"),
  480. ar->name, err.str);
  481. } else if (deb_format.major == 2) {
  482. const char deb_magic[] = ARCHIVEVERSION "\n";
  483. char adminmember[16 + 1];
  484. sprintf(adminmember, "%s%s", ADMINMEMBER,
  485. compressor_get_extension(control_compress_params.type));
  486. dpkg_ar_put_magic(ar);
  487. dpkg_ar_member_put_mem(ar, DEBMAGIC, deb_magic, strlen(deb_magic));
  488. dpkg_ar_member_put_file(ar, adminmember, gzfd, -1);
  489. } else {
  490. internerr("unknown deb format version %d.%d", deb_format.major, deb_format.minor);
  491. }
  492. close(gzfd);
  493. /* Control is done, now we need to archive the data. */
  494. if (deb_format.major == 0) {
  495. /* In old format, the data member is just concatenated after the
  496. * control member, so we do not need a temporary file and can use
  497. * the compression file descriptor. */
  498. gzfd = ar->fd;
  499. } else if (deb_format.major == 2) {
  500. /* Start by creating a new temporary file. Immediately unlink the
  501. * temporary file so others can't mess with it. */
  502. tfbuf = path_make_temp_template("dpkg-deb");
  503. gzfd = mkstemp(tfbuf);
  504. if (gzfd == -1)
  505. ohshite(_("failed to make temporary file (%s)"), _("data member"));
  506. /* Make sure it's gone, the fd will remain until we close it. */
  507. if (unlink(tfbuf))
  508. ohshit(_("failed to unlink temporary file (%s), %s"), _("data member"),
  509. tfbuf);
  510. free(tfbuf);
  511. } else {
  512. internerr("unknown deb format version %d.%d", deb_format.major, deb_format.minor);
  513. }
  514. /* Pack the directory into a tarball, feeding files from the callback. */
  515. tarball_pack(dir, file_treewalk_feed, &compress_params, gzfd);
  516. /* Okay, we have data.tar as well now, add it to the ar wrapper. */
  517. if (deb_format.major == 2) {
  518. char datamember[16 + 1];
  519. sprintf(datamember, "%s%s", DATAMEMBER,
  520. compressor_get_extension(compress_params.type));
  521. if (lseek(gzfd, 0, SEEK_SET))
  522. ohshite(_("failed to rewind temporary file (%s)"), _("data member"));
  523. dpkg_ar_member_put_file(ar, datamember, gzfd, -1);
  524. close(gzfd);
  525. }
  526. if (fsync(ar->fd))
  527. ohshite(_("unable to sync file '%s'"), ar->name);
  528. dpkg_ar_close(ar);
  529. free(debar);
  530. return 0;
  531. }