summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/passwd.cc
blob: d2f7b2c2e38f82f8f3eee3db81659d36cf29abd7 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
/* passwd.cc: getpwnam () and friends

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 <lm.h>
#include <stdlib.h>
#include <stdio.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "pinfo.h"
#include "cygheap.h"
#include "shared_info.h"
#include "miscfuncs.h"
#include "ldap.h"
#include "tls_pbuf.h"

/* Parse /etc/passwd line into passwd structure. */
bool
pwdgrp::parse_passwd ()
{
  pg_pwd &res = passwd ()[curr_lines];
  res.p.pw_name = next_str (':');
  res.p.pw_passwd = next_str (':');
  if (!next_num (res.p.pw_uid))
    return false;
  if (!next_num (res.p.pw_gid))
    return false;
  res.p.pw_comment = NULL;
  res.p.pw_gecos = next_str (':');
  res.p.pw_dir =  next_str (':');
  res.p.pw_shell = next_str (':');
  cygsid csid;
  if (csid.getfrompw_gecos (&res.p))
    RtlCopySid (SECURITY_MAX_SID_SIZE, res.sid, csid);
  /* lptr points to the \0 after pw_shell.  Increment by one to get the correct
     required buffer len in getpw_cp. */
  res.len = lptr - res.p.pw_name + 1;
  return true;
}

void
pwdgrp::init_pwd ()
{
  pwdgrp_buf_elem_size = sizeof (pg_pwd);
  parse = &pwdgrp::parse_passwd;
}

struct passwd *
pwdgrp::find_user (cygpsid &sid)
{
  for (ULONG i = 0; i < curr_lines; i++)
    if (sid == passwd ()[i].sid)
      return &passwd ()[i].p;
  return NULL;
}

struct passwd *
pwdgrp::find_user (const char *name)
{
  for (ULONG i = 0; i < curr_lines; i++)
    /* on Windows NT user names are case-insensitive */
    if (strcasematch (name, passwd ()[i].p.pw_name))
      return &passwd ()[i].p;
  return NULL;
}

struct passwd *
pwdgrp::find_user (uid_t uid)
{
  for (ULONG i = 0; i < curr_lines; i++)
    if (uid == passwd ()[i].p.pw_uid)
      return &passwd ()[i].p;
  return NULL;
}

struct passwd *
internal_getpwsid (cygpsid &sid, cyg_ldap *pldap)
{
  struct passwd *ret;

  cygheap->pg.nss_init ();
  /* Check caches first. */
  if (cygheap->pg.nss_cygserver_caching ()
      && (ret = cygheap->pg.pwd_cache.cygserver.find_user (sid)))
    return ret;
  if (cygheap->pg.nss_pwd_files ()
      && (ret = cygheap->pg.pwd_cache.file.find_user (sid)))
    return ret;
  if (cygheap->pg.nss_pwd_db ()
      && (ret = cygheap->pg.pwd_cache.win.find_user (sid)))
    return ret;
  /* Ask sources afterwards. */
  if (cygheap->pg.nss_cygserver_caching ()
      && (ret = cygheap->pg.pwd_cache.cygserver.add_user_from_cygserver (sid)))
    return ret;
  if (cygheap->pg.nss_pwd_files ())
    {
      cygheap->pg.pwd_cache.file.check_file ();
      if ((ret = cygheap->pg.pwd_cache.file.add_user_from_file (sid)))
	return ret;
    }
  if (cygheap->pg.nss_pwd_db ())
    return cygheap->pg.pwd_cache.win.add_user_from_windows (sid, pldap);
  return NULL;
}

/* This function gets only called from mkpasswd via cygwin_internal. */
struct passwd *
internal_getpwsid_from_db (cygpsid &sid)
{
  cygheap->pg.nss_init ();
  return cygheap->pg.pwd_cache.win.add_user_from_windows (sid);
}

