update repository matching
[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 /*
140  * find attribute
141  */
142
143 static const char *
144 find_attr(const char *txt, const char **atts)
145 {
146   for (; *atts; atts += 2)
147     {
148       if (!strcmp(*atts, txt))
149         return atts[1];
150     }
151   return 0;
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   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
249   if (sw->from != pd->state)
250     {
251 #if 1
252       fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
253       exit( 1 );
254 #endif
255       return;
256     }
257   pd->state = sw->to;
258   pd->docontent = sw->docontent;
259   pd->statedepth = pd->depth;
260   pd->lcontent = 0;
261   *pd->content = 0;
262
263   switch(pd->state)
264     {
265       case STATE_START:
266       break;
267       case STATE_UPDATES:
268       break;
269       /*
270        * <update from="rel-eng@fedoraproject.org"
271        *         status="stable"
272        *         type="bugfix" (enhancement, security)
273        *         version="1.4">
274        */
275       case STATE_UPDATE:
276       {
277         const char *from = 0, *status = 0, *type = 0, *version = 0;
278         for (; *atts; atts += 2)
279         {
280           if (!strcmp(*atts, "from"))
281             from = atts[1];
282           else if (!strcmp(*atts, "status"))
283             status = atts[1];
284           else if (!strcmp(*atts, "type"))
285             type = atts[1];
286           else if (!strcmp(*atts, "version"))
287             version = atts[1];
288         }
289         
290
291         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
292         pd->datanum = (pd->solvable - pool->solvables) - pd->repo->start;
293         repodata_extend(pd->data, pd->solvable - pool->solvables);      
294         repodata_extend(pd->data, pd->solvable - pool->solvables);
295         pd->datanum = repodata_get_handle(pd->data, pd->datanum);
296         
297
298         solvable->vendor = str2id(pool, from, 1);
299         solvable->evr = str2id(pool, version, 1);
300         solvable->arch = ARCH_NOARCH;
301         repodata_set_str(pd->data, pd->datanum, SOLVABLE_PATCHCATEGORY, type);
302       }
303       break;
304       /* <id>FEDORA-2007-4594</id> */
305       case STATE_ID:
306       break;
307       /* <title>imlib-1.9.15-6.fc8</title> */
308       case STATE_TITLE:
309       break;
310       /* <release>Fedora 8</release> */
311       case STATE_RELEASE:
312       break;
313       /*  <issued date="2008-03-21 21:36:55"/>
314       */
315       case STATE_ISSUED:
316       {
317         const char *date = 0;
318         for (; *atts; atts += 2)
319         {
320           if (!strcmp(*atts, "date"))
321             date = atts[1];
322         }
323         repodata_set_str(pd->data, pd->datanum, SOLVABLE_BUILDTIME, date);
324       }
325       break;
326       case STATE_REFERENCES:
327       break;
328       /*  <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=330471"
329        *             id="330471"
330        *             title="LDAP schema file missing for dhcpd"
331        *             type="bugzilla"/>
332        */
333       case STATE_REFERENCE:
334       {
335         const char *href = 0, *id = 0, *title = 0, *type = 0;
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 #if DO_ARRAY
348         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_HREF, href);
349         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_ID, id);
350         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_TITLE, title);
351         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_TYPE, type);
352 #endif
353       }
354       break;
355       /* <description>This update ...</description> */
356       case STATE_DESCRIPTION:
357       break;
358       /* <message type="confirm">This update ...</message> */
359       case STATE_MESSAGE:
360       break;
361       case STATE_PKGLIST:
362       break;
363       /* <collection short="F8" id="PRODUCT0001444"> */
364       case STATE_COLLECTION:
365         {
366           /* insert a REPOSITORY_UPDATES for every present collection id */
367           const char *cid;
368           if ((cid = find_attr("id", atts)))
369             repodata_add_poolstr_array(pd->data, -1, REPOSITORY_UPDATES, cid);
370           break;
371         }
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, *src = 0;
385         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
386         Id n, a, na;
387         Id rel_id;
388         
389
390         /* reset package_* markers, to be evaluated at </package> */
391         package_filename_seen = 0;
392         package_flags = 0;
393         
394
395         for (; *atts; atts += 2)
396         {
397           if (!strcmp(*atts, "arch"))
398             arch = atts[1];
399           else if (!strcmp(*atts, "name"))
400             name = atts[1];
401           else if (!strcmp(*atts, "src"))
402             src = atts[1];
403         }
404         /* generated Ids for name and arch */
405         n = str2id(pool, name, 1);
406         if (arch)
407           a = str2id(pool, arch, 1);
408         else
409           a = ARCH_NOARCH;
410         /*  now combine both to a single Id */
411         na = rel2id(pool, n, a, REL_ARCH, 1);
412         
413
414         rel_id = rel2id(pool, na, evr, REL_LT, 1);
415
416         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
417 #if DO_ARRAY
418         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_NAME, n);
419         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_EVR, evr);
420         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_ARCH, a);
421 #else
422         /* _FILENAME and _FLAGS are written at </package> */
423         if (1) {
424           const char *evrstr = id2str(pool, evr);
425           int buflen = strlen(name) + 1 + strlen(evrstr) + 1 + strlen(arch?arch:"") + 1;
426           char *buf;
427           if (!arch) arch = "";
428           buf = (char *)malloc(buflen);
429           if (!buf) exit(1);
430           sprintf(buf, "%s %s %s", name, evrstr, arch);
431           repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION, buf);
432           free(buf);
433         }
434 #endif
435       }
436       break;
437       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
438       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
439       case STATE_FILENAME:
440       break;
441       /* <reboot_suggested>True</reboot_suggested> */
442       case STATE_REBOOT:
443       break;
444       /* <restart_suggested>True</restart_suggested> */
445       case STATE_RESTART:
446       break;
447       /* <relogin_suggested>True</relogin_suggested> */
448       case STATE_RELOGIN:
449       break;
450       case NUMSTATES+1:
451         split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...}  Urgs!*/
452       break;
453       default:
454       break;
455     }
456   return;
457 }
458
459
460 static void XMLCALL
461 endElement(void *userData, const char *name)
462 {
463   struct parsedata *pd = userData;
464   Pool *pool = pd->pool;
465   Solvable *s = pd->solvable;
466   Repo *repo = pd->repo;
467
468 #if 0
469       fprintf(stderr, "end: %s\n", name);
470 #endif
471   if (pd->depth != pd->statedepth)
472     {
473       pd->depth--;
474 #if 1
475       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
476 #endif
477       return;
478     }
479
480   pd->depth--;
481   pd->statedepth--;
482   switch (pd->state)
483     {
484       case STATE_START:
485       break;
486       case STATE_UPDATES:
487       break;
488       case STATE_UPDATE:
489       s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
490       break;
491       case STATE_ID:
492       {
493         if (pd->content) {
494           s->name = str2id(pool, join2("patch", ":", pd->content), 1);
495         }
496       }
497       break;
498       /* <title>imlib-1.9.15-6.fc8</title> */
499       case STATE_TITLE:
500       {
501         while (pd->lcontent > 0
502                && *(pd->content + pd->lcontent - 1) == '\n')
503         {
504           --pd->lcontent;
505           *(pd->content + pd->lcontent) = 0;
506         }
507         repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
508       }
509       break;
510       /*
511        * <release>Fedora 8</release>
512        */
513       case STATE_RELEASE:
514       break;
515       case STATE_ISSUED:
516       break;
517       case STATE_REFERENCES:
518       break;
519       case STATE_REFERENCE:
520       break;
521       /*
522        * <description>This update ...</description>
523        */
524       case STATE_DESCRIPTION:
525       {
526         repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
527       }
528       break;   
529       break;
530       /*
531        * <message>Warning! ...</message>
532        */
533       case STATE_MESSAGE:
534       {
535         repodata_set_str(pd->data, pd->datanum, UPDATE_MESSAGE, pd->content);
536       }
537       break;
538       case STATE_PKGLIST:
539       break;
540       case STATE_COLLECTION:
541       break;
542       case STATE_NAME:
543       break;
544       case STATE_PACKAGE:
545       {
546 #if DO_ARRAY
547         /* write _FILENAME and _FLAGS at </package>
548          * to ensure all UPDATE_COLLECTION_* arrays are filled in parallel
549          */
550         if (!package_filename_seen)
551         {
552           repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION_FILENAME, "");
553         }
554         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_FLAGS, package_flags+1);
555 #endif
556       }
557       break;
558       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
559       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
560       case STATE_FILENAME:
561       {
562 #if DO_ARRAY
563         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION_FILENAME, pd->content);
564         package_filename_seen = 1;
565 #endif
566       }
567       break;
568       /* <reboot_suggested>True</reboot_suggested> */
569       case STATE_REBOOT:
570       {
571         if (pd->content
572             && (pd->content[0] == 'T'
573                 || pd->content[0] == 't'|| pd->content[0] == '1'))
574         {
575           /* FIXME: this is per-package, the global flag should be computed at runtime */
576           repodata_set_void(pd->data, pd->datanum, UPDATE_REBOOT);
577           package_flags = 1;
578         }
579       }
580       break;
581       /* <restart_suggested>True</restart_suggested> */
582       case STATE_RESTART:
583       {
584         if (pd->content
585             && (pd->content[0] == 'T'
586                 || pd->content[0] == 't' || pd->content[0] == '1'))
587         {
588           /* FIXME: this is per-package, the global flag should be computed at runtime */
589           repodata_set_void(pd->data, pd->datanum, UPDATE_RESTART);
590           package_flags = 2;
591         }
592       }
593       break;
594       /* <relogin_suggested>True</relogin_suggested> */
595       case STATE_RELOGIN:
596       {
597         if (pd->content
598             && (pd->content[0] == 'T'
599                 || pd->content[0] == 't' || pd->content[0] == '1'))
600         {
601           /* FIXME: this is per-package, the global flag should be computed at runtime */
602           repodata_set_void(pd->data, pd->datanum, UPDATE_RELOGIN);
603           package_flags = 2;
604         }
605       }
606       break;
607       default:
608       break;
609     }
610
611   pd->state = pd->sbtab[pd->state];
612   pd->docontent = 0;
613   
614
615   return;
616 }
617
618
619 static void XMLCALL
620 characterData(void *userData, const XML_Char *s, int len)
621 {
622   struct parsedata *pd = userData;
623   int l;
624   char *c;
625   if (!pd->docontent) {
626 #if 0
627     char *dup = strndup( s, len );
628   fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
629   free( dup );
630 #endif
631     return;
632   }
633   l = pd->lcontent + len + 1;
634   if (l > pd->acontent)
635     {
636       pd->content = realloc(pd->content, l + 256);
637       pd->acontent = l + 256;
638     }
639   c = pd->content + pd->lcontent;
640   pd->lcontent += len;
641   while (len-- > 0)
642     *c++ = *s++;
643   *c = 0;
644 }
645
646
647 #define BUFF_SIZE 8192
648
649 void
650 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
651 {
652   Pool *pool = repo->pool;
653   struct parsedata pd;
654   char buf[BUFF_SIZE];
655   int i, l;
656   struct stateswitch *sw;
657
658   memset(&pd, 0, sizeof(pd));
659   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
660     {
661       if (!pd.swtab[sw->from])
662         pd.swtab[sw->from] = sw;
663       pd.sbtab[sw->to] = sw->from;
664     }
665   pd.pool = pool;
666   pd.repo = repo;
667   pd.data = repo_add_repodata(pd.repo, 0);
668
669   pd.content = malloc(256);
670   pd.acontent = 256;
671   pd.lcontent = 0;
672   pd.tempstr = malloc(256);
673   pd.atemp = 256;
674   pd.ltemp = 0;
675   XML_Parser parser = XML_ParserCreate(NULL);
676   XML_SetUserData(parser, &pd);
677   XML_SetElementHandler(parser, startElement, endElement);
678   XML_SetCharacterDataHandler(parser, characterData);
679   for (;;)
680     {
681       l = fread(buf, 1, sizeof(buf), fp);
682       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
683         {
684           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));
685           exit(1);
686         }
687       if (l == 0)
688         break;
689     }
690   XML_ParserFree(parser);
691
692   if (pd.data)
693     repodata_internalize(pd.data);
694
695   free(pd.content);
696   join_freemem();
697 }
698
699 /* EOF */