xref: /titanic_50/usr/src/cmd/ssh/sshd/groupaccess.c (revision 67dbe2be0c0f1e2eb428b89088bb5667e8f0b9f6)
17c478bd9Sstevel@tonic-gate /*
2*67dbe2beSCasper H.S. Dik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3*67dbe2beSCasper H.S. Dik  * Use is subject to license terms.
4*67dbe2beSCasper H.S. Dik  */
5*67dbe2beSCasper H.S. Dik /*
67c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 Kevin Steves.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
97c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
107c478bd9Sstevel@tonic-gate  * are met:
117c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
127c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
137c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
147c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
157c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
187c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
197c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
207c478bd9Sstevel@tonic-gate  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
217c478bd9Sstevel@tonic-gate  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
227c478bd9Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
237c478bd9Sstevel@tonic-gate  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
247c478bd9Sstevel@tonic-gate  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
257c478bd9Sstevel@tonic-gate  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
267c478bd9Sstevel@tonic-gate  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include "includes.h"
307c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: groupaccess.c,v 1.5 2002/03/04 17:27:39 stevesk Exp $");
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "groupaccess.h"
337c478bd9Sstevel@tonic-gate #include "xmalloc.h"
347c478bd9Sstevel@tonic-gate #include "match.h"
357c478bd9Sstevel@tonic-gate #include "log.h"
36*67dbe2beSCasper H.S. Dik #include <alloca.h>
377c478bd9Sstevel@tonic-gate 
38*67dbe2beSCasper H.S. Dik static int ngroups, ngroups_lim;
39*67dbe2beSCasper H.S. Dik static char **groups_byname;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Initialize group access list for user with primary (base) and
437c478bd9Sstevel@tonic-gate  * supplementary groups.  Return the number of groups in the list.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate int
ga_init(const char * user,gid_t base)467c478bd9Sstevel@tonic-gate ga_init(const char *user, gid_t base)
477c478bd9Sstevel@tonic-gate {
48*67dbe2beSCasper H.S. Dik 	gid_t *groups_bygid;
497c478bd9Sstevel@tonic-gate 	int i, j;
507c478bd9Sstevel@tonic-gate 	struct group *gr;
517c478bd9Sstevel@tonic-gate 
52*67dbe2beSCasper H.S. Dik 	if (ngroups_lim == 0) {
53*67dbe2beSCasper H.S. Dik 		/* Add one for the base gid */
54*67dbe2beSCasper H.S. Dik 		ngroups_lim = sysconf(_SC_NGROUPS_MAX) + 1;
55*67dbe2beSCasper H.S. Dik 		groups_byname = malloc(sizeof (char *) * ngroups_lim);
56*67dbe2beSCasper H.S. Dik 	} else if (ngroups > 0)
577c478bd9Sstevel@tonic-gate 		ga_free();
587c478bd9Sstevel@tonic-gate 
59*67dbe2beSCasper H.S. Dik 	groups_bygid = alloca(ngroups_lim * sizeof (gid_t));
60*67dbe2beSCasper H.S. Dik 
61*67dbe2beSCasper H.S. Dik 	ngroups = ngroups_lim;
627c478bd9Sstevel@tonic-gate 	if (getgrouplist(user, base, groups_bygid, &ngroups) == -1)
637c478bd9Sstevel@tonic-gate 		log("getgrouplist: groups list too small");
647c478bd9Sstevel@tonic-gate 	for (i = 0, j = 0; i < ngroups; i++)
657c478bd9Sstevel@tonic-gate 		if ((gr = getgrgid(groups_bygid[i])) != NULL)
667c478bd9Sstevel@tonic-gate 			groups_byname[j++] = xstrdup(gr->gr_name);
677c478bd9Sstevel@tonic-gate 	return (ngroups = j);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /*
717c478bd9Sstevel@tonic-gate  * Return 1 if one of user's groups is contained in groups.
727c478bd9Sstevel@tonic-gate  * Return 0 otherwise.  Use match_pattern() for string comparison.
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate int
ga_match(char * const * groups,int n)757c478bd9Sstevel@tonic-gate ga_match(char * const *groups, int n)
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate 	int i, j;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	for (i = 0; i < ngroups; i++)
807c478bd9Sstevel@tonic-gate 		for (j = 0; j < n; j++)
817c478bd9Sstevel@tonic-gate 			if (match_pattern(groups_byname[i], groups[j]))
82*67dbe2beSCasper H.S. Dik 				return (1);
83*67dbe2beSCasper H.S. Dik 	return (0);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /*
87b07b2f5cSHuie-Ying Lee  * Return 1 if one of user's groups matches group_pattern list.
88b07b2f5cSHuie-Ying Lee  * Return 0 on negated or no match.
89b07b2f5cSHuie-Ying Lee  */
90b07b2f5cSHuie-Ying Lee int
ga_match_pattern_list(const char * group_pattern)91b07b2f5cSHuie-Ying Lee ga_match_pattern_list(const char *group_pattern)
92b07b2f5cSHuie-Ying Lee {
93b07b2f5cSHuie-Ying Lee 	int i, found = 0;
94b07b2f5cSHuie-Ying Lee 	size_t len = strlen(group_pattern);
95b07b2f5cSHuie-Ying Lee 
96b07b2f5cSHuie-Ying Lee 	for (i = 0; i < ngroups; i++) {
97b07b2f5cSHuie-Ying Lee 		switch (match_pattern_list(groups_byname[i],
98b07b2f5cSHuie-Ying Lee 		    group_pattern, len, 0)) {
99b07b2f5cSHuie-Ying Lee 		case -1:
100*67dbe2beSCasper H.S. Dik 			return (0);	/* Negated match wins */
101b07b2f5cSHuie-Ying Lee 		case 0:
102b07b2f5cSHuie-Ying Lee 			continue;
103b07b2f5cSHuie-Ying Lee 		case 1:
104b07b2f5cSHuie-Ying Lee 			found = 1;
105b07b2f5cSHuie-Ying Lee 		}
106b07b2f5cSHuie-Ying Lee 	}
107*67dbe2beSCasper H.S. Dik 	return (found);
108b07b2f5cSHuie-Ying Lee }
109b07b2f5cSHuie-Ying Lee 
110b07b2f5cSHuie-Ying Lee /*
1117c478bd9Sstevel@tonic-gate  * Free memory allocated for group access list.
1127c478bd9Sstevel@tonic-gate  */
1137c478bd9Sstevel@tonic-gate void
ga_free(void)1147c478bd9Sstevel@tonic-gate ga_free(void)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	int i;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (ngroups > 0) {
1197c478bd9Sstevel@tonic-gate 		for (i = 0; i < ngroups; i++)
1207c478bd9Sstevel@tonic-gate 			xfree(groups_byname[i]);
1217c478bd9Sstevel@tonic-gate 		ngroups = 0;
1227c478bd9Sstevel@tonic-gate 	}
1237c478bd9Sstevel@tonic-gate }
124