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