build.c 17 KB

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