struct passwd *
internal_getpwnam (const char *name, cyg_ldap *pldap)
{
  struct passwd *ret;

  cygheap->pg.nss_init ();
  /* Check caches first. */
  if (cygheap->pg.nss_cygserver_caching ()
      && (ret = cygheap->pg.pwd_cache.cygserver.find_user (name)))
    return ret;
  if (cygheap->pg.nss_pwd_files ()
      && (ret = cygheap->pg.pwd_cache.file.find_user (name)))
    return ret;
  if (cygheap->pg.nss_pwd_db ()
      && (ret = cygheap->pg.pwd_cache.win.find_user (name)))
    return ret;
  /* Ask sources afterwards. */
  if (cygheap->pg.nss_cygserver_caching ()
      && (ret = cygheap->pg.pwd_cache.cygserver.add_user_from_cygserver (name)))
    return ret;
  if (cygheap->pg.nss_pwd_files ())
    {
      cygheap->pg.pwd_cache.file.check_file ();
      if ((ret = cygheap->pg.pwd_cache.file.add_user_from_file (name)))
	return ret;
    }
  if (cygheap->pg.nss_pwd_db ())
    return cygheap->pg.pwd_cache.win.add_user_from_windows (name, pldap);
  return NULL;
}

struct passwd *
internal_getpwuid (uid_t uid, cyg_ldap *pldap)
{
  struct passwd *ret;

  cygheap->pg.nss_init ();
  /* Check caches first. */
  if (cygheap->pg.nss_cygserver_caching ()
      && (ret = cygheap->pg.pwd_cache.cygserver.find_user (uid)))
    return ret;
  if (cygheap->pg.nss_pwd_files ()
      && (ret = cygheap->pg.pwd_cache.file.find_user (uid)))
    return ret;
  if (cygheap->pg.nss_pwd_db ()
      && (ret = cygheap->pg.pwd_cache.win.find_user (uid)))
    return ret;
  /* Ask sources afterwards. */
  if (cygheap->pg.nss_cygserver_caching ()
      && (ret = cygheap->pg.pwd_cache.cygserver.add_user_from_cygserver (uid)))
    return ret;
  if (cygheap->pg.nss_pwd_files ())
    {
      cygheap->pg.pwd_cache.file.check_file ();
      if ((ret = cygheap->pg.pwd_cache.file.add_user_from_file (uid)))
	return ret;
    }
  if (cygheap->pg.nss_pwd_db () || uid == ILLEGAL_UID)
    return cygheap->pg.pwd_cache.win.add_user_from_windows (uid, pldap);
  return NULL;
}

/* getpwuid/getpwnam are not reentrant. */
static struct {
  struct passwd p;
  char *buf;
  size_t bufsiz;
} app_pw;

static struct passwd *
getpw_cp (struct passwd *temppw)
{
  if (!temppw)
    return NULL;
  pg_pwd *pw = (pg_pwd *) temppw;
  if (app_pw.bufsiz < pw->len)
    {
      char *newbuf = (char *) realloc (app_pw.buf, pw->len);
      if (!newbuf)
	{
	  set_errno (ENOMEM);
	  return NULL;
	}
      app_pw.buf = newbuf;
      app_pw.bufsiz = pw->len;
    }
  memcpy (app_pw.buf, pw->p.pw_name, pw->len);
  memcpy (&app_pw.p, &pw->p, sizeof pw->p);
  ptrdiff_t diff = app_pw.buf - pw->p.pw_name;
  app_pw.p.pw_name += diff;
  app_pw.p.pw_passwd += diff;
  app_pw.p.pw_gecos += diff;
  app_pw.p.pw_dir += diff;
  app_pw.p.pw_shell += diff;
  return &app_pw.p;
}

extern "C" struct passwd *
getpwuid32 (uid_t uid)
{
  struct passwd *temppw = internal_getpwuid (uid);
  pthread_testcancel ();
  return getpw_cp (temppw);
}

#ifdef __x86_64__
EXPORT_ALIAS (getpwuid32, getpwuid)
#else
extern "C" struct passwd *
getpwuid (__uid16_t uid)
{
  return getpwuid32 (uid16touid32 (uid));
}
#endif

