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