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