extern "C" int
getpwuid_r32 (uid_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
{
  *result = NULL;

  if (!pwd || !buffer)
    return ERANGE;

  struct passwd *temppw = internal_getpwuid (uid);
  pthread_testcancel ();
  if (!temppw)
    return 0;

  /* check needed buffer size. */
  size_t needsize = strlen (temppw->pw_name) + strlen (temppw->pw_passwd)
		    + strlen (temppw->pw_gecos) + strlen (temppw->pw_dir)
		    + strlen (temppw->pw_shell) + 5;
  if (needsize > bufsize)
    return ERANGE;

  /* make a copy of temppw */
  *result = pwd;
  pwd->pw_uid = temppw->pw_uid;
  pwd->pw_gid = temppw->pw_gid;
  buffer = stpcpy (pwd->pw_name = buffer, temppw->pw_name);
  buffer = stpcpy (pwd->pw_passwd = buffer + 1, temppw->pw_passwd);
  buffer = stpcpy (pwd->pw_gecos = buffer + 1, temppw->pw_gecos);
  buffer = stpcpy (pwd->pw_dir = buffer + 1, temppw->pw_dir);
  stpcpy (pwd->pw_shell = buffer + 1, temppw->pw_shell);
  pwd->pw_comment = NULL;
  return 0;
}

#ifdef __x86_64__
EXPORT_ALIAS (getpwuid_r32, getpwuid_r)
#else
extern "C" int
getpwuid_r (__uid16_t uid, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
{
  return getpwuid_r32 (uid16touid32 (uid), pwd, buffer, bufsize, result);
}
#endif

extern "C" struct passwd *
getpwnam (const char *name)
{
  struct passwd *temppw = internal_getpwnam (name);
  pthread_testcancel ();
  return getpw_cp (temppw);
}


/* the max size buffer we can expect to
 * use is returned via sysconf with _SC_GETPW_R_SIZE_MAX.
 * This may need updating! - Rob Collins April 2001.
 */
extern "C" int
getpwnam_r (const char *nam, struct passwd *pwd, char *buffer, size_t bufsize, struct passwd **result)
{
  *result = NULL;

  if (!pwd || !buffer || !nam)
    return ERANGE;

  struct passwd *temppw = internal_getpwnam (nam);
  pthread_testcancel ();

  if (!temppw)
    return 0;

  /* check needed buffer size. */
  size_t needsize = strlen (temppw->pw_name) + strlen (temppw->pw_passwd)
		    + strlen (temppw->pw_gecos) + strlen (temppw->pw_dir)
		    + strlen (temppw->pw_shell) + 5;
  if (needsize > bufsize)
    return ERANGE;

  /* make a copy of temppw */
  *result = pwd;
  pwd->pw_uid = temppw->pw_uid;
  pwd->pw_gid = temppw->pw_gid;
  buffer = stpcpy (pwd->pw_name = buffer, temppw->pw_name);
  buffer = stpcpy (pwd->pw_passwd = buffer + 1, temppw->pw_passwd);
  buffer = stpcpy (pwd->pw_gecos = buffer + 1, temppw->pw_gecos);
  buffer = stpcpy (pwd->pw_dir = buffer + 1, temppw->pw_dir);
  stpcpy (pwd->pw_shell = buffer + 1, temppw->pw_shell);
  pwd->pw_comment = NULL;
  return 0;
}

/* getpwent functions are not reentrant. */
static pw_ent pwent;

void
pg_ent::clear_cache ()
{
  if (pg.curr_lines)
    {
      if (state > from_file)
	cfree (group ? grp.g.gr_name : pwd.p.pw_name);
      pg.curr_lines = 0;
    }
}

void
pg_ent::setent (bool _group, int _enums, PCWSTR _enum_tdoms)
{
  cygheap->dom.init ();
  endent (_group);
  if (!_enums && !_enum_tdoms)
    {
      /* This is the default, when called from the usual setpwent/setgrent
         functions. */
      enums = cygheap->pg.nss_db_enums ();
      enum_tdoms = cygheap->pg.nss_db_enum_tdoms ();
      if (_group)
	{
	  from_files = cygheap->pg.nss_grp_files ();
	  from_db = cygheap->pg.nss_grp_db ();
	}
      else
	{
	  from_files = cygheap->pg.nss_pwd_files ();
	  from_db = cygheap->pg.nss_pwd_db ();
	}
    }
  else
    {
      /* This case is when called from mkpasswd/mkgroup via cygwin_internal. */
      enums = _enums;
      enum_tdoms = _enum_tdoms;
      from_files = false;
      from_db = true;
    }
  state = from_cache;
}

void *
pg_ent::getent (void)
{
  void *entry;

  switch (state)
    {
    case rewound:
      state = from_cache;
      /*FALLTHRU*/
    case from_cache:
      if (nss_db_enum_caches ()
	  && (entry = enumerate_caches ()))
	return entry;
      state = from_file;
      /*FALLTHRU*/
    case from_file:
      if (from_files
	  && nss_db_enum_files ()
	  && (entry = enumerate_file ()))
	return entry;
      state = from_builtin;
      /*FALLTHRU*/
    case from_builtin:
      if (from_db
	  && nss_db_enum_builtin ()
	  && (entry = enumerate_builtin ()))
	return entry;
      state = from_local;
      /*FALLTHRU*/
    case from_local:
      if (from_db
	  && nss_db_enum_local ()
	  && (!cygheap->dom.member_machine ()
	      || !nss_db_enum_primary ())
	  && (entry = enumerate_local ()))
	return entry;
      state = from_sam;
      /*FALLTHRU*/
    case from_sam:
      if (from_db
	  && nss_db_enum_local ()
	  /* Domain controller?  If so, sam and ad are one and the same
	     and "local ad" would list all domain accounts twice without
	     this test. */
	  && (cygheap->dom.account_flat_name ()[0] != L'@'
	      || !nss_db_enum_primary ())
	  && (entry = enumerate_sam ()))
	return entry;
      state = from_ad;
      /*FALLTHRU*/
    case from_ad:
      if (cygheap->dom.member_machine ()
	  && from_db
	  && (entry = enumerate_ad ()))
	return entry;
      state = finished;
      /*FALLTHRU*/
    case finished:
      break;
    }
  return NULL;
}

void
pg_ent::endent (bool _group)
{
  if (buf)
    {
      if (state == from_file)
	free (buf);
      else if (state == from_local || state == from_sam)
      	NetApiBufferFree (buf);
      buf = NULL;
    }
  if (!pg.curr_lines)
    {
      if ((group = _group))
	{
	  pg.init_grp ();
	  pg.pwdgrp_buf = (void *) &grp;
	}
      else
	{
	  pg.init_pwd ();
	  pg.pwdgrp_buf = (void *) &pwd;
	}
      pg.max_lines = 1;
    }
  else
    clear_cache ();
  cldap.close ();
  rl.close ();
  cnt = max = resume = 0;
  enums = 0;
  enum_tdoms = NULL;
  state = rewound;
}

void *
pg_ent::enumerate_file ()
{
  void *entry;

  if (!cnt)
    {
      pwdgrp &prf = group ? cygheap->pg.grp_cache.file
			  : cygheap->pg.pwd_cache.file;
      if (prf.check_file ())
	{
	  if (!buf)
	    buf = (char *) malloc (NT_MAX_PATH);
	  if (buf
	      && !rl.init (prf.file_attr (), buf, NT_MAX_PATH))
	    {
	      free (buf);
	      buf = NULL;
	    }
	}
    }
  ++cnt;
  if ((entry = pg.add_account_post_fetch (rl.gets (), false)))
    return entry;
  rl.close ();
  free (buf);
  buf = NULL;
  cnt = max = resume = 0;
  return NULL;
}

void *
pg_ent::enumerate_builtin ()
{
  static cygpsid *pwd_builtins[] = {
    &well_known_system_sid,
    &well_known_local_service_sid,
    &well_known_network_service_sid,
    &well_known_admins_sid,
    &trusted_installer_sid,
    NULL
  };
  static cygpsid *grp_builtins[] = {
    &well_known_system_sid,
    &trusted_installer_sid,
    NULL
  };

  cygpsid **builtins = group ? grp_builtins : pwd_builtins;
  if (!builtins[cnt])
    {
      cnt = max = resume = 0;
      return NULL;
    }
  cygsid sid (*builtins[cnt++]);
  fetch_user_arg_t arg;
  arg.type = SID_arg;
  arg.sid = &sid;
  char *line = pg.fetch_account_from_windows (arg);
  return pg.add_account_post_fetch (line, false);
} 

void *
pg_ent::enumerate_sam ()
{
  while (true)
    {
      if (!cnt)
	{
	  DWORD total;
	  NET_API_STATUS ret;

	  if (buf)
	    {
	      NetApiBufferFree (buf);
	      buf = NULL;
	    }
	  if (resume == ULONG_MAX)
	    ret = ERROR_NO_MORE_ITEMS;
	  else if (group)
	    ret = NetGroupEnum (NULL, 2, (PBYTE *) &buf, MAX_PREFERRED_LENGTH,
				&max, &total, &resume);
	  else
	    ret = NetUserEnum (NULL, 20, FILTER_NORMAL_ACCOUNT, (PBYTE *) &buf,
			       MAX_PREFERRED_LENGTH, &max, &total,
			       (PDWORD) &resume);
	  if (ret == NERR_Success)
	    resume = ULONG_MAX;
	  else if (ret != ERROR_MORE_DATA)
	    {
	      cnt = max = resume = 0;
	      return NULL;
	    }
	}
      while (cnt < max)
	{
	  cygsid sid (cygheap->dom.account_sid ());
	  sid_sub_auth (sid, sid_sub_auth_count (sid)) = 
	    group ? ((PGROUP_INFO_2) buf)[cnt].grpi2_group_id
		  : ((PUSER_INFO_20) buf)[cnt].usri20_user_id;
	  ++cnt;
	  ++sid_sub_auth_count (sid);
	  fetch_user_arg_t arg;
	  arg.type = SID_arg;
	  arg.sid = &sid;
	  char *line = pg.fetch_account_from_windows (arg);
	  if (line)
	    return pg.add_account_post_fetch (line, false);
	}
      cnt = 0;
    }
}

void *
pg_ent::enumerate_ad ()
{
  while (true)
    {
      if (!cnt)
	{
	  PDS_DOMAIN_TRUSTSW td;

	  if (!resume)
	    {
	      ++resume;
	      if (!nss_db_enum_primary ()
		  || cldap.enumerate_ad_accounts (NULL, group) != NO_ERROR)
		continue;
	      RtlInitUnicodeString (&dom, cygheap->dom.primary_flat_name ());
	    }
	  else if ((td = cygheap->dom.trusted_domain (resume - 1)))
	    {
	      ++resume;
	      /* Ignore primary domain in list of trusted domains only if all
		 trusted domains are enumerated anyway.  This handles an
		 annoying backward compatibility problem in mkpasswd/mkgroup.
		 Without this test, `mkpasswd -d PRIMARY_DOMAIN' wouldn't
		 work as expected. */
	      if (((enums & ENUM_TDOMS_ALL) && td->Flags & DS_DOMAIN_PRIMARY)
		  || !td->DomainSid
		  || (!nss_db_enum_tdom (td->NetbiosDomainName)
		      && !nss_db_enum_tdom (td->DnsDomainName))
		      || cldap.enumerate_ad_accounts (td->DnsDomainName, group)
			 != NO_ERROR)
		continue;
	      RtlInitUnicodeString (&dom, td->NetbiosDomainName);
	    }
	  else
	    {
	      cldap.close ();
	      return NULL;
	    }
	}
      ++cnt;
      cygsid sid;
      int ret = cldap.next_account (sid);
      if (ret == NO_ERROR)
	{
	  fetch_acc_t full;
	  fetch_user_arg_t arg;
	  UNICODE_STRING name;

	  arg.type = FULL_acc_arg;
	  arg.full_acc = &full;
	  full.sid = sid;
	  RtlInitUnicodeString (&name,
				cldap.get_string_attribute (L"sAMAccountName"));
	  full.name = &name;
	  full.dom = &dom;
	  if (sid_sub_auth (sid, 0) == SECURITY_BUILTIN_DOMAIN_RID)
	    full.acc_type = SidTypeAlias;
	  else
	    full.acc_type = group ? SidTypeGroup : SidTypeUser;
	  char *line = pg.fetch_account_from_windows (arg, &cldap);
	  if (line)
	    return pg.add_account_post_fetch (line, false);
	  ret = EIO;
	}
      if (ret != ENMFILE)
	{
	  cldap.close ();
	  set_errno (ret);
	  return NULL;
	}
      cnt = 0;
    }
}

void *
pw_ent::enumerate_caches ()
{
  switch (max)
    {
    case 0:
      if (cygheap->pg.nss_cygserver_caching ())
	{
	  pwdgrp &prc = cygheap->pg.pwd_cache.cygserver;
	  if (cnt < prc.cached_users ())
	    return &prc.passwd ()[cnt++].p;
	}
      cnt = 0;
      max = 1;
      /*FALLTHRU*/
    case 1:
      if (from_files)
	{
	  pwdgrp &prf = cygheap->pg.pwd_cache.file;
	  prf.check_file ();
	  if (cnt < prf.cached_users ())
	    return &prf.passwd ()[cnt++].p;
	}
      cnt = 0;
      max = 2;
      /*FALLTHRU*/
    default:
      if (from_db)
	{
	  pwdgrp &prw = cygheap->pg.pwd_cache.win;
	  if (cnt < prw.cached_users ())
	    return &prw.passwd ()[cnt++].p;
	}
      break;
    }
  cnt = max = 0;
  return NULL;
}

void *
pw_ent::enumerate_local ()
{
  return NULL;
}

struct passwd *
pw_ent::getpwent (void)
{
  if (state == rewound)
    setent (false);
  else
    clear_cache ();
  return (struct passwd *) getent ();
}

extern "C" void
setpwent ()
{
  pwent.setpwent ();
}

extern "C" struct passwd *
getpwent (void)
{
  return pwent.getpwent ();
}

extern "C" void
endpwent (void)
{
  pwent.endpwent ();
}

/* *_filtered functions are called from mkpasswd */
void *
setpwent_filtered (int enums, PCWSTR enum_tdoms)
{
  pw_ent *pw = new pw_ent;
  if (pw)
    pw->setpwent (enums, enum_tdoms);
  return (void *) pw;
}

void *
getpwent_filtered (void *pw)
{
  return (void *) ((pw_ent *) pw)->getpwent ();
}

void
endpwent_filtered (void *pw)
{
  ((pw_ent *) pw)->endpwent ();
}

#ifndef __x86_64__
extern "C" struct passwd *
getpwduid (__uid16_t)
{
  return NULL;
}
#endif

extern "C" int
setpassent (int)
{
  return 0;
}

static void
_getpass_close_fd (void *arg)
{
  if (arg)
    fclose ((FILE *) arg);
}

extern "C" char *
getpass (const char * prompt)
{
  char *pass = _my_tls.locals.pass;
  struct termios ti, newti;
  bool tc_set = false;

  /* Try to use controlling tty in the first place.  Use stdin and stderr
     only as fallback. */
  FILE *in = stdin, *err = stderr;
  FILE *tty = fopen ("/dev/tty", "w+b");
  pthread_cleanup_push  (_getpass_close_fd, tty);
  if (tty)
    {
      /* Set close-on-exec for obvious reasons. */
      fcntl (fileno (tty), F_SETFD, fcntl (fileno (tty), F_GETFD) | FD_CLOEXEC);
      in = err = tty;
    }

  /* Make sure to notice if stdin is closed. */
  if (fileno (in) >= 0)
    {
      flockfile (in);
      /* Change tty attributes if possible. */
      if (!tcgetattr (fileno (in), &ti))
	{
	  newti = ti;
	  newti.c_lflag &= ~(ECHO | ISIG); /* No echo, no signal handling. */
	  if (!tcsetattr (fileno (in), TCSANOW, &newti))
	    tc_set = true;
	}
      fputs (prompt, err);
      fflush (err);
      fgets (pass, _PASSWORD_LEN, in);
      fprintf (err, "\n");
      if (tc_set)
	tcsetattr (fileno (in), TCSANOW, &ti);
      funlockfile (in);
      char *crlf = strpbrk (pass, "\r\n");
      if (crlf)
	*crlf = '\0';
    }
  pthread_cleanup_pop (1);
  return pass;
}