cb8a7e34b9e6de719bdc5e220446d666c6d13d35
[platform/upstream/libsolv.git] / tools / repo_updateinfoxml.c
1
2 /*
3  * Copyright (c) 2007, Novell Inc.
4  *
5  * This program is licensed under the BSD license, read LICENSE.BSD
6  * for further information
7  */
8
9 #define _GNU_SOURCE
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <expat.h>
17
18 #include "pool.h"
19 #include "repo.h"
20 #include "repo_updateinfoxml.h"
21 #include "tools_util.h"
22
23 //#define TESTMM
24
25 /*
26  * <updates>
27  *   <update from="rel-eng@fedoraproject.org" status="stable" type="security" version="1.4">
28  *     <id>FEDORA-2007-4594</id>
29  *     <title>imlib-1.9.15-6.fc8</title>
30  *     <release>Fedora 8</release>
31  *     <issued date="2007-12-28 16:42:30"/>
32  *     <references>
33  *       <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"/>
34  *     </references>
35  *     <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>
36  *     <pkglist>
37  *       <collection short="F8">
38  *         <name>Fedora 8</name>
39  *         <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">
40  *           <filename>imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm</filename>
41  *           <reboot_suggested>True</reboot_suggested>
42  *         </package>
43  *       </collection>
44  *     </pkglist>
45  *   </update>
46  * </updates>
47 */
48
49 enum state {
50   STATE_START,
51   STATE_UPDATES,      /* 1 */
52   STATE_UPDATE,       /* 2 */
53   STATE_ID,           /* 3 */
54   STATE_TITLE,        /* 4 */
55   STATE_RELEASE,      /* 5 */
56   STATE_ISSUED,       /* 6 */
57   STATE_REFERENCES,   /* 7 */
58   STATE_REFERENCE,    /* 8 */
59   STATE_DESCRIPTION,  /* 9 */
60   STATE_PKGLIST,     /* 10 */
61   STATE_COLLECTION,  /* 11 */
62   STATE_NAME,        /* 12 */
63   STATE_PACKAGE,     /* 13 */
64   STATE_FILENAME,    /* 14 */
65   STATE_REBOOT,      /* 15 */
66   STATE_RESTART,     /* 16 */
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,      "references",      STATE_REFERENCES,  0 },
89   { STATE_UPDATE,      "pkglist",         STATE_PKGLIST,     0 },
90   { STATE_REFERENCES,  "reference",       STATE_REFERENCE,   0 },
91   { STATE_PKGLIST,     "collection",      STATE_COLLECTION,  0 },
92   { STATE_COLLECTION,  "name",            STATE_NAME,        1 },
93   { STATE_COLLECTION,  "package",         STATE_PACKAGE,     0 },
94   { STATE_PACKAGE,     "filename",        STATE_FILENAME,    1 },
95   { STATE_PACKAGE,     "reboot_suggested",STATE_REBOOT,      1 },
96   { STATE_PACKAGE,     "restart_suggested",STATE_RESTART,    1 },
97   { NUMSTATES }
98 };
99
100 struct parsedata {
101   int depth;
102   enum state state;
103   int statedepth;
104   char *content;
105   int lcontent;
106   int acontent;
107   int docontent;
108   Pool *pool;
109   Repo *repo;
110   Repodata *data;
111   unsigned int datanum;
112   Solvable *solvable;
113   unsigned int timestamp;
114   
115   struct stateswitch *swtab[NUMSTATES];
116   enum state sbtab[NUMSTATES];
117   char *tempstr;
118   int ltemp;
119   int atemp;
120 };
121
122 /*
123  * find attribute
124  */
125
126 /*
127 static const char *
128 find_attr(const char *txt, const char **atts)
129 {
130   for (; *atts; atts += 2)
131     {
132       if (!strcmp(*atts, txt))
133         return atts[1];
134     }
135   return 0;
136 }
137 */
138
139
140 /*
141  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
142  */
143
144 static Id
145 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
146 {
147   const char *e, *v, *r, *v2;
148   char *c;
149   int l;
150
151   e = v = r = 0;
152   for (; *atts; atts += 2)
153     {
154       if (!strcmp(*atts, "epoch"))
155         e = atts[1];
156       else if (!strcmp(*atts, "version"))
157         v = atts[1];
158       else if (!strcmp(*atts, "release"))
159         r = atts[1];
160     }
161   if (e && !strcmp(e, "0"))
162     e = 0;
163   if (v && !e)
164     {
165       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
166         ;
167       if (v2 > v && *v2 == ':')
168         e = "0";
169     }
170   l = 1;
171   if (e)
172     l += strlen(e) + 1;
173   if (v)
174     l += strlen(v);
175   if (r)
176     l += strlen(r) + 1;
177   if (l > pd->acontent)
178     {
179       pd->content = realloc(pd->content, l + 256);
180       pd->acontent = l + 256;
181     }
182   c = pd->content;
183   if (e)
184     {
185       strcpy(c, e);
186       c += strlen(c);
187       *c++ = ':';
188     }
189   if (v)
190     {
191       strcpy(c, v);
192       c += strlen(c);
193     }
194   if (r)
195     {
196       *c++ = '-';
197       strcpy(c, r);
198       c += strlen(c);
199     }
200   *c = 0;
201   if (!*pd->content)
202     return 0;
203 #if 0
204   fprintf(stderr, "evr: %s\n", pd->content);
205 #endif
206   return str2id(pool, pd->content, 1);
207 }
208
209
210
211 static void XMLCALL
212 startElement(void *userData, const char *name, const char **atts)
213 {
214   struct parsedata *pd = userData;
215   Pool *pool = pd->pool;
216   Solvable *solvable = pd->solvable;
217   struct stateswitch *sw;
218   /*const char *str; */
219
220 #if 0
221       fprintf(stderr, "start: [%d]%s\n", pd->state, name);
222 #endif
223   if (pd->depth != pd->statedepth)
224     {
225       pd->depth++;
226       return;
227     }
228
229   pd->depth++;
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: [%d]%s (from: %d)\n", sw->to, name, sw->from);
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         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
276         pd->datanum = (pd->solvable - pool->solvables) - pd->repo->start;
277         repodata_extend(pd->data, pd->solvable - pool->solvables);      
278         
279         solvable->vendor = str2id(pool, from, 1);
280         solvable->evr = str2id(pool, version, 1);
281         solvable->arch = ARCH_NOARCH;
282         repodata_set_str(pd->data, pd->datanum, UPDATE_SEVERITY, type);
283       }
284       break;
285       /* <id>FEDORA-2007-4594</id> */
286       case STATE_ID:
287       break;
288       /* <title>imlib-1.9.15-6.fc8</title> */
289       case STATE_TITLE:
290       break;
291       /* <release>Fedora 8</release> */
292       case STATE_RELEASE:
293       break;
294       /*  <issued date="2008-03-21 21:36:55"/>
295       */
296       case STATE_ISSUED:
297       {
298         const char *date = 0;
299         for (; *atts; atts += 2)
300         {
301           if (!strcmp(*atts, "date"))
302             date = atts[1];
303         }
304         repodata_set_str(pd->data, pd->datanum, UPDATE_TIMESTAMP, date);
305       }
306       break;
307       case STATE_REFERENCES:
308       break;
309       /*  <reference href="https://bugzilla.redhat.com/show_bug.cgi?id=330471"
310        *             id="330471"
311        *             title="LDAP schema file missing for dhcpd"
312        *             type="bugzilla"/>
313        */
314       case STATE_REFERENCE:
315       break;
316       /* <description>This update ...</description> */
317       case STATE_DESCRIPTION:
318       break;
319       case STATE_PKGLIST:
320       break;
321       /* <collection short="F8"> */
322       case STATE_COLLECTION:
323       break;
324       /* <name>Fedora 8</name> */ 
325       case STATE_NAME:
326       break;
327       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
328        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
329        *            version="1.9.15">
330        * 
331        * -> patch.conflicts: {name} < {version}.{release}
332        */
333       case STATE_PACKAGE:
334       {
335         const char *arch = 0, *name = 0, *src = 0;
336         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
337         Id n;
338         Id rel_id;
339         for (; *atts; atts += 2)
340         {
341           if (!strcmp(*atts, "arch"))
342             arch = atts[1];
343           else if (!strcmp(*atts, "name"))
344             name = atts[1];
345           else if (!strcmp(*atts, "src"))
346             src = atts[1];
347         }
348         n = str2id(pool, name, 1);
349         rel_id = rel2id(pool, n, evr, REL_LT, 1);
350
351         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
352       }
353       break;
354       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
355       case STATE_FILENAME:
356       break;
357       /* <reboot_suggested>True</reboot_suggested> */
358       case STATE_REBOOT:
359       break;
360       /* <restart_suggested>True</restart_suggested> */
361       case STATE_RESTART:
362       break;
363       case NUMSTATES+1:
364         split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...}  Urgs!*/
365       break;
366       default:
367       break;
368     }
369   return;
370 }
371
372
373 static void XMLCALL
374 endElement(void *userData, const char *name)
375 {
376   struct parsedata *pd = userData;
377   Pool *pool = pd->pool;
378   Solvable *s = pd->solvable;
379
380 #if 0
381       fprintf(stderr, "end: %s\n", name);
382 #endif
383   if (pd->depth != pd->statedepth)
384     {
385       pd->depth--;
386 #if 1
387       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
388 #endif
389       return;
390     }
391
392   pd->depth--;
393   pd->statedepth--;
394   switch (pd->state)
395     {
396       case STATE_START:
397       break;
398       case STATE_UPDATES:
399       break;
400       case STATE_UPDATE:
401       break;
402       case STATE_ID:
403       {
404         if (pd->content) {
405           s->name = str2id(pool, join2("patch", ":", pd->content), 1);
406         }
407       }
408       break;
409       /* <title>imlib-1.9.15-6.fc8</title> */
410       case STATE_TITLE:
411       {
412         while (pd->lcontent > 0
413                && *(pd->content + pd->lcontent - 1) == '\n')
414         {
415           --pd->lcontent;
416           *(pd->content + pd->lcontent) = 0;
417         }
418         repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
419       }
420       break;
421       /*
422        * <release>Fedora 8</release>
423        */
424       case STATE_RELEASE:
425       break;
426       case STATE_ISSUED:
427       break;
428       case STATE_REFERENCES:
429       break;
430       case STATE_REFERENCE:
431       break;
432       /*
433        * <description>This update ...</description>
434        */
435       case STATE_DESCRIPTION:
436       {
437         repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
438       }
439       break;   
440       case STATE_PKGLIST:
441       break;
442       case STATE_COLLECTION:
443       break;
444       case STATE_NAME:
445       break;
446       case STATE_PACKAGE:
447       break;
448       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
449       case STATE_FILENAME:
450       break;
451       /* <reboot_suggested>True</reboot_suggested> */
452       case STATE_REBOOT:
453       {
454         repodata_set_str(pd->data, pd->datanum, UPDATE_REBOOT, pd->content);
455       }
456       break;
457       /* <restart_suggested>True</restart_suggested> */
458       case STATE_RESTART:
459       {
460         repodata_set_str(pd->data, pd->datanum, UPDATE_RESTART, pd->content);
461       }
462       break;
463       default:
464       break;
465     }
466
467   pd->state = pd->sbtab[pd->state];
468   pd->docontent = 0;
469   
470   return;
471 }
472
473
474 static void XMLCALL
475 characterData(void *userData, const XML_Char *s, int len)
476 {
477   struct parsedata *pd = userData;
478   int l;
479   char *c;
480   if (!pd->docontent) {
481 #if 0
482     char *dup = strndup( s, len );
483   fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
484   free( dup );
485 #endif
486     return;
487   }
488   l = pd->lcontent + len + 1;
489   if (l > pd->acontent)
490     {
491       pd->content = realloc(pd->content, l + 256);
492       pd->acontent = l + 256;
493     }
494   c = pd->content + pd->lcontent;
495   pd->lcontent += len;
496   while (len-- > 0)
497     *c++ = *s++;
498   *c = 0;
499 }
500
501
502 #define BUFF_SIZE 8192
503
504 void
505 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
506 {
507   Pool *pool = repo->pool;
508   struct parsedata pd;
509   char buf[BUFF_SIZE];
510   int i, l;
511   struct stateswitch *sw;
512
513   memset(&pd, 0, sizeof(pd));
514   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
515     {
516       if (!pd.swtab[sw->from])
517         pd.swtab[sw->from] = sw;
518       pd.sbtab[sw->to] = sw->from;
519     }
520   pd.pool = pool;
521   pd.repo = repo;
522   pd.data = repo_add_repodata(pd.repo, 0);
523
524   pd.content = malloc(256);
525   pd.acontent = 256;
526   pd.lcontent = 0;
527   pd.tempstr = malloc(256);
528   pd.atemp = 256;
529   pd.ltemp = 0;
530   XML_Parser parser = XML_ParserCreate(NULL);
531   XML_SetUserData(parser, &pd);
532   XML_SetElementHandler(parser, startElement, endElement);
533   XML_SetCharacterDataHandler(parser, characterData);
534   for (;;)
535     {
536       l = fread(buf, 1, sizeof(buf), fp);
537       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
538         {
539           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));
540           exit(1);
541         }
542       if (l == 0)
543         break;
544     }
545   XML_ParserFree(parser);
546
547   if (pd.data)
548     repodata_internalize(pd.data);
549
550   free(pd.content);
551 }
552
553 /* EOF */