summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/pwdgrp.h
blob: 6397736830a2b31cdd655139b398d63c9e9a2aab (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
/* pwdgrp.h

   Stuff common to pwd and grp handling.

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. */

#pragma once

#include "sync.h"
#include "ldap.h"
#include "miscfuncs.h"
#include "userinfo.h"

/* These functions are needed to allow searching and walking through
   the passwd and group lists */
extern struct passwd *internal_getpwsid (cygpsid &, cyg_ldap * = NULL);
extern struct passwd *internal_getpwsid_from_db (cygpsid &sid);
extern struct passwd *internal_getpwnam (const char *, cyg_ldap * = NULL);
extern struct passwd *internal_getpwuid (uid_t, cyg_ldap * = NULL);
extern struct group *internal_getgrsid (cygpsid &, cyg_ldap * = NULL);
extern struct group *internal_getgrsid_from_db (cygpsid &sid);
extern struct group *internal_getgrgid (gid_t, cyg_ldap * = NULL);
extern struct group *internal_getgrnam (const char *, cyg_ldap * = NULL);

extern int internal_getgroups (int, gid_t *, cyg_ldap *);

/* These functions are called from mkpasswd/mkgroup via cygwin_internal. */
void *setpwent_filtered (int enums, PCWSTR enum_tdoms);
void *getpwent_filtered (void *gr);
void endpwent_filtered (void *gr);
void *setgrent_filtered (int enums, PCWSTR enum_tdoms);
void *getgrent_filtered (void *gr);
void endgrent_filtered (void *gr);

/* NOTE: The below sid members were cygsid's originally.  Don't do that.
   cygsid's are pointer based.  When adding new entries to the passwd or
   group caches, a crealloc call potenitally moves the entries and then
   the cygsid pointers point into neverneverland. */
struct pg_pwd
{
  struct passwd p;
  BYTE sid[SECURITY_MAX_SID_SIZE];
  size_t len;
};

struct pg_grp
{
  struct group g;
  BYTE sid[SECURITY_MAX_SID_SIZE];
  size_t len;
};

class pwdgrp
{
  friend class pg_ent;
  friend class pw_ent;
  friend class gr_ent;

  unsigned pwdgrp_buf_elem_size;
  void *pwdgrp_buf;
  bool (pwdgrp::*parse) ();
  char pwd_sep;
  UNICODE_STRING path;
  OBJECT_ATTRIBUTES attr;
  LARGE_INTEGER last_modified;
  char *lptr;
  ULONG curr_lines;
  ULONG max_lines;
  static muto pglock;

  bool parse_passwd ();
  bool parse_group ();
  char *add_line (char *);
  char *raw_ptr () const {return lptr;}
  char *next_str (char);
  bool next_num (unsigned long&);
  bool next_num (unsigned int& i)
  {
    unsigned long x;
    bool res = next_num (x);
    i = (unsigned int) x;
    return res;
  }
  inline bool next_num (int& i)
  {
    unsigned long x;
    bool res = next_num (x);
    i = (int) x;
    return res;
  }
  void *add_account_post_fetch (char *line, bool lock);
  void *add_account_from_file (cygpsid &sid);
  void *add_account_from_file (const char *name);
  void *add_account_from_file (uint32_t id);
  void *add_account_from_windows (cygpsid &sid, cyg_ldap *pldap = NULL);
  void *add_account_from_windows (const char *name, cyg_ldap *pldap = NULL);
  void *add_account_from_windows (uint32_t id, cyg_ldap *pldap = NULL);
  void *add_account_from_cygserver (cygpsid &sid);
  void *add_account_from_cygserver (const char *name);
  void *add_account_from_cygserver (uint32_t id);
  char *fetch_account_from_line (fetch_user_arg_t &arg, const char *line);
  char *fetch_account_from_file (fetch_user_arg_t &arg);
  char *fetch_account_from_windows (fetch_user_arg_t &arg,
				    cyg_ldap *pldap = NULL);
  char *fetch_account_from_cygserver (fetch_user_arg_t &arg);

public:
  ULONG cached_users () const { return curr_lines; }
  ULONG cached_groups () const { return curr_lines; }
  POBJECT_ATTRIBUTES file_attr () { return &attr; }
  bool check_file ();

  void init_pwd ();
  bool is_passwd () const { return pwdgrp_buf_elem_size == sizeof (pg_pwd); }
  pg_pwd *passwd () const { return (pg_pwd *) pwdgrp_buf; };
  struct passwd *add_user_from_cygserver (cygpsid &sid)
    { return (struct passwd *) add_account_from_cygserver (sid); }
  struct passwd *add_user_from_cygserver (const char *name)
    { return (struct passwd *) add_account_from_cygserver (name); }
  struct passwd *add_user_from_cygserver (uint32_t id)
    { return (struct passwd *) add_account_from_cygserver (id); }
  struct passwd *add_user_from_file (cygpsid &sid)
    { return (struct passwd *) add_account_from_file (sid); }
  struct passwd *add_user_from_file (const char *name)
    { return (struct passwd *) add_account_from_file (name); }
  struct passwd *add_user_from_file (uint32_t id)
    { return (struct passwd *) add_account_from_file (id); }
  struct passwd *add_user_from_windows (cygpsid &sid, cyg_ldap *pldap = NULL)
    { return (struct passwd *) add_account_from_windows (sid, pldap); }
  struct passwd *add_user_from_windows (const char *name,
  					cyg_ldap* pldap = NULL)
    { return (struct passwd *) add_account_from_windows (name, pldap); }
  struct passwd *add_user_from_windows (uint32_t id, cyg_ldap *pldap = NULL)
    { return (struct passwd *) add_account_from_windows (id, pldap); }
  struct passwd *find_user (cygpsid &sid);
  struct passwd *find_user (const char *name);
  struct passwd *find_user (uid_t uid);

  void init_grp ();
  bool is_group () const { return pwdgrp_buf_elem_size == sizeof (pg_grp); }
  pg_grp *group () const { return (pg_grp *) pwdgrp_buf; };
  struct group *add_group_from_cygserver (cygpsid &sid)
    { return (struct group *) add_account_from_cygserver (sid); }
  struct group *add_group_from_cygserver (const char *name)
    { return (struct group *) add_account_from_cygserver (name); }
  struct group *add_group_from_cygserver (uint32_t id)
    { return (struct group *) add_account_from_cygserver (id); }
  struct group *add_group_from_file (cygpsid &sid)
    { return (struct group *) add_account_from_file (sid); }
  struct group *add_group_from_file (const char *name)
    { return (struct group *) add_account_from_file (name); }
  struct group *add_group_from_file (uint32_t id)
    { return (struct group *) add_account_from_file (id); }
  struct group *add_group_from_windows (cygpsid &sid, cyg_ldap *pldap = NULL)
    { return (struct group *) add_account_from_windows (sid, pldap); }
  struct group *add_group_from_windows (const char *name,
					cyg_ldap *pldap = NULL)
    { return (struct group *) add_account_from_windows (name, pldap); }
  struct group *add_group_from_windows (uint32_t id, cyg_ldap *pldap = NULL)
    { return (struct group *) add_account_from_windows (id, pldap); }
  struct group *add_group_from_windows (fetch_acc_t &full_acc,
  					cyg_ldap *pldap = NULL);
  struct group *find_group (cygpsid &sid);
  struct group *find_group (const char *name);
  struct group *find_group (gid_t gid);
};

class pg_ent
{
protected:
  pwdgrp         pg;
  bool           group;
  pg_pwd         pwd;
  pg_grp         grp;
  NT_readline    rl;
  cyg_ldap       cldap;
  PCHAR          buf;
  ULONG          cnt;
  ULONG          max;
  ULONG_PTR      resume;
  int            enums;		/* ENUM_xxx values defined in sys/cygwin.h. */
  PCWSTR         enum_tdoms;
  bool           from_files;
  bool           from_db;
  UNICODE_STRING dom;
  enum {
    rewound = 0,
    from_cache,
    from_file,
    from_builtin,
    from_local,
    from_sam,
    from_ad,
    finished
  } state;

  void clear_cache ();
  inline bool nss_db_enum_caches () const { return !!(enums & ENUM_CACHE); }
  inline bool nss_db_enum_files () const { return !!(enums & ENUM_FILES); }
  inline bool nss_db_enum_builtin () const { return !!(enums & ENUM_BUILTIN); }
  inline bool nss_db_enum_local () const { return !!(enums & ENUM_LOCAL); }
  inline bool nss_db_enum_primary () const { return !!(enums & ENUM_PRIMARY); }
  inline bool nss_db_enum_tdom (PWCHAR domain)
    {
      if (enums & ENUM_TDOMS_ALL)
        return true;
      if (!(enums & ENUM_TDOMS) || !enum_tdoms || !domain)
        return false;
      for (PCWSTR td = enum_tdoms; td && *td; td = wcschr (td, L'\0'))
        if (!wcscasecmp (td, domain))
          return true;
      return false;
    }
  virtual void *enumerate_caches () = 0;
  virtual void *enumerate_file ();
  virtual void *enumerate_builtin ();
  virtual void *enumerate_local () = 0;
  virtual void *enumerate_sam ();
  virtual void *enumerate_ad ();

public:
  void setent (bool _group, int _enums = 0, PCWSTR _enum_tdoms = NULL);
  void *getent ();
  void endent (bool _group);
};

class pw_ent : public pg_ent
{
  void *enumerate_caches ();
  void *enumerate_local ();
public:
  inline void setpwent (int _enums = 0, PCWSTR _enum_tdoms = NULL)
    { setent (false, _enums, _enum_tdoms); }
  struct passwd *getpwent ();
  inline void endpwent () { endent (false); }
};

class gr_ent : public pg_ent
{
  void *enumerate_caches ();
  void *enumerate_local ();
public:
  inline void setgrent (int _enums = 0, PCWSTR _enum_tdoms = NULL)
    { setent (true, _enums, _enum_tdoms); }
  struct group *getgrent ();
  inline void endgrent () { endent (true); }
};

/* These inline methods have to be defined here so that pg_pwd and pg_grp
   are defined. */
inline BOOL cygsid::getfrompw (const struct passwd *pw)
  { return (*this = pw ? (PSID) ((pg_pwd *) pw)->sid : NO_SID) != NO_SID; }

inline BOOL cygsid::getfromgr (const struct group *gr)
  { return (*this = gr ? (PSID) ((pg_grp *) gr)->sid : NO_SID) != NO_SID; }

/* Use these functions if you just need the PSID. */
inline PSID sidfromuid (uid_t uid, cyg_ldap *pldap)
  {
    struct passwd *pw = internal_getpwuid (uid, pldap);
    if (pw)
      return (PSID) ((pg_pwd *) pw)->sid;
    return NO_SID;
  }
inline PSID sidfromgid (gid_t gid, cyg_ldap *pldap)
  {
    struct group *gr = internal_getgrgid (gid, pldap);
    if (gr)
      return (PSID) ((pg_grp *) gr)->sid;
    return NO_SID;
  }