Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / libcaptive / client / options-module.c
1 /* $Id$
2  * User options handling code of libcaptive
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 "captive/options.h"    /* self */
23 #include <glib/gmessages.h>
24 #include "captive/macros.h"
25 #include <glib/gstrfuncs.h>
26 #include <stdlib.h>
27 #include "captive/rtl-file.h"
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <sys/mman.h>
32 #include <openssl/md5.h>
33 #include <openssl/bn.h>
34 #include <openssl/crypto.h>
35 #include <ctype.h>
36
37
38 gboolean captive_options_module_load(struct captive_options_module *options_module,const gchar *pathname_utf8)
39 {
40         g_return_val_if_fail(options_module!=NULL,FALSE);
41         g_return_val_if_fail(pathname_utf8!=NULL,FALSE);
42
43         /* Open the Module */
44         options_module->type=CAPTIVE_OPTIONS_MODULE_TYPE_PE32;
45         options_module->pathname_utf8=g_strdup(pathname_utf8);
46         options_module->u.pe32.mapped=TRUE;
47         options_module->u.pe32.base=captive_rtl_file_mmap(
48                         &options_module->u.pe32.length, /* lenp */
49                         pathname_utf8,  /* path */
50                         O_RDONLY,       /* open_flags */
51                         PROT_READ,      /* mmap_prot */
52                         MAP_SHARED);    /* mmap_flags */
53         /* FIXME: convert errno instead of STATUS_INSUFFICIENT_RESOURCES */
54         g_return_val_if_fail(options_module->u.pe32.base!=NULL,FALSE);
55
56         if (options_module->u.pe32.length>=2
57                         && (('M'<<8U)|('Z'<<0U))==GUINT16_FROM_BE(*(const guint16 *)options_module->u.pe32.base)) {
58 unsigned char md5_bin[1+128/8]; /* 128 bits==16 bytes; '1+' for leading stub to prevent shorter output of BN_bn2hex() */
59 BIGNUM *bignum;
60 char *hex,*s;
61
62                 /* already done above */
63                 /* Calculate MD5 sum and convert it to hex string: */
64                 MD5(options_module->u.pe32.base,options_module->u.pe32.length,md5_bin+1);
65                 md5_bin[0]=0xFF;        /* stub to prevent shorter output of BN_bn2hex() */
66                 bignum=BN_bin2bn(md5_bin,1+128/8,NULL);
67                 hex=BN_bn2hex(bignum);
68                 g_assert(strlen(hex)==2*(1+128/8));
69                 options_module->u.pe32.md5=g_strdup(hex+2);
70                 OPENSSL_free(hex);
71                 BN_free(bignum);
72                 /* BN_bn2hex() returns uppercased string: */
73                 g_assert(strlen(options_module->u.pe32.md5)==32);
74                 for (s=options_module->u.pe32.md5;*s;s++) {
75                         g_assert(isxdigit(*s));
76                         *s=tolower(*s);
77                         g_assert(isxdigit(*s));
78                         }
79                 }
80         else {
81                 captive_rtl_file_munmap(options_module->u.pe32.base);
82                 options_module->type=CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE;
83                 options_module->u.gmodule.pathname=g_strdup(pathname_utf8);
84                 }
85
86         return TRUE;
87 }
88
89
90 void captive_options_module_copy(struct captive_options_module *dest,const struct captive_options_module *src)
91 {
92         g_return_if_fail(dest!=NULL);
93         g_return_if_fail(src!=NULL);
94
95         dest->pathname_utf8=g_strdup(src->pathname_utf8);
96
97         dest->type=src->type;
98         switch (src->type) {
99                 case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY: g_assert_not_reached();
100
101                 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
102                         dest->u.pe32.base=g_memdup(src->u.pe32.base,src->u.pe32.length);
103                         dest->u.pe32.length=src->u.pe32.length;
104                         dest->u.pe32.mapped=FALSE;
105                         dest->u.pe32.md5=g_strdup(src->u.pe32.md5);
106                         break;
107
108                 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
109                         dest->u.gmodule.pathname=g_strdup(src->u.gmodule.pathname);
110                         break;
111
112                 default: g_assert_not_reached();
113                 }
114 }
115
116
117 void captive_options_module_free(struct captive_options_module *options_module)
118 {
119         g_return_if_fail(options_module!=NULL);
120
121         g_free(options_module->pathname_utf8);
122
123         switch (options_module->type) {
124                 case CAPTIVE_OPTIONS_MODULE_TYPE_EMPTY:
125                         break;
126
127                 case CAPTIVE_OPTIONS_MODULE_TYPE_PE32:
128                         if (options_module->u.pe32.mapped)
129                                 captive_rtl_file_munmap(options_module->u.pe32.base);
130                         else
131                                 g_free(options_module->u.pe32.base);
132                         g_free(options_module->u.pe32.md5);
133                         break;
134
135                 case CAPTIVE_OPTIONS_MODULE_TYPE_GMODULE:
136                         g_free(options_module->u.gmodule.pathname);
137                         break;
138
139                 default: g_assert_not_reached();
140                 }
141 }