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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
|
/* -*- show-trailing-whitespace: t; indent-tabs: t -*-
* Copyright (c) 2003,2004,2005,2006 David Lichteblau
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _XOPEN_SOURCE
#define _POSIX_C_SOURCE (200809L)
#include <unistd.h>
#include <crypt.h>
#include <stdio.h>
#include "common.h"
#define fast_g_string_append_c(gstring, c) \
do { \
if ((gstring)->len + 1 >= (gstring)->allocated_len) \
g_string_append_c((gstring), (c)); \
else { \
(gstring)->str[(gstring)->len++] = (c); \
(gstring)->str[(gstring)->len] = 0; \
} \
} while (0)
static int
read_lhs(FILE *s, GString *lhs)
{
int c;
for (;;) {
switch ( c = getc_unlocked(s)) {
case ' ':
if (ferror(s)) syserr();
return 0;
case EOF:
fputs("Error: Unexpected EOF.\n", stderr);
return -1;
case '\n':
fputs("Error: Unexpected EOL.\n", stderr);
return -1;
case 0:
fputs("Error: Null byte not allowed.\n", stderr);
return -1;
default:
fast_g_string_append_c(lhs, c);
}
}
}
static int
read_backslashed(FILE *s, GString *data)
{
int c;
for (;;) {
switch ( c = getc_unlocked(s)) {
case '\n':
if (ferror(s)) syserr();
return 0;
case EOF:
goto error;
case '\\':
if ( (c = fgetc(s)) == EOF) goto error;
/* fall through */
default:
fast_g_string_append_c(data, c);
}
}
error:
fputs("Error: Unexpected EOF.\n", stderr);
return -1;
}
static int
read_ldif_attrval(FILE *s, GString *data)
{
int c;
for (;;)
switch ( c = getc_unlocked(s)) {
case '\n':
if ( (c = fgetc(s)) == ' ') /* folded line */ break;
ungetc(c, s);
if (ferror(s)) syserr();
return 0;
case EOF:
fputs("Error: Unexpected EOF.\n", stderr);
return -1;
default:
fast_g_string_append_c(data, c);
}
}
static int
read_from_file(GString *data, char *name)
{
int fd, n;
if ( (fd = open(name, O_RDONLY)) == -1) {
perror("open");
return -1;
}
data->len = 0;
n = 1024;
do {
int olen = data->len;
g_string_set_size(data, data->len + n);
if ( (n = read(fd, data->str + olen, n)) == -1) syserr();
data->len = olen + n;
} while (n > 0);
if (close(fd) == -1) syserr();
return 0;
}
static int
skip_comment(FILE *s)
{
int c;
for (;;)
switch ( c = fgetc(s)) {
case EOF:
fputs("Error: Unexpected EOF.\n", stderr);
return -1;
case '\n':
if ( (c = fgetc(s)) == ' ') /* folded line */ break;
ungetc(c, s);
if (ferror(s)) syserr();
return 0;
}
}
static char *saltbag
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890./";
static char *
cryptdes(char *key)
{
unsigned char salt[2];
int fd = open("/dev/random", 2);
if (fd == -1) {
puts("Sorry, crypt not available: Cannot open /dev/random.");
return 0;
}
if (read(fd, salt, 2) != 2) syserr();
close(fd);
salt[0] = saltbag[salt[0] & 63];
salt[1] = saltbag[salt[1] & 63];
return crypt(key, (char *) salt);
}
static char *
cryptmd5(char *key)
{
char *result;
unsigned char salt[11];
int i;
int fd = open("/dev/random", 2);
if (fd == -1) {
puts("Sorry, MD5 not available: Cannot open /dev/random.");
return 0;
}
salt[0] = '$';
salt[1] = '1';
salt[2] = '$';
if (read(fd, salt + 3, 8) != 8) syserr();
close(fd);
for (i = 3; i < 11; i++)
salt[i] = saltbag[salt[i] & 63];
result = crypt(key, (char *) salt);
if (!result || strlen(result) < 25) {
puts("Sorry, MD5 not available: Are you using the glibc?");
return 0;
}
return result;
}
/*
* Read a line in
* name ' ' (':' encoding)? value '\n'
* syntax, skipping comments. VALUE is parsed according to ENCODING.
* Empty NAME is allowed.
*
* 0: ok
* -1: fatal parse error
* -2: end of file or empty line
*/
static int
read_line1(FILE *s, GString *name, GString *value)
{
int c;
char *encoding;
g_string_truncate(name, 0);
g_string_truncate(value, 0);
/* skip comment lines */
do {
c = fgetc(s);
switch (c) {
case EOF:
if (ferror(s)) syserr();
return -2;
case '\n':
return -2;
case '#':
if (skip_comment(s) == -1) return -1;
break;
default:
ungetc(c, s);
c = -1;
}
} while (c != -1);
if (read_lhs(s, name) == -1) return -1;
if ( encoding = memchr(name->str, ':', name->len)) {
encoding++;
name->len = encoding - name->str - 1;
name->str[name->len] = 0;
}
if (!encoding || !strcmp(encoding, ";")) {
if (read_backslashed(s, value) == -1) return -1;
} else if (!*encoding) {
if (read_ldif_attrval(s, value) == -1) return -1;
} else if (!strcmp(encoding, ":")) {
unsigned char *ustr;
int len;
if (read_ldif_attrval(s, value) == -1) return -1;
ustr = (unsigned char *) value->str;;
if ( (len = read_base64(value->str, ustr, value->len)) == -1) {
fputs("Error: Invalid Base64 string.\n", stderr);
return -1;
}
value->len = len;
} else if (!strcmp(encoding, "<")) {
if (read_ldif_attrval(s, value) == -1) return -1;
if (strncmp(value->str, "file://", 7)) {
fputs("Error: Unknown URL scheme.\n", stderr);
return -1;
}
if (read_from_file(value, value->str + 7) == -1)
return -1;
} else if (!strcasecmp(encoding, "crypt")) {
char *hash;
if (read_ldif_attrval(s, value) == -1) return -1;
if ( !(hash = cryptdes(value->str))) return -1;
g_string_assign(value, "{CRYPT}");
g_string_append(value, hash);
} else if (!strcasecmp(encoding, "cryptmd5")) {
char *hash;
if (read_ldif_attrval(s, value) == -1) return -1;
if ( !(hash = cryptmd5(value->str))) return -1;
g_string_assign(value, "{CRYPT}");
g_string_append(value, hash);
} else if (!strcasecmp(encoding, "sha")) {
if (read_ldif_attrval(s, value) == -1) return -1;
g_string_assign(value, "{SHA}");
if (!g_string_append_sha(value, value->str)) return -1;
} else if (!strcasecmp(encoding, "ssha")) {
if (read_ldif_attrval(s, value) == -1) return -1;
g_string_assign(value, "{SSHA}");
if (!g_string_append_ssha(value, value->str)) return -1;
} else if (!strcasecmp(encoding, "md5")) {
if (read_ldif_attrval(s, value) == -1) return -1;
g_string_assign(value, "{MD5}");
if (!g_string_append_md5(value, value->str)) return -1;
} else if (!strcasecmp(encoding, "smd5")) {
if (read_ldif_attrval(s, value) == -1) return -1;
g_string_assign(value, "{SMD5}");
if (!g_string_append_smd5(value, value->str)) return -1;
} else {
char *ptr;
int n = strtol(encoding, &ptr, 10);
if (*ptr) {
fputs("Error: Unknown value encoding.\n", stderr);
return -1;
}
g_string_set_size(value, n);
if (fread(value->str, 1, n, s) != n) syserr();
}
return 0;
}
/*
* Read a line in
* name ' ' (':' encoding)? value '\n'
* syntax, skipping comments. VALUE is parsed according to ENCODING.
* Empty NAME is a parse error.
*
* 0: ok if name->len != 0
* 0: end of file or empty line if name->len == 0
* -1: parse error
*/
static int
read_line(FILE *s, GString *name, GString *value)
{
int rc = read_line1(s, name, value);
switch (rc) {
case -2:
return 0;
case -1:
return -1;
case 0:
if (!name->len) {
fputs("Error: Space at beginning of line.\n", stderr);
return -1;
}
return 0;
default:
abort();
}
}
static char *
read_rename_body(FILE *s, GString *tmp1, GString *tmp2, int *deleteoldrdn)
{
char *dn;
if (read_line(s, tmp1, tmp2) == -1)
return 0;
if (!tmp1->len) {
fputs("Error: Rename record lacks dn line.\n", stderr);
return 0;
}
*deleteoldrdn = !strcmp(tmp1->str, "replace");
if (!*deleteoldrdn && strcmp(tmp1->str, "add")) {
fputs("Error: Expected 'add' or 'replace' in rename record.\n",
stderr);
return 0;
}
dn = xdup(tmp2->str);
if (read_line(s, tmp1, tmp2) == -1) {
free(dn);
return 0;
}
if (tmp1->len) {
free(dn);
fputs("Error: Garbage at end of rename record.\n", stderr);
return 0;
}
return dn;
}
static int
read_nothing(FILE *s, GString *tmp1, GString *tmp2)
{
if (read_line(s, tmp1, tmp2) == -1)
return -1;
if (tmp1->len) {
fputs("Error: Garbage at end of record.\n", stderr);
return -1;
}
return 0;
}
static LDAPMod *
ldapmod4line(char *action, char *ad)
{
LDAPMod *m;
int op;
if (!strcmp(action, "add"))
op = LDAP_MOD_ADD;
else if (!strcmp(action, "delete"))
op = LDAP_MOD_DELETE;
else if (!strcmp(action, "replace"))
op = LDAP_MOD_REPLACE;
else {
fputs("Error: Invalid change marker.\n", stderr);
return 0;
}
m = xalloc(sizeof(LDAPMod));
m->mod_op = op | LDAP_MOD_BVALUES;
m->mod_type = xdup(ad);
return m;
}
static LDAPMod **
read_modify_body(FILE *s, GString *tmp1, GString *tmp2)
{
LDAPMod **result;
GPtrArray *mods = g_ptr_array_new();
GPtrArray *values;
LDAPMod *m = 0;
for (;;) {
switch (read_line1(s, tmp1, tmp2)) {
case 0:
break;
case -1:
goto error;
case -2:
if (m) {
g_ptr_array_add(values, 0);
m->mod_bvalues = (void *) values->pdata;
g_ptr_array_free(values, 0);
values = 0;
}
goto done;
default:
abort();
}
if (tmp1->len) {
if (m) {
g_ptr_array_add(values, 0);
m->mod_bvalues = (void *) values->pdata;
g_ptr_array_free(values, 0);
values = 0;
}
values = g_ptr_array_new();
if ( !(m = ldapmod4line(tmp1->str, tmp2->str)))
goto error;
g_ptr_array_add(mods, m);
} else
g_ptr_array_add(values, gstring2berval(tmp2));
}
done:
g_ptr_array_add(mods, 0);
result = (LDAPMod **) mods->pdata;
g_ptr_array_free(mods, 0);
return result;
error:
g_ptr_array_free(mods, 1);
if (values) {
int i;
for (i = 0; i < values->len; i++)
xfree_berval(values->pdata[i]);
g_ptr_array_free(values, 0);
}
return 0;
}
/*
* Lies die erste Zeile eines beliebigen Records nach position `offset' in `s'.
* Setze *pos (falls pos != 0).
* Liefere 0 bei Erfolg, -1 sonst.
* Bei Erfolg:
* - pos ist die exakte Anfangsposition.
* - Setze *key auf den Schluessel (falls key != 0).
* - Setze *dn auf den Distinguished Name (falls dn != 0).
* EOF ist kein Fehler und liefert *key = 0 (falls key != 0);
*/
static int
read_header(GString *tmp1, GString *tmp2,
FILE *s, long offset, char **key, char **dn, long *pos)
{
char **rdns = 0;
if (offset != -1)
if (fseek(s, offset, SEEK_SET) == -1) syserr();
do {
if (pos)
if ( (*pos = ftell(s)) == -1) syserr();
if (read_line(s, tmp1, tmp2) == -1) return -1;
if (tmp1->len == 0 && feof(s)) {
if (key) *key = 0;
return 0;
}
if (!strcmp(tmp1->str, "version")) {
if (strcmp(tmp2->str, "ldapvi")) {
fputs("Error: Invalid file format.\n", stderr);
return -1;
}
tmp1->len = 0;
}
} while (!tmp1->len);
rdns = ldap_explode_dn(tmp2->str, 0);
if (!rdns) {
fputs("Error: Invalid distinguished name string.\n", stderr);
return -1;
}
if (key) *key = xdup(tmp1->str);
if (dn) *dn = xdup(tmp2->str);
ldap_value_free(rdns);
return 0;
}
static int
read_attrval_body(GString *tmp1, GString *tmp2, FILE *s, tentry *entry)
{
for (;;) {
tattribute *attribute;
if (read_line(s, tmp1, tmp2) == -1)
return -1;
if (!tmp1->len)
break;
attribute = entry_find_attribute(entry, tmp1->str, 1);
attribute_append_value(attribute, tmp2->str, tmp2->len);
}
return 0;
}
/*
* Lies ein attrval-record nach position `offset' in `s'.
* Setze *pos (falls pos != 0).
* Liefere 0 bei Erfolg, -1 sonst.
* Bei Erfolg:
* - pos ist die exakte Anfangsposition.
* - Setze *entry auf den gelesenen Eintrag (falls entry != 0).
* - Setze *key auf den Schluessel (falls key != 0).
* EOF ist kein Fehler und liefert *key = 0 (falls key != 0);
*/
int
read_entry(FILE *s, long offset, char **key, tentry **entry, long *pos)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
char *dn;
char *k = 0;
tentry *e = 0;
int rc = read_header(tmp1, tmp2, s, offset, &k, &dn, pos);
if (rc || !k) goto cleanup;
e = entry_new(dn);
rc = read_attrval_body(tmp1, tmp2, s, e);
if (!rc) {
if (entry) {
*entry = e;
e = 0;
}
if (key) {
*key = k;
k = 0;
}
}
cleanup:
if (k) free(k);
if (e) entry_free(e);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
/*
* Lies die erste Zeile eines beliebigen Records nach position `offset' in `s'.
* Setze *pos (falls pos != 0).
* Liefere 0 bei Erfolg, -1 sonst.
* Bei Erfolg:
* - pos ist die exakte Anfangsposition.
* - Setze *key auf den Schluessel (falls key != 0).
*/
int
peek_entry(FILE *s, long offset, char **key, long *pos)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
int rc = read_header(tmp1, tmp2, s, offset, key, 0, pos);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
/*
* Lies ein rename-record nach position `offset' in `s'.
* Liefere 0 bei Erfolg, -1 sonst.
* Bei Erfolg:
* - Setze *dn1 auf den alten DN.
* - Setze *dn2 auf den neuen DN.
* - *deleteoldrdn auf 1 oder 0;
*/
int
read_rename(FILE *s, long offset, char **dn1, char **dn2, int *deleteoldrdn)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
char *olddn;
char *newdn;
int rc = read_header(tmp1, tmp2, s, offset, 0, &olddn, 0);
if (rc) {
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
newdn = read_rename_body(s, tmp1, tmp2, deleteoldrdn);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
if (!newdn) {
free(olddn);
return -1;
}
if (dn1) *dn1 = olddn; else free(olddn);
if (dn2) *dn2 = newdn; else free(newdn);
return 0;
}
int
read_delete(FILE *s, long offset, char **dn)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
char *str;
int rc = read_header(tmp1, tmp2, s, offset, 0, &str, 0);
if (rc) {
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
rc = read_nothing(s, tmp1, tmp2);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
if (rc == -1)
free(str);
else
*dn = str;
return rc;
}
/*
* Lies ein modify-record nach position `offset' in `s'.
* Liefere 0 bei Erfolg, -1 sonst.
* Bei Erfolg:
* - Setze *dn auf den DN.
* - Setze *mods auf die Aenderungen.
*/
int
read_modify(FILE *s, long offset, char **dn, LDAPMod ***mods)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
char *d;
LDAPMod **m;
int rc = read_header(tmp1, tmp2, s, offset, 0, &d, 0);
if (rc) {
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
m = read_modify_body(s, tmp1, tmp2);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
if (!m) {
free(d);
return -1;
}
if (dn) *dn = d; else free(d);
if (mods) *mods = m; else ldap_mods_free(m, 1);
return 0;
}
/*
* Parse a complete entry or changerecord and ignore it. Set *key accordingly.
* Leave the stream positioned after the entry.
*
* Treat EOF as success and set *key to NULL.
*
* return value:
* 0 on success
* -1 on parse error
*/
/*
* FIXME: Warum lesen wir hier nicht einfach bis zur naechsten leeren Zeile?
*/
int
skip_entry(FILE *s, long offset, char **key)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
char *k = 0;
int rc = read_header(tmp1, tmp2, s, offset, &k, 0, 0);
if (rc || !k)
;
else if (!strcmp(k, "modify")) {
LDAPMod **mods = read_modify_body(s, tmp1, tmp2);
if (mods)
ldap_mods_free(mods, 1);
else
rc = -1;
} else if (!strcmp(k, "rename")) {
int dor;
char *newdn = read_rename_body(s, tmp1, tmp2, &dor);
if (newdn)
free(newdn);
else
rc = -1;
} else if (!strcmp(k, "delete"))
rc = read_nothing(s, tmp1, tmp2);
else {
tentry *e = entry_new(xdup(""));
rc = read_attrval_body(tmp1, tmp2, s, e);
entry_free(e);
}
if (key) *key = k; else free(k);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
static int
read_profile_header(GString *tmp1, GString *tmp2, FILE *s, char **name)
{
do {
if (read_line(s, tmp1, tmp2) == -1) return -1;
if (tmp1->len == 0 && feof(s)) {
*name = 0;
return 0;
}
} while (!tmp1->len);
if (strcmp(tmp1->str, "profile")) {
fprintf(stderr,
"Error: Expected 'profile' in configuration,"
" found '%s' instead.\n",
tmp1->str);
return -1;
}
*name = xdup(tmp2->str);
return 0;
}
int
read_profile(FILE *s, tentry **entry)
{
GString *tmp1 = g_string_new("");
GString *tmp2 = g_string_new("");
char *name;
tentry *e = 0;
int rc = read_profile_header(tmp1, tmp2, s, &name);
if (rc || !name) goto cleanup;
e = entry_new(name);
rc = read_attrval_body(tmp1, tmp2, s, e);
if (!rc) {
*entry = e;
e = 0;
}
cleanup:
if (e) entry_free(e);
g_string_free(tmp1, 1);
g_string_free(tmp2, 1);
return rc;
}
tparser ldapvi_parser = {
read_entry,
peek_entry,
skip_entry,
read_rename,
read_delete,
read_modify,
print_ldapvi_entry
};
|