license_exists.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <err.h>
  11. #include <ccan/talloc/talloc.h>
  12. #include <ccan/str/str.h>
  13. #include <ccan/str_talloc/str_talloc.h>
  14. static struct doc_section *find_license_tag(const struct manifest *m)
  15. {
  16. struct doc_section *d;
  17. list_for_each(m->info_file->doc_sections, d, list) {
  18. if (!streq(d->function, m->basename))
  19. continue;
  20. if (streq(d->type, "license"))
  21. return d;
  22. }
  23. return NULL;
  24. }
  25. /* See GPLv2 and v2 (basically same wording) for interpreting versions:
  26. * the "any later version" means the recepient can choose. */
  27. static enum license which_license(struct doc_section *d)
  28. {
  29. /* This means "user chooses what version", including GPLv1! */
  30. if (streq(d->lines[0], "GPL"))
  31. return LICENSE_GPL;
  32. /* This means "v2 only". */
  33. if (streq(d->lines[0], "GPLv2"))
  34. return LICENSE_GPLv2;
  35. /* This means "v2 or above" at user's choice. */
  36. if (streq(d->lines[0], "GPL (v2 or any later version)"))
  37. return LICENSE_GPLv2_PLUS;
  38. /* This means "v3 or above" at user's choice. */
  39. if (streq(d->lines[0], "GPL (v3 or any later version)"))
  40. return LICENSE_GPLv3;
  41. /* This means "user chooses what version" */
  42. if (streq(d->lines[0], "LGPL"))
  43. return LICENSE_LGPL;
  44. /* This means "v2.1 only". */
  45. if (streq(d->lines[0], "LGPLv2.1"))
  46. return LICENSE_LGPLv2;
  47. /* This means "v2.1 or above" at user's choice. */
  48. if (streq(d->lines[0], "LGPL (v2.1 or any later version)"))
  49. return LICENSE_LGPLv2_PLUS;
  50. /* This means "v3 or above" at user's choice. */
  51. if (streq(d->lines[0], "LGPL (v3 or any later version)"))
  52. return LICENSE_LGPLv3;
  53. if (streq(d->lines[0], "BSD-MIT") || streq(d->lines[0], "MIT"))
  54. return LICENSE_MIT;
  55. if (streq(d->lines[0], "BSD (3 clause)"))
  56. return LICENSE_BSD;
  57. if (strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
  58. return LICENSE_PUBLIC_DOMAIN;
  59. return LICENSE_UNKNOWN;
  60. }
  61. static const char *expected_link(enum license license)
  62. {
  63. switch (license) {
  64. case LICENSE_LGPLv2_PLUS:
  65. case LICENSE_LGPLv2:
  66. return "../../licenses/LGPL-2.1";
  67. case LICENSE_LGPLv3:
  68. case LICENSE_LGPL:
  69. return "../../licenses/LGPL-3";
  70. case LICENSE_GPLv2_PLUS:
  71. case LICENSE_GPLv2:
  72. return "../../licenses/GPL-2";
  73. case LICENSE_GPLv3:
  74. case LICENSE_GPL:
  75. return "../../licenses/GPL-3";
  76. case LICENSE_BSD:
  77. return "../../licenses/BSD-3CLAUSE";
  78. case LICENSE_MIT:
  79. return "../../licenses/BSD-MIT";
  80. default:
  81. return NULL;
  82. }
  83. }
  84. static void handle_license_link(struct manifest *m, struct score *score)
  85. {
  86. struct doc_section *d = find_license_tag(m);
  87. const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
  88. const char *ldest = expected_link(m->license);
  89. char *q;
  90. printf(
  91. "Most modules want a copy of their license, so usually we create a\n"
  92. "LICENSE symlink into ../../licenses to avoid too many copies.\n");
  93. /* FIXME: make ask printf-like */
  94. q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
  95. ldest, d->lines[0]);
  96. if (ask(q)) {
  97. if (symlink(ldest, link) != 0)
  98. err(1, "Creating symlink %s -> %s", link, ldest);
  99. }
  100. }
  101. extern struct ccanlint license_exists;
  102. static void check_has_license(struct manifest *m,
  103. bool keep,
  104. unsigned int *timeleft, struct score *score)
  105. {
  106. char buf[PATH_MAX];
  107. ssize_t len;
  108. char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
  109. const char *expected;
  110. struct doc_section *d;
  111. d = find_license_tag(m);
  112. if (!d) {
  113. score->error = talloc_strdup(score, "No License: tag in _info");
  114. return;
  115. }
  116. m->license = which_license(d);
  117. if (m->license == LICENSE_UNKNOWN) {
  118. score_file_error(score, m->info_file, d->srcline,
  119. "WARNING: unknown License: in _info: %s",
  120. d->lines[0]);
  121. /* FIXME: For historical reasons, don't fail here. */
  122. score->pass = true;
  123. return;
  124. }
  125. /* If they have a license tag at all, we pass. */
  126. score->pass = true;
  127. expected = expected_link(m->license);
  128. len = readlink(license, buf, sizeof(buf));
  129. if (len < 0) {
  130. /* Could be a real file... OK if not a standard license. */
  131. if (errno == EINVAL) {
  132. if (!expected) {
  133. score->score = score->total;
  134. return;
  135. }
  136. score->error
  137. = talloc_asprintf(score,
  138. "License in _info is '%s',"
  139. " expect LICENSE symlink '%s'",
  140. d->lines[0], expected);
  141. return;
  142. }
  143. if (errno == ENOENT) {
  144. /* Public domain doesn't really need a file. */
  145. if (m->license == LICENSE_PUBLIC_DOMAIN) {
  146. score->score = score->total;
  147. return;
  148. }
  149. score->error = talloc_strdup(score,
  150. "LICENSE does not exist");
  151. if (expected)
  152. license_exists.handle = handle_license_link;
  153. return;
  154. }
  155. err(1, "readlink on %s", license);
  156. }
  157. if (len >= sizeof(buf))
  158. errx(1, "Reading symlink %s gave huge result", license);
  159. buf[len] = '\0';
  160. if (!strstarts(buf, "../../licenses/")) {
  161. score->error = talloc_asprintf(score,
  162. "Expected symlink to"
  163. " ../../licenses/..."
  164. " not %s", buf);
  165. return;
  166. }
  167. if (!expected) {
  168. score->error = talloc_asprintf(score,
  169. "License in _info is unknown '%s',"
  170. " but LICENSE symlink is '%s'",
  171. d->lines[0], buf);
  172. return;
  173. }
  174. if (!streq(buf, expected)) {
  175. score->error = talloc_asprintf(score,
  176. "Expected symlink to %s not %s",
  177. expected, buf);
  178. return;
  179. }
  180. score->pass = true;
  181. score->score = score->total;
  182. }
  183. struct ccanlint license_exists = {
  184. .key = "license_exists",
  185. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  186. .check = check_has_license,
  187. .needs = "info_exists"
  188. };
  189. REGISTER_TEST(license_exists);