write UPDATE_COLLECTION
[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, SOLVABLE_PATCHCATEGORY, 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, SOLVABLE_BUILDTIME, 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, a, na;
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         /* generated Ids for name and arch */
349         n = str2id(pool, name, 1);
350         if (arch)
351           a = str2id(pool, arch, 1);
352         else
353           a = ARCH_NOARCH;
354         /*  now combine both to a single Id */
355         na = rel2id(pool, n, a, REL_ARCH, 1);
356         
357         rel_id = rel2id(pool, na, evr, REL_LT, 1);
358
359         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
360         
361         if (1) {
362           const char *evrstr = id2str(pool, evr);
363           int buflen = strlen(name) + 1 + strlen(evrstr) + 1 + strlen(arch?arch:"") + 1;
364           char *buf;
365           if (!arch) arch = "";
366           buf = (char *)malloc(buflen);
367           if (!buf) exit(1);
368           sprintf(buf, "%s %s %s", name, evrstr, arch);
369           repodata_add_poolstr_array(pd->data, pd->datanum, UPDATE_COLLECTION, buf);
370           free(buf);
371         }
372       }
373       break;
374       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
375       case STATE_FILENAME:
376       break;
377       /* <reboot_suggested>True</reboot_suggested> */
378       case STATE_REBOOT:
379       break;
380       /* <restart_suggested>True</restart_suggested> */
381       case STATE_RESTART:
382       break;
383       case NUMSTATES+1:
384         split(NULL, NULL, 0); /* just to keep gcc happy about tools_util.h: static ... split() {...}  Urgs!*/
385       break;
386       default:
387       break;
388     }
389   return;
390 }
391
392
393 static void XMLCALL
394 endElement(void *userData, const char *name)
395 {
396   struct parsedata *pd = userData;
397   Pool *pool = pd->pool;
398   Solvable *s = pd->solvable;
399
400 #if 0
401       fprintf(stderr, "end: %s\n", name);
402 #endif
403   if (pd->depth != pd->statedepth)
404     {
405       pd->depth--;
406 #if 1
407       fprintf(stderr, "back from unknown %d %d %d\n", pd->state, pd->depth, pd->statedepth);
408 #endif
409       return;
410     }
411
412   pd->depth--;
413   pd->statedepth--;
414   switch (pd->state)
415     {
416       case STATE_START:
417       break;
418       case STATE_UPDATES:
419       break;
420       case STATE_UPDATE:
421       break;
422       case STATE_ID:
423       {
424         if (pd->content) {
425           s->name = str2id(pool, join2("patch", ":", pd->content), 1);
426         }
427       }
428       break;
429       /* <title>imlib-1.9.15-6.fc8</title> */
430       case STATE_TITLE:
431       {
432         while (pd->lcontent > 0
433                && *(pd->content + pd->lcontent - 1) == '\n')
434         {
435           --pd->lcontent;
436           *(pd->content + pd->lcontent) = 0;
437         }
438         repodata_set_str(pd->data, pd->datanum, SOLVABLE_SUMMARY, pd->content);
439       }
440       break;
441       /*
442        * <release>Fedora 8</release>
443        */
444       case STATE_RELEASE:
445       break;
446       case STATE_ISSUED:
447       break;
448       case STATE_REFERENCES:
449       break;
450       case STATE_REFERENCE:
451       break;
452       /*
453        * <description>This update ...</description>
454        */
455       case STATE_DESCRIPTION:
456       {
457         repodata_set_str(pd->data, pd->datanum, SOLVABLE_DESCRIPTION, pd->content);
458       }
459       break;   
460       case STATE_PKGLIST:
461       break;
462       case STATE_COLLECTION:
463       break;
464       case STATE_NAME:
465       break;
466       case STATE_PACKAGE:
467       break;
468       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */ 
469       case STATE_FILENAME:
470       break;
471       /* <reboot_suggested>True</reboot_suggested> */
472       case STATE_REBOOT:
473       {
474         repodata_set_void(pd->data, pd->datanum, UPDATE_REBOOT);
475       }
476       break;
477       /* <restart_suggested>True</restart_suggested> */
478       case STATE_RESTART:
479       {
480         repodata_set_void(pd->data, pd->datanum, UPDATE_RESTART);
481       }
482       break;
483       default:
484       break;
485     }
486
487   pd->state = pd->sbtab[pd->state];
488   pd->docontent = 0;
489   
490   return;
491 }
492
493
494 static void XMLCALL
495 characterData(void *userData, const XML_Char *s, int len)
496 {
497   struct parsedata *pd = userData;
498   int l;
499   char *c;
500   if (!pd->docontent) {
501 #if 0
502     char *dup = strndup( s, len );
503   fprintf(stderr, "Content: [%d]'%s'\n", pd->state, dup );
504   free( dup );
505 #endif
506     return;
507   }
508   l = pd->lcontent + len + 1;
509   if (l > pd->acontent)
510     {
511       pd->content = realloc(pd->content, l + 256);
512       pd->acontent = l + 256;
513     }
514   c = pd->content + pd->lcontent;
515   pd->lcontent += len;
516   while (len-- > 0)
517     *c++ = *s++;
518   *c = 0;
519 }
520
521
522 #define BUFF_SIZE 8192
523
524 void
525 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
526 {
527   Pool *pool = repo->pool;
528   struct parsedata pd;
529   char buf[BUFF_SIZE];
530   int i, l;
531   struct stateswitch *sw;
532
533   memset(&pd, 0, sizeof(pd));
534   for (i = 0, sw = stateswitches; sw->from != NUMSTATES; i++, sw++)
535     {
536       if (!pd.swtab[sw->from])
537         pd.swtab[sw->from] = sw;
538       pd.sbtab[sw->to] = sw->from;
539     }
540   pd.pool = pool;
541   pd.repo = repo;
542   pd.data = repo_add_repodata(pd.repo, 0);
543
544   pd.content = malloc(256);
545   pd.acontent = 256;
546   pd.lcontent = 0;
547   pd.tempstr = malloc(256);
548   pd.atemp = 256;
549   pd.ltemp = 0;
550   XML_Parser parser = XML_ParserCreate(NULL);
551   XML_SetUserData(parser, &pd);
552   XML_SetElementHandler(parser, startElement, endElement);
553   XML_SetCharacterDataHandler(parser, characterData);
554   for (;;)
555     {
556       l = fread(buf, 1, sizeof(buf), fp);
557       if (XML_Parse(parser, buf, l, l == 0) == XML_STATUS_ERROR)
558         {
559           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));
560           exit(1);
561         }
562       if (l == 0)
563         break;
564     }
565   XML_ParserFree(parser);
566
567   if (pd.data)
568     repodata_internalize(pd.data);
569
570   free(pd.content);
571 }
572
573 /* EOF */