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 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
|
/*
* $Id$
*/
/************************************************************************
* *
* Copyright (C) 2003 *
* Internet2 *
* All Rights Reserved *
* *
************************************************************************/
/*
* File: conf.c
*
* Author: Jeff W. Boote
* Internet2
*
* Date: Tue Sep 9 16:13:25 MDT 2003
*
* Description:
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdlib.h>
#include <I2util/utilP.h>
#include <I2util/conf.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
/*
* Function: I2GetConfLine
*
* Description:
* Read a single line from a file fp. remove leading whitespace,
* skip blank lines and comment lines. Put the result in the
* char buffer pointed at by lbuf, growing it as necessary.
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2GetConfLine(
I2ErrHandle eh,
FILE *fp,
int rc,
char **lbuf,
size_t *lbuf_max
)
{
int c;
char *line = *lbuf;
size_t i=0;
while((c = fgetc(fp)) != EOF){
/*
* If c is a newline - increment the row-counter.
* If lbuf already has content - break out, otherwise
* this was a leading blank line, continue until there
* is content.
*/
if(c == '\n'){
rc++;
if(i) break;
continue;
}
/*
* swallow comment lines
*/
if(!i && c == '#'){
while((c = fgetc(fp)) != EOF){
if(c == '\n'){
rc++;
break;
}
}
continue;
}
/*
* swallow leading whitespace
*/
if(!i && isspace((int)c)){
continue;
}
/*
* Check for line continuation.
*/
if(c == '\\'){
if(fgetc(fp) == '\n'){
rc++;
continue;
}
I2ErrLogP(eh,EINVAL,"Invalid use of \'\\\'");
return -rc;
}
/*
* make sure lbuf is large enough for this content
*/
if(i+2 > *lbuf_max){
while(i+2 > *lbuf_max){
*lbuf_max += I2LINEBUFINC;
}
*lbuf = realloc(line,sizeof(char) * *lbuf_max);
if(!*lbuf){
if(line){
free(line);
}
I2ErrLog(eh,"realloc(%u): %M",*lbuf_max);
return -rc;
}
line = *lbuf;
}
/*
* copy char
*/
line[i++] = c;
}
if(!i){
return 0;
}
line[i] = '\0';
if(c == EOF){
rc++;
}
return rc;
}
/*
* Function: I2ReadConfVar
*
* Description:
* Read the next non-comment line from the config file. The file
* should be in the format of:
* key [value] [#blah comment]
*
* key and value are delineated by whitespace. All leading and
* trailing whitespace is ignored. A trailing comment is legal and
* all charactors between a # and the trailing \n are ignored.
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2ReadConfVar(
FILE *fp,
int rc,
char *key,
char *val,
size_t max,
char **lbuf,
size_t *lbuf_max
)
{
char *line;
if((rc = I2GetConfLine(NULL,fp,rc,lbuf,lbuf_max)) > 0){
/*
* Pull off key.
*/
if(!(line = strtok(*lbuf,I2WSPACESET))){
rc = -rc;
goto DONE;
}
/*
* Ensure key is not too long.
*/
if(strlen(line)+1 > max){
rc = -rc;
goto DONE;
}
strcpy(key,line);
if((line = strtok(NULL,I2WSPACESET))){
/*
* If there is no "value" for this key, then
* a comment is valid.
*/
if(*line == '#'){
val[0] = '\0';
goto DONE;
}
/*
* Ensure value is not too long.
*/
if(strlen(line)+1 > max){
rc = -rc;
goto DONE;
}
strcpy(val,line);
}
else{
val[0] = '\0';
}
/*
* Ensure there is no trailing data
*/
if((line = strtok(NULL,I2WSPACESET))){
/*
* Comments are the only valid token.
*/
if(*line != '#'){
rc = -rc;
}
}
}
DONE:
return rc;
}
/*
* This is very similar to I2GetConfLine but does not allow line
* continuation - and copies comment/blank lines to tofp.
*/
static int
parsefileline(
I2ErrHandle eh,
FILE *fp,
int rc,
char **lbuf,
size_t *lbuf_max,
FILE *tofp
)
{
int c;
size_t i;
char *line = *lbuf;
size_t nc=0; /* number of "significant" chars in the line */
size_t ns=0; /* number of leading spaces */
while((c = fgetc(fp)) != EOF){
/*
* If c is a newline - increment the row-counter.
* If lbuf already has content - break out, otherwise
* this was a blank line, continue until there
* is content.
*/
if(c == '\n'){
rc++;
if(nc) break;
/* don't worry about leading spaces for blank lines */
if(tofp) fprintf(tofp,"\n");
ns=0;
continue;
}
/*
* swallow comment lines
*/
if(!nc && c == '#'){
if(tofp){
/* preserve leading spaces */
for(i=0;i<ns;i++) fprintf(tofp," ");
fprintf(tofp,"#");
}
while((c = fgetc(fp)) != EOF){
if(tofp)
fprintf(tofp,"%c",c);
if(c == '\n'){
rc++;
break;
}
}
continue;
}
/*
* swallow leading whitespace
* (These will be preserved for comment lines - and removed
* for all other lines.)
*/
if(!nc && isspace((int)c)){
ns++;
continue;
}
/*
* Leading spaces are only allowed in comment lines
*/
if(ns){
I2ErrLog(eh,"Leading spaces not allowed: line %d",rc);
return -rc;
}
/*
* make sure lbuf is large enough for this content
*/
if(nc+2 > *lbuf_max){
while(nc+2 > *lbuf_max){
*lbuf_max += I2LINEBUFINC;
}
*lbuf = realloc(line,sizeof(char) * *lbuf_max);
if(!*lbuf){
if(line){
free(line);
}
I2ErrLog(eh,"realloc(%u): %M",*lbuf_max);
return -rc;
}
line = *lbuf;
}
/*
* copy char
*/
line[nc++] = c;
}
if(!nc){
return 0;
}
line[nc] = '\0';
if(c == EOF){
rc++;
}
return rc;
}
/*
* Function: I2ParseKeyFile
*
* Description:
* Read a single line from a file fp. remove leading whitespace,
* skip blank lines and comment lines. Put the result in the
* char buffer pointed at by lbuf, growing it as necessary.
*
* Read a single identity/key from the keyfile. If tofp is set,
* then copy all "unmatched" lines from fp to tofp while parsing
* the file. If id_query is set, only return the entry that
* matches (if any does) skipping all others - and copying them
* to tofp if needed. A quick way to simply copy all remaining
* records to the tofp is to specify an id_query to a 0 length
* string (i.e. id_query[0] == '\0').
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2ParseKeyFile(
I2ErrHandle eh,
FILE *fp,
int rc,
char **lbuf,
size_t *lbuf_max,
FILE *tofp,
const char *id_query,
char *id_ret, /* [I2MAXIDENTITYLEN+1] or null */
uint8_t *key_ret /* [I2KEYLEN] or null */
)
{
char *line;
int i;
char rbuf[I2MAXIDENTITYLEN+1]; /* add one extra byte */
char *keystart;
uint8_t kbuf[I2KEYLEN];
/*
* If there is no keyfile, parsing is very, very fast.
*/
if(!fp){
return 0;
}
/*
* Fetch each non-blank, non-comment line from the keyfile.
* completely validate each line and then determine at the
* end of the loop if the caller is interested in this line or not.
* (This strict interpretation of the syntax of the keyfile should
* help find errors as quickly as possible instead of letting them
* hide until they actually bite someone.)
*/
while((rc = parsefileline(eh,fp,rc,lbuf,lbuf_max,tofp)) > 0){
line = *lbuf;
i=0;
/*
* Can potentially copy I2MAXIDENTITYLEN+1 bytes: rbuf is
* sized to handle this and the next 'if' is setup to
* detect this error condition.
*/
while(i <= I2MAXIDENTITYLEN){
if(isspace((int)*line) || (*line == '\0')){
break;
}
rbuf[i++] = *line++;
}
if( i > I2MAXIDENTITYLEN){
I2ErrLogP(eh,EINVAL,"Invalid identity name (too long)");
return -rc;
}
rbuf[i] = '\0';
/*
* Get the hexkey
*/
while(isspace((int)*line)){
line++;
}
keystart = line;
i=0;
while(*line != '\0'){
if(isspace((int)*line)){
break;
}
i++;
line++;
}
/*
* If i is not equal to the hex-encoded length of a key...
*/
if(i != (I2KEYLEN*2)){
I2ErrLogP(eh,EINVAL,"Invalid key length");
return -rc;
}
/*
* Make sure the only thing trailing the key is a comment
* or whitespace.
*/
while(*line != '\0'){
if(*line == '#'){
break;
}
if(!isspace((int)*line)){
I2ErrLogP(eh,EINVAL,"Invalid chars after key");
return -rc;
}
line++;
}
if(!I2HexDecode(keystart,kbuf,I2KEYLEN)){
I2ErrLogP(eh,EINVAL,"Invalid key: not hex?");
return -rc;
}
/*
* If a specific "identity" is being searched for: skip/copy
* lines that don't match and continue parsing the file.
*/
if(id_query && strncmp(rbuf,id_query,I2MAXIDENTITYLEN)){
/*
* Write line to tofp, then 'continue'
*/
if(tofp) fprintf(tofp,"%s\n",*lbuf);
continue;
}
/*
* caller is interested in this record - return the values.
*/
if(id_ret){
strncpy(id_ret,rbuf,sizeof(rbuf));
}
if(key_ret){
memcpy(key_ret,kbuf,I2KEYLEN);
}
break;
}
return rc;
}
/*
* Function: I2WriteKeyLine
*
* Description:
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2WriteKeyLine(
I2ErrHandle eh,
FILE *fp,
const char *id,
const uint8_t *key
)
{
int ret;
char hbuf[(I2KEYLEN*2)+1]; /* size for hex version */
if(!id || (id[0] == '\0') || (strlen(id) > I2MAXIDENTITYLEN)){
I2ErrLogP(eh,EINVAL,"I2WriteKeyLine(): Invalid identity name");
return -1;
}
I2HexEncode(hbuf,key,I2KEYLEN);
/*
* if fprintf has an error, set ret < 0 for a failure return.
*/
ret = fprintf(fp,"%s\t%s\n",id,hbuf);
if(ret > 0) ret = 0;
return ret;
}
/*
* Function: I2ParsePFFile
*
* Description:
* Read a single line from a file fp. remove leading whitespace,
* skip blank lines and comment lines. Put the result in the
* char buffer pointed at by lbuf, growing it as necessary.
*
* Read a single identity/pass-phrase from the pffile. If tofp
* is set, then copy all "unmatched" lines from fp to tofp while
* parsing the file. If id_query is set, only return the entry that
* matches (if any does) skipping all others - and copying them
* to tofp if needed. A quick way to simply copy all remaining
* records to the tofp is to specify an id_query to a 0 length
* string (i.e. id_query[0] == '\0').
*
* The entry that is returned currently breaks the line, so
* *lbuf can not just be written back to the file directly.
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2ParsePFFile(
I2ErrHandle eh,
FILE *filep,
FILE *tofilep,
int rc,
const char *id_query,
char **id_ret, /* points in lbuf */
char **pf_ret, /* points in lbuf */
size_t *pf_len,
char **lbuf,
size_t *lbuf_max
)
{
char *line;
char *ts;
char *hex;
char *pf;
size_t id_len;
size_t hex_len;
size_t idq_len;
size_t hex_oset;
/*
* If there is no pffile, parsing is very, very fast.
*/
if(!filep){
return 0;
}
if(id_query){
idq_len = strlen(id_query);
}
/*
* Fetch each non-blank, non-comment line from the pffile.
*
* Completely validate each line and then determine at the
* end of the loop if the caller is interested in this line or not.
* (This strict interpretation of the syntax of the pffile should
* help find errors as quickly as possible instead of letting them
* hide until they actually bite someone.)
*
* Valid syntax is:
*
*/
while((rc = parsefileline(eh,filep,rc,lbuf,lbuf_max,tofilep)) > 0){
size_t used,needed;
/*
* line starts with id
*/
line = *lbuf;
/*
* Find beginning of hex-pf
*/
id_len = strcspn(line,I2WSPACESET);
hex = line + id_len;
/*
* Must be at least one space separator
*/
if(!isspace((int)*hex)){
return -rc;
}
/*
* If a specific "identity" is being searched for: skip/copy
* lines that don't match and continue parsing the file.
*/
if(id_query &&
((idq_len == 0) || strncmp(line,id_query,MIN(id_len,idq_len)))){
/*
* Write line to tofilep, then 'continue'
*/
if(tofilep) fprintf(tofilep,"%s\n",*lbuf);
continue;
}
/* Terminate id, then eat trailing white-space */
*hex = '\0';
hex++;
while(isspace((int)*hex)){
hex++;
}
hex_oset = hex - line;
/* how long is the hex-pf */
hex_len = strcspn(hex,I2WSPACESET);
/*
* eat trailing white-space - and terminate hex-pf
*/
ts = hex + hex_len;
while(isspace((int)*ts)){
*ts = '\0';
ts++;
}
/*
* hex can not be nul, must be multiple of 2,
* and must be the last thing on the line.
*/
if(!hex_len || (hex_len % 2) || (*ts != '\0')){
return -rc;
}
/*
* Make sure there is enough room after full line in lbuf to hold
* return pf. (ts currently points at the terminating nul
* byte for the full line.)
*/
used = (ts - *lbuf) + 1;
needed = (hex_len / 2);
/*
* make sure lbuf is large enough for this content
*/
if((used + needed) > *lbuf_max){
while((used + needed) > *lbuf_max){
*lbuf_max += I2LINEBUFINC;
}
*lbuf = realloc(line,sizeof(char) * *lbuf_max);
if(!*lbuf){
if(line){
free(line);
}
I2ErrLog(eh,"realloc(%u): %M",*lbuf_max);
return -rc;
}
line = *lbuf;
}
/*
* Terminate id, and point pf to beginning of binary pf
* to be returned.
*/
hex = line + hex_oset;
pf = line + used;
if(!I2HexDecode(hex,(uint8_t *)pf,hex_len/2)){
I2ErrLogP(eh,EINVAL,"Invalid key: not hex?");
return -rc;
}
if(id_ret){
*id_ret = line;
}
if(pf_ret){
*pf_ret = pf;
}
if(pf_len){
*pf_len = hex_len/2;
}
break;
}
return rc;
}
/*
* Function: I2WritePFLine
*
* Description:
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2WritePFLine(
I2ErrHandle eh,
FILE *fp,
const char *id, /* nul terminated */
const uint8_t *bytes,
size_t nbytes,
char **lbuf, /* memory for hex string */
size_t *lbuf_max
)
{
int ret;
char *line = *lbuf;
if(!id || (id[0] == '\0')){
I2ErrLogP(eh,EINVAL,"I2WriteKeyLine(): Invalid identity name");
return -1;
}
/*
* make sure lbuf is large enough for this content
*/
if((nbytes*2 + 1) > *lbuf_max){
while((nbytes*2 + 1) > *lbuf_max){
*lbuf_max += I2LINEBUFINC;
}
*lbuf = realloc(line,sizeof(char) * *lbuf_max);
if(!*lbuf){
if(line){
free(line);
}
I2ErrLog(eh,"realloc(%u): %M",*lbuf_max);
return -1;
}
line = *lbuf;
}
I2HexEncode(line,bytes,nbytes);
/*
* if fprintf has an error, set ret < 0 for a failure return.
*/
ret = fprintf(fp,"%s\t%s\n",id,line);
if(ret > 0) ret = 0;
return ret;
}
/*
* Function: I2StrToNum
*
* Description:
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2StrToNum(
I2numT *limnum,
char *limstr
)
{
size_t silen=0;
size_t len;
char *endptr;
I2numT ret, mult=1;
while(isdigit((int)limstr[silen])){
silen++;
}
len = strlen(limstr);
if(len != silen){
/*
* Ensure that there is at most one non-digit and that it
* is the last char.
*/
if((len - silen) > 1){
return -1;
}
switch (tolower(limstr[silen])){
case 'z':
mult *= 1000; /* 1e21 */
case 'e':
mult *= 1000; /* 1e18 */
case 'p':
mult *= 1000; /* 1e15 */
case 't':
mult *= 1000; /* 1e12 */
case 'g':
mult *= 1000; /* 1e9 */
case 'm':
mult *= 1000; /* 1e6 */
case 'k':
mult *= 1000; /* 1e3 */
break;
default:
return -1;
/* UNREACHED */
}
limstr[silen] = '\0';
}
ret = strtoull(limstr, &endptr, 10);
if(endptr != &limstr[silen]){
return -1;
}
if(ret == 0){
*limnum = 0;
return 0;
}
/* Check for overflow. */
*limnum = ret * mult;
return ((*limnum)/mult != ret)? (-1) : 0;
}
/*
* Function: I2StrToByte
*
* Description:
*
* In Args:
*
* Out Args:
*
* Scope:
* Returns:
* Side Effect:
*/
int
I2StrToByte(
I2numT *limnum,
char *limstr
)
{
size_t silen=0;
size_t len;
char *endptr;
I2numT ret, mult=1;
while(isdigit((int)limstr[silen])){
silen++;
}
len = strlen(limstr);
if(len != silen){
/*
* Ensure that there is at most one non-digit and that it
* is the last char.
*/
if((len - silen) > 1){
return -1;
}
switch (tolower(limstr[silen])){
case 'z':
mult <<= 10;
case 'e':
mult <<= 10;
case 'p':
mult <<= 10;
case 't':
mult <<= 10;
case 'g':
mult <<= 10;
case 'm':
mult <<= 10;
case 'k':
mult <<= 10;
break;
default:
return -1;
/* UNREACHED */
}
limstr[silen] = '\0';
}
ret = strtoull(limstr, &endptr, 10);
if(endptr != &limstr[silen]){
return -1;
}
if(ret == 0){
*limnum = 0;
return 0;
}
/* Check for overflow. */
*limnum = ret * mult;
return ((*limnum/mult) != ret)? (-1) : 0;
}
|