1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/*
* $Id$
* 'OpenSSL for Ruby' project
* Copyright (C) 2001-2002 Michal Rokos <m.rokos@sh.cvut.cz>
* All rights reserved.
*/
/*
* This program is licenced under the same licence as Ruby.
* (See the file 'LICENCE'.)
*/
#include "ossl.h"
#define WrapConfig(klass, obj, conf) do { \
if (!conf) { \
ossl_raise(rb_eRuntimeError, "Config wasn't intitialized!"); \
} \
obj = Data_Wrap_Struct(klass, 0, NCONF_free, conf); \
} while (0)
#define GetConfig(obj, conf) do { \
Data_Get_Struct(obj, CONF, conf); \
if (!conf) { \
ossl_raise(rb_eRuntimeError, "Config wasn't intitialized!"); \
} \
} while (0)
#define SafeGetConfig(obj, conf) do { \
OSSL_Check_Kind(obj, cConfig); \
GetConfig(obj, conf); \
} while(0);
/*
* Classes
*/
VALUE cConfig;
VALUE eConfigError;
/*
* Public
*/
static CONF *parse_config(VALUE, CONF*);
CONF *
GetConfigPtr(VALUE obj)
{
CONF *conf;
SafeGetConfig(obj, conf);
return conf;
}
CONF *
DupConfigPtr(VALUE obj)
{
CONF *conf;
VALUE str;
OSSL_Check_Kind(obj, cConfig);
str = rb_funcall(obj, rb_intern("to_s"), 0);
return parse_config(str, NULL);
}
/*
* Private
*/
static CONF *
parse_config(VALUE str, CONF *dst)
{
CONF *conf;
BIO *bio;
long eline = -1;
VALUE obj;
bio = ossl_obj2bio(str);
conf = dst ? dst : NCONF_new(NULL);
if(!conf){
BIO_free(bio);
ossl_raise(eConfigError, NULL);
}
if(!NCONF_load_bio(conf, bio, &eline)){
BIO_free(bio);
if(!dst) NCONF_free(conf);
if (eline <= 0) ossl_raise(eConfigError, "wrong config format");
else ossl_raise(eConfigError, "error in line %d", eline);
ossl_raise(eConfigError, NULL);
}
BIO_free(bio);
return conf;
}
static VALUE
ossl_config_s_parse(VALUE klass, VALUE str)
{
CONF *conf;
VALUE obj;
conf = parse_config(str, NULL);
WrapConfig(klass, obj, conf);
return obj;
}
static VALUE
ossl_config_s_alloc(VALUE klass)
{
CONF *conf;
VALUE obj;
if(!(conf = NCONF_new(NULL)))
ossl_raise(eConfigError, NULL);
WrapConfig(klass, obj, conf);
return obj;
}
static VALUE
ossl_config_copy(VALUE self, VALUE other)
{
VALUE str;
CONF *conf;
GetConfig(other, conf);
str = rb_funcall(self, rb_intern("to_s"), 0);
parse_config(str, conf);
return self;
}
static VALUE
ossl_config_initialize(int argc, VALUE *argv, VALUE self)
{
CONF *conf;
long eline = -1;
char *filename;
VALUE path;
GetConfig(self, conf);
rb_scan_args(argc, argv, "01", &path);
if(!NIL_P(path)){
SafeStringValue(path);
filename = StringValuePtr(path);
if (!NCONF_load(conf, filename, &eline)){
if (eline <= 0)
ossl_raise(eConfigError, "wrong config file %s", filename);
else
ossl_raise(eConfigError, "error in %s:%d", filename, eline);
}
}
else _CONF_new_data(conf);
return self;
}
static VALUE
ossl_config_add_value(VALUE self, VALUE section, VALUE name, VALUE value)
{
CONF *conf;
CONF_VALUE *sv, *cv;
GetConfig(self, conf);
StringValue(section);
StringValue(name);
StringValue(value);
if(!(sv = _CONF_get_section(conf, RSTRING(section)->ptr))){
if(!(sv = _CONF_new_section(conf, RSTRING(section)->ptr))){
ossl_raise(eConfigError, NULL);
}
}
if(!(cv = OPENSSL_malloc(sizeof(CONF_VALUE)))){
ossl_raise(eConfigError, NULL);
}
cv->name = BUF_strdup(RSTRING(name)->ptr);
cv->value = BUF_strdup(RSTRING(value)->ptr);
if(!cv->name || !cv->value || !_CONF_add_string(conf, sv, cv)){
OPENSSL_free(cv->name);
OPENSSL_free(cv->value);
OPENSSL_free(cv);
ossl_raise(eConfigError, "_CONF_add_string failure");
}
return value;
}
static VALUE
ossl_config_get_value(VALUE self, VALUE section, VALUE name)
{
CONF *conf;
char *str;
GetConfig(self, conf);
StringValue(section);
StringValue(name);
str = NCONF_get_string(conf, RSTRING(section)->ptr, RSTRING(name)->ptr);
if(!str) ossl_raise(eConfigError, NULL);
return rb_str_new2(str);
}
static VALUE
ossl_config_get_value_old(int argc, VALUE *argv, VALUE self)
{
VALUE section, name;
rb_scan_args(argc, argv, "11", §ion, &name);
rb_warn("Config#value is deprecated; use Config#get_value");
return ossl_config_get_value(self, section, name);
}
static VALUE
set_conf_section_i(VALUE i, VALUE *arg)
{
VALUE name, value;
name = rb_ary_entry(i, 0);
value = rb_ary_entry(i, 1);
ossl_config_add_value(arg[0], arg[1], name, value);
return Qnil;
}
static VALUE
ossl_config_set_section(VALUE self, VALUE section, VALUE hash)
{
VALUE arg[2] = { self, section };
rb_iterate(rb_each, hash, set_conf_section_i, (VALUE)arg);
return hash;
}
/*
* Get all numbers as strings - use str.to_i to convert
* long number = CONF_get_number(confp->config, sect, StringValuePtr(item));
*/
static VALUE
ossl_config_get_section(VALUE self, VALUE section)
{
CONF *conf;
STACK_OF(CONF_VALUE) *sk;
CONF_VALUE *entry;
int i, entries;
VALUE hash;
GetConfig(self, conf);
if (!(sk = NCONF_get_section(conf, StringValuePtr(section)))) {
ossl_raise(eConfigError, NULL);
}
hash = rb_hash_new();
if ((entries = sk_CONF_VALUE_num(sk)) < 0) {
OSSL_Debug("# of items in section is < 0?!?");
return hash;
}
for (i=0; i<entries; i++) {
entry = sk_CONF_VALUE_value(sk, i);
rb_hash_aset(hash, rb_str_new2(entry->name), rb_str_new2(entry->value));
}
return hash;
}
static VALUE
ossl_config_get_section_old(VALUE self, VALUE section)
{
rb_warn("Config#section is deprecated; use Config#[]");
return ossl_config_get_section(self, section);
}
static void
get_conf_section(CONF_VALUE *cv, VALUE ary)
{
if(cv->name) return;
rb_ary_push(ary, rb_str_new2(cv->section));
}
static IMPLEMENT_LHASH_DOALL_ARG_FN(get_conf_section, CONF_VALUE*, VALUE);
static VALUE
ossl_config_get_sections(VALUE self)
{
CONF *conf;
VALUE ary;
GetConfig(self, conf);
ary = rb_ary_new();
lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(get_conf_section), (void*)ary);
return ary;
}
static void
dump_conf_value(CONF_VALUE *cv, VALUE str)
{
STACK_OF(CONF_VALUE) *sk;
CONF_VALUE *v;
int i, num;
if (cv->name) return;
sk = (STACK_OF(CONF_VALUE)*)cv->value;
num = sk_CONF_VALUE_num(sk);
rb_str_cat2(str, "[ ");
rb_str_cat2(str, cv->section);
rb_str_cat2(str, " ]\n");
for(i = 0; i < num; i++){
v = sk_CONF_VALUE_value(sk, i);
rb_str_cat2(str, v->name ? v->name : "None");
rb_str_cat2(str, "=");
rb_str_cat2(str, v->value ? v->value : "None");
rb_str_cat2(str, "\n");
}
rb_str_cat2(str, "\n");
}
static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_conf_value, CONF_VALUE*, VALUE);
static VALUE
dump_conf(CONF *conf)
{
VALUE str;
str = rb_str_new(0, 0);
lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_conf_value), (void*)str);
return str;
}
static VALUE
ossl_config_to_s(VALUE self)
{
CONF *conf;
GetConfig(self, conf);
return dump_conf(conf);
}
static VALUE
ossl_config_inspect(VALUE self)
{
VALUE str, ary = ossl_config_get_sections(self);
char *cname = rb_class2name(rb_obj_class(self));
str = rb_str_new2("#<");
rb_str_cat2(str, cname);
rb_str_cat2(str, " sections=");
rb_str_append(str, rb_inspect(ary));
rb_str_cat2(str, ">");
return str;
}
/*
* INIT
*/
void
Init_ossl_config()
{
eConfigError = rb_define_class_under(mOSSL, "ConfigError", eOSSLError);
cConfig = rb_define_class_under(mOSSL, "Config", rb_cObject);
rb_define_const(cConfig, "DEFAULT_CONFIG_FILE",
rb_str_new2(CONF_get1_default_config_file()));
rb_define_singleton_method(cConfig, "parse", ossl_config_s_parse, 1);
rb_define_alias(CLASS_OF(cConfig), "load", "new");
rb_define_alloc_func(cConfig, ossl_config_s_alloc);
rb_define_copy_func(cConfig, ossl_config_copy);
rb_define_method(cConfig, "initialize", ossl_config_initialize, -1);
rb_define_method(cConfig, "get_value", ossl_config_get_value, 2);
rb_define_method(cConfig, "value", ossl_config_get_value_old, -1);
rb_define_method(cConfig, "add_value", ossl_config_add_value, 3);
rb_define_method(cConfig, "[]", ossl_config_get_section, 1);
rb_define_method(cConfig, "section", ossl_config_get_section_old, 1);
rb_define_method(cConfig, "[]=", ossl_config_set_section, 2);
rb_define_method(cConfig, "sections", ossl_config_get_sections, 0);
rb_define_method(cConfig, "to_s", ossl_config_to_s, 0);
rb_define_method(cConfig, "inspect", ossl_config_inspect, 0);
}
|