summaryrefslogtreecommitdiff
path: root/usr/src/lib/libgss/g_utils.c
blob: 7e7183f912e1d41202d89e2874b78372f993e2e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <errno.h>
#include <gssapi/gssapi.h>
#include <gssapi/gssapi_ext.h>
#include <synch.h>

#define	Q_DEFAULT		"default"
#define	BUFLEN			256

static int qop_num_pair_cnt;
static const char    QOP_NUM_FILE[] = "/etc/gss/qop";
static qop_num	qop_num_pairs[MAX_QOP_NUM_PAIRS+1];
static mutex_t qopfile_lock = DEFAULTMUTEX;

static OM_uint32 __gss_read_qop_file(void);

/*
 * This routine fetches qop and num from "/etc/gss/qop".
 * There is a memory leak associated with rereading this file,
 * because we can't free the qop_num_pairs array when we reread
 * the file (some callers may have been given these pointers).
 * In general, this memory leak should be a small one, because
 * we don't expect the qop file to be changed and reread often.
 */
static OM_uint32
__gss_read_qop_file(void)
{
	char 	buf[BUFLEN];	/* one line from the file */
	char	*name, *next;
	char	*qopname, *num_str;
	char 	*line;
	FILE 	*fp;
	static int last = 0;
	struct stat stbuf;
	OM_uint32 major = GSS_S_COMPLETE;

	(void) mutex_lock(&qopfile_lock);
	if (stat(QOP_NUM_FILE, &stbuf) != 0 || stbuf.st_mtime < last) {
		if (!qop_num_pairs[0].qop) {
			major = GSS_S_FAILURE;
		}
		goto done;
	}
	last = stbuf.st_mtime;

	fp = fopen(QOP_NUM_FILE, "r");
	if (fp == (FILE *)0) {
		major = GSS_S_FAILURE;
		goto done;
	}

	/*
	 * For each line in the file parse it appropriately.
	 * File format : qopname	num(int)
	 * Note that we silently ignore corrupt entries.
	 */
	qop_num_pair_cnt = 0;
	while (!feof(fp)) {
		line = fgets(buf, BUFLEN, fp);
		if (line == NULL)
			break;

		/* Skip comments and blank lines */
		if ((*line == '#') || (*line == '\n'))
			continue;

		/* Skip trailing comments */
		next = strchr(line, '#');
		if (next)
			*next = '\0';

		name = &(buf[0]);
		while (isspace(*name))
			name++;
		if (*name == '\0')	/* blank line */
			continue;

		qopname = name;	/* will contain qop name */
		while (!isspace(*qopname))
			qopname++;
		if (*qopname == '\0') {
			continue;
		}
		next = qopname+1;
		*qopname = '\0';	/* null terminate qopname */
		qop_num_pairs[qop_num_pair_cnt].qop = strdup(name);
		if (qop_num_pairs[qop_num_pair_cnt].qop == NULL)
			continue;

		name = next;
		while (isspace(*name))
			name++;
		if (*name == '\0') { 	/* end of line, no num */
			free(qop_num_pairs[qop_num_pair_cnt].qop);
			continue;
		}
		num_str = name;	/* will contain num (n) */
		while (!isspace(*num_str))
			num_str++;
		next = num_str+1;
		*num_str++ = '\0';	/* null terminate num_str */

		qop_num_pairs[qop_num_pair_cnt].num = (OM_uint32)atoi(name);
		name = next;
		while (isspace(*name))
			name++;
		if (*name == '\0') { 	/* end of line, no mechanism */
			free(qop_num_pairs[qop_num_pair_cnt].qop);
			continue;
		}
		num_str = name;	/* will contain mech */
		while (!isspace(*num_str))
			num_str++;
		*num_str = '\0';

		qop_num_pairs[qop_num_pair_cnt].mech = strdup(name);
		if (qop_num_pairs[qop_num_pair_cnt].mech == NULL) {
			free(qop_num_pairs[qop_num_pair_cnt].qop);
			continue;
		}

		if (qop_num_pair_cnt++ >= MAX_QOP_NUM_PAIRS)
			break;
	}
	(void) fclose(fp);
done:
	(void) mutex_unlock(&qopfile_lock);
	return (major);
}

