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