build.c 18 KB

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