pkg_parse: fix segfault when parsing descriptions with leading newlines
[project/opkg-lede.git] / libopkg / pkg_parse.c
1 /* pkg_parse.c - the opkg package management system
2
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5 Steven M. Ayer
6 Copyright (C) 2002 Compaq Computer Corporation
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17 */
18
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <unistd.h>
22
23 #include "pkg.h"
24 #include "opkg_utils.h"
25 #include "pkg_parse.h"
26 #include "pkg_depends.h"
27 #include "libbb/libbb.h"
28
29 #include "file_util.h"
30 #include "parse_util.h"
31
32 static void parse_status(pkg_t * pkg, const char *sstr)
33 {
34 char sw_str[64], sf_str[64], ss_str[64];
35
36 if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) {
37 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
38 pkg->name);
39 return;
40 }
41
42 pkg->state_want = pkg_state_want_from_str(sw_str);
43 pkg->state_flag |= pkg_state_flag_from_str(sf_str);
44 pkg->state_status = pkg_state_status_from_str(ss_str);
45 }
46
47 static void parse_conffiles(pkg_t * pkg, const char *cstr)
48 {
49 conffile_list_t *cl;
50 char file_name[1024], md5sum[85];
51
52 if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
53 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
54 pkg->name);
55 return;
56 }
57
58 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
59
60 if (cl)
61 conffile_list_append(cl, file_name, md5sum);
62 }
63
64 int parse_version(pkg_t * pkg, const char *vstr)
65 {
66 char *colon, *dup, *rev;
67
68 if (strncmp(vstr, "Version:", 8) == 0)
69 vstr += 8;
70
71 while (*vstr && isspace(*vstr))
72 vstr++;
73
74 colon = strchr(vstr, ':');
75 if (colon) {
76 errno = 0;
77 pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
78 if (errno) {
79 opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
80 }
81 vstr = ++colon;
82 }
83
84
85 dup = xstrdup(vstr);
86 rev = strrchr(dup, '-');
87
88 if (rev) {
89 *rev++ = '\0';
90 pkg_set_string(pkg, PKG_REVISION, rev);
91 }
92
93 pkg_set_string(pkg, PKG_VERSION, dup);
94 free(dup);
95
96 return 0;
97 }
98
99 static char *parse_architecture(pkg_t *pkg, const char *str)
100 {
101 const char *s = str;
102 const char *e;
103
104 while (isspace(*s))
105 s++;
106
107 e = s + strlen(s);
108
109 while (e > s && isspace(*e))
110 e--;
111
112 return pkg_set_architecture(pkg, s, e - s);
113 }
114
115 static void parse_alternatives(pkg_t *pkg, char *list)
116 {
117 char *item, *tok;
118 struct pkg_alternatives *pkg_alts;
119 struct pkg_alternative **alts;
120 int nalts;
121
122 pkg_alts = pkg_get_ptr(pkg, PKG_ALTERNATIVES);
123 if (!pkg_alts) {
124 nalts = 0;
125 alts = NULL;
126 } else {
127 nalts = pkg_alts->nalts;
128 alts = pkg_alts->alts;
129 }
130
131 for (item = strtok_r(list, ",", &tok);
132 item;
133 item = strtok_r(NULL, ",", &tok)) {
134 enum pkg_alternative_field i;
135 char *val, *tok1;
136 /* the assignment was intended to quash the -Wmaybe-uninitialized warnings */
137 int prio = prio;
138 char *path = path, *altpath = altpath;
139
140 for (i = PAF_PRIO, val = strtok_r(item, ":", &tok1);
141 val && i < __PAF_MAX;
142 val = strtok_r(NULL, ":", &tok1), i++) {
143 switch (i) {
144 case PAF_PRIO:
145 prio = atoi(val);
146 break;
147 case PAF_PATH:
148 path = val;
149 break;
150 case PAF_ALTPATH:
151 altpath = val;
152 break;
153 default:
154 break;
155 }
156 }
157 if (!val && i == __PAF_MAX) {
158 char *_path, *_altpath;
159 struct pkg_alternative *alt;
160
161 /*
162 * - path must be absolute
163 * - altpath must be non-empty
164 */
165 if (path[0] != '/' || !altpath[0])
166 continue;
167
168 alt = calloc_a(sizeof(*alt),
169 &_path, strlen(path) + 1,
170 &_altpath, strlen(altpath) + 1);
171 if (!alt)
172 continue;
173 strcpy(_path, path);
174 strcpy(_altpath, altpath);
175 alt->prio = prio;
176 alt->path = _path;
177 alt->altpath = _altpath;
178 alts = xrealloc(alts, sizeof(*alts) * (nalts + 1));
179 alts[nalts++] = alt;
180 }
181 }
182
183 if (nalts > 0) {
184 if (!pkg_alts)
185 pkg_alts = xmalloc(sizeof(*pkg_alts));
186 pkg_alts->nalts = nalts;
187 pkg_alts->alts = alts;
188 pkg_set_ptr(pkg, PKG_ALTERNATIVES, pkg_alts);
189 }
190 }
191
192 int pkg_parse_line(void *ptr, char *line, uint mask)
193 {
194 pkg_t *pkg = (pkg_t *) ptr;
195 abstract_pkg_t *ab_pkg = NULL;
196 conffile_list_t *cl;
197
198 /* these flags are a bit hackish... */
199 static int reading_conffiles = 0, reading_description = 0;
200 static char *description = NULL;
201 int ret = 0;
202
203 /* Exclude globally masked fields. */
204 mask |= conf->pfm;
205
206 /* Flip the semantics of the mask. */
207 mask ^= PFM_ALL;
208
209 switch (*line) {
210 case 'A':
211 if ((mask & PFM_ALTERNATIVES) && is_field("Alternatives", line))
212 parse_alternatives(pkg, line + strlen("Alternatives") + 1);
213 else if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line))
214 parse_architecture(pkg, line + strlen("Architecture") + 1);
215 else if ((mask & PFM_AUTO_INSTALLED)
216 && is_field("Auto-Installed", line)) {
217 char *tmp = parse_simple("Auto-Installed", line);
218 if (strcmp(tmp, "yes") == 0)
219 pkg->auto_installed = 1;
220 free(tmp);
221 }
222 break;
223
224 case 'C':
225 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
226 reading_conffiles = 1;
227 reading_description = 0;
228
229 cl = xcalloc(1, sizeof(*cl));
230 conffile_list_init(cl);
231 pkg_set_ptr(pkg, PKG_CONFFILES, cl);
232
233 goto dont_reset_flags;
234 } else if ((mask & PFM_CONFLICTS)
235 && is_field("Conflicts", line))
236 parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
237 break;
238
239 case 'D':
240 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
241 description = parse_simple("Description", line);
242 reading_conffiles = 0;
243 reading_description = 1;
244 goto dont_reset_flags;
245 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
246 parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
247 break;
248
249 case 'E':
250 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
251 char *tmp = parse_simple("Essential", line);
252 if (strcmp(tmp, "yes") == 0)
253 pkg->essential = 1;
254 free(tmp);
255 }
256 break;
257
258 case 'F':
259 if ((mask & PFM_FILENAME) && is_field("Filename", line))
260 pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
261 break;
262
263 case 'I':
264 if ((mask & PFM_INSTALLED_SIZE)
265 && is_field("Installed-Size", line)) {
266 pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
267 } else if ((mask & PFM_INSTALLED_TIME)
268 && is_field("Installed-Time", line)) {
269 pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
270 }
271 break;
272
273 case 'M':
274 if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line)))
275 pkg_set_md5(pkg, line + strlen("MD5sum") + 1);
276 else if ((mask & PFM_MAINTAINER)
277 && is_field("Maintainer", line))
278 pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
279 break;
280
281 case 'P':
282 if ((mask & PFM_PACKAGE) && is_field("Package", line)) {
283 pkg->name = parse_simple("Package", line);
284 ab_pkg = abstract_pkg_fetch_by_name(pkg->name);
285
286 if (ab_pkg && (ab_pkg->state_flag & SF_NEED_DETAIL)) {
287 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
288 opkg_msg(DEBUG, "propagating abpkg flag to pkg %s\n", pkg->name);
289 pkg->state_flag |= SF_NEED_DETAIL;
290 }
291 }
292 }
293 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
294 pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
295 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
296 parse_providelist(pkg, line + strlen("Provides") + 1);
297 else if ((mask & PFM_PRE_DEPENDS)
298 && is_field("Pre-Depends", line))
299 parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
300 break;
301
302 case 'R':
303 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
304 parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
305 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
306 parse_replacelist(pkg, line + strlen("Replaces") + 1);
307 break;
308
309 case 'S':
310 if ((mask & PFM_SECTION) && is_field("Section", line))
311 pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
312 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
313 pkg_set_sha256(pkg, line + strlen("SHA256sum") + 1);
314 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
315 pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
316 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
317 pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
318 else if ((mask & PFM_STATUS) && is_field("Status", line))
319 parse_status(pkg, line);
320 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
321 parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
322 break;
323
324 case 'T':
325 if ((mask & PFM_TAGS) && is_field("Tags", line))
326 pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
327 break;
328
329 case 'V':
330 if ((mask & PFM_VERSION) && is_field("Version", line))
331 parse_version(pkg, line);
332 break;
333
334 case ' ':
335 if ((mask & PFM_DESCRIPTION) && reading_description) {
336 size_t len = (description ? strlen(description) : 0)
337 + (isatty(1) ? 1 : 0) + strlen(line) + 1;
338
339 description = description ? xrealloc(description, len)
340 : xcalloc(len, 1);
341
342 if (isatty(1))
343 strcat(description, "\n");
344
345 strcat(description, line);
346 goto dont_reset_flags;
347 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
348 parse_conffiles(pkg, line);
349 goto dont_reset_flags;
350 }
351
352 /* FALLTHROUGH */
353 default:
354 /* For package lists, signifies end of package. */
355 if (line_is_blank(line)) {
356 ret = 1;
357 break;
358 }
359 }
360
361 if (reading_description && description) {
362 pkg_set_string(pkg, PKG_DESCRIPTION, description);
363 free(description);
364 reading_description = 0;
365 description = NULL;
366 }
367
368 reading_conffiles = 0;
369
370 dont_reset_flags:
371
372 return ret;
373 }
374
375 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
376 {
377 int ret;
378 char *buf;
379 const size_t len = 4096;
380
381 buf = xmalloc(len);
382 ret =
383 parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
384 len);
385 free(buf);
386
387 if (pkg->name == NULL) {
388 /* probably just a blank line */
389 ret = 1;
390 }
391
392 return ret;
393 }