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