- make peace with gcc46
[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   const char *p;
131   struct tm tm;
132
133   if (!date || !*date)
134     return 0;
135   for (p = date; *p >= '0' && *p <= '9'; p++)
136     ;
137   if (!*p)
138     return atoi(date);
139   memset(&tm, 0, sizeof(tm));
140   if (!strptime(date, "%F%T", &tm))
141     return 0;
142   return timegm(&tm);
143 }
144
145 /*
146  * if we have seen a <filename>...
147  * inside of <package>...
148  *
149  *
150  * If not, we must insert an empty filename to UPDATE_COLLECTION_FILENAME
151  * at </package> in order to keep all UPDATE_COLLECTION_* arrays in sync
152  */
153
154 /*
155  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
156  */
157
158 static Id
159 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
160 {
161   const char *e, *v, *r, *v2;
162   char *c;
163   int l;
164
165   e = v = r = 0;
166   for (; *atts; atts += 2)
167     {
168       if (!strcmp(*atts, "epoch"))
169         e = atts[1];
170       else if (!strcmp(*atts, "version"))
171         v = atts[1];
172       else if (!strcmp(*atts, "release"))
173         r = atts[1];
174     }
175   if (e && !strcmp(e, "0"))
176     e = 0;
177   if (v && !e)
178     {
179       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
180         ;
181       if (v2 > v && *v2 == ':')
182         e = "0";
183     }
184   l = 1;
185   if (e)
186     l += strlen(e) + 1;
187   if (v)
188     l += strlen(v);
189   if (r)
190     l += strlen(r) + 1;
191   if (l > pd->acontent)
192     {
193       pd->content = realloc(pd->content, l + 256);
194       pd->acontent = l + 256;
195     }
196   c = pd->content;
197   if (e)
198     {
199       strcpy(c, e);
200       c += strlen(c);
201       *c++ = ':';
202     }
203   if (v)
204     {
205       strcpy(c, v);
206       c += strlen(c);
207     }
208   if (r)
209     {
210       *c++ = '-';
211       strcpy(c, r);
212       c += strlen(c);
213     }
214   *c = 0;
215   if (!*pd->content)
216     return 0;
217 #if 0
218   fprintf(stderr, "evr: %s\n", pd->content);
219 #endif
220   return str2id(pool, pd->content, 1);
221 }
222
223
224
225 static void XMLCALL
226 startElement(void *userData, const char *name, const char **atts)
227 {
228   struct parsedata *pd = userData;
229   Pool *pool = pd->pool;
230   Solvable *solvable = pd->solvable;
231   struct stateswitch *sw;
232   /*const char *str; */
233
234 #if 0
235   fprintf(stderr, "start: [%d]%s\n", pd->state, name);
236 #endif
237   if (pd->depth != pd->statedepth)
238     {
239       pd->depth++;
240       return;
241     }
242
243   pd->depth++;
244   if (!pd->swtab[pd->state])
245     return;
246   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
247     if (!strcmp(sw->ename, name))
248       break;
249
250   if (sw->from != pd->state)
251     {
252 #if 0
253       fprintf(stderr, "into unknown: %s (from: %d)\n", name, pd->state);
254       exit( 1 );
255 #endif
256       return;
257     }
258   pd->state = sw->to;
259   pd->docontent = sw->docontent;
260   pd->statedepth = pd->depth;
261   pd->lcontent = 0;
262   *pd->content = 0;
263
264   switch(pd->state)
265     {
266     case STATE_START:
267       break;
268     case STATE_UPDATES:
269       break;
270       /*
271        * <update from="rel-eng@fedoraproject.org"
272        *         status="stable"
273        *         type="bugfix" (enhancement, security)
274        *         version="1.4">
275        */
276     case STATE_UPDATE:
277       {
278         const char *from = 0, *type = 0, *version = 0;
279         for (; *atts; atts += 2)
280           {
281             if (!strcmp(*atts, "from"))
282               from = 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             time_t t = datestr2timestamp(date);
322             if (t)
323               repodata_set_num(pd->data, pd->datanum, SOLVABLE_BUILDTIME, t);
324           }
325       }
326       break;
327     case STATE_REFERENCES:
328       break;
329       /*  <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=330471"
330        *             id="330471"
331        *             title="LDAP schema file missing for dhcpd"
332        *             type="bugzilla"/>
333        */
334     case STATE_REFERENCE:
335       {
336         const char *href = 0, *id = 0, *title = 0, *type = 0;
337         Id handle;
338         for (; *atts; atts += 2)
339           {
340             if (!strcmp(*atts, "href"))
341               href = atts[1];
342             else if (!strcmp(*atts, "id"))
343               id = atts[1];
344             else if (!strcmp(*atts, "title"))
345               title = atts[1];
346             else if (!strcmp(*atts, "type"))
347               type = atts[1];
348           }
349         handle = repodata_new_handle(pd->data);
350         if (href)
351           repodata_set_str(pd->data, handle, UPDATE_REFERENCE_HREF, href);
352         if (id)
353           repodata_set_str(pd->data, handle, UPDATE_REFERENCE_ID, id);
354         if (title)
355           repodata_set_str(pd->data, handle, UPDATE_REFERENCE_TITLE, title);
356         if (type)
357           repodata_set_poolstr(pd->data, handle, UPDATE_REFERENCE_TYPE, type);
358         repodata_add_flexarray(pd->data, pd->datanum, UPDATE_REFERENCE, handle);
359       }
360       break;
361       /* <description>This update ...</description> */
362     case STATE_DESCRIPTION:
363       break;
364       /* <message type="confirm">This update ...</message> */
365     case STATE_MESSAGE:
366       break;
367     case STATE_PKGLIST:
368       break;
369       /* <collection short="F8" */
370     case STATE_COLLECTION:
371       break;
372       /* <name>Fedora 8</name> */
373     case STATE_NAME:
374       break;
375       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
376        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
377        *            version="1.9.15">
378        *
379        *
380        * -> patch.conflicts: {name} < {version}.{release}
381        */
382     case STATE_PACKAGE:
383       {
384         const char *arch = 0, *name = 0;
385         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
386         Id n, a = 0;
387         Id rel_id;
388
389         for (; *atts; atts += 2)
390           {
391             if (!strcmp(*atts, "arch"))
392               arch = atts[1];
393             else if (!strcmp(*atts, "name"))
394               name = 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 */