OM_uint32
__gss_qop_to_num(
	char		*qop,
	char		*mech,
	OM_uint32	*num
)
{
	int i;
	OM_uint32 major = GSS_S_FAILURE;

	if (!num)
		return (GSS_S_CALL_INACCESSIBLE_WRITE);

	if (qop == NULL || strlen(qop) == 0 ||
			strcasecmp(qop, Q_DEFAULT) == 0) {
		*num = GSS_C_QOP_DEFAULT;
		return (GSS_S_COMPLETE);
	}

	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
		return (major);

	for (i = 0; i < qop_num_pair_cnt; i++) {
		if ((strcasecmp(mech, qop_num_pairs[i].mech) == 0) &&
		    (strcasecmp(qop, qop_num_pairs[i].qop) == 0)) {
			*num = qop_num_pairs[i].num;
			return (GSS_S_COMPLETE);
		}
	}

	return (GSS_S_FAILURE);
}

OM_uint32
__gss_num_to_qop(
	char		*mech,
	OM_uint32	num,
	char		**qop
)
{
	int i;
	OM_uint32 major;

	if (!qop)
		return (GSS_S_CALL_INACCESSIBLE_WRITE);
	*qop = NULL;

	if (num == GSS_C_QOP_DEFAULT) {
		*qop = Q_DEFAULT;
		return (GSS_S_COMPLETE);
	}

	if (mech == NULL)
		return (GSS_S_CALL_INACCESSIBLE_READ);

	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
		return (major);

	for (i = 0; i < qop_num_pair_cnt; i++) {
		if ((strcasecmp(mech, qop_num_pairs[i].mech) == 0) &&
		    (num == qop_num_pairs[i].num)) {
			*qop = qop_num_pairs[i].qop;
			return (GSS_S_COMPLETE);
		}
	}
	return (GSS_S_FAILURE);
}

/*
 * For a given mechanism pass back qop information about it in a buffer
 * of size MAX_QOPS_PER_MECH+1.
 */
OM_uint32
__gss_get_mech_info(
	char		*mech,
	char		**qops
)
{
	int i, cnt = 0;
	OM_uint32 major = GSS_S_COMPLETE;

	if (!qops)
		return (GSS_S_CALL_INACCESSIBLE_WRITE);
	*qops = NULL;

	if (!mech)
		return (GSS_S_CALL_INACCESSIBLE_READ);

	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
		return (major);

	for (i = 0; i < qop_num_pair_cnt; i++) {
		if (strcmp(mech, qop_num_pairs[i].mech) == 0) {
		    if (cnt >= MAX_QOPS_PER_MECH) {
			return (GSS_S_FAILURE);
		    }
		    qops[cnt++] = qop_num_pairs[i].qop;
		}
	}
	qops[cnt] = NULL;
	return (GSS_S_COMPLETE);
}

/*
 * Copy the qop values and names for the mechanism back in a qop_num
 * buffer of size MAX_QOPS_PER_MECH provided by the caller.
 */
OM_uint32
__gss_mech_qops(
	char *mech,
	qop_num *mechqops,
	int *numqop
)
{
	int i;
	OM_uint32 major;
	int cnt = 0;

	if (!mechqops || !numqop)
		return (GSS_S_CALL_INACCESSIBLE_WRITE);
	*numqop = 0;

	if (!mech)
		return (GSS_S_CALL_INACCESSIBLE_READ);

	if ((major = __gss_read_qop_file()) != GSS_S_COMPLETE)
		return (major);

	for (i = 0; i < qop_num_pair_cnt; i++) {
	    if (strcasecmp(mech, qop_num_pairs[i].mech) == 0) {
		if (cnt >= MAX_QOPS_PER_MECH) {
			return (GSS_S_FAILURE);
		}
		mechqops[cnt++] = qop_num_pairs[i];
	    }
	}
	*numqop = cnt;
	return (GSS_S_COMPLETE);
}