6af74f220ff26b8ccd5aa82cd8e0458d80beb611
[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   Id handle;
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  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
159  */
160 static Id
161 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
162 {
163   const char *e, *v, *r, *v2;
164   char *c;
165   int l;
166
167   e = v = r = 0;
168   for (; *atts; atts += 2)
169     {
170       if (!strcmp(*atts, "epoch"))
171         e = atts[1];
172       else if (!strcmp(*atts, "version"))
173         v = atts[1];
174       else if (!strcmp(*atts, "release"))
175         r = atts[1];
176     }
177   if (e && (!*e || !strcmp(e, "0")))
178     e = 0;
179   if (v && !e)
180     {
181       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
182         ;
183       if (v2 > v && *v2 == ':')
184         e = "0";
185     }
186   l = 1;
187   if (e)
188     l += strlen(e) + 1;
189   if (v)
190     l += strlen(v);
191   if (r)
192     l += strlen(r) + 1;
193   if (l > pd->acontent)
194     {
195       pd->content = realloc(pd->content, l + 256);
196       pd->acontent = l + 256;
197     }
198   c = pd->content;
199   if (e)
200     {
201       strcpy(c, e);
202       c += strlen(c);
203       *c++ = ':';
204     }
205   if (v)
206     {
207       strcpy(c, v);
208       c += strlen(c);
209     }
210   if (r)
211     {
212       *c++ = '-';
213       strcpy(c, r);
214       c += strlen(c);
215     }
216   *c = 0;
217   if (!*pd->content)
218     return 0;
219 #if 0
220   fprintf(stderr, "evr: %s\n", pd->content);
221 #endif
222   return pool_str2id(pool, pd->content, 1);
223 }
224
225
226
227 static void XMLCALL
228 startElement(void *userData, const char *name, const char **atts)
229 {
230   struct parsedata *pd = userData;
231   Pool *pool = pd->pool;
232   Solvable *solvable = pd->solvable;
233   struct stateswitch *sw;
234   /*const char *str; */
235
236 #if 0
237   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
238 #endif
239   if (pd->depth != pd->statedepth)
240     {
241       pd->depth++;
242       return;
243     }
244
245   pd->depth++;
246   if (!pd->swtab[pd->state])
247     return;
248   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
249     if (!strcmp(sw->ename, name))
250       break;
251
252   if (sw->from != pd->state)
253     {
254 #if 0
255       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
256 #endif
257       return;
258     }
259   pd->state = sw->to;
260   pd->docontent = sw->docontent;
261   pd->statedepth = pd->depth;
262   pd->lcontent = 0;
263   *pd->content = 0;
264
265   switch(pd->state)
266     {
267     case STATE_START:
268       break;
269     case STATE_UPDATES:
270       break;
271       /*
272        * <update from="rel-eng@fedoraproject.org"
273        *         status="stable"
274        *         type="bugfix" (enhancement, security)
275        *         version="1.4">
276        */
277     case STATE_UPDATE:
278       {
279         const char *from = 0, *type = 0, *version = 0;
280         for (; *atts; atts += 2)
281           {
282             if (!strcmp(*atts, "from"))
283               from = atts[1];
284             else if (!strcmp(*atts, "type"))
285               type = atts[1];
286             else if (!strcmp(*atts, "version"))
287               version = atts[1];
288           }
289         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
290         pd->handle = pd->solvable - pool->solvables;
291
292         solvable->vendor = pool_str2id(pool, from, 1);
293         solvable->evr = pool_str2id(pool, version, 1);
294         solvable->arch = ARCH_NOARCH;
295         if (type)
296           repodata_set_str(pd->data, pd->handle, SOLVABLE_PATCHCATEGORY, type);
297         pd->buildtime = (time_t)0;
298       }
299       break;
300       /* <id>FEDORA-2007-4594</id> */
301     case STATE_ID:
302       break;
303       /* <title>imlib-1.9.15-6.fc8</title> */
304     case STATE_TITLE:
305       break;
306       /* <release>Fedora 8</release> */
307     case STATE_RELEASE:
308       break;
309       /*  <issued date="2008-03-21 21:36:55"/>
310       */
311     case STATE_ISSUED:
312     case STATE_UPDATED:
313       {
314         const char *date = 0;
315         for (; *atts; atts += 2)
316           {
317             if (!strcmp(*atts, "date"))
318               date = atts[1];
319           }
320         if (date)
321           {
322             time_t t = datestr2timestamp(date);
323             if (t && t > pd->buildtime)
324               pd->buildtime = t;
325           }
326       }
327       break;
328     case STATE_REFERENCES:
329       break;
330       /*  <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=330471"
331        *             id="330471"
332        *             title="LDAP schema file missing for dhcpd"
333        *             type="bugzilla"/>
334        */
335     case STATE_REFERENCE:
336       {
337         const char *href = 0, *id = 0, *title = 0, *type = 0;
338         Id refhandle;
339         for (; *atts; atts += 2)
340           {
341             if (!strcmp(*atts, "href"))
342               href = atts[1];
343             else if (!strcmp(*atts, "id"))
344               id = atts[1];
345             else if (!strcmp(*atts, "title"))
346               title = atts[1];
347             else if (!strcmp(*atts, "type"))
348               type = atts[1];
349           }
350         refhandle = repodata_new_handle(pd->data);
351         if (href)
352           repodata_set_str(pd->data, refhandle, UPDATE_REFERENCE_HREF, href);
353         if (id)
354           repodata_set_str(pd->data, refhandle, UPDATE_REFERENCE_ID, id);
355         if (title)
356           repodata_set_str(pd->data, refhandle, UPDATE_REFERENCE_TITLE, title);
357         if (type)
358           repodata_set_poolstr(pd->data, refhandle, UPDATE_REFERENCE_TYPE, type);
359         repodata_add_flexarray(pd->data, pd->handle, UPDATE_REFERENCE, refhandle);
360       }
361       break;
362       /* <description>This update ...</description> */
363     case STATE_DESCRIPTION:
364       break;
365       /* <message type="confirm">This update ...</message> */
366     case STATE_MESSAGE:
367       break;
368     case STATE_PKGLIST:
369       break;
370       /* <collection short="F8" */
371     case STATE_COLLECTION:
372       break;
373       /* <name>Fedora 8</name> */
374     case STATE_NAME:
375       break;
376       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
377        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
378        *            version="1.9.15">
379        *
380        *
381        * -> patch.conflicts: {name} < {version}.{release}
382        */
383     case STATE_PACKAGE:
384       {
385         const char *arch = 0, *name = 0;
386         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
387         Id n, a = 0;
388         Id rel_id;
389
390         for (; *atts; atts += 2)
391           {
392             if (!strcmp(*atts, "arch"))
393               arch = atts[1];
394             else if (!strcmp(*atts, "name"))
395               name = atts[1];
396           }
397         /* generated Id for name */
398         n = pool_str2id(pool, name, 1);
399         rel_id = n;
400         if (arch)
401           {
402             /*  generate Id for arch and combine with name */
403             a = pool_str2id(pool, arch, 1);
404             rel_id = pool_rel2id(pool, n, a, REL_ARCH, 1);
405           }
406         rel_id = pool_rel2id(pool, rel_id, evr, REL_LT, 1);
407         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
408
409         /* who needs the collection anyway? */
410         pd->collhandle = repodata_new_handle(pd->data);
411         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_NAME, n);
412         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_EVR, evr);
413         if (a)
414           repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_ARCH, a);
415         break;
416       }
417       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
418       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
419     case STATE_FILENAME:
420       break;
421       /* <reboot_suggested>True</reboot_suggested> */
422     case STATE_REBOOT:
423       break;
424       /* <restart_suggested>True</restart_suggested> */
425     case STATE_RESTART:
426       break;
427       /* <relogin_suggested>True</relogin_suggested> */
428     case STATE_RELOGIN:
429       break;
430     default:
431       break;
432     }
433   return;
434 }
435
436
437 static void XMLCALL
438 endElement(void *userData, const char *name)
439 {
440   struct parsedata *pd = userData;
441   Pool *pool = pd->pool;
442   Solvable *s = pd->solvable;
443   Repo *repo = pd->repo;
444
445 #if 0
446       fprintf(stderr, "end: %s\n", name);
447 #endif
448   if (pd->depth != pd->statedepth)
449     {
450       pd->depth--;
451 #if 0
452       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
453 #endif
454       return;
455     }
456
457   pd->depth--;
458   pd->statedepth--;
459   switch (pd->state)
460     {
461     case STATE_START:
462       break;
463     case STATE_UPDATES:
464       break;
465     case STATE_UPDATE:
466       s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
467       if (pd->buildtime)
468         {
469           repodata_set_num(pd->data, pd->handle, SOLVABLE_BUILDTIME, pd->buildtime);
470           pd->buildtime = (time_t)0;
471         }
472       break;
473     case STATE_ID:
474       s->name = pool_str2id(pool, join2(&pd->jd, "patch", ":", pd->content), 1);
475       break;
476       /* <title>imlib-1.9.15-6.fc8</title> */
477     case STATE_TITLE:
478       while (pd->lcontent > 0 && pd->content[pd->lcontent - 1] == '\n')
479         pd->content[--pd->lcontent] = 0;
480       repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, pd->content);
481       break;
482     case STATE_SEVERITY:
483       repodata_set_poolstr(pd->data, pd->handle, UPDATE_SEVERITY, pd->content);
484       break;
485     case STATE_RIGHTS:
486       repodata_set_poolstr(pd->data, pd->handle, UPDATE_RIGHTS, pd->content);
487       break;
488       /*
489        * <release>Fedora 8</release>
490        */
491     case STATE_RELEASE:
492       break;
493     case STATE_ISSUED:
494       break;
495     case STATE_REFERENCES:
496       break;
497     case STATE_REFERENCE:
498       break;
499       /*
500        * <description>This update ...</description>
501        */
502     case STATE_DESCRIPTION:
503       repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, pd->content);
504       break;
505       /*
506        * <message>Warning! ...</message>
507        */
508     case STATE_MESSAGE:
509       repodata_set_str(pd->data, pd->handle, UPDATE_MESSAGE, pd->content);
510       break;
511     case STATE_PKGLIST:
512       break;
513     case STATE_COLLECTION:
514       break;
515     case STATE_NAME:
516       break;
517     case STATE_PACKAGE:
518       repodata_add_flexarray(pd->data, pd->handle, UPDATE_COLLECTION, pd->collhandle);
519       pd->collhandle = 0;
520       break;
521       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
522       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
523     case STATE_FILENAME:
524       repodata_set_str(pd->data, pd->collhandle, UPDATE_COLLECTION_FILENAME, pd->content);
525       break;
526       /* <reboot_suggested>True</reboot_suggested> */
527     case STATE_REBOOT:
528       if (pd->content[0] == 'T' || pd->content[0] == 't'|| pd->content[0] == '1')
529         {
530           /* FIXME: this is per-package, the global flag should be computed at runtime */
531           repodata_set_void(pd->data, pd->handle, UPDATE_REBOOT);
532           repodata_set_void(pd->data, pd->collhandle, UPDATE_REBOOT);
533         }
534       break;
535       /* <restart_suggested>True</restart_suggested> */
536     case STATE_RESTART:
537       if (pd->content[0] == 'T' || pd->content[0] == 't'|| pd->content[0] == '1')
538         {
539           /* FIXME: this is per-package, the global flag should be computed at runtime */
540           repodata_set_void(pd->data, pd->handle, UPDATE_RESTART);
541           repodata_set_void(pd->data, pd->collhandle, UPDATE_RESTART);
542         }
543       break;
544       /* <relogin_suggested>True</relogin_suggested> */
545     case STATE_RELOGIN:
546       if (pd->content[0] == 'T' || pd->content[0] == 't'|| pd->content[0] == '1')
547         {
548           /* FIXME: this is per-package, the global flag should be computed at runtime */
549           repodata_set_void(pd->data, pd->handle, UPDATE_RELOGIN);
550           repodata_set_void(pd->data, pd->collhandle, UPDATE_RELOGIN);
551         }
552       break;
553     default:
554       break;
555     }
556
557   pd->state = pd->sbtab[pd->state];
558   pd->docontent = 0;
559 }
560
561
562 static void XMLCALL
563 characterData(void *userData, const XML_Char *s, int len)
564 {
565   struct parsedata *pd = userData;
566   int l;
567   char *c;
568
569   if (!pd->docontent)
570     {
571 #if 0
572       fprintf(stderr, "Content: [%d]'%.*s'\n", pd->state, len, s);
573 #endif
574       return;
575     }
576   l = pd->lcontent + len + 1;
577   if (l > pd->acontent)
578     {
579       pd->content = realloc(pd->content, l + 256);
580       pd->acontent = l + 256;
581     }
582   c = pd->content + pd->lcontent;
583   pd->lcontent += len;
584   while (len-- > 0)
585     *c++ = *s++;
586   *c = 0;
587 }
588
589
590 #define BUFF_SIZE 8192
591
592 int
593 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
594 {
595   Pool *pool = repo->pool;
596   struct parsedata pd;
597   char buf[BUFF_SIZE];
598   int i, l;
599   struct stateswitch *sw;
600   Repodata *data;
601   XML_Parser parser;
602
603   data = repo_add_repodata(repo, flags);
604
605   memset(&pd, 0, sizeof(pd));
606   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
607     {
608       if (!pd.swtab[sw->from])
609         pd.swtab[sw->from] = sw;
610       pd.sbtab[sw->to] = sw->from;
611     }
612   pd.pool = pool;
613   pd.repo = repo;
614   pd.data = data;
615
616   pd.content = malloc(256);
617   pd.acontent = 256;
618   pd.lcontent = 0;
619   parser = XML_ParserCreate(NULL);
620   XML_SetUserData(parser, &pd);
621   XML_SetElementHandler(parser, startElement, endElement);
622   XML_SetCharacterDataHandler(parser, characterData);
623   for (;;)
624     {
625       l = fread(buf, 1, sizeof(buf), fp);
626       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
627         {
628           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));
629           break;
630         }
631       if (l == 0)
632         break;
633     }
634   XML_ParserFree(parser);
635   free(pd.content);
636   join_freemem(&pd.jd);
637
638   if (!(flags & REPO_NO_INTERNALIZE))
639     repodata_internalize(data);
640   return pd.ret;
641 }
642