xref: /onnv-gate/usr/src/lib/krb5/kadm5/srv/server_dict.c (revision 7934:6aeeafc994de)
10Sstevel@tonic-gate 
20Sstevel@tonic-gate /*
30Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  *	Openvision retains the copyright to derivative works of
60Sstevel@tonic-gate  *	this source code.  Do *NOT* create a derivative of this
70Sstevel@tonic-gate  *	source code before consulting with your legal department.
80Sstevel@tonic-gate  *	Do *NOT* integrate *ANY* of this source code into another
90Sstevel@tonic-gate  *	product before consulting with your legal department.
100Sstevel@tonic-gate  *
110Sstevel@tonic-gate  *	For further information, read the top-level Openvision
120Sstevel@tonic-gate  *	copyright which is contained in the top-level MIT Kerberos
130Sstevel@tonic-gate  *	copyright.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
160Sstevel@tonic-gate  *
170Sstevel@tonic-gate  */
180Sstevel@tonic-gate 
190Sstevel@tonic-gate 
200Sstevel@tonic-gate /*
210Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
220Sstevel@tonic-gate  *
23*7934SMark.Phalan@Sun.COM  * $Header$
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #if !defined(lint) && !defined(__CODECENTER__)
27*7934SMark.Phalan@Sun.COM static char *rcsid = "$Header$";
280Sstevel@tonic-gate #endif
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include    <sys/types.h>
310Sstevel@tonic-gate #include    <sys/file.h>
320Sstevel@tonic-gate #include    <fcntl.h>
330Sstevel@tonic-gate #include    <sys/stat.h>
340Sstevel@tonic-gate #include    <unistd.h>
352881Smp153739 #include <errno.h>
36*7934SMark.Phalan@Sun.COM #include    "server_internal.h"
370Sstevel@tonic-gate #include    <kadm5/admin.h>
380Sstevel@tonic-gate #include    <stdlib.h>
390Sstevel@tonic-gate #include    <stdio.h>
400Sstevel@tonic-gate #include    <string.h>
412881Smp153739 #ifdef HAVE_MEMORY_H
420Sstevel@tonic-gate #include    <memory.h>
432881Smp153739 #endif
442881Smp153739 #include    "adm_proto.h"
450Sstevel@tonic-gate #include    <syslog.h>
460Sstevel@tonic-gate #include    <libintl.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static char	    **word_list = NULL;	    /* list of word pointers */
490Sstevel@tonic-gate static char	    *word_block = NULL;	    /* actual word data */
502881Smp153739 static unsigned int word_count = 0;	    /* number of words */
512881Smp153739 
520Sstevel@tonic-gate 
530Sstevel@tonic-gate /*
540Sstevel@tonic-gate  * Function: word_compare
550Sstevel@tonic-gate  *
560Sstevel@tonic-gate  * Purpose: compare two words in the dictionary.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * Arguments:
590Sstevel@tonic-gate  *	w1		(input)	pointer to first word
600Sstevel@tonic-gate  *	w2		(input) pointer to second word
610Sstevel@tonic-gate  *	<return value>	result of strcmp
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * Requires:
640Sstevel@tonic-gate  *	w1 and w2 to point to valid memory
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  */
670Sstevel@tonic-gate 
680Sstevel@tonic-gate static int
word_compare(const void * s1,const void * s2)690Sstevel@tonic-gate word_compare(const void *s1, const void *s2)
700Sstevel@tonic-gate {
712881Smp153739     return (strcasecmp(*(const char **)s1, *(const char **)s2));
720Sstevel@tonic-gate }
730Sstevel@tonic-gate 
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate  * Function: init-dict
760Sstevel@tonic-gate  *
770Sstevel@tonic-gate  * Purpose: Initialize in memory word dictionary
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * Arguments:
800Sstevel@tonic-gate  *	    none
812881Smp153739  *	    <return value> KADM5_OK on success errno on failure;
820Sstevel@tonic-gate  * 			   (but success on ENOENT)
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  * Requires:
850Sstevel@tonic-gate  *	If WORDFILE exists, it must contain a list of words,
860Sstevel@tonic-gate  *	one word per-line.
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * Effects:
890Sstevel@tonic-gate  *	If WORDFILE exists, it is read into memory sorted for future
900Sstevel@tonic-gate  * use.  If it does not exist, it syslogs an error message and returns
910Sstevel@tonic-gate  * success.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * Modifies:
940Sstevel@tonic-gate  *	word_list to point to a chunck of allocated memory containing
950Sstevel@tonic-gate  *	pointers to words
960Sstevel@tonic-gate  *	word_block to contain the dictionary.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate 
init_dict(kadm5_config_params * params)1000Sstevel@tonic-gate int init_dict(kadm5_config_params *params)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate     int		    fd,
1030Sstevel@tonic-gate 		    len,
1040Sstevel@tonic-gate 		    i;
1050Sstevel@tonic-gate     char	    *p,
1060Sstevel@tonic-gate 		    *t;
1070Sstevel@tonic-gate     struct  stat    sb;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate     if(word_list != NULL && word_block != NULL)
1100Sstevel@tonic-gate 	return KADM5_OK;
1110Sstevel@tonic-gate     if (! (params->mask & KADM5_CONFIG_DICT_FILE)) {
112*7934SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
1132881Smp153739 	 krb5_klog_syslog(LOG_INFO,
1140Sstevel@tonic-gate 		dgettext(TEXT_DOMAIN,
1150Sstevel@tonic-gate 			"No dictionary file specified, continuing "
1160Sstevel@tonic-gate 			"without one."));
1170Sstevel@tonic-gate 	 return KADM5_OK;
1180Sstevel@tonic-gate     }
1190Sstevel@tonic-gate     if ((fd = open(params->dict_file, O_RDONLY)) == -1) {
1200Sstevel@tonic-gate 	 if (errno == ENOENT) {
121*7934SMark.Phalan@Sun.COM 	/* Solaris Kerberos */
1222881Smp153739 	      krb5_klog_syslog(LOG_ERR,
1230Sstevel@tonic-gate 		     dgettext(TEXT_DOMAIN,
1240Sstevel@tonic-gate 			"WARNING!  Cannot find dictionary file %s, "
1250Sstevel@tonic-gate 			     "continuing without one."), params->dict_file);
1260Sstevel@tonic-gate 	      return KADM5_OK;
1270Sstevel@tonic-gate 	 } else
1280Sstevel@tonic-gate 	      return errno;
1290Sstevel@tonic-gate     }
1300Sstevel@tonic-gate     if (fstat(fd, &sb) == -1)
1310Sstevel@tonic-gate 	return errno;
1320Sstevel@tonic-gate     if ((word_block = (char *) malloc(sb.st_size + 1)) == NULL)
1330Sstevel@tonic-gate 	return errno;
1340Sstevel@tonic-gate     if (read(fd, word_block, sb.st_size) != sb.st_size)
1350Sstevel@tonic-gate 	return errno;
1360Sstevel@tonic-gate     (void) close(fd);
1370Sstevel@tonic-gate     word_block[sb.st_size] = '\0';
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate     p = word_block;
1400Sstevel@tonic-gate     len = sb.st_size;
1410Sstevel@tonic-gate     while(len > 0 && (t = memchr(p, '\n', len)) != NULL) {
1420Sstevel@tonic-gate 	*t = '\0';
1430Sstevel@tonic-gate 	len -= t - p + 1;
1440Sstevel@tonic-gate 	p = t + 1;
1450Sstevel@tonic-gate 	word_count++;
1460Sstevel@tonic-gate     }
1470Sstevel@tonic-gate     if ((word_list = (char **) malloc(word_count * sizeof(char *))) == NULL)
1480Sstevel@tonic-gate 	return errno;
1490Sstevel@tonic-gate     p = word_block;
1500Sstevel@tonic-gate     for (i = 0; i < word_count; i++) {
1510Sstevel@tonic-gate 	word_list[i] = p;
1520Sstevel@tonic-gate 	p += strlen(p) + 1;
1530Sstevel@tonic-gate     }
1540Sstevel@tonic-gate     qsort(word_list, word_count, sizeof(char *), word_compare);
1550Sstevel@tonic-gate     return KADM5_OK;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate  * Function: find_word
1600Sstevel@tonic-gate  *
1610Sstevel@tonic-gate  * Purpose: See if the specified word exists in the in-core dictionary
1620Sstevel@tonic-gate  *
1630Sstevel@tonic-gate  * Arguments:
1640Sstevel@tonic-gate  *	word		(input) word to search for.
1650Sstevel@tonic-gate  * 	<return value>	WORD_NOT_FOUND if not in dictionary,
1660Sstevel@tonic-gate  *			KADM5_OK if if found word
1670Sstevel@tonic-gate  *			errno if init needs to be called and returns an
1680Sstevel@tonic-gate  *			error
1690Sstevel@tonic-gate  *
1700Sstevel@tonic-gate  * Requires:
1710Sstevel@tonic-gate  *	word to be a null terminated string.
1720Sstevel@tonic-gate  *	That word_list and word_block besetup
1730Sstevel@tonic-gate  *
1740Sstevel@tonic-gate  * Effects:
1750Sstevel@tonic-gate  *	finds word in dictionary.
1760Sstevel@tonic-gate  * Modifies:
1770Sstevel@tonic-gate  *	nothing.
1780Sstevel@tonic-gate  *
1790Sstevel@tonic-gate  */
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate int
find_word(const char * word)1820Sstevel@tonic-gate find_word(const char *word)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate     char    **value;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate     if(word_list == NULL || word_block == NULL)
1870Sstevel@tonic-gate 	    return WORD_NOT_FOUND;
1880Sstevel@tonic-gate     if ((value = (char **) bsearch(&word, word_list, word_count, sizeof(char *),
1890Sstevel@tonic-gate 				   word_compare)) == NULL)
1900Sstevel@tonic-gate 	return WORD_NOT_FOUND;
1910Sstevel@tonic-gate     else
1920Sstevel@tonic-gate 	return KADM5_OK;
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate  * Function: destroy_dict
1970Sstevel@tonic-gate  *
1980Sstevel@tonic-gate  * Purpose: destroy in-core copy of dictionary.
1990Sstevel@tonic-gate  *
2000Sstevel@tonic-gate  * Arguments:
2010Sstevel@tonic-gate  *	    none
2020Sstevel@tonic-gate  *	    <return value>  none
2030Sstevel@tonic-gate  * Requires:
2040Sstevel@tonic-gate  *	    nothing
2050Sstevel@tonic-gate  * Effects:
2060Sstevel@tonic-gate  *	frees up memory occupied by word_list and word_block
2070Sstevel@tonic-gate  *	sets count back to 0, and resets the pointers to NULL
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  * Modifies:
2100Sstevel@tonic-gate  *	word_list, word_block, and word_count.
2110Sstevel@tonic-gate  *
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate void
destroy_dict(void)2150Sstevel@tonic-gate destroy_dict(void)
2160Sstevel@tonic-gate {
2170Sstevel@tonic-gate     if(word_list) {
2180Sstevel@tonic-gate 	free(word_list);
2190Sstevel@tonic-gate 	word_list = NULL;
2200Sstevel@tonic-gate     }
2210Sstevel@tonic-gate     if(word_block) {
2220Sstevel@tonic-gate 	free(word_block);
2230Sstevel@tonic-gate 	word_block = NULL;
2240Sstevel@tonic-gate     }
2250Sstevel@tonic-gate     if(word_count)
2260Sstevel@tonic-gate 	word_count = 0;
2270Sstevel@tonic-gate     return;
2280Sstevel@tonic-gate }
229