summaryrefslogtreecommitdiff
path: root/sys/src/ape/lib/ap/plan9/_getpw.c
blob: c75f1dbfe28919428a0674899eb4e9f710b54673 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "lib.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "sys9.h"
#include "dir.h"

/*
 * Search /adm/users for line with second field ==  *pname (if
 * not NULL), else with first field == *pnum.  Return non-zero
 * if found, and fill in *pnum, *pname, and *plist to fields
 * 1, 2, and 4
 */

enum {NAMEMAX = 20, MEMOMAX = 40 };

static char *admusers = "/adm/users";

/* we hold a fixed-length memo list of past lookups, and use a move-to-front
    strategy to organize the list
*/
typedef struct Memo {
	char		name[NAMEMAX];
	int		num;
	char		*glist;
} Memo;

static Memo *memo[MEMOMAX];
static int nmemo = 0;

int
_getpw(int *pnum, char **pname, char **plist)
{
	Dir *d;
	int f, n, i, j, matchnum, m, matched;
	char *eline, *f1, *f2, *f3, *f4;
	Memo *mem;
	static char *au = NULL;
	vlong length;

	if(!pname)
		return 0;
	if(au == NULL){
		d = _dirstat(admusers);
		if(d == nil)
			return 0;
		length = d->length;
		free(d);
		if((au = (char *)malloc(length+2)) == NULL)
			return 0;
		f = open(admusers, O_RDONLY);
		if(f < 0)
			return 0;
		n = read(f, au, length);
		if(n < 0)
			return 0;
		au[n] = 0;
	}
	mem = nil;
	matchnum = (*pname == NULL);
	matched = 0;
	/* try using memo */
	for(i = 0; i<nmemo; i++) {
		mem = memo[i];
		if(matchnum)
			matched = (mem->num == *pnum);
		else
			matched = (strcmp(mem->name, *pname) == 0);
		if(matched)
			break;
	}
	if(!matched)
		for(f1 = au, eline = au; !matched && *eline; f1 = eline+1){
			eline = strchr(f1, '\n');
			if(!eline)
				eline = strchr(f1, 0);
			if(*f1 == '#' || *f1 == '\n')
				continue;
			n = eline-f1;
			f2 = memchr(f1, ':', n);
			if(!f2)
				continue;
			f2++;
			f3 = memchr(f2, ':', n-(f2-f1));
			if(!f3)
				continue;
			f3++;
			f4 = memchr(f3, ':', n-(f3-f1));
			if(!f4)
				continue;
			f4++;
			if(matchnum)
				matched = (atoi(f1) == *pnum);
			else{
				int length;

				length = f3-f2-1;
				matched = length==strlen(*pname) && memcmp(*pname, f2, length)==0;
			}
			if(matched){
				/* allocate and fill in a Memo structure */
				mem = (Memo*)malloc(sizeof(struct Memo));
				if(!mem)
					return 0;
				m = (f3-f2)-1;
				if(m > NAMEMAX-1)
					m = NAMEMAX-1;
				memcpy(mem->name, f2, m);
				mem->name[m] = 0;
				mem->num = atoi(f1);
				m = n-(f4-f1);
				if(m > 0){
					mem->glist = (char*)malloc(m+1);
					if(mem->glist) {
						memcpy(mem->glist, f4, m);
						mem->glist[m] = 0;
					}
				} else
					mem->glist = 0;
				/* prepare for following move-to-front */
				if(nmemo == MEMOMAX) {
					free(memo[nmemo-1]);
					i = nmemo-1;
				} else {
					i = nmemo++;
				}
			}
		}
	if(matched) {
		if(matchnum)
			*pname = mem->name;
		else
			*pnum = mem->num;
		if(plist)
			*plist = mem->glist;
		if(i > 0) {
			/* make room at front */
			for(j = i; j > 0; j--)
				memo[j] = memo[j-1];
		}
		memo[0] = mem;
		return 1;
	}
	return 0;
}

char **
_grpmems(char *list)
{
	char **v;
	char *p;
	static char *holdvec[200];
	static char holdlist[1024];

	v = holdvec;
	if(list != 0){
		memset(holdlist, 0, sizeof(holdlist));
		strncpy(holdlist, list, sizeof(holdlist)-1);
		p = holdlist;
		while(v< &holdvec[sizeof(holdvec)]-1 && *p){
			*v++ = p;
			p = strchr(p, ',');
			if(p == 0)
				break;
			*p++ = 0;
		}
	}
	*v = 0;
	return holdvec;
}