xref: /netbsd-src/usr.sbin/rpc.pcnfsd/pcnfsd_cache.c (revision 421949a31fb0942d3d87278998c2d7d432d8b3cb)
1 /*	$NetBSD: pcnfsd_cache.c,v 1.5 2018/01/23 21:06:25 sevan Exp $	*/
2 
3 /* RE_SID: @(%)/usr/dosnfs/shades_SCCS/unix/pcnfsd/v2/src/SCCS/s.pcnfsd_cache.c 1.1 91/09/03 12:45:14 SMI */
4 /*
5 **=====================================================================
6 ** Copyright (c) 1986,1987,1988,1989,1990,1991 by Sun Microsystems, Inc.
7 **	@(#)pcnfsd_cache.c	1.1	9/3/91
8 **=====================================================================
9 */
10 /*
11 **=====================================================================
12 **             I N C L U D E   F I L E   S E C T I O N                *
13 **                                                                    *
14 ** If your port requires different include files, add a suitable      *
15 ** #define in the customization section, and make the inclusion or    *
16 ** exclusion of the files conditional on this.                        *
17 **=====================================================================
18 */
19 
20 #include <errno.h>
21 #include <pwd.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include "common.h"
27 #include "pcnfsd.h"
28 #include "extern.h"
29 
30 /*
31 **---------------------------------------------------------------------
32 **                       Misc. variable definitions
33 **---------------------------------------------------------------------
34 */
35 
36 #ifdef USER_CACHE
37 #define CACHE_SIZE 16		/* keep it small, as linear searches are done */
38 struct cache {
39 	int     cuid;
40 	int     cgid;
41 	char    cpw[32];
42 	char    cuname[10];	/* keep this even for machines with alignment
43 				 * problems */
44 }       User_cache[CACHE_SIZE];
45 
46 
47 /*
48 **---------------------------------------------------------------------
49 **                 User cache support procedures
50 **---------------------------------------------------------------------
51 */
52 
53 
54 int
check_cache(char * name,char * pw,int * p_uid,int * p_gid)55 check_cache(char *name, char *pw, int *p_uid, int *p_gid)
56 {
57 	int     i;
58 	int     c1, c2;
59 
60 	for (i = 0; i < CACHE_SIZE; i++) {
61 		if (!strcmp(User_cache[i].cuname, name)) {
62 			c1 = strlen(pw);
63 			c2 = strlen(User_cache[i].cpw);
64 			if ((!c1 && !c2) ||
65 			    !(strcmp(User_cache[i].cpw,
66 				    crypt(pw, User_cache[i].cpw)))) {
67 				*p_uid = User_cache[i].cuid;
68 				*p_gid = User_cache[i].cgid;
69 				return (1);
70 			}
71 			User_cache[i].cuname[0] = '\0';	/* nuke entry */
72 			return (0);
73 		}
74 	}
75 	return (0);
76 }
77 
78 void
add_cache_entry(struct passwd * p)79 add_cache_entry(struct passwd *p)
80 {
81 	int     i;
82 
83 	for (i = CACHE_SIZE - 1; i > 0; i--)
84 		User_cache[i] = User_cache[i - 1];
85 	User_cache[0].cuid = p->pw_uid;
86 	User_cache[0].cgid = p->pw_gid;
87 	(void) strlcpy(User_cache[0].cpw, p->pw_passwd,
88 	    sizeof(User_cache[0].cpw));
89 	(void) strlcpy(User_cache[0].cuname, p->pw_name,
90 	    sizeof(User_cache[0].cuname));
91 }
92 
93 #endif				/* USER_CACHE */
94