xref: /onnv-gate/usr/src/lib/libc/port/gen/getpw.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include "file64.h"
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <mtlib.h>
360Sstevel@tonic-gate #include <ctype.h>
370Sstevel@tonic-gate #include <thread.h>
380Sstevel@tonic-gate #include <synch.h>
390Sstevel@tonic-gate #include <stdio.h>
400Sstevel@tonic-gate #include "stdiom.h"
410Sstevel@tonic-gate #include "libc.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static FILE *pwf;
440Sstevel@tonic-gate static mutex_t _pwlock = DEFAULTMUTEX;
450Sstevel@tonic-gate const char *PASSWD = "/etc/passwd";
460Sstevel@tonic-gate 
470Sstevel@tonic-gate int
getpw(uid_t uid,char buf[])480Sstevel@tonic-gate getpw(uid_t uid, char buf[])
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	int n, c;
510Sstevel@tonic-gate 	char *bp;
520Sstevel@tonic-gate 	FILE *fp;
530Sstevel@tonic-gate 	rmutex_t *lk;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	if (pwf == NULL) {
561914Scasper 		fp = fopen(PASSWD, "rF");
570Sstevel@tonic-gate 		lmutex_lock(&_pwlock);
580Sstevel@tonic-gate 		if (pwf == NULL) {
590Sstevel@tonic-gate 			if ((pwf = fp) == NULL) {
600Sstevel@tonic-gate 				lmutex_unlock(&_pwlock);
610Sstevel@tonic-gate 				return (1);
620Sstevel@tonic-gate 			}
630Sstevel@tonic-gate 			fp = NULL;
640Sstevel@tonic-gate 		}
650Sstevel@tonic-gate 		lmutex_unlock(&_pwlock);
660Sstevel@tonic-gate 		if (fp != NULL)		/* someone beat us to it */
670Sstevel@tonic-gate 			(void) fclose(fp);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	FLOCKFILE(lk, pwf);
710Sstevel@tonic-gate 	_rewind_unlocked(pwf);
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	for (;;) {
740Sstevel@tonic-gate 		bp = buf;
750Sstevel@tonic-gate 		while ((c = GETC(pwf)) != '\n') {
760Sstevel@tonic-gate 			if (c == EOF) {
770Sstevel@tonic-gate 				FUNLOCKFILE(lk);
780Sstevel@tonic-gate 				return (1);
790Sstevel@tonic-gate 			}
800Sstevel@tonic-gate 			*bp++ = (char)c;
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		*bp = '\0';
830Sstevel@tonic-gate 		bp = buf;
840Sstevel@tonic-gate 		n = 3;
850Sstevel@tonic-gate 		while (--n)
860Sstevel@tonic-gate 			while ((c = *bp++) != ':')
870Sstevel@tonic-gate 				if (c == '\n') {
880Sstevel@tonic-gate 					FUNLOCKFILE(lk);
890Sstevel@tonic-gate 					return (1);
900Sstevel@tonic-gate 				}
910Sstevel@tonic-gate 		while ((c = *bp++) != ':')
920Sstevel@tonic-gate 			if (isdigit(c))
930Sstevel@tonic-gate 				n = n*10+c-'0';
940Sstevel@tonic-gate 			else
950Sstevel@tonic-gate 				continue;
960Sstevel@tonic-gate 		if (n == uid) {
970Sstevel@tonic-gate 			FUNLOCKFILE(lk);
980Sstevel@tonic-gate 			return (0);
990Sstevel@tonic-gate 		}
1000Sstevel@tonic-gate 	}
1010Sstevel@tonic-gate }
102