2000-02-17 20:38:33 +01:00
|
|
|
/* grp.cc
|
|
|
|
|
|
|
|
Copyright 1996, 1997, 1998, 2000 Cygnus Solutions.
|
|
|
|
|
|
|
|
Original stubs by Jason Molenda of Cygnus Support, crash@cygnus.com
|
|
|
|
First implementation by Gunther Ebert, gunther.ebert@ixos-leipzig.de
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 18:28:18 +02:00
|
|
|
#include "winsup.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <grp.h>
|
2001-04-30 20:21:48 +02:00
|
|
|
#include <wininet.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2000-08-29 20:59:26 +02:00
|
|
|
#include <errno.h>
|
2000-08-22 07:10:20 +02:00
|
|
|
#include "sync.h"
|
|
|
|
#include "sigproc.h"
|
2000-08-12 07:35:42 +02:00
|
|
|
#include "pinfo.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
2000-11-15 01:13:09 +01:00
|
|
|
#include "cygheap.h"
|
2000-08-29 20:59:26 +02:00
|
|
|
#include "cygerrno.h"
|
2001-04-16 16:02:42 +02:00
|
|
|
#include "security.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Read /etc/group only once for better performance. This is done
|
|
|
|
on the first call that needs information from it. */
|
|
|
|
|
|
|
|
static NO_COPY const char *etc_group = "/etc/group";
|
|
|
|
static struct group *group_buf = NULL; /* group contents in memory */
|
|
|
|
static int curr_lines = 0;
|
|
|
|
static int max_lines = 0;
|
|
|
|
|
|
|
|
/* Position in the group cache */
|
|
|
|
#ifdef _MT_SAFE
|
|
|
|
#define grp_pos _reent_winsup()->_grp_pos
|
|
|
|
#else
|
|
|
|
static int grp_pos = 0;
|
|
|
|
#endif
|
|
|
|
|
2001-04-20 15:02:32 +02:00
|
|
|
/* Set to loaded when /etc/passwd has been read in by read_etc_passwd ().
|
|
|
|
Set to emulated if passwd is emulated. */
|
|
|
|
/* Functions in this file need to check the value of passwd_state
|
|
|
|
and read in the password file if it isn't set. */
|
|
|
|
enum grp_state {
|
|
|
|
uninitialized = 0,
|
|
|
|
initializing,
|
|
|
|
emulated,
|
|
|
|
loaded
|
|
|
|
};
|
|
|
|
static grp_state group_state = uninitialized;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
static int
|
|
|
|
parse_grp (struct group &grp, const char *line)
|
|
|
|
{
|
|
|
|
int len = strlen(line);
|
|
|
|
char *newline = (char *) malloc (len + 1);
|
|
|
|
(void) memcpy (newline, line, len + 1);
|
|
|
|
|
|
|
|
if (newline[--len] == '\n')
|
|
|
|
newline[len] = '\0';
|
|
|
|
|
|
|
|
char *dp = strchr (newline, ':');
|
|
|
|
|
|
|
|
if (!dp)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*dp++ = '\0';
|
|
|
|
grp.gr_name = newline;
|
|
|
|
|
|
|
|
grp.gr_passwd = dp;
|
|
|
|
dp = strchr (grp.gr_passwd, ':');
|
|
|
|
if (dp)
|
|
|
|
{
|
|
|
|
*dp++ = '\0';
|
|
|
|
if (!strlen (grp.gr_passwd))
|
|
|
|
grp.gr_passwd = NULL;
|
|
|
|
|
2000-07-01 19:30:35 +02:00
|
|
|
grp.gr_gid = strtol (dp, NULL, 10);
|
2000-02-17 20:38:33 +01:00
|
|
|
dp = strchr (dp, ':');
|
|
|
|
if (dp)
|
|
|
|
{
|
|
|
|
if (*++dp)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
char *cp;
|
|
|
|
|
|
|
|
for (cp = dp; (cp = strchr (cp, ',')) != NULL; ++cp)
|
|
|
|
++i;
|
|
|
|
char **namearray = (char **) calloc (i + 2, sizeof (char *));
|
|
|
|
if (namearray)
|
|
|
|
{
|
|
|
|
i = 0;
|
|
|
|
for (cp = dp; (cp = strchr (dp, ',')) != NULL; dp = cp + 1)
|
|
|
|
{
|
|
|
|
*cp = '\0';
|
|
|
|
namearray[i++] = dp;
|
|
|
|
}
|
|
|
|
namearray[i++] = dp;
|
|
|
|
namearray[i] = NULL;
|
|
|
|
}
|
|
|
|
grp.gr_mem = namearray;
|
|
|
|
}
|
|
|
|
else
|
2000-04-03 20:08:13 +02:00
|
|
|
grp.gr_mem = (char **) calloc (1, sizeof (char *));
|
2000-02-17 20:38:33 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read one line from /etc/group into the group cache */
|
|
|
|
static void
|
|
|
|
add_grp_line (const char *line)
|
|
|
|
{
|
|
|
|
if (curr_lines == max_lines)
|
|
|
|
{
|
|
|
|
max_lines += 10;
|
|
|
|
group_buf = (struct group *) realloc (group_buf, max_lines * sizeof (struct group));
|
|
|
|
}
|
|
|
|
if (parse_grp (group_buf[curr_lines], line))
|
|
|
|
curr_lines++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
/* Read in /etc/group and save contents in the group cache */
|
|
|
|
/* This sets group_in_memory_p to 1 so functions in this file can
|
|
|
|
tell that /etc/group has been read in */
|
|
|
|
/* FIXME: should be static but this is called in uinfo_init outside this
|
|
|
|
file */
|
|
|
|
void
|
|
|
|
read_etc_group ()
|
|
|
|
{
|
2001-01-28 06:51:15 +01:00
|
|
|
char linebuf [200];
|
2001-04-30 20:21:48 +02:00
|
|
|
char group_name [UNLEN + 1];
|
|
|
|
DWORD group_name_len = UNLEN + 1;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
strncpy (group_name, "Administrators", sizeof (group_name));
|
|
|
|
|
2001-05-19 07:29:00 +02:00
|
|
|
static NO_COPY pthread_mutex_t etc_group_mutex = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
|
2001-04-20 15:02:32 +02:00
|
|
|
pthread_mutex_lock (&etc_group_mutex);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-04-20 15:02:32 +02:00
|
|
|
/* if we got blocked by the mutex, then etc_group may have been processed */
|
|
|
|
if (group_state != uninitialized)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-04-20 15:02:32 +02:00
|
|
|
pthread_mutex_unlock(&etc_group_mutex);
|
|
|
|
return;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
2001-04-20 15:02:32 +02:00
|
|
|
|
|
|
|
if (group_state != initializing)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-04-20 15:02:32 +02:00
|
|
|
group_state = initializing;
|
|
|
|
|
|
|
|
FILE *f = fopen (etc_group, "rt");
|
|
|
|
|
|
|
|
if (f)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2001-04-20 15:02:32 +02:00
|
|
|
while (fgets (linebuf, sizeof (linebuf), f) != NULL)
|
|
|
|
{
|
|
|
|
if (strlen (linebuf))
|
|
|
|
add_grp_line (linebuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (f);
|
|
|
|
group_state = loaded;
|
|
|
|
}
|
|
|
|
else /* /etc/group doesn't exist -- create default one in memory */
|
|
|
|
{
|
2001-04-30 20:21:48 +02:00
|
|
|
char domain_name [INTERNET_MAX_HOST_NAME_LENGTH + 1];
|
|
|
|
DWORD domain_name_len = INTERNET_MAX_HOST_NAME_LENGTH + 1;
|
2001-04-20 15:02:32 +02:00
|
|
|
SID_NAME_USE acType;
|
|
|
|
debug_printf ("Emulating /etc/group");
|
|
|
|
if (! LookupAccountSidA (NULL ,
|
* fork.cc (fork): Eliminate superfluous call to getuid().
* security.h: New define `NO_SID'. Remove declarations of functions
moved to methods into class cygsid.
(class cygsid): Declare new methods `getfromstr', `get_sid',
`getfrompw', `getfromgr', `get_rid', `get_uid', `get_gid', `string'
and new constructors and operators =, == and !=.
Declare new global cygsids `well_known_XXX_sid' substituting the
corresponding `get_XXX_sid' functions. Remove declarations of
these functions.
* sec_helper.cc (well_known_admin_sid): New global variable.
(well_known_system_sid): Ditto
(well_known_creator_owner_sid): Ditto
(well_known_world_sid): Ditto
(cygsid::string): New method, substituting `convert_sid_to_string_sid'.
(cygsid::get_sid): New method, substituting `get_sid'.
(cygsid::getfromstr): New method, substituting
`convert_string_sid_to_sid'.
(cygsid::getfrompw): New method, substituting `get_pw_sid'.
(cygsid::getfromgr): New method, substituting `get_gr_sid'.
(cygsid::get_id): New method, substituting `get_id_from_sid'.
(get_admin_sid): Eliminated.
(get_system_sid): Ditto.
(get_creator_owner_sid): Ditto.
(get_world_sid): Ditto.
* grp.cc: Use new cygsid methods and well known sids throughout.
* registry.cc: Ditto.
* sec_acl.cc: Ditto.
* security.cc: Ditto.
* shared.cc: Ditto.
* syscalls.cc (seteuid): Ditto. Eliminate redundant conditional.
* uinfo.cc (internal_getlogin): Ditto.
* spawn.cc (spawn_guts) Revert previous patch.
2001-05-15 21:23:31 +02:00
|
|
|
well_known_admin_sid,
|
2001-04-20 15:02:32 +02:00
|
|
|
group_name,
|
|
|
|
&group_name_len,
|
|
|
|
domain_name,
|
|
|
|
&domain_name_len,
|
|
|
|
&acType))
|
|
|
|
{
|
|
|
|
strcpy (group_name, "unknown");
|
|
|
|
debug_printf ("Failed to get local admins group name. %E");
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2001-04-20 15:02:32 +02:00
|
|
|
snprintf (linebuf, sizeof (linebuf), "%s::%u:\n", group_name, DEFAULT_GID);
|
|
|
|
add_grp_line (linebuf);
|
|
|
|
group_state = emulated;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2001-04-20 15:02:32 +02:00
|
|
|
pthread_mutex_unlock(&etc_group_mutex);
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
struct group *
|
|
|
|
getgrgid (gid_t gid)
|
|
|
|
{
|
|
|
|
struct group * default_grp = NULL;
|
2001-04-20 15:02:32 +02:00
|
|
|
if (group_state <= initializing)
|
2000-02-17 20:38:33 +01:00
|
|
|
read_etc_group();
|
|
|
|
|
|
|
|
for (int i = 0; i < curr_lines; i++)
|
|
|
|
{
|
|
|
|
if (group_buf[i].gr_gid == DEFAULT_GID)
|
|
|
|
default_grp = group_buf + i;
|
|
|
|
if (group_buf[i].gr_gid == gid)
|
|
|
|
return group_buf + i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return default_grp;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
struct group *
|
|
|
|
getgrnam (const char *name)
|
|
|
|
{
|
2001-04-20 15:02:32 +02:00
|
|
|
if (group_state <= initializing)
|
2000-02-17 20:38:33 +01:00
|
|
|
read_etc_group();
|
|
|
|
|
|
|
|
for (int i = 0; i < curr_lines; i++)
|
|
|
|
if (strcasematch (group_buf[i].gr_name, name))
|
|
|
|
return group_buf + i;
|
|
|
|
|
|
|
|
/* Didn't find requested group */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void
|
|
|
|
endgrent()
|
|
|
|
{
|
|
|
|
grp_pos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
struct group *
|
|
|
|
getgrent()
|
|
|
|
{
|
2001-04-20 15:02:32 +02:00
|
|
|
if (group_state <= initializing)
|
2000-02-17 20:38:33 +01:00
|
|
|
read_etc_group();
|
|
|
|
|
|
|
|
if (grp_pos < curr_lines)
|
|
|
|
return group_buf + grp_pos++;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
void
|
|
|
|
setgrent ()
|
|
|
|
{
|
|
|
|
grp_pos = 0;
|
|
|
|
}
|
|
|
|
|
2001-04-25 11:43:25 +02:00
|
|
|
/* Internal function. ONLY USE THIS INTERNALLY, NEVER `getgrent'!!! */
|
|
|
|
struct group *
|
|
|
|
internal_getgrent (int pos)
|
|
|
|
{
|
|
|
|
if (group_state <= initializing)
|
|
|
|
read_etc_group();
|
|
|
|
|
|
|
|
if (pos < curr_lines)
|
|
|
|
return group_buf + pos;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
int
|
|
|
|
getgroups (int gidsetsize, gid_t *grouplist, gid_t gid, const char *username)
|
|
|
|
{
|
2001-04-16 16:02:42 +02:00
|
|
|
HANDLE hToken = NULL;
|
|
|
|
DWORD size;
|
|
|
|
int cnt = 0;
|
2001-04-25 11:43:25 +02:00
|
|
|
struct group *gr;
|
2001-04-16 16:02:42 +02:00
|
|
|
|
2001-04-20 15:02:32 +02:00
|
|
|
if (group_state <= initializing)
|
2000-02-17 20:38:33 +01:00
|
|
|
read_etc_group();
|
|
|
|
|
2001-04-16 16:02:42 +02:00
|
|
|
if (allow_ntsec &&
|
2001-04-25 11:43:25 +02:00
|
|
|
OpenProcessToken (hMainProc, TOKEN_QUERY, &hToken))
|
2001-04-16 16:02:42 +02:00
|
|
|
{
|
2001-04-25 11:43:25 +02:00
|
|
|
if (GetTokenInformation (hToken, TokenGroups, NULL, 0, &size)
|
|
|
|
|| GetLastError () == ERROR_INSUFFICIENT_BUFFER)
|
2001-04-16 16:02:42 +02:00
|
|
|
{
|
2001-04-25 11:43:25 +02:00
|
|
|
char buf[size];
|
|
|
|
TOKEN_GROUPS *groups = (TOKEN_GROUPS *) buf;
|
|
|
|
|
|
|
|
if (GetTokenInformation (hToken, TokenGroups, buf, size, &size))
|
2001-04-16 16:02:42 +02:00
|
|
|
{
|
2001-04-25 11:43:25 +02:00
|
|
|
cygsid sid;
|
|
|
|
|
|
|
|
for (int gidx = 0; (gr = internal_getgrent (gidx)); ++gidx)
|
* fork.cc (fork): Eliminate superfluous call to getuid().
* security.h: New define `NO_SID'. Remove declarations of functions
moved to methods into class cygsid.
(class cygsid): Declare new methods `getfromstr', `get_sid',
`getfrompw', `getfromgr', `get_rid', `get_uid', `get_gid', `string'
and new constructors and operators =, == and !=.
Declare new global cygsids `well_known_XXX_sid' substituting the
corresponding `get_XXX_sid' functions. Remove declarations of
these functions.
* sec_helper.cc (well_known_admin_sid): New global variable.
(well_known_system_sid): Ditto
(well_known_creator_owner_sid): Ditto
(well_known_world_sid): Ditto
(cygsid::string): New method, substituting `convert_sid_to_string_sid'.
(cygsid::get_sid): New method, substituting `get_sid'.
(cygsid::getfromstr): New method, substituting
`convert_string_sid_to_sid'.
(cygsid::getfrompw): New method, substituting `get_pw_sid'.
(cygsid::getfromgr): New method, substituting `get_gr_sid'.
(cygsid::get_id): New method, substituting `get_id_from_sid'.
(get_admin_sid): Eliminated.
(get_system_sid): Ditto.
(get_creator_owner_sid): Ditto.
(get_world_sid): Ditto.
* grp.cc: Use new cygsid methods and well known sids throughout.
* registry.cc: Ditto.
* sec_acl.cc: Ditto.
* security.cc: Ditto.
* shared.cc: Ditto.
* syscalls.cc (seteuid): Ditto. Eliminate redundant conditional.
* uinfo.cc (internal_getlogin): Ditto.
* spawn.cc (spawn_guts) Revert previous patch.
2001-05-15 21:23:31 +02:00
|
|
|
if (sid.getfromgr (gr))
|
2001-04-25 11:43:25 +02:00
|
|
|
for (DWORD pg = 0; pg < groups->GroupCount; ++pg)
|
|
|
|
if (sid == groups->Groups[pg].Sid)
|
|
|
|
{
|
|
|
|
if (cnt < gidsetsize)
|
|
|
|
grouplist[cnt] = gr->gr_gid;
|
|
|
|
++cnt;
|
|
|
|
if (gidsetsize && cnt > gidsetsize)
|
|
|
|
{
|
|
|
|
CloseHandle (hToken);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-04-16 16:02:42 +02:00
|
|
|
}
|
|
|
|
}
|
2001-04-25 11:43:25 +02:00
|
|
|
else
|
|
|
|
debug_printf ("%d = GetTokenInformation(NULL) %E", size);
|
2001-04-16 16:02:42 +02:00
|
|
|
CloseHandle (hToken);
|
2001-04-25 11:43:25 +02:00
|
|
|
if (cnt)
|
|
|
|
return cnt;
|
2001-04-16 16:02:42 +02:00
|
|
|
}
|
2001-04-25 11:43:25 +02:00
|
|
|
|
|
|
|
for (int gidx = 0; (gr = internal_getgrent (gidx)); ++gidx)
|
|
|
|
if (gid == gr->gr_gid)
|
|
|
|
{
|
|
|
|
if (cnt < gidsetsize)
|
|
|
|
grouplist[cnt] = gr->gr_gid;
|
|
|
|
++cnt;
|
|
|
|
if (gidsetsize && cnt > gidsetsize)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else if (gr->gr_mem)
|
|
|
|
for (int gi = 0; gr->gr_mem[gi]; ++gi)
|
|
|
|
if (strcasematch (username, gr->gr_mem[gi]))
|
2001-04-16 16:02:42 +02:00
|
|
|
{
|
|
|
|
if (cnt < gidsetsize)
|
2001-04-25 11:43:25 +02:00
|
|
|
grouplist[cnt] = gr->gr_gid;
|
2001-04-16 16:02:42 +02:00
|
|
|
++cnt;
|
|
|
|
if (gidsetsize && cnt > gidsetsize)
|
|
|
|
goto error;
|
|
|
|
}
|
2001-04-25 11:43:25 +02:00
|
|
|
return cnt;
|
2000-08-29 20:59:26 +02:00
|
|
|
|
|
|
|
error:
|
2000-09-04 19:52:42 +02:00
|
|
|
set_errno (EINVAL);
|
2000-08-29 20:59:26 +02:00
|
|
|
return -1;
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
int
|
|
|
|
getgroups (int gidsetsize, gid_t *grouplist)
|
|
|
|
{
|
2000-11-15 01:13:09 +01:00
|
|
|
return getgroups (gidsetsize, grouplist, myself->gid, cygheap->user.name ());
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C"
|
|
|
|
int
|
2000-02-21 06:20:38 +01:00
|
|
|
initgroups (const char *, gid_t)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|