xref: /onnv-gate/usr/src/lib/libc/port/gen/getpw.c (revision 1914:8a8c5f225b1b)
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
5*1914Scasper  * Common Development and Distribution License (the "License").
6*1914Scasper  * 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  */
210Sstevel@tonic-gate /*
22*1914Scasper  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
290Sstevel@tonic-gate /*	  All Rights Reserved  	*/
300Sstevel@tonic-gate 
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*	3.0 SID #	1.2	*/
330Sstevel@tonic-gate #pragma weak getpw = _getpw
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "synonyms.h"
360Sstevel@tonic-gate #include "file64.h"
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <mtlib.h>
390Sstevel@tonic-gate #include <ctype.h>
400Sstevel@tonic-gate #include <thread.h>
410Sstevel@tonic-gate #include <synch.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include "stdiom.h"
440Sstevel@tonic-gate #include "libc.h"
450Sstevel@tonic-gate 
460Sstevel@tonic-gate static FILE *pwf;
470Sstevel@tonic-gate static mutex_t _pwlock = DEFAULTMUTEX;
480Sstevel@tonic-gate const char *PASSWD = "/etc/passwd";
490Sstevel@tonic-gate 
500Sstevel@tonic-gate int
510Sstevel@tonic-gate getpw(uid_t uid, char buf[])
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	int n, c;
540Sstevel@tonic-gate 	char *bp;
550Sstevel@tonic-gate 	FILE *fp;
560Sstevel@tonic-gate 	rmutex_t *lk;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate 	if (pwf == NULL) {
59*1914Scasper 		fp = fopen(PASSWD, "rF");
600Sstevel@tonic-gate 		lmutex_lock(&_pwlock);
610Sstevel@tonic-gate 		if (pwf == NULL) {
620Sstevel@tonic-gate 			if ((pwf = fp) == NULL) {
630Sstevel@tonic-gate 				lmutex_unlock(&_pwlock);
640Sstevel@tonic-gate 				return (1);
650Sstevel@tonic-gate 			}
660Sstevel@tonic-gate 			fp = NULL;
670Sstevel@tonic-gate 		}
680Sstevel@tonic-gate 		lmutex_unlock(&_pwlock);
690Sstevel@tonic-gate 		if (fp != NULL)		/* someone beat us to it */
700Sstevel@tonic-gate 			(void) fclose(fp);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	FLOCKFILE(lk, pwf);
740Sstevel@tonic-gate 	_rewind_unlocked(pwf);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	for (;;) {
770Sstevel@tonic-gate 		bp = buf;
780Sstevel@tonic-gate 		while ((c = GETC(pwf)) != '\n') {
790Sstevel@tonic-gate 			if (c == EOF) {
800Sstevel@tonic-gate 				FUNLOCKFILE(lk);
810Sstevel@tonic-gate 				return (1);
820Sstevel@tonic-gate 			}
830Sstevel@tonic-gate 			*bp++ = (char)c;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 		*bp = '\0';
860Sstevel@tonic-gate 		bp = buf;
870Sstevel@tonic-gate 		n = 3;
880Sstevel@tonic-gate 		while (--n)
890Sstevel@tonic-gate 			while ((c = *bp++) != ':')
900Sstevel@tonic-gate 				if (c == '\n') {
910Sstevel@tonic-gate 					FUNLOCKFILE(lk);
920Sstevel@tonic-gate 					return (1);
930Sstevel@tonic-gate 				}
940Sstevel@tonic-gate 		while ((c = *bp++) != ':')
950Sstevel@tonic-gate 			if (isdigit(c))
960Sstevel@tonic-gate 				n = n*10+c-'0';
970Sstevel@tonic-gate 			else
980Sstevel@tonic-gate 				continue;
990Sstevel@tonic-gate 		if (n == uid) {
1000Sstevel@tonic-gate 			FUNLOCKFILE(lk);
1010Sstevel@tonic-gate 			return (0);
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate }
105