summaryrefslogtreecommitdiff
path: root/usr/src/lib/passwdutil/switch_utils.c
blob: 77680a36ef6133a25d7af36773ea251cb6f208c2 (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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/types.h>
#include <nsswitch.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#include <stdlib.h>
#include <unistd.h>

#include "ns_sldap.h"
#include <nss_dbdefs.h>
#include <nsswitch.h>
#include <pwd.h>
#include <shadow.h>
#include <rpcsvc/nis.h>

#include "passwdutil.h"

static struct passwd *nisplus_getpw_from_master(const char *, char *);
static struct spwd *nisplus_getsp_from_master(const char *, char *);

/*
 * name_to_int(rep)
 *
 * Translate the repository to a bitmask.
 * if we don't recognise the repository name, we return REP_ERANGE
 */
int
name_to_int(char *rep_name)
{
	int result = REP_ERANGE;

	if (strcmp(rep_name, "files") == 0)
		result = REP_FILES;
	else if (strcmp(rep_name, "nis") == 0)
		result = REP_NIS;
	else if (strcmp(rep_name, "nisplus") == 0)
		result = REP_NISPLUS;
	else if (strcmp(rep_name, "ldap") == 0)
		result = REP_LDAP;
	else if (strcmp(rep_name, "compat") == 0) {
		struct __nsw_switchconfig *cfg;
		enum   __nsw_parse_err pserr;

		cfg = __nsw_getconfig("passwd_compat", &pserr);
		if (cfg == NULL) {
			result = REP_FILES | REP_NIS;
		} else {
			if (strcmp(cfg->lookups->service_name, "nisplus") == 0)
				result = REP_FILES | REP_NISPLUS;
			else if (strcmp(cfg->lookups->service_name, "ldap") ==
			    0)
				result = REP_FILES | REP_LDAP;
			else
				result = REP_ERANGE;
			__nsw_freeconfig(cfg);
		}
	}

	return (result);
}

/*
 * Figure out which repository we use in compat mode.
 */
int
get_compat_mode(void)
{
	struct __nsw_switchconfig *cfg;
	enum   __nsw_parse_err pserr;
	int result = REP_COMPAT_NIS;

	if ((cfg = __nsw_getconfig("passwd_compat", &pserr)) != NULL) {
		if (strcmp(cfg->lookups->service_name, "nisplus") == 0)
			result = REP_COMPAT_NISPLUS;
		else if (strcmp(cfg->lookups->service_name, "ldap") == 0)
			result = REP_COMPAT_LDAP;
	}
	__nsw_freeconfig(cfg);

	return (result);
}

/*
 * get_ns(rep, accesstype)
 *
 * returns a bitmask of repositories to use based on either
 *   1. the repository that is given as argument
 *   2. the nsswitch.conf file
 *   3. the type of access requested
 *
 * "accesstype" indicates whether we are reading from or writing to the
 * repository. We need to know this since "compat" will translate into
 * REP_NSS (the nss-switch) for READ access (needed to decode
 * the black-magic '+' entries) but it translates into a bitmask
 * on WRITE access.
 *
 * If we detect read-access in compat mode, we augment the result
 * with one of REP_COMPAT_{NIS,NISPLUS,LDAP}. We need this in order to
 * implement ATTR_REP_NAME in nss_getpwnam.
 *
 * A return value of REP_NOREP indicates an error.
 */
int
get_ns(pwu_repository_t *rep, int accesstype)
{
	struct __nsw_switchconfig *conf = NULL;
	enum __nsw_parse_err pserr;
	struct __nsw_lookup *lkp;
	struct __nsw_lookup *lkp2;
	int result = REP_NOREP;

	if (rep != PWU_DEFAULT_REP) {
		result = name_to_int(rep->type);
		return (result);
	}

	conf = __nsw_getconfig("passwd", &pserr);
	if (conf == NULL) {
		/*
		 * No config found. The user didn't supply a repository,
		 * so we try to change the password in the default
		 * repositories (files and nis) even though we cannot
		 * find the name service switch entry. (Backward compat)
		 */
		syslog(LOG_ERR, "passwdutil.so: nameservice switch entry for "
				"passwd not found.");
		result = REP_FILES | REP_NIS;
		return (result);
	}

	lkp = conf->lookups;

	/*
	 * Supported nsswitch.conf can have a maximum of 2 repositories.
	 * If we encounter an unsupported nsswitch.conf, we return REP_NSS
	 * to fall back to the nsswitch backend.
	 */
	if (conf->num_lookups == 1) {
		/* files or compat */

		if (strcmp(lkp->service_name, "files") == 0) {
			result = name_to_int(lkp->service_name);
		} else if (strcmp(lkp->service_name, "compat") == 0) {
			if (accesstype == PWU_READ)
				result = REP_NSS | get_compat_mode();
			else
				result = name_to_int(lkp->service_name);
		} else
			result = REP_NSS;

	} else if (conf->num_lookups == 2) {
		lkp2 = lkp->next;
		if (strcmp(lkp->service_name, "files") == 0) {
			result = REP_FILES;
			if (strcmp(lkp2->service_name, "ldap") == 0)
				result |= REP_LDAP;
			else if (strcmp(lkp2->service_name, "nis") == 0)
				result |= REP_NIS;
			else if (strcmp(lkp2->service_name, "nisplus") == 0)
				result |= REP_NISPLUS;
			else
				result = REP_NSS;
		} else {
			result = REP_NSS;
		}
	} else {
		result = REP_NSS;
	}

	__nsw_freeconfig(conf);
	return (result);
}

static void
nss_ldap_passwd(p)
	nss_db_params_t	*p;
{
	p->name = NSS_DBNAM_PASSWD;
	p->flags |= NSS_USE_DEFAULT_CONFIG;
	p->default_config = "ldap";
}

static void
nss_ldap_shadow(p)
	nss_db_params_t	*p;
{
	p->name = NSS_DBNAM_SHADOW;
	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
	p->flags |= NSS_USE_DEFAULT_CONFIG;
	p->default_config = "ldap";
}


#ifdef PAM_NIS
static void
nss_nis_passwd(p)
	nss_db_params_t	*p;
{
	p->name = NSS_DBNAM_PASSWD;
	p->flags |= NSS_USE_DEFAULT_CONFIG;
	p->default_config = "nis";
}

static void
nss_nis_shadow(p)
	nss_db_params_t	*p;
{
	p->name = NSS_DBNAM_SHADOW;
	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
	p->flags |= NSS_USE_DEFAULT_CONFIG;
	p->default_config = "nis";
}
#endif /* PAM_NIS */


static void
nss_nisplus_passwd(p)
	nss_db_params_t	*p;
{
	p->name = NSS_DBNAM_PASSWD;
	p->flags |= NSS_USE_DEFAULT_CONFIG;
	p->default_config = "nisplus";
}

static void
nss_nisplus_shadow(p)
	nss_db_params_t	*p;
{
	p->name = NSS_DBNAM_SHADOW;
	p->config_name    = NSS_DBNAM_PASSWD;	/* Use config for "passwd" */
	p->flags |= NSS_USE_DEFAULT_CONFIG;
	p->default_config = "nisplus";
}


static char *
gettok(nextpp)
	char	**nextpp;
{
	char	*p = *nextpp;
	char	*q = p;
	char	c;

	if (p == 0) {
		return (0);
	}
	while ((c = *q) != '\0' && c != ':') {
		q++;
	}
	if (c == '\0') {
		*nextpp = 0;
	} else {
		*q++ = '\0';
		*nextpp = q;
	}
	return (p);
}

/*
 * Return values: 0 = success, 1 = parse error, 2 = erange ...
 * The structure pointer passed in is a structure in the caller's space
 * wherein the field pointers would be set to areas in the buffer if
 * need be. instring and buffer should be separate areas.
 */
static int
str2passwd(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
{
	struct passwd	*passwd	= (struct passwd *)ent;
	char		*p, *next;
	int		black_magic;	/* "+" or "-" entry */

	if (lenstr + 1 > buflen) {
		return (NSS_STR_PARSE_ERANGE);
	}
	/*
	 * We copy the input string into the output buffer and
	 * operate on it in place.
	 */
	(void) memcpy(buffer, instr, lenstr);
	buffer[lenstr] = '\0';

	next = buffer;

	passwd->pw_name = p = gettok(&next);		/* username */
	if (*p == '\0') {
		/* Empty username;  not allowed */
		return (NSS_STR_PARSE_PARSE);
	}
	black_magic = (*p == '+' || *p == '-');
	if (black_magic) {
		passwd->pw_uid	= UID_NOBODY;
		passwd->pw_gid	= GID_NOBODY;
		/*
		 * pwconv tests pw_passwd and pw_age == NULL
		 */
		passwd->pw_passwd = "";
		passwd->pw_age	= "";
		/*
		 * the rest of the passwd entry is "optional"
		 */
		passwd->pw_comment = "";
		passwd->pw_gecos = "";
		passwd->pw_dir	= "";
		passwd->pw_shell = "";
	}

	passwd->pw_passwd = p = gettok(&next);		/* password */
	if (p == 0) {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}
	for (; *p != '\0'; p++) {			/* age */
		if (*p == ',') {
			*p++ = '\0';
			break;
		}
	}
	passwd->pw_age = p;

	p = next;					/* uid */
	if (p == 0 || *p == '\0') {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}
	if (!black_magic) {
		passwd->pw_uid = strtol(p, &next, 10);
		if (next == p) {
			/* uid field should be nonempty */
			return (NSS_STR_PARSE_PARSE);
		}
		/*
		 * The old code (in 2.0 thru 2.5) would check
		 * for the uid being negative, or being greater
		 * than 60001 (the rfs limit).  If it met either of
		 * these conditions, the uid was translated to 60001.
		 *
		 * Now we just check for ephemeral uids; anything else
		 * is administrative policy
		 */
		if (passwd->pw_uid > MAXUID)
			passwd->pw_uid = UID_NOBODY;
	}
	if (*next++ != ':') {
		if (black_magic)
			p = gettok(&next);
		else
			return (NSS_STR_PARSE_PARSE);
	}
	p = next;					/* gid */
	if (p == 0 || *p == '\0') {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}
	if (!black_magic) {
		passwd->pw_gid = strtol(p, &next, 10);
		if (next == p) {
			/* gid field should be nonempty */
			return (NSS_STR_PARSE_PARSE);
		}
		/*
		 * gid should be non-negative; anything else
		 * is administrative policy.
		 */
		if (passwd->pw_gid > MAXUID)
			passwd->pw_gid = GID_NOBODY;
	}
	if (*next++ != ':') {
		if (black_magic)
			p = gettok(&next);
		else
			return (NSS_STR_PARSE_PARSE);
	}

	passwd->pw_gecos = passwd->pw_comment = p = gettok(&next);
	if (p == 0) {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}

	passwd->pw_dir = p = gettok(&next);
	if (p == 0) {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}

	passwd->pw_shell = p = gettok(&next);
	if (p == 0) {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}

	/* Better not be any more fields... */
	if (next == 0) {
		/* Successfully parsed and stored */
		return (NSS_STR_PARSE_SUCCESS);
	}
	return (NSS_STR_PARSE_PARSE);
}

typedef const char *constp;

/*
 * Return value 1 means success and more input, 0 means error or no more
 */
static int
getfield(nextp, limit, uns, valp)
	constp		*nextp;
	constp		limit;
	int		uns;
	void		*valp;
{
	constp		p = *nextp;
	char		*endfield;
	char		numbuf[12];  /* Holds -2^31 and trailing ':' */
	int		len;
	long		x;
	unsigned long	ux;

	if (p == 0 || p >= limit) {
		return (0);
	}
	if (*p == ':') {
		p++;
		*nextp = p;
		return (p < limit);
	}
	if ((len = limit - p) > sizeof (numbuf) - 1) {
		len = sizeof (numbuf) - 1;
	}
	/*
	 * We want to use strtol() and we have a readonly non-zero-terminated
	 *   string, so first we copy and terminate the interesting bit.
	 *   Ugh.  (It's convenient to terminate with a colon rather than \0).
	 */
	if ((endfield = memccpy(numbuf, p, ':', len)) == 0) {
		if (len != limit - p) {
			/* Error -- field is too big to be a legit number */
			return (0);
		}
		numbuf[len] = ':';
		p = limit;
	} else {
		p += (endfield - numbuf);
	}
	if (uns) {
		ux = strtoul(numbuf, &endfield, 10);
		if (*endfield != ':') {
			/* Error -- expected <integer><colon> */
			return (0);
		}
		*((unsigned int *)valp) = (unsigned int)ux;
	} else {
		x = strtol(numbuf, &endfield, 10);
		if (*endfield != ':') {
			/* Error -- expected <integer><colon> */
			return (0);
		}
		*((int *)valp) = (int)x;
	}
	*nextp = p;
	return (p < limit);
}

/*
 *  str2spwd() -- convert a string to a shadow passwd entry.  The parser is
 *	more liberal than the passwd or group parsers;  since it's legitimate
 *	for almost all the fields here to be blank, the parser lets one omit
 *	any number of blank fields at the end of the entry.  The acceptable
 *	forms for '+' and '-' entries are the same as those for normal entries.
 *  === Is this likely to do more harm than good?
 *
 * Return values: 0 = success, 1 = parse error, 2 = erange ...
 * The structure pointer passed in is a structure in the caller's space
 * wherein the field pointers would be set to areas in the buffer if
 * need be. instring and buffer should be separate areas.
 */
int
str2spwd(instr, lenstr, ent, buffer, buflen)
	const char	*instr;
	int		lenstr;
	void	*ent; /* really (struct spwd *) */
	char	*buffer;
	int	buflen;
{
	struct spwd	*shadow	= (struct spwd *)ent;
	const char	*p = instr, *limit;
	char		*bufp;
	int	lencopy, black_magic;

	limit = p + lenstr;
	if ((p = memchr(instr, ':', lenstr)) == 0 ||
		++p >= limit ||
		(p = memchr(p, ':', limit - p)) == 0) {
		lencopy = lenstr;
		p = 0;
	} else {
		lencopy = p - instr;
		p++;
	}
	if (lencopy + 1 > buflen) {
		return (NSS_STR_PARSE_ERANGE);
	}
	(void) memcpy(buffer, instr, lencopy);
	buffer[lencopy] = 0;

	black_magic = (*instr == '+' || *instr == '-');
	shadow->sp_namp = bufp = buffer;
	shadow->sp_pwdp	= 0;
	shadow->sp_lstchg = -1;
	shadow->sp_min	= -1;
	shadow->sp_max	= -1;
	shadow->sp_warn	= -1;
	shadow->sp_inact = -1;
	shadow->sp_expire = -1;
	shadow->sp_flag	= 0;

	if ((bufp = strchr(bufp, ':')) == 0) {
		if (black_magic)
			return (NSS_STR_PARSE_SUCCESS);
		else
			return (NSS_STR_PARSE_PARSE);
	}
	*bufp++ = '\0';

	shadow->sp_pwdp = bufp;
	if (instr == 0) {
		if ((bufp = strchr(bufp, ':')) == 0) {
			if (black_magic)
				return (NSS_STR_PARSE_SUCCESS);
			else
				return (NSS_STR_PARSE_PARSE);
		}
		*bufp++ = '\0';
		p = bufp;
	} /* else p was set when we copied name and passwd into the buffer */

	if (!getfield(&p, limit, 0, &shadow->sp_lstchg))
			return (NSS_STR_PARSE_SUCCESS);
	if (!getfield(&p, limit, 0, &shadow->sp_min))
			return (NSS_STR_PARSE_SUCCESS);
	if (!getfield(&p, limit, 0, &shadow->sp_max))
			return (NSS_STR_PARSE_SUCCESS);
	if (!getfield(&p, limit, 0, &shadow->sp_warn))
			return (NSS_STR_PARSE_SUCCESS);
	if (!getfield(&p, limit, 0, &shadow->sp_inact))
			return (NSS_STR_PARSE_SUCCESS);
	if (!getfield(&p, limit, 0, &shadow->sp_expire))
			return (NSS_STR_PARSE_SUCCESS);
	if (!getfield(&p, limit, 1, &shadow->sp_flag))
			return (NSS_STR_PARSE_SUCCESS);
	if (p != limit) {
		/* Syntax error -- garbage at end of line */
		return (NSS_STR_PARSE_PARSE);
	}
	return (NSS_STR_PARSE_SUCCESS);
}

static nss_XbyY_buf_t *buffer;
static DEFINE_NSS_DB_ROOT(db_root);

#define	GETBUF()	\
	NSS_XbyY_ALLOC(&buffer, sizeof (struct passwd), NSS_BUFLEN_PASSWD)

#pragma fini(endutilpwent)

static void
endutilpwent(void)
{
	NSS_XbyY_FREE(&buffer);
	nss_delete(&db_root);
}

struct passwd *
getpwnam_from(const char *name, pwu_repository_t *rep, int reptype)
{
	nss_XbyY_buf_t  *b = GETBUF();
	nss_XbyY_args_t arg;

	if (b == 0)
		return (0);

	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
	arg.key.name = name;

	switch (reptype) {
	case REP_LDAP:
		(void) nss_search(&db_root, nss_ldap_passwd,
		    NSS_DBOP_PASSWD_BYNAME, &arg);
		break;
	case REP_NISPLUS:
		if (rep && rep->scope)
			return (nisplus_getpw_from_master(name, rep->scope));

		(void) nss_search(&db_root, nss_nisplus_passwd,
		    NSS_DBOP_PASSWD_BYNAME, &arg);
		break;
#ifdef PAM_NIS
	case REP_NIS:
		(void) nss_search(&db_root, nss_nis_passwd,
		    NSS_DBOP_PASSWD_BYNAME, &arg);
		break;
#endif
	default:
		return (NULL);
	}

	return (struct passwd *)NSS_XbyY_FINI(&arg);
}

/*ARGSUSED*/
struct passwd *
getpwuid_from(uid_t uid, pwu_repository_t *rep, int reptype)
{
	nss_XbyY_buf_t  *b = GETBUF();
	nss_XbyY_args_t arg;

	if (b == 0)
		return (0);

	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2passwd);
	arg.key.uid = uid;

	switch (reptype) {
	case REP_LDAP:
		(void) nss_search(&db_root, nss_ldap_passwd,
		    NSS_DBOP_PASSWD_BYUID, &arg);
		break;
	case REP_NISPLUS:
		(void) nss_search(&db_root, nss_nisplus_passwd,
		    NSS_DBOP_PASSWD_BYUID, &arg);
		break;
#ifdef PAM_NIS
	case REP_NIS:
		(void) nss_search(&db_root, nss_nis_passwd,
		    NSS_DBOP_PASSWD_BYUID, &arg);
		break;
#endif
	default:
		return (NULL);
	}

	return (struct passwd *)NSS_XbyY_FINI(&arg);
}

static nss_XbyY_buf_t *spbuf;
static DEFINE_NSS_DB_ROOT(spdb_root);

#define	GETSPBUF()	\
	NSS_XbyY_ALLOC(&spbuf, sizeof (struct spwd), NSS_BUFLEN_SHADOW)

#pragma fini(endutilspent)

static void
endutilspent(void)
{
	NSS_XbyY_FREE(&spbuf);
	nss_delete(&spdb_root);
}

struct spwd *
getspnam_from(const char *name, pwu_repository_t *rep, int reptype)
{
	nss_XbyY_buf_t  *b = GETSPBUF();
	nss_XbyY_args_t arg;

	if (b == 0)
		return (0);

	NSS_XbyY_INIT(&arg, b->result, b->buffer, b->buflen, str2spwd);
	arg.key.name = name;
	switch (reptype) {
	case REP_LDAP:
		(void) nss_search(&spdb_root, nss_ldap_shadow,
		    NSS_DBOP_SHADOW_BYNAME, &arg);
		break;
	case REP_NISPLUS:
		if (rep && rep->scope)
			return (nisplus_getsp_from_master(name, rep->scope));

		(void) nss_search(&spdb_root, nss_nisplus_shadow,
		    NSS_DBOP_SHADOW_BYNAME, &arg);
		break;
#ifdef PAM_NIS
	case REP_NIS:
		(void) nss_search(&spdb_root, nss_nis_shadow,
		    NSS_DBOP_SHADOW_BYNAME, &arg);
		break;
#endif
	default:
		return (NULL);
	}
	return (struct spwd *)NSS_XbyY_FINI(&arg);
}


static nis_result *
nisplus_match(const char *name, char *domain, char *buf, int len)
{
	int n;
	int flags;
	nis_result *res;
	nis_object *object;

	n = snprintf(buf, len, "[name=%s],passwd.org_dir.%s", name, domain);
	if (n >= len) {
		syslog(LOG_ERR, "nisplus_match: name too long");
		return (NULL);
	}
	if (buf[n-1] != '.') {
		if (n == len-1) {
			syslog(LOG_ERR, "nisplus_match: name too long");
			return (NULL);
		}
		buf[n++] = '.';
		buf[n] = '\0';
	}

	flags = USE_DGRAM | FOLLOW_LINKS | FOLLOW_PATH | MASTER_ONLY;

	res = nis_list(buf, flags, NULL, NULL);

	if (res == NULL) {
		syslog(LOG_ERR, "nisplus_match: nis_list returned NULL");
		return (NULL);
	}

	if (NIS_RES_STATUS(res) != NIS_SUCCESS || NIS_RES_NUMOBJ(res) != 1) {
		syslog(LOG_ERR, "nisplus_match: match failed: %s",
		    nis_sperrno(NIS_RES_STATUS(res)));
		nis_freeresult(res);
		return (NULL);
	}

	object = NIS_RES_OBJECT(res);

	if (object->EN_data.en_cols.en_cols_len < 8) {
		syslog(LOG_ERR, "nisplus_match: "
		    "not a valid passwd table entry for user %s", name);
		nis_freeresult(res);
		return (NULL);
	}

	return (res);
}

#define	SAFE_STRDUP(dst, idx) \
	if ((idx) <= 3 && ENTRY_VAL(nret, (idx)) == NULL) { \
		syslog(LOG_ERR, \
		    "passwdutil: missing field from password entry"); \
		goto error; \
	} \
	len = ENTRY_LEN(nret, (idx)); \
	(dst) = malloc(len+1); \
	if ((dst) == NULL) { \
		syslog(LOG_ERR, "passwdutil: out of memory"); \
		goto error; \
	} \
	(dst)[len] = '\0'; \
	(void) strncpy((dst), \
	    ENTRY_VAL(nret, (idx)) ? ENTRY_VAL(nret, (idx)) : "", \
	    len);



static struct passwd *
nisplus_getpw_from_master(const char *name, char *domain)
{
	char lookup[NIS_MAXNAMELEN+1];
	nis_result *res;
	nis_object *nret;
	int len;
	char *p;
	struct passwd *pw;

	if ((pw = calloc(1, sizeof (*pw))) == NULL)
		return (NULL);

	res = nisplus_match(name, domain, lookup, sizeof (lookup));

	if (res == NULL)
		return (NULL);

	nret = NIS_RES_OBJECT(res);

	SAFE_STRDUP(pw->pw_name, 0);

	if ((pw->pw_passwd = strdup("x")) == NULL) {
		syslog(LOG_ERR, "passwdutil: out of memory");
		goto error;
	}

	SAFE_STRDUP(p, 2);
	pw->pw_uid = atoi(p);
	free(p);

	SAFE_STRDUP(p, 3);
	pw->pw_gid = atoi(p);
	free(p);

	pw->pw_age = NULL;
	pw->pw_comment = NULL;

/*CONSTANTCONDITION*/
	SAFE_STRDUP(pw->pw_gecos, 4);

/*CONSTANTCONDITION*/
	SAFE_STRDUP(pw->pw_dir, 5);

/*CONSTANTCONDITION*/
	SAFE_STRDUP(pw->pw_shell, 6);

	nis_freeresult(res);

	return (pw);

error:
	nis_freeresult(res);
	if (pw->pw_name)
		free(pw->pw_name);
	if (pw->pw_passwd)
		free(pw->pw_passwd);
	if (pw->pw_gecos)
		free(pw->pw_gecos);
	if (pw->pw_dir)
		free(pw->pw_dir);
	if (pw->pw_shell)
		free(pw->pw_shell);
	free(pw);

	return (NULL);
}

/*
 * struct spwd * nisplus_getsp_from_master()
 *
 * Get the shadow structure from a NIS+ master.
 * This routine normally runs with EUID==0. This can cause trouble
 * if the NIS+ tables are locked down so that only the owner can
 * access the encrypted password. If we detect that scenario, we switch
 * EUID to the owner of the record and refetch it.
 */
static struct spwd *
nisplus_getsp_from_master(const char *name, char *domain)
{
	char lookup[NIS_MAXNAMELEN+1];
	nis_result *res = NULL;
	nis_object *nret = NULL;
	int len;
	struct spwd *spw;
	char *shadow = NULL;
	const char *p = NULL;
	const char *limit;

	res = nisplus_match(name, domain, lookup, sizeof (lookup));
	if (res == NULL)
		return (NULL);

	nret = NIS_RES_OBJECT(res);

	/*CONSTANTCONDITION*/
	SAFE_STRDUP(shadow, 7);

	/*
	 * If we got "*NP*" as password, try again with EUID set to
	 * the UID of the record-owner.
	 */
	if (strncmp(shadow, "*NP*", 4) == 0) {
		char *p;
		uid_t owner_uid;
		uid_t euid = geteuid();

		SAFE_STRDUP(p, 2);	/* record-owner field */
		owner_uid = atoi(p);
		free(p);

		if (owner_uid != euid) {
			/* re-obtain entry using owners EUID */
			free(shadow);
			nis_freeresult(res);

			(void) seteuid(owner_uid);
			res = nisplus_match(name, domain, lookup,
			    sizeof (lookup));
			(void) seteuid(euid);

			if (res == NULL)
				return (NULL);
			nret = NIS_RES_OBJECT(res);

			/*CONSTANTCONDITION*/
			SAFE_STRDUP(shadow, 7);
		}
	}

	if ((spw = calloc(1, sizeof (*spw))) == NULL) {
		nis_freeresult(res);
		return (NULL);
	}

	SAFE_STRDUP(spw->sp_namp, 0);
	SAFE_STRDUP(spw->sp_pwdp, 1);

	nis_freeresult(res);

	limit = shadow + strlen(shadow) + 1;
	p = shadow;

	spw->sp_lstchg = -1;
	spw->sp_min	= -1;
	spw->sp_max	= -1;
	spw->sp_warn	= -1;
	spw->sp_inact = -1;
	spw->sp_expire = -1;
	spw->sp_flag	= 0;

	if (!getfield(&p, limit, 0, &spw->sp_lstchg))
		goto out;

	if (!getfield(&p, limit, 0, &spw->sp_min))
		goto out;

	if (!getfield(&p, limit, 0, &spw->sp_max))
		goto out;

	if (!getfield(&p, limit, 0, &spw->sp_warn))
		goto out;

	if (!getfield(&p, limit, 0, &spw->sp_inact))
		goto out;

	if (!getfield(&p, limit, 0, &spw->sp_expire))
		goto out;

	if (!getfield(&p, limit, 1, &spw->sp_flag))
		goto out;

	if (p != limit) {
		syslog(LOG_ERR, "passwdutil: garbage at end of record");
		goto error;
	}

out:
	free(shadow);
	return (spw);

error:
	if (spw->sp_namp)
		free(spw->sp_namp);
	if (spw->sp_pwdp)
		free(spw->sp_pwdp);
	free(spw);
	if (shadow)
		free(shadow);
	return (NULL);
}