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