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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
|
%% options
copyright owner = Dirk Krause
copyright year = 2012-xxxx
license = bsd
%% module
#include "dk3all.h"
#include "itadmin.h"
$!trace-include
/** Key names in configuration file.
*/
static dkChar const * const itadmin_config_keys[] = {
$!string-table macro=dkT
#
# 0: Database host
#
database-host
#
# 1: Database name
#
database-name
#
# 2: my.cnf file containing credential for database connection
#
database-credentials-file
#
# 3: VLAN for which we have to write a dhcpd.conf file
#
dhcpd.conf-vlan
#
# 4: Organization name
#
organization
#
# 5: Organizational unit name
#
organizational-unit
#
# 6: Administrator name
#
administrator-name
#
# 7: Flag: Create dhcpd.conf file
#
create-dhcpd.conf
#
# 8: Database type
#
database-type
#
# 9: LDAP base
#
ldap-base
#
# 10: use koma-script
#
use-koma-script
#
# 11: release lease on shutdown
#
msft-release-lease-on-shutdown
$!end
};
/** Items for "msft release lease on shutdown".
*/
static const char * const msft_release_keys[] = {
"none", "pool", "all", NULL
};
/** Names of SQL database types.
*/
static dkChar const * const itadmin_config_database_types[] = {
$!string-table macro=dkT
#
# 0: MySQL
#
MySQL
$!end
};
/** Keywords to watch out for when processing a credential file.
Typically this is a .my.cnf file.
*/
static dkChar const * const itadmin_config_credential_keys[] = {
$!string-table macro=dkT
#
# 0: Section title "client"
#
client
#
# 1: Key name "user"
#
user
#
# 2: Key name "password"
#
password
$!end
};
/** Set one string entry in job structure.
@param job Job structure to modify.
@param resptr Pointer to result pointer.
@param srcptr Source string.
@return 1 on success, 0 on error.
*/
static
int
itadmin_config_set_string(
itadmin_job *job,
dkChar const **resptr,
dkChar const *srcptr
)
{
dkChar const *np; /* New string duplicate. */
int back = 0;
$? "+ itadmin_config_set_string \"%!ds\"", TR_STR(srcptr)
np = dk3str_dup_app(srcptr,job->app);
if(np) {
dk3_release(*resptr);
*resptr = np;
back = 1;
} else {
/* ERROR: Memory allocation failed! */
} $? "- itadmin_config_set_string %d", back
return back;
}
/** Configure a boolean value setting.
@param job Job structure.
@param bp Pointer to result variable.
@param defv Default value, used for empty or non-boolean strings.
@param txt Configuration text.
@return 1 on success, 0 on error.
*/
static
int
itadmin_config_set_bool(itadmin_job *job, int *bp, int defv, dkChar const *txt)
{
dkChar lb[ITADMIN_CONFIG_LINE_SIZE]; /* Copy of value. */
dkChar *p1; /* Start of text in copy. */
int back = 1;
$? "+ itadmin_config_set_bool \"%!ds\"", TR_STR(txt)
if(txt) {
p1 = dk3str_start(txt, NULL);
if(p1) {
if(dk3str_len(p1) < DK3_SIZEOF(lb,dkChar)) {
dk3str_cpy_not_overlapped(lb, p1);
dk3str_normalize(lb,NULL,dkT(' '));
if(dk3str_is_bool(lb)) {
*bp = ((dk3str_is_on(lb)) ? 1 : 0);
} else { $? "! not a boolean"
back = 0;
*bp = defv;
/* ERROR: Not a boolean! */
dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 106, 107, lb);
}
} else { $? "! text too long"
back = 0;
*bp = defv;
/* ERROR: Text too long! */
dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 108, 109, p1);
}
} else { $? ". empty text"
*bp = defv;
}
} else { $? ". no text"
*bp = defv;
} $? "- itadmin_config_set_bool %d", back
return back;
}
/** Set database type.
@param job Job structure.
@param txt Text for database type (at this time only "MySQL"
can be used).
@return 1 on success, 0 on error.
*/
static
int
itadmin_config_set_dbtype(itadmin_job *job, dkChar const *txt)
{
int back = 0;
int i; /* Index of type in array of known types. */
if(txt) {
i = dk3str_array_index(itadmin_config_database_types, txt, 0);
if(i >= 0) {
job->dbt = i;
back = 1;
} else {
/* ERROR: No such database type! */
dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 110, 111, txt);
}
} else {
/* ERROR: Empty text. */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 112);
}
return back;
}
/** Handler function for one configuration file line.
@param obj Reader object.
@param il Input line to process (constant text).
@return 1 for OK, 0 for recoverable error, -1 for abort.
*/
static
int
itadmin_config_const_line_handler(void *obj, dkChar const *il)
{
dkChar line[ITADMIN_CONFIG_LINE_SIZE]; /* Private copy of line.*/
itadmin_job *job;
dkChar *p1; /* Start of line. */
dkChar *p2; /* Start of value in line. */
int back = 0;
$? "+ itadmin_config_const_line_handler \"%!ds\"", TR_STR(il)
if((obj) && (il)) {
job = (itadmin_job *)obj;
if(dk3str_len(il) < DK3_SIZEOF(line,dkChar)) {
dk3str_cpy_not_overlapped(line, il);
dk3str_delnl(line);
p1 = dk3str_start(line, NULL);
if(p1) {
if(*p1 == dkT('#')) {
back = 1; /* Comment line. */
} else {
p2 = dk3str_chr(p1, dkT('='));
if(p2) {
*(p2++) = dkT('\0');
p2 = dk3str_start(p2, NULL);
if(p2) {
dk3str_normalize(p1, NULL, dkT('-'));
switch(dk3str_array_index(itadmin_config_keys, p1, 0)) {
case 0: { /* database host */
back = itadmin_config_set_string(job, &(job->dbhn), p2);
} break;
case 1: { /* database name */
back = itadmin_config_set_string(job, &(job->dbn), p2);
} break;
case 2: { /* credentials file */
back = itadmin_config_set_string(job, &(job->dbcf), p2);
} break;
case 3: { /* VLAN name */
back = itadmin_config_set_string(job, &(job->vlan), p2);
if(back) {
job->f_dh = 1;
}
} break;
case 4: { /* organization */
back = itadmin_config_set_string(job, &(job->org), p2);
} break;
case 5: { /* organizational unit */
back = itadmin_config_set_string(job, &(job->ou), p2);
} break;
case 6: { /* administrator name */
back = itadmin_config_set_string(job, &(job->admn), p2);
} break;
case 7: {
back = itadmin_config_set_bool(job, &(job->f_dh), 1, p2);
} break;
case 8: {
back = itadmin_config_set_dbtype(job, p2);
} break;
case 9: {
dk3str_normalize(p2, NULL, ' ');
back = itadmin_config_set_string(job, &(job->ldapb), p2);
} break;
case 10: {
if(dk3str_is_bool(p2)) {
back = 1;
job->ukoma = ((dk3str_is_on(p2)) ? 1 : 0);
} else {
/* ERROR: Boolean expected! */
dk3app_log_3(job->app,DK3_LL_ERROR,job->msg,106,107,p2);
}
} break;
case 11: {
switch(dk3str_array_index(msft_release_keys, p2, 0)) {
case 0: {
job->rel = 0; back = 1;
} break;
case 1: {
job->rel = 1; back = 1;
} break;
case 2: {
job->rel = 2; back = 1;
} break;
default: {
/* ##### ERROR: Illegal keyword */
} break;
}
} break;
default: {
/* ERROR: Unknown option ... */
dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 113, 114, p1);
} break;
}
} else {
/* ERROR: No value! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 115);
}
} else {
/* ERROR: Missing '=' */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 115);
}
}
} else {
back = 1; /* Empty line. */
}
} else {
/* ERROR: Input line too long! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 116);
}
} $? "- itadmin_config_const_line_handler %d", back
return back;
}
/** Handler function for one configuration file line.
@param obj Reader object.
@param il Input line to process.
@return 1 for OK, 0 for recoverable error, -1 for abort.
*/
static
int
itadmin_cred_file_const_line_handler(void *obj, dkChar const *il)
{
dkChar line[ITADMIN_CONFIG_LINE_SIZE]; /* Copy of line. */
dkChar *p1; /* Start of key. */
dkChar *p2; /* Start of value. */
itadmin_cred_reader *reader; /* Help structure to read creds. */
int back = 0;
$? "+ itadmin_cred_file_const_line_handler \"%!ds\"", il
if((obj) && (il)) { $? ". objects ok"
reader = (itadmin_cred_reader *)obj;
if(dk3str_len(il) < DK3_SIZEOF(line,dkChar)) { $? ". length ok"
dk3str_cpy_not_overlapped(line, il);
dk3str_delnl(line);
p1 = dk3str_start(line, NULL);
if(p1) { $? ". p1"
if(*p1 == dkT('#')) { $? ". comment"
back = 1; /* Ignore comment. */
} else { $? ". no comment"
if(*p1 == dkT('[')) { $? ". section"
reader->f_clnt = 0;
p1++;
p2 = dk3str_chr(p1, dkT(']'));
if(p2) { $? ". section end ok"
back = 1;
*p2 = dkT('\0');
if(dk3str_cmp(p1, itadmin_config_credential_keys[0]) == 0) {
reader->f_clnt = 1;
}
} else { $? "! no section end"
/* ERROR: Syntax! */
dk3app_log_1(
(reader->job)->app, DK3_LL_ERROR, (reader->job)->msg, 117
);
}
} else { $? ". not a section"
back = 1;
p2 = dk3str_chr(p1, dkT('='));
if(p2) { $? ". value found"
*(p2++) = dkT('\0');
p2 = dk3str_start(p2, NULL);
if(p2) { $? ". value found"
dk3str_normalize(p1,NULL,dkT(' '));
switch(dk3str_array_index(itadmin_config_credential_keys,p1,0))
{
case 1: { /* user */ $? ". user"
back = itadmin_config_set_string(
reader->job, &((reader->job)->dbus), p2
);
} break;
case 2: { /* password */ $? ". password"
back = itadmin_config_set_string(
reader->job, &((reader->job)->dbpw), p2
);
} break;
}
} else { $? "! no value"
/* Empty key. */
}
} else { $? "! no value"
/* Not a key=value line. */
}
}
}
} else { $? ". empty line"
back = 1; /* Ignore empty line. */
}
} else { $? "! length"
/* ERROR: Line too long! */
dk3app_log_1(
(reader->job)->app, DK3_LL_ERROR, (reader->job)->msg, 116
);
}
} else { $? "! args"
} $? "- itadmin_cred_file_const_line_handler %d", back
return back;
}
/** Handler function for one configuration file line.
@param obj Reader object.
@param il Input line to process.
@return 1 for OK, 0 for recoverable error, -1 for abort.
*/
static
int
itadmin_cred_file_line_handler(void *obj, dkChar *il)
{
int back;
back = itadmin_cred_file_const_line_handler(obj, il);
return back;
}
/** Handler function for one configuration file line.
@param obj Reader object.
@param il Input line to process.
@return 1 for OK, 0 for recoverable error, -1 for abort.
*/
static
int
itadmin_config_line_handler(void *obj, dkChar *il)
{
int back;
back = itadmin_config_const_line_handler(obj, il);
return back;
}
/** Process one configuration file.
@param job Job structure.
@param fn File name to process.
@return 1 on success, 0 on error.
*/
static
int
itadmin_config_process_one_file(itadmin_job *job, dkChar const *fn)
{
dkChar line[ITADMIN_CONFIG_LINE_SIZE]; /* Copy of line. */
int back = 0;
$? "+ itadmin_config_process_one_file %!ds", TR_STR(fn)
back = dk3stream_process_filename_lines_app(
(void *)job,
itadmin_config_line_handler,
fn,
line,
DK3_SIZEOF(line,dkChar),
dk3app_get_encoding(job->app),
dk3app_get_input_file_encoding(job->app),
job->app
);
if(back < 1) {
back = 0;
} $? "- itadmin_config_process_one_file %d", back
return back;
}
/** Read credentials file to obtain database user name and password.
@param job Job structure.
@return 1 on success, 0 on error.
*/
static
int
itadmin_config_read_credentials(itadmin_job *job)
{
dkChar fnb[DK3_MAX_PATH]; /* File name buffer. */
dkChar line[ITADMIN_CONFIG_LINE_SIZE]; /* Input line buffer. */
itadmin_cred_reader reader; /* Helper structure to read creds. */
int back = 0;
$? "+ itadmin_config_read_credentials"
if(dk3str_len(job->dbcf) < DK3_SIZEOF(fnb,dkChar)) {
dk3str_cpy_not_overlapped(fnb, job->dbcf);
dk3str_correct_filename(fnb);
if(dk3sf_must_expand(fnb)) {
/* ERROR: File name must not contain wildcards! */
dk3app_log_3(job->app, DK3_LL_ERROR, job->msg, 118, 119, fnb);
} else {
if(!dk3str_is_abs_path(fnb)) {
/* WARNING: File name should be specified as absolute path! */
dk3app_log_3(job->app, DK3_LL_WARNING, job->msg, 120, 121, fnb);
}
reader.job = job;
reader.f_clnt = 1; $? ". really read credentials"
back = dk3stream_process_filename_sanitized_lines_app(
(void *)(&reader),
itadmin_cred_file_line_handler,
fnb,
line,
DK3_SIZEOF(line,dkChar),
dk3app_get_encoding(job->app),
dk3app_get_input_file_encoding(job->app),
job->app
);
}
} else {
/* ERROR: File name too long! */
dk3app_log_i3(job->app, DK3_LL_ERROR, 65, 66, job->dbcf);
}
if(job->ec) { $? "! any error during run"
back = 0;
} $? "- itadmin_config_read_credentials %d", back
return back;
}
int
itadmin_config_read(itadmin_job *job)
{
dkChar fnb[DK3_MAX_PATH]; /* Copy of file name. */
dk3_search_t *res; /* Search result, config. */
dk3_dir_t *fne; /* File name expander. */
dk3_stat_t const *es; /* Expandend file status. */
dkChar const *fn; /* File name. */
dkChar const *en; /* Expanded file name. */
dkChar const *fnorig; /* Original file name. */
int back = 0;
int have_file = 0; /* Flag: File found. */
int have_error = 0; /* Flag: Error occured. */
int argc; /* Number of file names. */
int i; /* Current file name index. */
$? "+ itadmin_config_read"
/*
Process default configuration files.
*/
res = dk3app_find_config_file(job->app, (job->noloc)[4], 0);
if(res) {
dk3search_reset(res);
while((fn = dk3search_next(res)) != NULL) {
have_file = 1;
if(!itadmin_config_process_one_file(job, fn)) {
have_error = 1;
}
}
dk3search_close(res);
}
/*
Process configuration file(s) specified on command line.
*/
argc = dk3opt_get_num_args(job->opt);
for(i = 0; i < argc; i++) {
fnorig = dk3opt_get_arg(job->opt, i);
if(fnorig) {
if(dk3str_len(fnorig) < DK3_SIZEOF(fnb,dkChar)) {
dk3str_cpy_not_overlapped(fnb, fnorig);
dk3str_correct_filename(fnb);
if(dk3sf_must_expand(fnb)) {
fne = dk3dir_fne_open_app(fnb, job->app);
if(fne) {
while(dk3dir_get_next_file(fne)) {
en = dk3dir_get_fullname(fne);
es = dk3dir_get_stat(fne);
if((en) && (es)) {
switch((es->ft) & (~(DK3_FT_SYMLINK))) {
case DK3_FT_REGULAR: {
have_file = 1;
if(!itadmin_config_process_one_file(job, fnb)) {
have_error = 1;
}
} break;
default: {
have_error = 1;
/* ERROR: Wrong file type! */
dk3app_log_i3(job->app, DK3_LL_ERROR, 255, 256, en);
} break;
}
} else {
/* BUG: No complete information available! */
}
}
dk3dir_close(fne);
} else {
/* ERROR: Failed to expand file name! */
}
} else {
have_file = 1;
if(!itadmin_config_process_one_file(job, fnb)) {
have_error = 1;
}
}
} else {
/* ERROR: File name fnorig too long! */
dk3app_log_i3(job->app, DK3_LL_ERROR, 65, 66, fnorig);
}
} else {
/* BUG: No pointer returned! */
}
}
/*
Final tests and error messages.
*/
if(have_file) { $? ". files found"
if(!(have_error)) { $? ". no errors"
back = 1;
if(!(job->dbhn)) { $? "! db host"
/* ERROR: Missing database host name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 122);
back = 0;
}
if(!(job->dbn)) { $? "! db name"
/* ERROR: Missing database name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 123);
back = 0;
}
if(!(job->vlan)) { $? "! vlan"
/* Warning: Missing VLAN name! */
dk3app_log_1(job->app, DK3_LL_WARNING, job->msg, 124);
}
if(!(job->org)) { $? "! org"
/* ERROR: Missing organization name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 125);
back = 0;
}
if(!(job->ou)) { $? "! ou"
/* ERROR: Missing organizational unit name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 126);
back = 0;
}
if(!(job->admn)) { $? "! admn"
/* ERROR: Missing administrator name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 127);
back = 0;
}
if(!(job->dbcf)) { $? "! dbcf"
/* ERROR: Missing database credentials file name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 128);
back = 0;
} else { $? ". dbcf"
if(back) { $? ". back"
back = itadmin_config_read_credentials(job);
if(back) {
if(!(job->dbus)) { $? "! dbus"
/* ERROR: Missing database user name! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 129);
back = 0;
}
if(!(job->dbpw)) { $? "! dbpw"
/* ERROR: Missing database user password! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 130);
back = 0;
}
}
}
}
if(back) {
if(!(job->ukoma)) {
if(dk3str_casecmp(dkT("de"), dk3app_get_language(job->app)) == 0) {
dk3app_log_1(job->app, DK3_LL_INFO, job->msg, 302);
}
}
}
if(!(back)) {
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 132);
}
} else { $? "! errors found"
}
} else { $? "! no files"
/* ERROR: No configuration file found for processing! */
dk3app_log_1(job->app, DK3_LL_ERROR, job->msg, 131);
} $? "- itadmin_config_read %d", back
return back;
}
|