ff84d32122814b3476d5bf5896067d9ac546d490
[platform/upstream/libsolv.git] / ext / 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 _GNU_SOURCE
9 #define _XOPEN_SOURCE /* glibc2 needs this */
10 #include <sys/types.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15
16 #include "pool.h"
17 #include "repo.h"
18 #include "solv_xmlparser.h"
19 #include "repo_updateinfoxml.h"
20 #define DISABLE_SPLIT
21 #include "tools_util.h"
22
23 /*
24  * <updates>
25  *   <update from="rel-eng@fedoraproject.org" status="stable" type="security" version="1.4">
26  *     <id>FEDORA-2007-4594</id>
27  *     <title>imlib-1.9.15-6.fc8</title>
28  *     <severity>Important</severity>
29  *     <release>Fedora 8</release>
30  *     <rights>Copyright 2007 Company Inc</rights>
31  *     <issued date="2007-12-28 16:42:30"/>
32  *     <updated date="2008-03-14 12:00:00"/>
33  *     <references>
34  *       <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"/>
35  *     </references>
36  *     <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>
37  *     <pkglist>
38  *       <collection short="F8">
39  *         <name>Fedora 8</name>
40  *         <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">
41  *           <filename>imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm</filename>
42  *           <reboot_suggested>True</reboot_suggested>
43  *         </package>
44  *       </collection>
45  *     </pkglist>
46  *   </update>
47  * </updates>
48 */
49
50 enum state {
51   STATE_START,
52   STATE_UPDATES,
53   STATE_UPDATE,
54   STATE_ID,
55   STATE_TITLE,
56   STATE_RELEASE,
57   STATE_ISSUED,
58   STATE_UPDATED,
59   STATE_MESSAGE,
60   STATE_REFERENCES,
61   STATE_REFERENCE,
62   STATE_DESCRIPTION,
63   STATE_PKGLIST,
64   STATE_COLLECTION,
65   STATE_NAME,
66   STATE_PACKAGE,
67   STATE_FILENAME,
68   STATE_REBOOT,
69   STATE_RESTART,
70   STATE_RELOGIN,
71   STATE_RIGHTS,
72   STATE_SEVERITY,
73   NUMSTATES
74 };
75
76 static struct solv_xmlparser_element stateswitches[] = {
77   { STATE_START,       "updates",         STATE_UPDATES,     0 },
78   { STATE_START,       "update",          STATE_UPDATE,      0 },
79   { STATE_UPDATES,     "update",          STATE_UPDATE,      0 },
80   { STATE_UPDATE,      "id",              STATE_ID,          1 },
81   { STATE_UPDATE,      "title",           STATE_TITLE,       1 },
82   { STATE_UPDATE,      "severity",        STATE_SEVERITY,    1 },
83   { STATE_UPDATE,      "rights",          STATE_RIGHTS,      1 },
84   { STATE_UPDATE,      "release",         STATE_RELEASE,     1 },
85   { STATE_UPDATE,      "issued",          STATE_ISSUED,      0 },
86   { STATE_UPDATE,      "updated",         STATE_UPDATED,     0 },
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   { STATE_PACKAGE,     "relogin_suggested",STATE_RELOGIN,    1 },
99   { NUMSTATES }
100 };
101
102 struct parsedata {
103   int ret;
104   Pool *pool;
105   Repo *repo;
106   Repodata *data;
107   Id handle;
108   Solvable *solvable;
109   time_t buildtime;
110   Id collhandle;
111   struct solv_xmlparser xmlp;
112   struct joindata jd;
113 };
114
115 /*
116  * Convert date strings ("1287746075" or "2010-10-22 13:14:35")
117  * to timestamp.
118  */
119 static time_t
120 datestr2timestamp(const char *date)
121 {
122   const char *p;
123   struct tm tm;
124
125   if (!date || !*date)
126     return 0;
127   for (p = date; *p >= '0' && *p <= '9'; p++)
128     ;
129   if (!*p)
130     return atoi(date);
131   memset(&tm, 0, sizeof(tm));
132   if (!strptime(date, "%F%T", &tm))
133     return 0;
134   return timegm(&tm);
135 }
136
137 /*
138  * create evr (as Id) from 'epoch', 'version' and 'release' attributes
139  */
140 static Id
141 makeevr_atts(Pool *pool, struct parsedata *pd, const char **atts)
142 {
143   const char *e, *v, *r, *v2;
144   char *c, *space;
145   int l;
146
147   e = v = r = 0;
148   for (; *atts; atts += 2)
149     {
150       if (!strcmp(*atts, "epoch"))
151         e = atts[1];
152       else if (!strcmp(*atts, "version"))
153         v = atts[1];
154       else if (!strcmp(*atts, "release"))
155         r = atts[1];
156     }
157   if (e && (!*e || !strcmp(e, "0")))
158     e = 0;
159   if (v && !e)
160     {
161       for (v2 = v; *v2 >= '0' && *v2 <= '9'; v2++)
162         ;
163       if (v2 > v && *v2 == ':')
164         e = "0";
165     }
166   l = 1;
167   if (e)
168     l += strlen(e) + 1;
169   if (v)
170     l += strlen(v);
171   if (r)
172     l += strlen(r) + 1;
173   
174   c = space = solv_xmlparser_contentspace(&pd->xmlp, l);
175   if (e)
176     {
177       strcpy(c, e);
178       c += strlen(c);
179       *c++ = ':';
180     }
181   if (v)
182     {
183       strcpy(c, v);
184       c += strlen(c);
185     }
186   if (r)
187     {
188       *c++ = '-';
189       strcpy(c, r);
190       c += strlen(c);
191     }
192   *c = 0;
193   if (!*space)
194     return 0;
195 #if 0
196   fprintf(stderr, "evr: %s\n", space);
197 #endif
198   return pool_str2id(pool, space, 1);
199 }
200
201
202
203 static void
204 startElement(struct solv_xmlparser *xmlp, int state, const char *name, const char **atts)
205 {
206   struct parsedata *pd = xmlp->userdata;
207   Pool *pool = pd->pool;
208   Solvable *solvable = pd->solvable;
209
210   switch(state)
211     {
212       /*
213        * <update from="rel-eng@fedoraproject.org"
214        *         status="stable"
215        *         type="bugfix" (enhancement, security)
216        *         version="1.4">
217        */
218     case STATE_UPDATE:
219       {
220         const char *from = 0, *type = 0, *version = 0;
221         for (; *atts; atts += 2)
222           {
223             if (!strcmp(*atts, "from"))
224               from = atts[1];
225             else if (!strcmp(*atts, "type"))
226               type = atts[1];
227             else if (!strcmp(*atts, "version"))
228               version = atts[1];
229           }
230         solvable = pd->solvable = pool_id2solvable(pool, repo_add_solvable(pd->repo));
231         pd->handle = pd->solvable - pool->solvables;
232
233         solvable->vendor = pool_str2id(pool, from, 1);
234         solvable->evr = pool_str2id(pool, version, 1);
235         solvable->arch = ARCH_NOARCH;
236         if (type)
237           repodata_set_str(pd->data, pd->handle, SOLVABLE_PATCHCATEGORY, type);
238         pd->buildtime = (time_t)0;
239       }
240       break;
241
242     case STATE_ISSUED:
243     case STATE_UPDATED:
244       {
245         const char *date = solv_xmlparser_find_attr("date", atts);
246         if (date)
247           {
248             time_t t = datestr2timestamp(date);
249             if (t && t > pd->buildtime)
250               pd->buildtime = t;
251           }
252       }
253       break;
254
255     case STATE_REFERENCE:
256       {
257         const char *href = 0, *id = 0, *title = 0, *type = 0;
258         Id refhandle;
259         for (; *atts; atts += 2)
260           {
261             if (!strcmp(*atts, "href"))
262               href = atts[1];
263             else if (!strcmp(*atts, "id"))
264               id = atts[1];
265             else if (!strcmp(*atts, "title"))
266               title = atts[1];
267             else if (!strcmp(*atts, "type"))
268               type = atts[1];
269           }
270         refhandle = repodata_new_handle(pd->data);
271         if (href)
272           repodata_set_str(pd->data, refhandle, UPDATE_REFERENCE_HREF, href);
273         if (id)
274           repodata_set_str(pd->data, refhandle, UPDATE_REFERENCE_ID, id);
275         if (title)
276           repodata_set_str(pd->data, refhandle, UPDATE_REFERENCE_TITLE, title);
277         if (type)
278           repodata_set_poolstr(pd->data, refhandle, UPDATE_REFERENCE_TYPE, type);
279         repodata_add_flexarray(pd->data, pd->handle, UPDATE_REFERENCE, refhandle);
280       }
281       break;
282
283       /*   <package arch="ppc64" name="imlib-debuginfo" release="6.fc8"
284        *            src="http://download.fedoraproject.org/pub/fedora/linux/updates/8/ppc64/imlib-debuginfo-1.9.15-6.fc8.ppc64.rpm"
285        *            version="1.9.15">
286        *
287        *
288        * -> patch.conflicts: {name} < {version}.{release}
289        */
290     case STATE_PACKAGE:
291       {
292         const char *arch = 0, *name = 0;
293         Id evr = makeevr_atts(pool, pd, atts); /* parse "epoch", "version", "release" */
294         Id n, a = 0;
295         Id rel_id;
296
297         for (; *atts; atts += 2)
298           {
299             if (!strcmp(*atts, "arch"))
300               arch = atts[1];
301             else if (!strcmp(*atts, "name"))
302               name = atts[1];
303           }
304         /* generated Id for name */
305         n = pool_str2id(pool, name, 1);
306         rel_id = n;
307         if (arch)
308           {
309             /*  generate Id for arch and combine with name */
310             a = pool_str2id(pool, arch, 1);
311             rel_id = pool_rel2id(pool, n, a, REL_ARCH, 1);
312           }
313         rel_id = pool_rel2id(pool, rel_id, evr, REL_LT, 1);
314         solvable->conflicts = repo_addid_dep(pd->repo, solvable->conflicts, rel_id, 0);
315
316         /* who needs the collection anyway? */
317         pd->collhandle = repodata_new_handle(pd->data);
318         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_NAME, n);
319         repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_EVR, evr);
320         if (a)
321           repodata_set_id(pd->data, pd->collhandle, UPDATE_COLLECTION_ARCH, a);
322         break;
323       }
324
325     default:
326       break;
327     }
328   return;
329 }
330
331
332 static void
333 endElement(struct solv_xmlparser *xmlp, int state, char *content)
334 {
335   struct parsedata *pd = xmlp->userdata;
336   Pool *pool = pd->pool;
337   Solvable *s = pd->solvable;
338   Repo *repo = pd->repo;
339
340   switch (state)
341     {
342     case STATE_UPDATE:
343       s->provides = repo_addid_dep(repo, s->provides, pool_rel2id(pool, s->name, s->evr, REL_EQ, 1), 0);
344       if (pd->buildtime)
345         {
346           repodata_set_num(pd->data, pd->handle, SOLVABLE_BUILDTIME, pd->buildtime);
347           pd->buildtime = (time_t)0;
348         }
349       break;
350
351     case STATE_ID:
352       s->name = pool_str2id(pool, join2(&pd->jd, "patch", ":", content), 1);
353       break;
354
355       /* <title>imlib-1.9.15-6.fc8</title> */
356     case STATE_TITLE:
357       /* strip trailing newlines */
358       while (pd->xmlp.lcontent > 0 && content[pd->xmlp.lcontent - 1] == '\n')
359         content[--pd->xmlp.lcontent] = 0;
360       repodata_set_str(pd->data, pd->handle, SOLVABLE_SUMMARY, content);
361       break;
362
363     case STATE_SEVERITY:
364       repodata_set_poolstr(pd->data, pd->handle, UPDATE_SEVERITY, content);
365       break;
366
367     case STATE_RIGHTS:
368       repodata_set_poolstr(pd->data, pd->handle, UPDATE_RIGHTS, content);
369       break;
370
371       /*
372        * <description>This update ...</description>
373        */
374     case STATE_DESCRIPTION:
375       repodata_set_str(pd->data, pd->handle, SOLVABLE_DESCRIPTION, content);
376       break;
377
378       /*
379        * <message>Warning! ...</message>
380        */
381     case STATE_MESSAGE:
382       repodata_set_str(pd->data, pd->handle, UPDATE_MESSAGE, content);
383       break;
384
385     case STATE_PACKAGE:
386       repodata_add_flexarray(pd->data, pd->handle, UPDATE_COLLECTION, pd->collhandle);
387       pd->collhandle = 0;
388       break;
389
390       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
391       /* <filename>libntlm-0.4.2-1.fc8.x86_64.rpm</filename> */
392     case STATE_FILENAME:
393       repodata_set_str(pd->data, pd->collhandle, UPDATE_COLLECTION_FILENAME, content);
394       break;
395
396       /* <reboot_suggested>True</reboot_suggested> */
397     case STATE_REBOOT:
398       if (content[0] == 'T' || content[0] == 't'|| content[0] == '1')
399         {
400           /* FIXME: this is per-package, the global flag should be computed at runtime */
401           repodata_set_void(pd->data, pd->handle, UPDATE_REBOOT);
402           repodata_set_void(pd->data, pd->collhandle, UPDATE_REBOOT);
403         }
404       break;
405
406       /* <restart_suggested>True</restart_suggested> */
407     case STATE_RESTART:
408       if (content[0] == 'T' || content[0] == 't'|| content[0] == '1')
409         {
410           /* FIXME: this is per-package, the global flag should be computed at runtime */
411           repodata_set_void(pd->data, pd->handle, UPDATE_RESTART);
412           repodata_set_void(pd->data, pd->collhandle, UPDATE_RESTART);
413         }
414       break;
415
416       /* <relogin_suggested>True</relogin_suggested> */
417     case STATE_RELOGIN:
418       if (content[0] == 'T' || content[0] == 't'|| content[0] == '1')
419         {
420           /* FIXME: this is per-package, the global flag should be computed at runtime */
421           repodata_set_void(pd->data, pd->handle, UPDATE_RELOGIN);
422           repodata_set_void(pd->data, pd->collhandle, UPDATE_RELOGIN);
423         }
424       break;
425     default:
426       break;
427     }
428 }
429
430 int
431 repo_add_updateinfoxml(Repo *repo, FILE *fp, int flags)
432 {
433   Pool *pool = repo->pool;
434   Repodata *data;
435   struct parsedata pd;
436
437   data = repo_add_repodata(repo, flags);
438
439   memset(&pd, 0, sizeof(pd));
440   pd.pool = pool;
441   pd.repo = repo;
442   pd.data = data;
443   solv_xmlparser_init(&pd.xmlp, stateswitches, &pd, startElement, endElement);
444   if (solv_xmlparser_parse(&pd.xmlp, fp) != SOLV_XMLPARSER_OK)
445     pd.ret = pool_error(pool, -1, "repo_updateinfoxml: %s at line %u:%u", pd.xmlp.errstr, pd.xmlp.line, pd.xmlp.column);
446   solv_xmlparser_free(&pd.xmlp);
447   join_freemem(&pd.jd);
448
449   if (!(flags & REPO_NO_INTERNALIZE))
450     repodata_internalize(data);
451   return pd.ret;
452 }
453