forward ports from 11.0 branch
[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   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   { NUMSTATES }
99 };
100
101 struct parsedata {
102   int depth;
103   enum state state;
104   int statedepth;
105   char *content;
106   int lcontent;
107   int acontent;
108   int docontent;
109   Pool *pool;
110   Repo *repo;
111   Repodata *data;
112   unsigned int datanum;
113   Solvable *solvable;
114   unsigned int timestamp;
115   
116
117   struct stateswitch *swtab[NUMSTATES];
118   enum state sbtab[NUMSTATES];
119   char *tempstr;
120   int ltemp;
121   int atemp;
122 };
123
124 /*
125  * if we have seen a <filename>...
126  * inside of <package>...
127  * 
128  *
129  * If not, we must insert an empty filename to UPDATE_COLLECTION_FILENAME
130  * at </package> in order to keep all UPDATE_COLLECTION_* arrays in sync
131  */
132
133 static int package_filename_seen = 0;
134 static int package_flags = 0; /* same for reboot/restart flags, to be written at </package> */
135
136
137 /*
138  * find attribute
139  */
140
141 /*
142 static const char *
143 find_attr(const char *txt, const char **atts)
144 {
145   for (; *atts; atts += 2)
146     {
147       if (!strcmp(*atts, txt))
148         return atts[1];
149     }
150   return 0;
151 }
152 */
153
154
155 /*
156  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
157  */
158
159 static Id
160 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
161 {
162   const char *e, *v, *r, *v2;
163   char *c;
164   int l;
165
166   e = v = r = 0;
167   for (; *atts; atts += 2)
168     {
169       if (!strcmp(*atts, "epoch"))
170         e = atts[1];
171       else if (!strcmp(*atts, "version"))
172         v = atts[1];
173       else if (!strcmp(*atts, "release"))
174         r = atts[1];
175     }
176   if (e && !strcmp(e, "0"))
177     e = 0;
178   if (v && !e)
179     {
180       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
181         ;
182       if (v2 > v && *v2 == ':')
183         e = "0";
184     }
185   l = 1;
186   if (e)
187     l += strlen(e) + 1;
188   if (v)
189     l += strlen(v);
190   if (r)
191     l += strlen(r) + 1;
192   if (l > pd->acontent)
193     {
194       pd->content = realloc(pd->content, l + 256);
195       pd->acontent = l + 256;
196     }
197   c = pd->content;
198   if (e)
199     {
200       strcpy(c, e);
201       c += strlen(c);
202       *c++ = ':';
203     }
204   if (v)
205     {
206       strcpy(c, v);
207       c += strlen(c);
208     }
209   if (r)
210     {
211       *c++ = '-';
212       strcpy(c, r);
213       c += strlen(c);
214     }
215   *c = 0;
216   if (!*pd->content)
217     return 0;
218 #if 0
219   fprintf(stderr, "evr: %s\n", pd->content);
220 #endif
221   return str2id(pool, pd->content, 1);
222 }
223
224
225
226 static void XMLCALL
227 startElement(void *userData, const char *name, const char **atts)
228 {
229   struct parsedata *pd = userData;
230   Pool *pool = pd->pool;
231   Solvable *solvable = pd->solvable;
232   struct stateswitch *sw;
233   /*const char *str; */
234
235 #if 0
236       fprintf(stderr, "start: [%d]%s\n", pd->state, name);
237 #endif
238   if (pd->depth != pd->statedepth)
239     {
240       pd->depth++;
241       return;
242     }
243
244   pd->depth++;
245   for (sw = pd->swtab[pd->state]; sw->from == pd->state; sw++)  /* find name in statetable */
246     if (!strcmp(sw->ename, name))
247       break;
248   
249
250   if (sw->from != pd->state)
251     {
252 #if 1
253       fprintf(stderr, "into unknown: [%d]%s (from: %d)\n", sw->to, name, sw->from);
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, *status = 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, "status"))
284             status = atts[1];
285           else if (!strcmp(*atts, "type"))
286             type = atts[1];
287           else if (!strcmp(*atts, "version"))
288             version = atts[1];
289         }
290         
291
292         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
293         pd->datanum = (pd->solvable - pool->solvables) - pd->repo->start;
294         repodata_extend(pd->data, pd->solvable - pool->solvables);      
295         repodata_extend(pd->data, pd->solvable - pool->solvables);
296         pd->datanum = repodata_get_handle(pd->data, pd->datanum);
297         
298
299         solvable->vendor = str2id(pool, from, 1);
300         solvable->evr = str2id(pool, version, 1);
301         solvable->arch = ARCH_NOARCH;
302         repodata_set_str(pd->data, pd->datanum, SOLVABLE_PATCHCATEGORY, type);
303       }
304       break;
305       /* <id>FEDORA-2007-4594</id> */
306       case STATE_ID:
307       break;
308       /* <title>imlib-1.9.15-6.fc8</title> */
309       case STATE_TITLE:
310       break;
311       /* <release>Fedora 8</release> */
312       case STATE_RELEASE:
313       break;
314       /*  <issued date="2008-03-21 21:36:55"/>
315       */
316       case STATE_ISSUED:
317       {
318         const char *date = 0;
319         for (; *atts; atts += 2)
320         {
321           if (!strcmp(*atts, "date"))
322             date = atts[1];
323         }
324         repodata_set_str(pd->data, pd->datanum, SOLVABLE_BUILDTIME, date);
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         for (; *atts; atts += 2)
338         {
339           if (!strcmp(*atts, "href"))
340             href = atts[1];
341           else if (!strcmp(*atts, "id"))
342             id = atts[1];
343           else if (!strcmp(*atts, "title"))
344             title = atts[1];
345           else if (!strcmp(*atts, "type"))
346             type = atts[1];
347         }
348 #if DO_ARRAY
349         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_HREF, href);
350         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_ID, id);
351         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_TITLE, title);
352         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_REFERENCE_TYPE, type);
353 #endif
354       }
355       break;
356       /* <description>This update ...</description> */
357       case STATE_DESCRIPTION:
358       break;
359       /* <message type="confirm">This update ...</message> */
360       case STATE_MESSAGE:
361       break;
362       case STATE_PKGLIST:
363       break;
364       /* <collection short="F8"> */
365       case STATE_COLLECTION:
366       break;
367       /* <name>Fedora 8</name> */ 
368       case STATE_NAME:
369       break;
370       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
371        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
372        *            version="1.9.15">
373        * 
374        *
375        * -> patch.conflicts: {name} < {version}.{release}
376        */
377       case STATE_PACKAGE:
378       {
379         const char *arch = 0, *name = 0, *src = 0;
380         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
381         Id n, a, na;
382         Id rel_id;
383         
384
385         /* reset package_* markers, to be evaluated at </package> */
386         package_filename_seen = 0;
387         package_flags = 0;
388         
389
390         for (; *atts; atts += 2)
391         {
392           if (!strcmp(*atts, "arch"))
393             arch = atts[1];
394           else if (!strcmp(*atts, "name"))
395             name = atts[1];
396           else if (!strcmp(*atts, "src"))
397             src = atts[1];
398         }
399         /* generated Ids for name and arch */
400         n = str2id(pool, name, 1);
401         if (arch)
402           a = str2id(pool, arch, 1);
403         else
404           a = ARCH_NOARCH;
405         /*  now combine both to a single Id */
406         na = rel2id(pool, n, a, REL_ARCH, 1);
407         
408
409         rel_id = rel2id(pool, na, evr, REL_LT, 1);
410
411         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
412 #if DO_ARRAY
413         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_NAME, n);
414         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_EVR, evr);
415         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_ARCH, a);
416 #else
417         /* _FILENAME and _FLAGS are written at </package> */
418         if (1) {
419           const char *evrstr = id2str(pool, evr);
420           int buflen = strlen(name) + 1 + strlen(evrstr) + 1 + strlen(arch?arch:"") + 1;
421           char *buf;
422           if (!arch) arch = "";
423           buf = (char *)malloc(buflen);
424           if (!buf) exit(1);
425           sprintf(buf, "%s %s %s", name, evrstr, arch);
426           repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION, buf);
427           free(buf);
428         }
429 #endif
430       }
431       break;
432       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
433       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
434       case STATE_FILENAME:
435       break;
436       /* <reboot_suggested>True</reboot_suggested> */
437       case STATE_REBOOT:
438       break;
439       /* <restart_suggested>True</restart_suggested> */
440       case STATE_RESTART:
441       break;
442       case NUMSTATES+1:
443         split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...}  Urgs!*/
444       break;
445       default:
446       break;
447     }
448   return;
449 }
450
451
452 static void XMLCALL
453 endElement(void *userData, const char *name)
454 {
455   struct parsedata *pd = userData;
456   Pool *pool = pd->pool;
457   Solvable *s = pd->solvable;
458   Repo *repo = pd->repo;
459
460 #if 0
461       fprintf(stderr, "end: %s\n", name);
462 #endif
463   if (pd->depth != pd->statedepth)
464     {
465       pd->depth--;
466 #if 1
467       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
468 #endif
469       return;
470     }
471
472   pd->depth--;
473   pd->statedepth--;
474   switch (pd->state)
475     {
476       case STATE_START:
477       break;
478       case STATE_UPDATES:
479       break;
480       case STATE_UPDATE:
481       s->provides = repo_addid_dep(repo, s->provides, rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
482       break;
483       case STATE_ID:
484       {
485         if (pd->content) {
486           s->name = str2id(pool, join2("patch", ":", pd->content), 1);
487         }
488       }
489       break;
490       /* <title>imlib-1.9.15-6.fc8</title> */
491       case STATE_TITLE:
492       {
493         while (pd->lcontent > 0
494                && *(pd->content + pd->lcontent - 1) == '\n')
495         {
496           --pd->lcontent;
497           *(pd->content + pd->lcontent) = 0;
498         }
499         repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
500       }
501       break;
502       /*
503        * <release>Fedora 8</release>
504        */
505       case STATE_RELEASE:
506       break;
507       case STATE_ISSUED:
508       break;
509       case STATE_REFERENCES:
510       break;
511       case STATE_REFERENCE:
512       break;
513       /*
514        * <description>This update ...</description>
515        */
516       case STATE_DESCRIPTION:
517       {
518         repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
519       }
520       break;   
521       break;
522       /*
523        * <message>Warning! ...</message>
524        */
525       case STATE_MESSAGE:
526       {
527         repodata_set_str(pd->data, pd->datanum, UPDATE_MESSAGE, pd->content);
528       }
529       break;
530       case STATE_PKGLIST:
531       break;
532       case STATE_COLLECTION:
533       break;
534       case STATE_NAME:
535       break;
536       case STATE_PACKAGE:
537       {
538 #if DO_ARRAY
539         /* write _FILENAME and _FLAGS at </package>
540          * to ensure all UPDATE_COLLECTION_* arrays are filled in parallel
541          */
542         if (!package_filename_seen)
543         {
544           repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION_FILENAME, "");
545         }
546         repodata_add_idarray(pd->data, pd->datanum, UPDATE_COLLECTION_FLAGS, package_flags+1);
547 #endif
548       }
549       break;
550       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
551       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
552       case STATE_FILENAME:
553       {
554 #if DO_ARRAY
555         repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION_FILENAME, pd->content);
556         package_filename_seen = 1;
557 #endif
558       }
559       break;
560       /* <reboot_suggested>True</reboot_suggested> */
561       case STATE_REBOOT:
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_REBOOT);
569           package_flags = 1;
570         }
571       }
572       break;
573       /* <restart_suggested>True</restart_suggested> */
574       case STATE_RESTART:
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_RESTART);
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 */