- add pool_error and pool_errstr. get rid of lots of exit() calls.
[platform/upstream/libsolv.git] / ext / repo_updateinfoxml.c
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #define _GNU_SOURCE
9 #define _XOPEN_SOURCE /* glibc2 needs this */
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <expat.h>
17 #include <time.h>
18
19 #include "pool.h"
20 #include "repo.h"
21 #include "repo_updateinfoxml.h"
22 #define DISABLE_SPLIT
23 #include "tools_util.h"
24
25 /*
26  * <updates>
27  *   <update from="rel-eng@fedoraproject.org" status="stable" type="security" version="1.4">
28  *     <id>FEDORA-2007-4594</id>
29  *     <title>imlib-1.9.15-6.fc8</title>
30  *     <severity>Important</severity>
31  *     <release>Fedora 8</release>
32  *     <rights>Copyright 2007 Company Inc</rights>
33  *     <issued date="2007-12-28 16:42:30"/>
34  *     <updated date="2008-03-14 12:00:00"/>
35  *     <references>
36  *       <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=426091" id="426091" title="CVE-2007-3568 imlib: infinite loop DoS using crafted BMP image" type="bugzilla"/>
37  *     </references>
38  *     <description>This update includes a fix for a denial-of-service issue (CVE-2007-3568) whereby an attacker who could get an imlib-using user to view a  specially-crafted BMP image could cause the user's CPU to go into an infinite loop.</description>
39  *     <pkglist>
40  *       <collection short="F8">
41  *         <name>Fedora 8</name>
42  *         <package arch="ppc64" name="imlib-debuginfo" release="6.fc8" src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm" version="1.9.15">
43  *           <filename>imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm</filename>
44  *           <reboot_suggested>True</reboot_suggested>
45  *         </package>
46  *       </collection>
47  *     </pkglist>
48  *   </update>
49  * </updates>
50 */
51
52 enum state {
53   STATE_START,
54   STATE_UPDATES,
55   STATE_UPDATE,
56   STATE_ID,
57   STATE_TITLE,
58   STATE_RELEASE,
59   STATE_ISSUED,
60   STATE_UPDATED,
61   STATE_MESSAGE,
62   STATE_REFERENCES,
63   STATE_REFERENCE,
64   STATE_DESCRIPTION,
65   STATE_PKGLIST,
66   STATE_COLLECTION,
67   STATE_NAME,
68   STATE_PACKAGE,
69   STATE_FILENAME,
70   STATE_REBOOT,
71   STATE_RESTART,
72   STATE_RELOGIN,
73   STATE_RIGHTS,
74   STATE_SEVERITY,
75   NUMSTATES
76 };
77
78 struct stateswitch {
79   enum state from;
80   char *ename;
81   enum state to;
82   int docontent;
83 };
84
85
86 /* !! must be sorted by first column !! */
87 static struct stateswitch stateswitches[] = {
88   { STATE_START,       "updates",         STATE_UPDATES,     0 },
89   { STATE_START,       "update",          STATE_UPDATE,      0 },
90   { STATE_UPDATES,     "update",          STATE_UPDATE,      0 },
91   { STATE_UPDATE,      "id",              STATE_ID,          1 },
92   { STATE_UPDATE,      "title",           STATE_TITLE,       1 },
93   { STATE_UPDATE,      "severity",        STATE_SEVERITY,    1 },
94   { STATE_UPDATE,      "rights",          STATE_RIGHTS,      1 },
95   { STATE_UPDATE,      "release",         STATE_RELEASE,     1 },
96   { STATE_UPDATE,      "issued",          STATE_ISSUED,      0 },
97   { STATE_UPDATE,      "updated",         STATE_UPDATED,     0 },
98   { STATE_UPDATE,      "description",     STATE_DESCRIPTION, 1 },
99   { STATE_UPDATE,      "message",         STATE_MESSAGE    , 1 },
100   { STATE_UPDATE,      "references",      STATE_REFERENCES,  0 },
101   { STATE_UPDATE,      "pkglist",         STATE_PKGLIST,     0 },
102   { STATE_REFERENCES,  "reference",       STATE_REFERENCE,   0 },
103   { STATE_PKGLIST,     "collection",      STATE_COLLECTION,  0 },
104   { STATE_COLLECTION,  "name",            STATE_NAME,        1 },
105   { STATE_COLLECTION,  "package",         STATE_PACKAGE,     0 },
106   { STATE_PACKAGE,     "filename",        STATE_FILENAME,    1 },
107   { STATE_PACKAGE,     "reboot_suggested",STATE_REBOOT,      1 },
108   { STATE_PACKAGE,     "restart_suggested",STATE_RESTART,    1 },
109   { STATE_PACKAGE,     "relogin_suggested",STATE_RELOGIN,    1 },
110   { NUMSTATES }
111 };
112
113 struct parsedata {
114   int ret;
115   int depth;
116   enum state state;
117   int statedepth;
118   char *content;
119   int lcontent;
120   int acontent;
121   int docontent;
122   Pool *pool;
123   Repo *repo;
124   Repodata *data;
125   unsigned int datanum;
126   Solvable *solvable;
127   time_t buildtime;
128   Id collhandle;
129   struct joindata jd;
130
131   struct stateswitch *swtab[NUMSTATES];
132   enum state sbtab[NUMSTATES];
133 };
134
135 /*
136  * Convert date strings ("1287746075" or "2010-10-22 13:14:35")
137  * to timestamp.
138  */
139 static time_t
140 datestr2timestamp(const char *date)
141 {
142   const char *p;
143   struct tm tm;
144
145   if (!date || !*date)
146     return 0;
147   for (p = date; *p >= '0' && *p <= '9'; p++)
148     ;
149   if (!*p)
150     return atoi(date);
151   memset(&tm, 0, sizeof(tm));
152   if (!strptime(date, "%F%T", &tm))
153     return 0;
154   return timegm(&tm);
155 }
156
157 /*
158  * if we have seen a <filename>...
159  * inside of <package>...
160  *
161  *
162  * If not, we must insert an empty filename to UPDATE_COLLECTION_FILENAME
163  * at </package> in order to keep all UPDATE_COLLECTION_* arrays in sync
164  */
165
166 /*
167  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
168  */
169
170 static Id
171 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
172 {
173   const char *e, *v, *r, *v2;
174   char *c;
175   int l;
176
177   e = v = r = 0;
178   for (; *atts; atts += 2)
179     {
180       if (!strcmp(*atts, "epoch"))
181         e = atts[1];
182       else if (!strcmp(*atts, "version"))
183         v = atts[1];
184       else if (!strcmp(*atts, "release"))
185         r = atts[1];
186     }
187   if (e && !strcmp(e, "0"))
188     e = 0;
189   if (v && !e)
190     {
191       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
192         ;
193       if (v2 > v && *v2 == ':')
194         e = "0";
195     }
196   l = 1;
197   if (e)
198     l += strlen(e) + 1;
199   if (v)
200     l += strlen(v);
201   if (r)
202     l += strlen(r) + 1;
203   if (l > pd->acontent)
204     {
205       pd->content = realloc(pd->content, l + 256);
206       pd->acontent = l + 256;
207     }
208   c = pd->content;
209   if (e)
210     {
211       strcpy(c, e);
212       c += strlen(c);
213       *c++ = ':';
214     }
215   if (v)
216     {
217       strcpy(c, v);
218       c += strlen(c);
219     }
220   if (r)
221     {
222       *c++ = '-';
223       strcpy(c, r);
224       c += strlen(c);
225     }
226   *c = 0;
227   if (!*pd->content)
228     return 0;
229 #if 0
230   fprintf(stderr, "evr: %s\n", pd->content);
231 #endif
232   return pool_str2id(pool, pd->content, 1);
233 }
234
235
236
237 static void XMLCALL
238 startElement(void *userData, const char *name, const char **atts)
239 {
240   struct parsedata *pd = userData;
241   Pool *pool = pd->pool;
242   Solvable *solvable = pd->solvable;
243   struct stateswitch *sw;
244   /*const char *str; */
245
246 #if 0
247   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
248 #endif
249   if (pd->depth != pd->statedepth)
250     {
251       pd->depth++;
252       return;
253     }
254
255   pd->depth++;
256   if (!pd->swtab[pd->state])
257     return;
258   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
259     if (!strcmp(sw->ename, name))
260       break;
261
262   if (sw->from != pd->state)
263     {
264 #if 0
265       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
266 #endif
267       return;
268     }
269   pd->state = sw->to;
270   pd->docontent = sw->docontent;
271   pd->statedepth = pd->depth;
272   pd->lcontent = 0;
273   *pd->content = 0;
274
275   switch(pd->state)
276     {
277     case STATE_START:
278       break;
279     case STATE_UPDATES:
280       break;
281       /*
282        * <update from="rel-eng@fedoraproject.org"
283        *         status="stable"
284        *         type="bugfix" (enhancement, security)
285        *         version="1.4">
286        */
287     case STATE_UPDATE:
288       {
289         const char *from = 0, *type = 0, *version = 0;
290         for (; *atts; atts += 2)
291           {
292             if (!strcmp(*atts, "from"))
293               from = atts[1];
294             else if (!strcmp(*atts, "type"))
295               type = atts[1];
296             else if (!strcmp(*atts, "version"))
297               version = atts[1];
298           }
299
300
301         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
302         pd->datanum = pd->solvable - pool->solvables;
303
304         solvable->vendor = pool_str2id(pool, from, 1);
305         solvable->evr = pool_str2id(pool, version, 1);
306         solvable->arch = ARCH_NOARCH;
307         if (type)
308           repodata_set_str(pd->data, pd->datanum, SOLVABLE_PATCHCATEGORY, type);
309         pd->buildtime = (time_t)0;
310       }
311       break;
312       /* <id>FEDORA-2007-4594</id> */
313     case STATE_ID:
314       break;
315       /* <title>imlib-1.9.15-6.fc8</title> */
316     case STATE_TITLE:
317       break;
318       /* <release>Fedora 8</release> */
319     case STATE_RELEASE:
320       break;
321       /*  <issued date="2008-03-21 21:36:55"/>
322       */
323     case STATE_ISSUED:
324     case STATE_UPDATED:
325       {
326         const char *date = 0;
327         for (; *atts; atts += 2)
328           {
329             if (!strcmp(*atts, "date"))
330               date = atts[1];
331           }
332         if (date)
333           {
334             time_t t = datestr2timestamp(date);
335             if (t && t > pd->buildtime)
336               pd->buildtime = t;
337           }
338       }
339       break;
340     case STATE_REFERENCES:
341       break;
342       /*  <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=330471"
343        *             id="330471"
344        *             title="LDAP schema file missing for dhcpd"
345        *             type="bugzilla"/>
346        */
347     case STATE_REFERENCE:
348       {
349         const char *href = 0, *id = 0, *title = 0, *type = 0;
350         Id handle;
351         for (; *atts; atts += 2)
352           {
353             if (!strcmp(*atts, "href"))
354               href = atts[1];
355             else if (!strcmp(*atts, "id"))
356               id = atts[1];
357             else if (!strcmp(*atts, "title"))
358               title = atts[1];
359             else if (!strcmp(*atts, "type"))
360               type = atts[1];
361           }
362         handle = repodata_new_handle(pd->data);
363         if (href)
364           repodata_set_str(pd->data, handle, UPDATE_REFERENCE_HREF, href);
365         if (id)
366           repodata_set_str(pd->data, handle, UPDATE_REFERENCE_ID, id);
367         if (title)
368           repodata_set_str(pd->data, handle, UPDATE_REFERENCE_TITLE, title);
369         if (type)
370           repodata_set_poolstr(pd->data, handle, UPDATE_REFERENCE_TYPE, type);
371         repodata_add_flexarray(pd->data, pd->datanum, UPDATE_REFERENCE, handle);
372       }
373       break;
374       /* <description>This update ...</description> */
375     case STATE_DESCRIPTION:
376       break;
377       /* <message type="confirm">This update ...</message> */
378     case STATE_MESSAGE:
379       break;
380     case STATE_PKGLIST:
381       break;
382       /* <collection short="F8" */
383     case STATE_COLLECTION:
384       break;
385       /* <name>Fedora 8</name> */
386     case STATE_NAME:
387       break;
388       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
389        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
390        *            version="1.9.15">
391        *
392        *
393        * -> patch.conflicts: {name} < {version}.{release}
394        */
395     case STATE_PACKAGE:
396       {
397         const char *arch = 0, *name = 0;
398         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
399         Id n, a = 0;
400         Id rel_id;
401
402         for (; *atts; atts += 2)
403           {
404             if (!strcmp(*atts, "arch"))
405               arch = atts[1];
406             else if (!strcmp(*atts, "name"))
407               name = atts[1];
408           }
409         /* generated Id for name */
410         n = pool_str2id(pool, name, 1);
411         rel_id = n;
412         if (arch)
413           {
414             /*  generate Id for arch and combine with name */
415             a = pool_str2id(pool, arch, 1);
416             rel_id = pool_rel2id(pool, n, a, REL_ARCH, 1);
417           }
418         rel_id = pool_rel2id(pool, rel_id, evr, REL_LT, 1);
419
420         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
421
422         /* who needs the collection anyway? */
423         pd->collhandle = repodata_new_handle(pd->data);
424         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_NAME, n);
425         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_EVR, evr);
426         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_ARCH, a);
427         break;
428       }
429       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
430       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
431     case STATE_FILENAME:
432       break;
433       /* <reboot_suggested>True</reboot_suggested> */
434     case STATE_REBOOT:
435       break;
436       /* <restart_suggested>True</restart_suggested> */
437     case STATE_RESTART:
438       break;
439       /* <relogin_suggested>True</relogin_suggested> */
440     case STATE_RELOGIN:
441       break;
442     default:
443       break;
444     }
445   return;
446 }
447
448
449 static void XMLCALL
450 endElement(void *userData, const char *name)
451 {
452   struct parsedata *pd = userData;
453   Pool *pool = pd->pool;
454   Solvable *s = pd->solvable;
455   Repo *repo = pd->repo;
456
457 #if 0
458       fprintf(stderr, "end: %s\n", name);
459 #endif
460   if (pd->depth != pd->statedepth)
461     {
462       pd->depth--;
463 #if 0
464       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
465 #endif
466       return;
467     }
468
469   pd->depth--;
470   pd->statedepth--;
471   switch (pd->state)
472     {
473     case STATE_START:
474       break;
475     case STATE_UPDATES:
476       break;
477     case STATE_UPDATE:
478       s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
479       if (pd->buildtime)
480         {
481           repodata_set_num(pd->data, pd->datanum, SOLVABLE_BUILDTIME, pd->buildtime);
482           pd->buildtime = (time_t)0;
483         }
484       break;
485     case STATE_ID:
486       s->name = pool_str2id(pool, join2(&pd->jd, "patch", ":", pd->content), 1);
487       break;
488       /* <title>imlib-1.9.15-6.fc8</title> */
489     case STATE_TITLE:
490       while (pd->lcontent > 0 && pd->content[pd->lcontent - 1] == '\n')
491         pd->content[--pd->lcontent] = 0;
492       repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
493       break;
494     case STATE_SEVERITY:
495       repodata_set_poolstr(pd->data, pd->datanum, UPDATE_SEVERITY, pd->content);
496       break;
497     case STATE_RIGHTS:
498       repodata_set_poolstr(pd->data, pd->datanum, UPDATE_RIGHTS, pd->content);
499       break;
500       /*
501        * <release>Fedora 8</release>
502        */
503     case STATE_RELEASE:
504       break;
505     case STATE_ISSUED:
506       break;
507     case STATE_REFERENCES:
508       break;
509     case STATE_REFERENCE:
510       break;
511       /*
512        * <description>This update ...</description>
513        */
514     case STATE_DESCRIPTION:
515       repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
516       break;
517       /*
518        * <message>Warning! ...</message>
519        */
520     case STATE_MESSAGE:
521       repodata_set_str(pd->data, pd->datanum, UPDATE_MESSAGE, pd->content);
522       break;
523     case STATE_PKGLIST:
524       break;
525     case STATE_COLLECTION:
526       break;
527     case STATE_NAME:
528       break;
529     case STATE_PACKAGE:
530       repodata_add_flexarray(pd->data, pd->datanum, UPDATE_COLLECTION, pd->collhandle);
531       pd->collhandle = 0;
532       break;
533       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
534       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
535     case STATE_FILENAME:
536       repodata_set_str(pd->data, pd->collhandle, UPDATE_COLLECTION_FILENAME, pd->content);
537       break;
538       /* <reboot_suggested>True</reboot_suggested> */
539     case STATE_REBOOT:
540       if (pd->content[0] == 'T' || pd->content[0] == 't'|| pd->content[0] == '1')
541         {
542           /* FIXME: this is per-package, the global flag should be computed at runtime */
543           repodata_set_void(pd->data, pd->datanum, UPDATE_REBOOT);
544           repodata_set_void(pd->data, pd->collhandle, UPDATE_REBOOT);
545         }
546       break;
547       /* <restart_suggested>True</restart_suggested> */
548     case STATE_RESTART:
549       if (pd->content[0] == 'T' || pd->content[0] == 't'|| pd->content[0] == '1')
550         {
551           /* FIXME: this is per-package, the global flag should be computed at runtime */
552           repodata_set_void(pd->data, pd->datanum, UPDATE_RESTART);
553           repodata_set_void(pd->data, pd->collhandle, UPDATE_RESTART);
554         }
555       break;
556       /* <relogin_suggested>True</relogin_suggested> */
557     case STATE_RELOGIN:
558       if (pd->content[0] == 'T' || pd->content[0] == 't'|| pd->content[0] == '1')
559         {
560           /* FIXME: this is per-package, the global flag should be computed at runtime */
561           repodata_set_void(pd->data, pd->datanum, UPDATE_RELOGIN);
562           repodata_set_void(pd->data, pd->collhandle, UPDATE_RELOGIN);
563         }
564       break;
565     default:
566       break;
567     }
568
569   pd->state = pd->sbtab[pd->state];
570   pd->docontent = 0;
571 }
572
573
574 static void XMLCALL
575 characterData(void *userData, const XML_Char *s, int len)
576 {
577   struct parsedata *pd = userData;
578   int l;
579   char *c;
580
581   if (!pd->docontent)
582     {
583 #if 0
584       fprintf(stderr, "Content: [%d]'%.*s'\n", pd->state, len, s);
585 #endif
586       return;
587     }
588   l = pd->lcontent + len + 1;
589   if (l > pd->acontent)
590     {
591       pd->content = realloc(pd->content, l + 256);
592       pd->acontent = l + 256;
593     }
594   c = pd->content + pd->lcontent;
595   pd->lcontent += len;
596   while (len-- > 0)
597     *c++ = *s++;
598   *c = 0;
599 }
600
601
602 #define BUFF_SIZE 8192
603
604 int
605 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
606 {
607   Pool *pool = repo->pool;
608   struct parsedata pd;
609   char buf[BUFF_SIZE];
610   int i, l;
611   struct stateswitch *sw;
612   Repodata *data;
613   XML_Parser parser;
614
615   data = repo_add_repodata(repo, flags);
616
617   memset(&pd, 0, sizeof(pd));
618   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
619     {
620       if (!pd.swtab[sw->from])
621         pd.swtab[sw->from] = sw;
622       pd.sbtab[sw->to] = sw->from;
623     }
624   pd.pool = pool;
625   pd.repo = repo;
626   pd.data = data;
627
628   pd.content = malloc(256);
629   pd.acontent = 256;
630   pd.lcontent = 0;
631   parser = XML_ParserCreate(NULL);
632   XML_SetUserData(parser, &pd);
633   XML_SetElementHandler(parser, startElement, endElement);
634   XML_SetCharacterDataHandler(parser, characterData);
635   for (;;)
636     {
637       l = fread(buf, 1, sizeof(buf), fp);
638       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
639         {
640           pd.ret = pool_error(pool, -1, "repo_updateinfoxml: %s at line %u:%u", XML_ErrorString(XML_GetErrorCode(parser)), (unsigned int)XML_GetCurrentLineNumber(parser), (unsigned int)XML_GetCurrentColumnNumber(parser));
641           break;
642         }
643       if (l == 0)
644         break;
645     }
646   XML_ParserFree(parser);
647   free(pd.content);
648   join_freemem(&pd.jd);
649
650   if (!(flags & REPO_NO_INTERNALIZE))
651     repodata_internalize(data);
652   return pd.ret;
653 }
654