summaryrefslogtreecommitdiff
path: root/usr.sbin/rpc.pcnfsd/pcnfsd_cache.c
blob: 9053205cedc22f545a533a0ee2d37211841ebd01 (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
/*	$OpenBSD: pcnfsd_cache.c,v 1.4 2001/08/19 19:16:12 ericj Exp $	*/
/*	$NetBSD: pcnfsd_cache.c,v 1.2 1995/07/25 22:20:37 gwr Exp $	*/

/* RE_SID: @(%)/usr/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_cache.c 1.1 91/09/03 12:45:14 SMI */
/*
 *=====================================================================
 * Copyright (c) 1986,1987,1988,1989,1990,1991 by Sun Microsystems, Inc.
 *	@(#)pcnfsd_cache.c	1.1	9/3/91
 *=====================================================================
 */

#include <stdio.h>
#include <pwd.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#include "pcnfsd.h"

#define CACHE_SIZE 16		/* keep it small, as linear searches are
				 * done */
struct cache 
{
       int   cuid;
       int   cgid;
       char  cpw[_PASSWORD_LEN];
       char  cuname[10];	/* keep this even for machines
				 * with alignment problems */
} User_cache[CACHE_SIZE];

int
check_cache(name, pw, p_uid, p_gid)
	char *name, *pw;
	int *p_uid, *p_gid;
{
	int i, c1, c2;

	for (i = 0; i < CACHE_SIZE; i++) {
		if (!strcmp(User_cache[i].cuname, name)) {
           		c1 = strlen(pw);
	       		c2 = strlen(User_cache[i].cpw);
	        	if ((!c1 && !c2) ||
	  	       	    !(strcmp(User_cache[i].cpw,
		       	           crypt(pw, User_cache[i].cpw)))) {
		        	*p_uid = User_cache[i].cuid;
		        	*p_gid = User_cache[i].cgid;
		        	return (1);
		    	}
		    	User_cache[i].cuname[0] = '\0'; /* nuke entry */
           		return (0);
       		}
	}
	return (0);
}

void
add_cache_entry(p)
	struct passwd *p;
{
	int i;

	for (i = CACHE_SIZE - 1; i > 0; i--)
		User_cache[i] = User_cache[i - 1];
	User_cache[0].cuid = p->pw_uid;
	User_cache[0].cgid = p->pw_gid;
	(void)strncpy(User_cache[0].cpw, p->pw_passwd, sizeof User_cache[0].cpw-1);
	User_cache[0].cpw[sizeof User_cache[0].cpw-1] = '\0';
	(void)strncpy(User_cache[0].cuname, p->pw_name, sizeof User_cache[0].cuname-1);
	User_cache[0].cuname[sizeof User_cache[0].cuname-1] = '\0';
}