Bootstrap of 'captive-install-acquire' for W32 modules acquiration process.
[captive.git] / src / install / acquire / captivemodid.c
1 /* $Id$
2  * W32 disk modules identifier for acquiration installation utility
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "captivemodid.h"       /* self */
23 #include <glib/gmessages.h>
24 #include <libxml/xmlreader.h>
25 #include <glib/ghash.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <libgnomevfs/gnome-vfs-file-size.h>
29 #include <openssl/md5.h>
30 #include <openssl/bn.h>
31 #include <openssl/crypto.h>
32 #include <glib/gstrfuncs.h>
33 #include <ctype.h>
34
35 #include <captive/macros.h>
36
37
38 gchar *calc_md5(gconstpointer base,size_t length)
39 {
40 unsigned char md5_bin[1+128/8]; /* 128 bits==16 bytes; '1+' for leading stub to prevent shorter output of BN_bn2hex() */
41 BIGNUM *bignum;
42 char *hex;
43 gchar *r,*gs;
44
45         /* already done above */
46         /* Calculate MD5 sum and convert it to hex string: */
47         MD5(base,length,md5_bin+1);
48         md5_bin[0]=0xFF;  /* stub to prevent shorter output of BN_bn2hex() */
49         bignum=BN_bin2bn(md5_bin,1+128/8,NULL);
50         hex=BN_bn2hex(bignum);
51         g_assert(strlen(hex)==2*(1+128/8));
52         r=g_strdup(hex+2);
53         OPENSSL_free(hex);
54         BN_free(bignum);
55
56         g_assert(strlen(r)==32);
57         for (gs=r;*gs;gs++) {
58                 g_assert(isxdigit(*gs));
59                 *gs=tolower(*gs);
60                 g_assert(isxdigit(*gs));
61                 }
62         return r;
63 }
64
65
66 /* map: GINT_TO_POINTER(captivemodid_module.length) -> !=NULL */
67 static GHashTable *module_valid_length_hash;
68
69 static void module_valid_length_hash_init(void)
70 {
71         if (module_valid_length_hash)
72                 return;
73         module_valid_length_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
74 }
75
76 /* map: (const xmlChar *)md5 -> (struct captivemodid_module *) */
77 static GHashTable *module_md5_hash;
78
79 static void module_md5_hash_init(void)
80 {
81         if (module_md5_hash)
82                 return;
83         module_md5_hash=g_hash_table_new(g_str_hash,g_str_equal);
84 }
85
86 static void captivemodid_load_module(struct captivemodid_module *module)
87 {
88 struct captivemodid_module *module_md5_conflict;
89
90         module_md5_hash_init();
91         if ((module_md5_conflict=g_hash_table_lookup(module_md5_hash,module->md5))) {
92                 g_warning(_("Ignoring module \"%s\" as it has MD5 conflict with: %s"),
93                                 module->id,module_md5_conflict->id);
94                 return;
95                 }
96         g_hash_table_insert(module_md5_hash,(/* de-const */ xmlChar *)module->md5,module);
97         module_valid_length_hash_init();
98         g_hash_table_insert(module_valid_length_hash,GINT_TO_POINTER(module->length),module_valid_length_hash);
99 }
100
101 gboolean captivemodid_module_length_is_valid(GnomeVFSFileSize file_size)
102 {
103 gint file_size_gint;
104
105         if ((GnomeVFSFileSize)(file_size_gint=file_size)!=file_size)    /* Size too big to be valid. */
106                 return FALSE;
107         return !!g_hash_table_lookup(module_valid_length_hash,GINT_TO_POINTER(file_size_gint));
108 }
109
110 struct captivemodid_module *captivemodid_module_md5_lookup(const gchar *file_md5)
111 {
112         g_return_val_if_fail(file_md5!=NULL,NULL);
113
114         return g_hash_table_lookup(module_md5_hash,file_md5);
115 }
116
117 static xmlChar *captivemodid_load_module_xml_get_attr
118                 (const gchar *captivemodid_pathname,xmlTextReader *xml_reader,const gchar *attr_name)
119 {
120 xmlChar *r;
121
122         if (!(r=xmlTextReaderGetAttribute(xml_reader,attr_name))) {
123                 /* FIXME: File line identification? */
124                 g_warning(_("%s: Undefined attributes: %s"),captivemodid_pathname,attr_name);
125                 return NULL;
126                 }
127         return r;
128 }
129
130 static long captivemodid_load_module_xml_get_attr_l
131                 (const gchar *captivemodid_pathname,xmlTextReader *xml_reader,const gchar *attr_name,long num_min,long num_max)
132 {
133 xmlChar *string;
134 long r;
135 char *ends;
136
137         g_return_val_if_fail(num_min-1<num_min,-1);
138         g_return_val_if_fail(num_min<=num_max,num_min-1);
139         g_return_val_if_fail(LONG_MIN<num_min,LONG_MIN);
140         g_return_val_if_fail(num_max<LONG_MAX,num_min-1);
141
142         if (!(string=captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,attr_name)))
143                 return num_min-1;
144         r=strtol(string,&ends,0);
145         xmlFree(string);
146         if (r<num_min || r>num_max) {
147                 g_warning(_("%s: Numer of out range %ld..%ld: %ld"),captivemodid_pathname,num_min,num_max,r);
148                 return num_min-1;
149                 }
150         return r;
151 }
152
153 static void captivemodid_load_module_xml(const gchar *captivemodid_pathname,xmlTextReader *xml_reader)
154 {
155 struct captivemodid_module *module;
156
157         captive_new0(module);
158         if (!(module->type=captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"type")))
159                 goto fail_free_module;
160         if (!(module->md5 =captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"md5")))
161                 goto fail_free_module;
162         if (strlen(module->md5)!=strspn(module->md5,"0123456789abcdef")) {
163                 g_warning(_("%s: Attribute 'md5' can be only lower-cased hexstring: %s"),captivemodid_pathname,module->md5);
164                 goto fail_free_module;
165                 }
166         if (strlen(module->md5)!=32) {
167                 g_warning(_("%s: Attribute 'md5' length must be 32: %s"),captivemodid_pathname,module->md5);
168                 goto fail_free_module;
169                 }
170         if (!(module->id  =captivemodid_load_module_xml_get_attr(captivemodid_pathname,xml_reader,"id")))
171                 goto fail_free_module;
172         if (0>=(module->length=captivemodid_load_module_xml_get_attr_l(captivemodid_pathname,xml_reader,"length",1,G_MAXINT-1)))
173                 goto fail_free_module;
174         if (G_MININT>=(module->priority=captivemodid_load_module_xml_get_attr_l(captivemodid_pathname,xml_reader,"priority",
175                         G_MININT+1,G_MAXINT-1)))
176                 goto fail_free_module;
177         captivemodid_load_module(module);
178         return;
179
180 fail_free_module:
181         xmlFree((xmlChar *)module->type);
182         xmlFree((xmlChar *)module->md5);
183         xmlFree((xmlChar *)module->id);
184         g_free(module);
185 }
186
187
188 void captivemodid_load(const gchar *captivemodid_pathname)
189 {
190 xmlTextReader *xml_reader;
191
192         xml_reader=xmlNewTextReaderFilename(captivemodid_pathname);
193         g_assert(xml_reader!=NULL);
194         while (1==xmlTextReaderRead(xml_reader)) {
195                 switch (xmlTextReaderNodeType(xml_reader)) {
196
197                         case XML_READER_TYPE_COMMENT:
198                                 break;
199
200                         case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
201                                 break;
202
203                         case XML_READER_TYPE_TEXT:      /* Even empty nodes have some '#text'. */
204                                 break;
205
206                         case XML_READER_TYPE_END_ELEMENT:       /* We do not track tag ends. */
207                                 break;
208
209                         case XML_READER_TYPE_ELEMENT: {
210 const xmlChar *xml_name;
211
212                                 xml_name=xmlTextReaderName(xml_reader);
213                                 /**/ if (!xmlStrcmp(xml_name,"modid")) {        /* root tag */
214                                         }
215                                 else if (!xmlStrcmp(xml_name,"module"))
216                                         captivemodid_load_module_xml(captivemodid_pathname,xml_reader);
217                                 else g_warning(_("%s: Unknown ELEMENT node: %s"),captivemodid_pathname,xml_name);
218                                 xmlFree((xmlChar *)xml_name);
219                                 } break;
220
221                         default: g_assert_not_reached();
222                         }
223                 }
224         xmlFreeTextReader(xml_reader);
225 }