dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / gss / gsscred / gsscred_file.c
blob77b537859f29e26b5d60393197cbef6df6894ccd
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <ctype.h>
33 #include "gsscred.h"
36 * gsscred utility
37 * Manages mapping between a security principal name and unix uid.
38 * Implementation file for the file based gsscred utility.
41 #define MAX_ENTRY_LEN 1024
43 static const char credFile[] = "/etc/gss/gsscred_db";
44 static const char credFileTmp[] = "/etc/gss/gsscred_db.tmp";
45 static const int expNameTokIdLen = 2;
46 static const int mechOidLenLen = 2;
47 static const int krb5OidTagLen = 1;
48 static const int krb5OidLenLen = 1;
49 static const int nameLen = 4;
50 static const int krb5OidLen = 9;
53 * Multiply by two given that the token has already gone through hex string
54 * expansion.
56 #define NAME_OFFSET (expNameTokIdLen + mechOidLenLen + krb5OidTagLen + \
57 krb5OidLenLen + krb5OidLen + nameLen) * 2
59 static int matchEntry(const char *entry, const gss_buffer_t name,
60 const char *uid, uid_t *uidOut);
63 * file_addGssCredEntry
65 * Adds a new entry to the gsscred table.
66 * Does not check for duplicate entries.
68 int file_addGssCredEntry(const gss_buffer_t hexName, const char *uid,
69 const char *comment, char **errDetails)
71 FILE *fp;
72 char tmpBuf[256];
74 if ((fp = fopen(credFile, "a")) == NULL) {
75 if (errDetails) {
76 (void) snprintf(tmpBuf, sizeof (tmpBuf),
77 gettext("Unable to open gsscred file [%s]"),
78 credFile);
79 *errDetails = strdup(tmpBuf);
81 return (0);
84 (void) fprintf(fp,
85 "%s\t%s\t%s\n", (char *)hexName->value, uid, comment);
86 (void) fclose(fp);
87 return (1);
88 } /* ******* file_addGssCredEntry ****** */
93 * file_getGssCredEntry
95 * Searches the file for the file matching the name. Since the name
96 * contains a mechanism identifier, to search for all names for a given
97 * mechanism just supply the mechanism portion in the name buffer.
98 * To search by uid only, supply a non-null value of uid.
100 int file_getGssCredEntry(const gss_buffer_t name, const char *uid,
101 char **errDetails)
103 FILE *fp;
104 char entry[MAX_ENTRY_LEN+1];
106 if ((fp = fopen(credFile, "r")) == NULL) {
108 if (errDetails) {
109 (void) snprintf(entry, sizeof (entry),
110 gettext("Unable to open gsscred file [%s]"),
111 credFile);
112 *errDetails = strdup(entry);
115 return (0);
118 /* go through the file in sequential order */
119 while (fgets(entry, MAX_ENTRY_LEN, fp) != NULL) {
120 /* is there any search criteria */
121 if (name == NULL && uid == NULL) {
122 (void) fprintf(stdout, "%s", entry);
123 continue;
126 if (matchEntry(entry, name, uid, NULL))
127 (void) fprintf(stdout, "%s", entry);
129 } /* while */
131 (void) fclose(fp);
132 return (1);
133 } /* file_getGssCredEntry */
136 * file_getGssCredUid
138 * GSS entry point for retrieving user uid information.
139 * We need to go through the entire file to ensure that
140 * the last matching entry is retrieved - this is because
141 * new entries are added to the end, and in case of
142 * duplicates we want to get the latest entry.
145 file_getGssCredUid(const gss_buffer_t expName, uid_t *uidOut)
147 FILE *fp;
148 char entry[MAX_ENTRY_LEN+1];
149 int retVal = 0;
151 if ((fp = fopen(credFile, "r")) == NULL)
152 return (0);
154 /* go through the entire file in sequential order */
155 while (fgets(entry, MAX_ENTRY_LEN, fp) != NULL) {
156 if (matchEntry(entry, expName, NULL, uidOut)) {
157 retVal = 1;
159 } /* while */
161 (void) fclose(fp);
162 return (retVal);
163 } /* file_getGssCredUid */
169 * file_deleteGssCredEntry
171 * removes entries form file that match the delete criteria
173 int file_deleteGssCredEntry(const gss_buffer_t name, const char *uid,
174 char **errDetails)
176 FILE *fp, *tempFp;
177 char entry[MAX_ENTRY_LEN+1];
178 int foundOne = 0;
180 /* are we deleting everyone? */
181 if (name == NULL && uid == NULL) {
183 if ((fp = fopen(credFile, "w")) == NULL) {
185 if (errDetails) {
186 (void) snprintf(entry, sizeof (entry),
187 gettext("Unable to open gsscred"
188 " file [%s]"),
189 credFile);
190 *errDetails = strdup(entry);
192 return (0);
195 (void) fclose(fp);
196 return (1);
199 /* selective delete - might still be everyone */
200 if ((fp = fopen(credFile, "r")) == NULL) {
202 if (errDetails) {
203 (void) snprintf(entry, sizeof (entry),
204 gettext("Unable to open gsscred file [%s]"),
205 credFile);
206 *errDetails = strdup(entry);
208 return (0);
211 /* also need to open temp file */
212 if ((tempFp = fopen(credFileTmp, "w")) == NULL) {
213 if (errDetails) {
214 (void) snprintf(entry, sizeof (entry),
215 gettext("Unable to open gsscred temporary"
216 " file [%s]"),
217 credFileTmp);
218 *errDetails = strdup(entry);
221 (void) fclose(fp);
222 return (0);
225 /* go through all the entries sequentially removing ones that match */
226 while (fgets(entry, MAX_ENTRY_LEN, fp) != NULL) {
228 if (!matchEntry(entry, name, uid, NULL))
229 (void) fputs(entry, tempFp);
230 else
231 foundOne = 1;
233 (void) fclose(tempFp);
234 (void) fclose(fp);
236 /* now make the tempfile the gsscred file */
237 (void) rename(credFileTmp, credFile);
238 (void) unlink(credFileTmp);
240 if (!foundOne) {
241 *errDetails = strdup(gettext("No users found"));
242 return (0);
244 return (1);
245 } /* file_deleteGssCredEntry */
251 * match entry
253 * checks if the specified entry matches the supplied criteria
254 * returns 1 if yes, 0 if no
255 * uidOut value can be used to retrieve the uid from the entry
256 * when the uid string is passed in, the uidOut value is not set
258 static int matchEntry(const char *entry, const gss_buffer_t name,
259 const char *uid, uid_t *uidOut)
261 char fullEntry[MAX_ENTRY_LEN+1], *item, *item_buf, *name_buf;
262 char dilims[] = "\t \n";
264 * item_len is the length of the token in the gsscred_db.
265 * name_len is the length of the token passed to this function.
267 int item_len, name_len;
269 * This is the hex encoding of the beginning of all exported name
270 * tokens for the Kerberos V mechanism. We need this to detect old,
271 * incorrectly exported name tokens; see below.
273 char *krb5_ntok_prefix = "0401000B06092A864886F712010202";
275 * This is the hex encoded GSS_C_NT_USER_NAME OID, needed for the same
276 * reason as krb5_ntok_prefix.
278 char *gss_u_name = "2A864886F71201020101";
280 if (entry == NULL || isspace(*entry))
281 return (0);
283 /* save the entry since strtok will chop it up */
284 (void) strcpy(fullEntry, entry);
286 if ((item = strtok(fullEntry, dilims)) == NULL)
287 return (0);
289 /* do we need to search the name */
290 if (name != NULL) {
292 item_len = strlen(item);
293 name_len = name->length;
294 name_buf = name->value;
296 /* we can match the prefix of the string */
297 if (item_len < name_len)
298 return (0);
300 if (strncmp(item, name->value, name_len) != 0) {
303 * The following section is needed in order to detect
304 * two existing errant formats in the gsscred db.
306 * 1. Exported names that have a trailing null byte
307 * ("00" in two hex characters) with the name length
308 * incremented to account for the extra null byte.
310 * 2. Exported names that have the name type length
311 * and name type OID prepended to the exported name.
314 if (strncmp(name->value, krb5_ntok_prefix,
315 strlen(krb5_ntok_prefix)) != 0)
316 return (0);
318 if (strncmp(item, krb5_ntok_prefix,
319 strlen(krb5_ntok_prefix)) != 0)
320 return (0);
322 if ((item_buf = strstr(item, gss_u_name)) == NULL)
323 return (0);
325 item_buf += strlen(gss_u_name);
327 name_buf += NAME_OFFSET;
329 if ((strlen(item_buf) != strlen(name_buf)) &&
330 (strncmp(item_buf + (strlen(item_buf) - 2), "00", 2)
331 != 0))
332 return (0);
335 * Here we compare the end of name_len, given
336 * that item_len could have two extra "00"
337 * representing the null byte.
339 if (strncmp(item_buf, name_buf, name_len - NAME_OFFSET)
340 != 0)
341 return (0);
342 } else
344 * We only strncmp() so we could check for old,
345 * broken exported name tokens for the krb5 mech.
346 * For any other exported name tokens we want exact
347 * matches only.
349 if (item_len != name_len)
350 return (0);
352 /* do we need to check the uid - if not then we found it */
353 if (uid == NULL) {
354 /* do we ned to parse out the uid ? */
355 if (uidOut) {
356 if ((item = strtok(NULL, dilims)) == NULL)
357 return (0);
358 *uidOut = atol(item);
360 return (1);
363 /* continue with checking the uid */
366 if (uid == NULL)
367 return (1);
369 /* get the next token from the string - the uid */
370 if ((item = strtok(NULL, dilims)) == NULL)
371 return (0);
373 if (strcmp(item, uid) == 0)
374 return (1);
376 return (0);
377 } /* ******* matchEntry ****** */