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