dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / krb5 / kadm5 / srv / server_dict.c
blobc36edd60039feb79b82e7f70cd981b1515364078
2 /*
3 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
5 * Openvision retains the copyright to derivative works of
6 * this source code. Do *NOT* create a derivative of this
7 * source code before consulting with your legal department.
8 * Do *NOT* integrate *ANY* of this source code into another
9 * product before consulting with your legal department.
11 * For further information, read the top-level Openvision
12 * copyright which is contained in the top-level MIT Kerberos
13 * copyright.
15 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
21 * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved
23 * $Header$
26 static char *rcsid = "$Header$";
28 #include <sys/types.h>
29 #include <sys/file.h>
30 #include <fcntl.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include "server_internal.h"
35 #include <kadm5/admin.h>
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #ifdef HAVE_MEMORY_H
40 #include <memory.h>
41 #endif
42 #include "adm_proto.h"
43 #include <syslog.h>
44 #include <libintl.h>
46 static char **word_list = NULL; /* list of word pointers */
47 static char *word_block = NULL; /* actual word data */
48 static unsigned int word_count = 0; /* number of words */
52 * Function: word_compare
54 * Purpose: compare two words in the dictionary.
56 * Arguments:
57 * w1 (input) pointer to first word
58 * w2 (input) pointer to second word
59 * <return value> result of strcmp
61 * Requires:
62 * w1 and w2 to point to valid memory
66 static int
67 word_compare(const void *s1, const void *s2)
69 return (strcasecmp(*(const char **)s1, *(const char **)s2));
73 * Function: init-dict
75 * Purpose: Initialize in memory word dictionary
77 * Arguments:
78 * none
79 * <return value> KADM5_OK on success errno on failure;
80 * (but success on ENOENT)
82 * Requires:
83 * If WORDFILE exists, it must contain a list of words,
84 * one word per-line.
86 * Effects:
87 * If WORDFILE exists, it is read into memory sorted for future
88 * use. If it does not exist, it syslogs an error message and returns
89 * success.
91 * Modifies:
92 * word_list to point to a chunck of allocated memory containing
93 * pointers to words
94 * word_block to contain the dictionary.
98 int init_dict(kadm5_config_params *params)
100 int fd,
101 len,
103 char *p,
105 struct stat sb;
107 if(word_list != NULL && word_block != NULL)
108 return KADM5_OK;
109 if (! (params->mask & KADM5_CONFIG_DICT_FILE)) {
110 /* Solaris Kerberos */
111 krb5_klog_syslog(LOG_INFO,
112 dgettext(TEXT_DOMAIN,
113 "No dictionary file specified, continuing "
114 "without one."));
115 return KADM5_OK;
117 if ((fd = open(params->dict_file, O_RDONLY)) == -1) {
118 if (errno == ENOENT) {
119 /* Solaris Kerberos */
120 krb5_klog_syslog(LOG_ERR,
121 dgettext(TEXT_DOMAIN,
122 "WARNING! Cannot find dictionary file %s, "
123 "continuing without one."), params->dict_file);
124 return KADM5_OK;
125 } else
126 return errno;
128 if (fstat(fd, &sb) == -1)
129 return errno;
130 if ((word_block = (char *) malloc(sb.st_size + 1)) == NULL)
131 return errno;
132 if (read(fd, word_block, sb.st_size) != sb.st_size)
133 return errno;
134 (void) close(fd);
135 word_block[sb.st_size] = '\0';
137 p = word_block;
138 len = sb.st_size;
139 while(len > 0 && (t = memchr(p, '\n', len)) != NULL) {
140 *t = '\0';
141 len -= t - p + 1;
142 p = t + 1;
143 word_count++;
145 if ((word_list = (char **) malloc(word_count * sizeof(char *))) == NULL)
146 return errno;
147 p = word_block;
148 for (i = 0; i < word_count; i++) {
149 word_list[i] = p;
150 p += strlen(p) + 1;
152 qsort(word_list, word_count, sizeof(char *), word_compare);
153 return KADM5_OK;
157 * Function: find_word
159 * Purpose: See if the specified word exists in the in-core dictionary
161 * Arguments:
162 * word (input) word to search for.
163 * <return value> WORD_NOT_FOUND if not in dictionary,
164 * KADM5_OK if if found word
165 * errno if init needs to be called and returns an
166 * error
168 * Requires:
169 * word to be a null terminated string.
170 * That word_list and word_block besetup
172 * Effects:
173 * finds word in dictionary.
174 * Modifies:
175 * nothing.
180 find_word(const char *word)
182 char **value;
184 if(word_list == NULL || word_block == NULL)
185 return WORD_NOT_FOUND;
186 if ((value = (char **) bsearch(&word, word_list, word_count, sizeof(char *),
187 word_compare)) == NULL)
188 return WORD_NOT_FOUND;
189 else
190 return KADM5_OK;
194 * Function: destroy_dict
196 * Purpose: destroy in-core copy of dictionary.
198 * Arguments:
199 * none
200 * <return value> none
201 * Requires:
202 * nothing
203 * Effects:
204 * frees up memory occupied by word_list and word_block
205 * sets count back to 0, and resets the pointers to NULL
207 * Modifies:
208 * word_list, word_block, and word_count.
212 void
213 destroy_dict(void)
215 if(word_list) {
216 free(word_list);
217 word_list = NULL;
219 if(word_block) {
220 free(word_block);
221 word_block = NULL;
223 if(word_count)
224 word_count = 0;
225 return;