aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/grp.cc
blob: d3431a37f9931689c94985c522b203a53764370f (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* 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. */

#include "winsup.h"
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "sync.h"
#include "sigproc.h"
#include "pinfo.h"
#include "cygheap.h"
#include "cygerrno.h"
#include "security.h"

/* Read /etc/group only once for better performance.  This is done
   on the first call that needs information from it. */

#define MAX_DOMAIN_NAME	100

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

/* Set to 1 when /etc/group has been read in by read_etc_group (). */
/* Functions in this file need to check the value of group_in_memory_p
   and read in the group file if it isn't set. */
/* FIXME: This should be static but this is called in uinfo_init outside
   this file */
int group_in_memory_p = 0;

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;

      grp.gr_gid = strtol (dp, NULL, 10);
      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
            grp.gr_mem = (char **) calloc (1, sizeof (char *));
          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++;
}

extern PSID get_admin_sid ();

/* 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 ()
{
  extern int group_sem;
  char linebuf [200];
  char group_name [MAX_USER_NAME];
  DWORD group_name_len = MAX_USER_NAME;

  strncpy (group_name, "Administrators", sizeof (group_name));

  ++group_sem;
  FILE *f = fopen (etc_group, "rt");
  --group_sem;

  if (f)
    {
      while (fgets (linebuf, sizeof (linebuf), f) != NULL)
	{
	  if (strlen (linebuf))
	    add_grp_line (linebuf);
	}

      fclose (f);
    }
  else /* /etc/group doesn't exist -- create default one in memory */
    {
      char domain_name [MAX_DOMAIN_NAME];
      DWORD domain_name_len = MAX_DOMAIN_NAME;
      SID_NAME_USE acType;
      debug_printf ("Emulating /etc/group");
      if (! LookupAccountSidA (NULL ,
				get_admin_sid () ,
				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");
        }

      snprintf (linebuf, sizeof (linebuf), "%s::%u:\n", group_name, DEFAULT_GID);
      add_grp_line (linebuf);
    }

  group_in_memory_p = 1;
}

extern "C"
struct group *
getgrgid (gid_t gid)
{
  struct group * default_grp = NULL;
  if (!group_in_memory_p)
    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)
{
  if (!group_in_memory_p)
    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()
{
  if (!group_in_memory_p)
    read_etc_group();

  if (grp_pos < curr_lines)
    return group_buf + grp_pos++;

  return NULL;
}

extern "C"
void
setgrent ()
{
  grp_pos = 0;
}

int
getgroups (int gidsetsize, gid_t *grouplist, gid_t gid, const char *username)
{
  HANDLE hToken = NULL;
  char buf[4096];
  DWORD size;
  int cnt = 0;

  if (!group_in_memory_p)
    read_etc_group();

  if (allow_ntsec &&
      OpenProcessToken (hMainProc, TOKEN_QUERY, &hToken) &&
      GetTokenInformation (hToken, TokenGroups, buf, 4096, &size))
    {
      TOKEN_GROUPS *groups = (TOKEN_GROUPS *) buf;
      char ssid[256];

      for (DWORD pg = 0; pg < groups->GroupCount; ++pg)
        {
	  convert_sid_to_string_sid (groups->Groups[pg].Sid, ssid);
          for (int gg = 0; gg < curr_lines; ++gg)
	    {
	      if (group_buf[gg].gr_passwd &&
	          !strcmp (group_buf[gg].gr_passwd, ssid))
	        {
		  if (cnt < gidsetsize)
		    grouplist[cnt] = group_buf[gg].gr_gid;
		  ++cnt;
		  if (gidsetsize && cnt > gidsetsize)
		    goto error;
		  break;
		}
	    }
        }
      CloseHandle (hToken);
      return cnt;
    }
  else
    {
      for (int i = 0; i < curr_lines; ++i)
	if (gid == group_buf[i].gr_gid)
	  {
	    if (cnt < gidsetsize)
	      grouplist[cnt] = group_buf[i].gr_gid;
	    ++cnt;
	    if (gidsetsize && cnt > gidsetsize)
	      goto error;
	  }
	else if (group_buf[i].gr_mem)
	  for (int gi = 0; group_buf[i].gr_mem[gi]; ++gi)
	    if (strcasematch (username, group_buf[i].gr_mem[gi]))
	      {
		if (cnt < gidsetsize)
		  grouplist[cnt] = group_buf[i].gr_gid;
		++cnt;
		if (gidsetsize && cnt > gidsetsize)
		  goto error;
	      }
      return cnt;
    }

error:
  set_errno (EINVAL);
  return -1;
}

extern "C"
int
getgroups (int gidsetsize, gid_t *grouplist)
{
  return getgroups (gidsetsize, grouplist, myself->gid, cygheap->user.name ());
}

extern "C"
int
initgroups (const char *, gid_t)
{
  return 0;
}