xref: /titanic_44/usr/src/lib/libgss/g_utils.c (revision 004388ebfdfe2ed7dfd2d153a876dfcc22d2c006)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*004388ebScasper  * Common Development and Distribution License (the "License").
6*004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*004388ebScasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/stat.h>
307c478bd9Sstevel@tonic-gate #include <stdio.h>
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <ctype.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
367c478bd9Sstevel@tonic-gate #include <gssapi/gssapi_ext.h>
377c478bd9Sstevel@tonic-gate #include <synch.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #define	Q_DEFAULT		"default"
407c478bd9Sstevel@tonic-gate #define	BUFLEN			256
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static int qop_num_pair_cnt;
437c478bd9Sstevel@tonic-gate static const char    QOP_NUM_FILE[] = "/etc/gss/qop";
447c478bd9Sstevel@tonic-gate static qop_num	qop_num_pairs[MAX_QOP_NUM_PAIRS+1];
457c478bd9Sstevel@tonic-gate static mutex_t qopfile_lock = DEFAULTMUTEX;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate static OM_uint32 __gss_read_qop_file(void);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * This routine fetches qop and num from "/etc/gss/qop".
517c478bd9Sstevel@tonic-gate  * There is a memory leak associated with rereading this file,
527c478bd9Sstevel@tonic-gate  * because we can't free the qop_num_pairs array when we reread
537c478bd9Sstevel@tonic-gate  * the file (some callers may have been given these pointers).
547c478bd9Sstevel@tonic-gate  * In general, this memory leak should be a small one, because
557c478bd9Sstevel@tonic-gate  * we don't expect the qop file to be changed and reread often.
567c478bd9Sstevel@tonic-gate  */
577c478bd9Sstevel@tonic-gate static OM_uint32
__gss_read_qop_file(void)587c478bd9Sstevel@tonic-gate __gss_read_qop_file(void)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	char 	buf[BUFLEN];	/* one line from the file */
617c478bd9Sstevel@tonic-gate 	char	*name, *next;
627c478bd9Sstevel@tonic-gate 	char	*qopname, *num_str;
637c478bd9Sstevel@tonic-gate 	char 	*line;
647c478bd9Sstevel@tonic-gate 	FILE 	*fp;
657c478bd9Sstevel@tonic-gate 	static int last = 0;
667c478bd9Sstevel@tonic-gate 	struct stat stbuf;
677c478bd9Sstevel@tonic-gate 	OM_uint32 major = GSS_S_COMPLETE;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 	(void) mutex_lock(&qopfile_lock);
707c478bd9Sstevel@tonic-gate 	if (stat(QOP_NUM_FILE, &stbuf) != 0 || stbuf.st_mtime < last) {
717c478bd9Sstevel@tonic-gate 		if (!qop_num_pairs[0].qop) {
727c478bd9Sstevel@tonic-gate 			major = GSS_S_FAILURE;
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 		goto done;
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 	last = stbuf.st_mtime;
777c478bd9Sstevel@tonic-gate 
78*004388ebScasper 	fp = fopen(QOP_NUM_FILE, "rF");
797c478bd9Sstevel@tonic-gate 	if (fp == (FILE *)0) {
807c478bd9Sstevel@tonic-gate 		major = GSS_S_FAILURE;
817c478bd9Sstevel@tonic-gate 		goto done;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * For each line in the file parse it appropriately.
867c478bd9Sstevel@tonic-gate 	 * File format : qopname	num(int)
877c478bd9Sstevel@tonic-gate 	 * Note that we silently ignore corrupt entries.
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	qop_num_pair_cnt = 0;
907c478bd9Sstevel@tonic-gate 	while (!feof(fp)) {
917c478bd9Sstevel@tonic-gate 		line = fgets(buf, BUFLEN, fp);
927c478bd9Sstevel@tonic-gate 		if (line == NULL)
937c478bd9Sstevel@tonic-gate 			break;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		/* Skip comments and blank lines */
967c478bd9Sstevel@tonic-gate 		if ((*line == '#') || (*line == '\n'))
977c478bd9Sstevel@tonic-gate 			continue;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		/* Skip trailing comments */
1007c478bd9Sstevel@tonic-gate 		next = strchr(line, '#');
1017c478bd9Sstevel@tonic-gate 		if (next)
1027c478bd9Sstevel@tonic-gate 			*next = '\0';
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 		name = &(buf[0]);
1057c478bd9Sstevel@tonic-gate 		while (isspace(*name))
1067c478bd9Sstevel@tonic-gate 			name++;
1077c478bd9Sstevel@tonic-gate 		if (*name == '\0')	/* blank line */
1087c478bd9Sstevel@tonic-gate 			continue;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		qopname = name;	/* will contain qop name */
1117c478bd9Sstevel@tonic-gate 		while (!isspace(*qopname))
1127c478bd9Sstevel@tonic-gate 			qopname++;
1137c478bd9Sstevel@tonic-gate 		if (*qopname == '\0') {
1147c478bd9Sstevel@tonic-gate 			continue;
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 		next = qopname+1;
1177c478bd9Sstevel@tonic-gate 		*qopname = '\0';	/* null terminate qopname */
1187c478bd9Sstevel@tonic-gate 		qop_num_pairs[qop_num_pair_cnt].qop = strdup(name);
1197c478bd9Sstevel@tonic-gate 		if (qop_num_pairs[qop_num_pair_cnt].qop == NULL)
1207c478bd9Sstevel@tonic-gate 			continue;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		name = next;
1237c478bd9Sstevel@tonic-gate 		while (isspace(*name))
1247c478bd9Sstevel@tonic-gate 			name++;
1257c478bd9Sstevel@tonic-gate 		if (*name == '\0') { 	/* end of line, no num */
1267c478bd9Sstevel@tonic-gate 			free(qop_num_pairs[qop_num_pair_cnt].qop);
1277c478bd9Sstevel@tonic-gate 			continue;
1287c478bd9Sstevel@tonic-gate 		}
1297c478bd9Sstevel@tonic-gate 		num_str = name;	/* will contain num (n) */
1307c478bd9Sstevel@tonic-gate 		while (!isspace(*num_str))
1317c478bd9Sstevel@tonic-gate 			num_str++;
1327c478bd9Sstevel@tonic-gate 		next = num_str+1;
1337c478bd9Sstevel@tonic-gate 		*num_str++ = '\0';	/* null terminate num_str */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 		qop_num_pairs[qop_num_pair_cnt].num = (OM_uint32)atoi(name);
1367c478bd9Sstevel@tonic-gate 		name = next;
1377c478bd9Sstevel@tonic-gate 		while (isspace(*name))
1387c478bd9Sstevel@tonic-gate 			name++;
1397c478bd9Sstevel@tonic-gate 		if (*name == '\0') { 	/* end of line, no mechanism */
1407c478bd9Sstevel@tonic-gate 			free(qop_num_pairs[qop_num_pair_cnt].qop);
1417c478bd9Sstevel@tonic-gate 			continue;
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 		num_str = name;	/* will contain mech */
1447c478bd9Sstevel@tonic-gate 		while (!isspace(*num_str))
1457c478bd9Sstevel@tonic-gate 			num_str++;
1467c478bd9Sstevel@tonic-gate 		*num_str = '\0';
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		qop_num_pairs[qop_num_pair_cnt].mech = strdup(name);
1497c478bd9Sstevel@tonic-gate 		if (qop_num_pairs[qop_num_pair_cnt].mech == NULL) {
1507c478bd9Sstevel@tonic-gate 			free(qop_num_pairs[qop_num_pair_cnt].qop);
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		if (qop_num_pair_cnt++ >= MAX_QOP_NUM_PAIRS)
1557c478bd9Sstevel@tonic-gate 			break;
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
1587c478bd9Sstevel@tonic-gate done:
1597c478bd9Sstevel@tonic-gate 	(void) mutex_unlock(&qopfile_lock);
1607c478bd9Sstevel@tonic-gate 	return (major);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate OM_uint32
__gss_qop_to_num(char * qop,char * mech,OM_uint32 * num)1647c478bd9Sstevel@tonic-gate __gss_qop_to_num(
1657c478bd9Sstevel@tonic-gate 	char		*qop,
1667c478bd9Sstevel@tonic-gate 	char		*mech,
1677c478bd9Sstevel@tonic-gate 	OM_uint32	*num
1687c478bd9Sstevel@tonic-gate )
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	int i;
1717c478bd9Sstevel@tonic-gate 	OM_uint32 major = GSS_S_FAILURE;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	if (!num)
1747c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (qop == NULL || strlen(qop) == 0 ||
1777c478bd9Sstevel@tonic-gate 			strcasecmp(qop, Q_DEFAULT) == 0) {
1787c478bd9Sstevel@tonic-gate 		*num = GSS_C_QOP_DEFAULT;
1797c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
1837c478bd9Sstevel@tonic-gate 		return (major);
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	for (i = 0; i < qop_num_pair_cnt; i++) {
1867c478bd9Sstevel@tonic-gate 		if ((strcasecmp(mech, qop_num_pairs[i].mech) == 0) &&
1877c478bd9Sstevel@tonic-gate 		    (strcasecmp(qop, qop_num_pairs[i].qop) == 0)) {
1887c478bd9Sstevel@tonic-gate 			*num = qop_num_pairs[i].num;
1897c478bd9Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
1907c478bd9Sstevel@tonic-gate 		}
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate OM_uint32
__gss_num_to_qop(char * mech,OM_uint32 num,char ** qop)1977c478bd9Sstevel@tonic-gate __gss_num_to_qop(
1987c478bd9Sstevel@tonic-gate 	char		*mech,
1997c478bd9Sstevel@tonic-gate 	OM_uint32	num,
2007c478bd9Sstevel@tonic-gate 	char		**qop
2017c478bd9Sstevel@tonic-gate )
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	int i;
2047c478bd9Sstevel@tonic-gate 	OM_uint32 major;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	if (!qop)
2077c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2087c478bd9Sstevel@tonic-gate 	*qop = NULL;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (num == GSS_C_QOP_DEFAULT) {
2117c478bd9Sstevel@tonic-gate 		*qop = Q_DEFAULT;
2127c478bd9Sstevel@tonic-gate 		return (GSS_S_COMPLETE);
2137c478bd9Sstevel@tonic-gate 	}
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	if (mech == NULL)
2167c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
2197c478bd9Sstevel@tonic-gate 		return (major);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	for (i = 0; i < qop_num_pair_cnt; i++) {
2227c478bd9Sstevel@tonic-gate 		if ((strcasecmp(mech, qop_num_pairs[i].mech) == 0) &&
2237c478bd9Sstevel@tonic-gate 		    (num == qop_num_pairs[i].num)) {
2247c478bd9Sstevel@tonic-gate 			*qop = qop_num_pairs[i].qop;
2257c478bd9Sstevel@tonic-gate 			return (GSS_S_COMPLETE);
2267c478bd9Sstevel@tonic-gate 		}
2277c478bd9Sstevel@tonic-gate 	}
2287c478bd9Sstevel@tonic-gate 	return (GSS_S_FAILURE);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * For a given mechanism pass back qop information about it in a buffer
2337c478bd9Sstevel@tonic-gate  * of size MAX_QOPS_PER_MECH+1.
2347c478bd9Sstevel@tonic-gate  */
2357c478bd9Sstevel@tonic-gate OM_uint32
__gss_get_mech_info(char * mech,char ** qops)2367c478bd9Sstevel@tonic-gate __gss_get_mech_info(
2377c478bd9Sstevel@tonic-gate 	char		*mech,
2387c478bd9Sstevel@tonic-gate 	char		**qops
2397c478bd9Sstevel@tonic-gate )
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	int i, cnt = 0;
2427c478bd9Sstevel@tonic-gate 	OM_uint32 major = GSS_S_COMPLETE;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (!qops)
2457c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2467c478bd9Sstevel@tonic-gate 	*qops = NULL;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 	if (!mech)
2497c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
2527c478bd9Sstevel@tonic-gate 		return (major);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	for (i = 0; i < qop_num_pair_cnt; i++) {
2557c478bd9Sstevel@tonic-gate 		if (strcmp(mech, qop_num_pairs[i].mech) == 0) {
2567c478bd9Sstevel@tonic-gate 		    if (cnt >= MAX_QOPS_PER_MECH) {
2577c478bd9Sstevel@tonic-gate 			return (GSS_S_FAILURE);
2587c478bd9Sstevel@tonic-gate 		    }
2597c478bd9Sstevel@tonic-gate 		    qops[cnt++] = qop_num_pairs[i].qop;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 	qops[cnt] = NULL;
2637c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Copy the qop values and names for the mechanism back in a qop_num
2687c478bd9Sstevel@tonic-gate  * buffer of size MAX_QOPS_PER_MECH provided by the caller.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate OM_uint32
__gss_mech_qops(char * mech,qop_num * mechqops,int * numqop)2717c478bd9Sstevel@tonic-gate __gss_mech_qops(
2727c478bd9Sstevel@tonic-gate 	char *mech,
2737c478bd9Sstevel@tonic-gate 	qop_num *mechqops,
2747c478bd9Sstevel@tonic-gate 	int *numqop
2757c478bd9Sstevel@tonic-gate )
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	int i;
2787c478bd9Sstevel@tonic-gate 	OM_uint32 major;
2797c478bd9Sstevel@tonic-gate 	int cnt = 0;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	if (!mechqops || !numqop)
2827c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_WRITE);
2837c478bd9Sstevel@tonic-gate 	*numqop = 0;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (!mech)
2867c478bd9Sstevel@tonic-gate 		return (GSS_S_CALL_INACCESSIBLE_READ);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
2897c478bd9Sstevel@tonic-gate 		return (major);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	for (i = 0; i < qop_num_pair_cnt; i++) {
2927c478bd9Sstevel@tonic-gate 	    if (strcasecmp(mech, qop_num_pairs[i].mech) == 0) {
2937c478bd9Sstevel@tonic-gate 		if (cnt >= MAX_QOPS_PER_MECH) {
2947c478bd9Sstevel@tonic-gate 			return (GSS_S_FAILURE);
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 		mechqops[cnt++] = qop_num_pairs[i];
2977c478bd9Sstevel@tonic-gate 	    }
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	*numqop = cnt;
3007c478bd9Sstevel@tonic-gate 	return (GSS_S_COMPLETE);
3017c478bd9Sstevel@tonic-gate }
302