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