Imported Upstream version 3.3.3
[debian/amanda] / device-src / s3-util.c
1 /*
2  * Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17  *
18  * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
20  */
21
22
23 #ifdef HAVE_CONFIG_H
24 /* use a relative path here to avoid conflicting with Perl's config.h. */
25 #include "../config/config.h"
26 #endif
27 #ifdef HAVE_REGEX_H
28 #include <sys/types.h>
29 #include <regex.h>
30 #endif
31 #ifdef HAVE_AMANDA_H
32 #include "amanda.h"
33 #endif
34
35 #include <glib.h>
36 #include <openssl/md5.h>
37 #include <openssl/bio.h>
38 #include <openssl/evp.h>
39 #include <openssl/bn.h>
40 #include "s3-util.h"
41
42 #ifdef HAVE_REGEX_H
43 int
44 s3_regexec_wrap(regex_t *regex,
45            const char *str,
46            size_t nmatch,
47            regmatch_t pmatch[],
48            int eflags)
49 {
50     char *message;
51     int size;
52     int reg_result;
53
54     reg_result = regexec(regex, str, nmatch, pmatch, eflags);
55     if (reg_result != 0 && reg_result != REG_NOMATCH) {
56         size = regerror(reg_result, regex, NULL, 0);
57         message = g_malloc(size);
58         regerror(reg_result, regex, message, size);
59
60         /* this is programmer error (bad regexp), so just log
61          * and abort().  There's no good way to signal a
62          * permanaent error from interpret_response. */
63         g_critical(_("Regex error: %s"), message);
64     }
65
66     return reg_result;
67 }
68 #else
69
70 int
71 s3_regexec_wrap(regex_t *regex,
72            const char *str,
73            size_t nmatch,
74            regmatch_t pmatch[],
75            int eflags)
76 {
77     GMatchInfo *match_info;
78     int ret = REG_NOERROR;
79     guint i;
80
81     g_assert(regex && *regex);
82     g_regex_match(*regex, str, eflags, &match_info);
83     if (g_match_info_matches(match_info)) {
84         g_assert(g_match_info_get_match_count(match_info) <= (glong) nmatch);
85         for (i = 0; i < nmatch; i++) {
86             pmatch[i].rm_eo = pmatch[i].rm_so = -1;
87             g_match_info_fetch_pos(match_info, i, &pmatch[i].rm_so, &pmatch[i].rm_eo);
88         }
89     } else {
90         ret = REG_NOMATCH;
91     }
92     g_match_info_free(match_info);
93     return ret;
94 }
95 #endif
96
97 #ifndef HAVE_AMANDA_H
98 char*
99 find_regex_substring(const char* base_string, const regmatch_t match)
100 {
101     g_assert(match.rm_eo >= match.rm_so);
102     return g_strndup(base_string+match.rm_so, match.rm_eo - match.rm_so);
103 }
104 #endif
105
106 gchar*
107 s3_base64_encode(const GByteArray *to_enc) {
108     BIO *bio_b64 = NULL, *bio_buff = NULL;
109     long bio_b64_len;
110     char *bio_b64_data = NULL, *ret = NULL;
111     if (!to_enc) return NULL;
112
113     /* Initialize base64 encoding filter */
114     bio_b64 = BIO_new(BIO_f_base64());
115     g_assert(bio_b64);
116     BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL);
117
118     /* Initialize memory buffer for the base64 encoding */
119     bio_buff = BIO_new(BIO_s_mem());
120     g_assert(bio_buff);
121     bio_buff = BIO_push(bio_b64, bio_buff);
122
123     /* Write the MD5 hash into the buffer to encode it in base64 */
124     BIO_write(bio_buff, to_enc->data, to_enc->len);
125     /* BIO_flush is a macro and GCC 4.1.2 complains without this cast*/
126     (void) BIO_flush(bio_buff);
127
128     /* Pull out the base64 encoding of the MD5 hash */
129     bio_b64_len = BIO_get_mem_data(bio_buff, &bio_b64_data);
130     g_assert(bio_b64_data);
131     ret = g_strndup(bio_b64_data, bio_b64_len);
132
133     /* If bio_b64 is freed separately, freeing bio_buff will
134      * invalidly free memory and potentially segfault.
135      */
136     BIO_free_all(bio_buff);
137     return ret;
138 }
139
140 gchar*
141 s3_hex_encode(const GByteArray *to_enc)  {
142     guint i;
143     gchar *ret = NULL, table[] = "0123456789abcdef";
144     if (!to_enc) return NULL;
145
146     ret = g_new(gchar, to_enc->len*2 + 1);
147     for (i = 0; i < to_enc->len; i++) {
148         /* most significant 4 bits */
149         ret[i*2] = table[to_enc->data[i] >> 4];
150         /* least significant 4 bits */
151         ret[i*2 + 1] = table[to_enc->data[i] & 0xf];
152     }
153     ret[to_enc->len*2] = '\0';
154
155     return ret;
156 }
157
158 GByteArray*
159 s3_compute_md5_hash(const GByteArray *to_hash) {
160     MD5_CTX md5_ctx;
161     GByteArray *ret;
162     if (!to_hash) return NULL;
163
164     ret = g_byte_array_sized_new(S3_MD5_HASH_BYTE_LEN);
165     g_byte_array_set_size(ret, S3_MD5_HASH_BYTE_LEN);
166
167     MD5_Init(&md5_ctx);
168     MD5_Update(&md5_ctx, to_hash->data, to_hash->len);
169     MD5_Final(ret->data, &md5_ctx);
170
171     return ret;
172 }