FreeBSD/contrib/ntp/sntp/crypto.c
/* Load keys from the specified keyfile into the key structures.
 * Returns -1 if the reading failed, otherwise it returns the 
 * number of keys it read
 */
Load keys from the specified keyfile into the key structures.
Returns -1 if the reading failed, otherwise it returns the
number of keys it read
Load keys from the specified keyfile into the key structures . Returns - 1 if the reading failed otherwise it returns the number of keys it read
int
auth_init(
	const char *keyfile,
	struct key **keys
	)
{
	FILE *keyf = fopen(keyfile, "r"); 
	struct key *prev = NULL;
	int scan_cnt, line_cnt = 0;
	char kbuf[200];
	char keystring[129];
	if (keyf == NULL) {
		if (debug)
			printf("sntp auth_init: Couldn't open key file %s for reading!\n", keyfile);
		return -1;
	}
	if (feof(keyf)) {
		if (debug)
			printf("sntp auth_init: Key file %s is empty!\n", keyfile);
		fclose(keyf);
		return -1;
	}
	key_cnt = 0;
	while (!feof(keyf)) {
		char * octothorpe;
		struct key *act;
		int goodline = 0;
		if (NULL == fgets(kbuf, sizeof(kbuf), keyf))
			continue;
		kbuf[sizeof(kbuf) - 1] = '\0';
		octothorpe = strchr(kbuf, '#');
		if (octothorpe)
			*octothorpe = '\0';
		act = emalloc(sizeof(*act));
		scan_cnt = sscanf(kbuf, "%d %9s %128s", &act->key_id, act->type, keystring);
		if (scan_cnt == 3) {
			int len = strlen(keystring);
			if (len <= 20) {
				act->key_len = len;
				memcpy(act->key_seq, keystring, len + 1);
				goodline = 1;
			} else if ((len & 1) != 0) {
				goodline = 0; 
			} else {
				int j;
				goodline = 1;
				act->key_len = len >> 1;
				for (j = 0; j < len; j+=2) {
					int val;
					val = (hex_val(keystring[j]) << 4) |
					       hex_val(keystring[j+1]);
					if (val < 0) {
						goodline = 0; 
						break;
					}
					act->key_seq[j>>1] = (char)val;
				}
			}
		}
		if (goodline) {
			act->next = NULL;
			if (NULL == prev)
				*keys = act;
			else
				prev->next = act;
			prev = act;
			key_cnt++;
		} else {
			msyslog(LOG_DEBUG, "auth_init: scanf %d items, skipping line %d.",
				scan_cnt, line_cnt);
			free(act);
		}
		line_cnt++;
	}
	fclose(keyf);
	key_ptr = *keys;
	return key_cnt;
}
int auth_init ( const char * keyfile , struct key * * keys ) { FILE * keyf = fopen ( keyfile , " " ) ; struct key * prev = NULL ; int scan_cnt , line_cnt = 0 ; char kbuf [ 200 ] ; char keystring [ 129 ] ; if ( keyf == NULL ) { if ( debug ) printf ( " \n " , keyfile ) ; return -1 ; } if ( feof ( keyf ) ) { if ( debug ) printf ( " \n " , keyfile ) ; fclose ( keyf ) ; return -1 ; } key_cnt = 0 ; while ( ! feof ( keyf ) ) { char * octothorpe ; struct key * act ; int goodline = 0 ; if ( NULL == fgets ( kbuf , sizeof ( kbuf ) , keyf ) ) continue ; kbuf [ sizeof ( kbuf ) - 1 ] = ' \0 ' ; octothorpe = strchr ( kbuf , ' ' ) ; if ( octothorpe ) * octothorpe = ' \0 ' ; act = emalloc ( sizeof ( * act ) ) ; scan_cnt = sscanf ( kbuf , " " , & act -> key_id , act -> type , keystring ) ; if ( scan_cnt == 3 ) { int len = strlen ( keystring ) ; if ( len <= 20 ) { act -> key_len = len ; memcpy ( act -> key_seq , keystring , len + 1 ) ; goodline = 1 ; } else if ( ( len & 1 ) != 0 ) { goodline = 0 ; } else { int j ; goodline = 1 ; act -> key_len = len >> 1 ; for ( j = 0 ; j < len ; j += 2 ) { int val ; val = ( hex_val ( keystring [ j ] ) << 4 ) | hex_val ( keystring [ j + 1 ] ) ; if ( val < 0 ) { goodline = 0 ; break ; } act -> key_seq [ j >> 1 ] = ( char ) val ; } } } if ( goodline ) { act -> next = NULL ; if ( NULL == prev ) * keys = act ; else prev -> next = act ; prev = act ; key_cnt ++ ; } else { msyslog ( LOG_DEBUG , " " , scan_cnt , line_cnt ) ; free ( act ) ; } line_cnt ++ ; } fclose ( keyf ) ; key_ptr = * keys ; return key_cnt ; }
Load keys from the specified keyfile into the key structures.
Load keys from the specified keyfile into the key structures .
/* it's bad */ /* it's bad */