xref: /titanic_50/usr/src/lib/krb5/kadm5/srv/server_dict.c (revision 25cf1a301a396c38e8adf52c15f537b80d2483f7)
1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
2 
3 /*
4  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
5  *
6  *	Openvision retains the copyright to derivative works of
7  *	this source code.  Do *NOT* create a derivative of this
8  *	source code before consulting with your legal department.
9  *	Do *NOT* integrate *ANY* of this source code into another
10  *	product before consulting with your legal department.
11  *
12  *	For further information, read the top-level Openvision
13  *	copyright which is contained in the top-level MIT Kerberos
14  *	copyright.
15  *
16  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
17  *
18  */
19 
20 
21 /*
22  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
23  *
24  * $Header: /afs/athena.mit.edu/astaff/project/krbdev/.cvsroot/src/lib/kadm5/srv/server_dict.c,v 1.2 1996/10/18 19:45:52 bjaspan Exp $
25  */
26 
27 #if !defined(lint) && !defined(__CODECENTER__)
28 static char *rcsid = "$Header: /afs/athena.mit.edu/astaff/project/krbdev/.cvsroot/src/lib/kadm5/srv/server_dict.c,v 1.2 1996/10/18 19:45:52 bjaspan Exp $";
29 #endif
30 
31 #include    <sys/types.h>
32 #include    <sys/file.h>
33 #include    <fcntl.h>
34 #include    <sys/stat.h>
35 #include    <unistd.h>
36 #include    <kadm5/admin.h>
37 #include    <stdlib.h>
38 #include    <stdio.h>
39 #include    <string.h>
40 #include    <memory.h>
41 #include    <syslog.h>
42 #include    <libintl.h>
43 #include    "server_internal.h"
44 
45 static char	    **word_list = NULL;	    /* list of word pointers */
46 static char	    *word_block = NULL;	    /* actual word data */
47 static int	    word_count = 0;	    /* number of words */
48 extern int	    errno;
49 
50 /*
51  * Function: word_compare
52  *
53  * Purpose: compare two words in the dictionary.
54  *
55  * Arguments:
56  *	w1		(input)	pointer to first word
57  *	w2		(input) pointer to second word
58  *	<return value>	result of strcmp
59  *
60  * Requires:
61  *	w1 and w2 to point to valid memory
62  *
63  */
64 
65 static int
66 word_compare(const void *s1, const void *s2)
67 {
68     return (strcasecmp(*(char **)s1, *(char **)s2));
69 }
70 
71 /*
72  * Function: init-dict
73  *
74  * Purpose: Initialize in memory word dictionary
75  *
76  * Arguments:
77  *	    none
78  *	    <return value> KADM5_OK on sucsess errno on failure;
79  * 			   (but success on ENOENT)
80  *
81  * Requires:
82  *	If WORDFILE exists, it must contain a list of words,
83  *	one word per-line.
84  *
85  * Effects:
86  *	If WORDFILE exists, it is read into memory sorted for future
87  * use.  If it does not exist, it syslogs an error message and returns
88  * success.
89  *
90  * Modifies:
91  *	word_list to point to a chunck of allocated memory containing
92  *	pointers to words
93  *	word_block to contain the dictionary.
94  *
95  */
96 
97 int init_dict(kadm5_config_params *params)
98 {
99     int		    fd,
100 		    len,
101 		    i;
102     char	    *p,
103 		    *t;
104     struct  stat    sb;
105 
106     if(word_list != NULL && word_block != NULL)
107 	return KADM5_OK;
108     if (! (params->mask & KADM5_CONFIG_DICT_FILE)) {
109 	 syslog(LOG_INFO,
110 		dgettext(TEXT_DOMAIN,
111 			"No dictionary file specified, continuing "
112 			"without one."));
113 	 return KADM5_OK;
114     }
115     if ((fd = open(params->dict_file, O_RDONLY)) == -1) {
116 	 if (errno == ENOENT) {
117 	      syslog(LOG_ERR,
118 		     dgettext(TEXT_DOMAIN,
119 			"WARNING!  Cannot find dictionary file %s, "
120 			     "continuing without one."), params->dict_file);
121 	      return KADM5_OK;
122 	 } else
123 	      return errno;
124     }
125     if (fstat(fd, &sb) == -1)
126 	return errno;
127     if ((word_block = (char *) malloc(sb.st_size + 1)) == NULL)
128 	return errno;
129     if (read(fd, word_block, sb.st_size) != sb.st_size)
130 	return errno;
131     (void) close(fd);
132     word_block[sb.st_size] = '\0';
133 
134     p = word_block;
135     len = sb.st_size;
136     while(len > 0 && (t = memchr(p, '\n', len)) != NULL) {
137 	*t = '\0';
138 	len -= t - p + 1;
139 	p = t + 1;
140 	word_count++;
141     }
142     if ((word_list = (char **) malloc(word_count * sizeof(char *))) == NULL)
143 	return errno;
144     p = word_block;
145     for (i = 0; i < word_count; i++) {
146 	word_list[i] = p;
147 	p += strlen(p) + 1;
148     }
149     qsort(word_list, word_count, sizeof(char *), word_compare);
150     return KADM5_OK;
151 }
152 
153 /*
154  * Function: find_word
155  *
156  * Purpose: See if the specified word exists in the in-core dictionary
157  *
158  * Arguments:
159  *	word		(input) word to search for.
160  * 	<return value>	WORD_NOT_FOUND if not in dictionary,
161  *			KADM5_OK if if found word
162  *			errno if init needs to be called and returns an
163  *			error
164  *
165  * Requires:
166  *	word to be a null terminated string.
167  *	That word_list and word_block besetup
168  *
169  * Effects:
170  *	finds word in dictionary.
171  * Modifies:
172  *	nothing.
173  *
174  */
175 
176 int
177 find_word(const char *word)
178 {
179     char    **value;
180 
181     if(word_list == NULL || word_block == NULL)
182 	    return WORD_NOT_FOUND;
183     if ((value = (char **) bsearch(&word, word_list, word_count, sizeof(char *),
184 				   word_compare)) == NULL)
185 	return WORD_NOT_FOUND;
186     else
187 	return KADM5_OK;
188 }
189 
190 /*
191  * Function: destroy_dict
192  *
193  * Purpose: destroy in-core copy of dictionary.
194  *
195  * Arguments:
196  *	    none
197  *	    <return value>  none
198  * Requires:
199  *	    nothing
200  * Effects:
201  *	frees up memory occupied by word_list and word_block
202  *	sets count back to 0, and resets the pointers to NULL
203  *
204  * Modifies:
205  *	word_list, word_block, and word_count.
206  *
207  */
208 
209 void
210 destroy_dict(void)
211 {
212     if(word_list) {
213 	free(word_list);
214 	word_list = NULL;
215     }
216     if(word_block) {
217 	free(word_block);
218 	word_block = NULL;
219     }
220     if(word_count)
221 	word_count = 0;
222     return;
223 }
224