Changes in v9fs broke pipesrv. Fix it.
[npfs.git] / libnpfs / simpleusers.c
blob51d18f19d7b7dcf9fdce0df5401a93816853a3c9
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include "npfs.h"
4 #include "npfsimpl.h"
6 static struct Npusercache {
7 pthread_mutex_t lock;
8 Npuser* users;
9 } usercache = { PTHREAD_MUTEX_INITIALIZER, 0 };
11 static struct Npgroupcache {
12 pthread_mutex_t lock;
13 Npgroup* groups;
14 } groupcache = { PTHREAD_MUTEX_INITIALIZER, 0 };
16 static Npuser *
17 np_simpl_uname2user(Npuserpool *up, char *uname)
19 Npuser *u;
21 pthread_mutex_lock(&usercache.lock);
22 for(u = usercache.users; u; u = u->next) {
23 if(strcmp(u->uname, uname) == 0)
24 break;
26 if(!u) {
27 u = np_malloc(sizeof(*u) + strlen(uname) + 1);
28 pthread_mutex_init(&u->lock, NULL);
29 u->refcount = 1;
30 u->upool = up;
31 u->uid = -1;
32 u->uname = (char *)u + sizeof(*u);
33 strcpy(u->uname, uname);
34 u->dfltgroup = NULL;
35 u->ngroups = 0;
36 u->groups = NULL;
37 u->next = NULL;
38 u->dfltgroup = (*up->gname2group)(up, uname);
40 u->next = usercache.users;
41 usercache.users = u;
43 np_user_incref(u);
44 pthread_mutex_unlock(&usercache.lock);
45 return u;
48 static Npuser *
49 np_simpl_uid2user(Npuserpool *up, u32 uid)
51 return NULL;
54 static Npgroup *
55 np_simpl_gname2group(Npuserpool *up, char *gname)
57 Npgroup *g;
59 pthread_mutex_lock(&groupcache.lock);
60 for(g = groupcache.groups; g; g = g->next) {
61 if(strcmp(g->gname, gname) == 0)
62 break;
65 if(!g) {
66 g = np_malloc(sizeof(*g) + strlen(gname) + 1);
67 pthread_mutex_init(&g->lock, NULL);
68 g->refcount = 1;
69 g->upool = up;
70 g->gid = -1;
71 g->gname = (char *)g + sizeof(*g);
72 strcpy(g->gname, gname);
74 g->next = groupcache.groups;
75 groupcache.groups = g;
77 np_group_incref(g);
78 pthread_mutex_unlock(&groupcache.lock);
79 return g;
82 static Npgroup *
83 np_simpl_gid2group(Npuserpool *up, u32 gid)
85 return NULL;
88 static int
89 np_simpl_ismember(Npuserpool *up, Npuser *u, Npgroup *g)
91 return 0; // XXX something fancier?
94 static void
95 np_simpl_udestroy(Npuserpool *up, Npuser *u)
99 static void
100 np_simpl_gdestroy(Npuserpool *up, Npgroup *g)
104 static Npuserpool upool = {
105 NULL,
106 np_simpl_uname2user,
107 np_simpl_uid2user,
108 np_simpl_gname2group,
109 np_simpl_gid2group,
110 np_simpl_ismember,
111 np_simpl_udestroy,
112 np_simpl_gdestroy,
115 Npuserpool *np_default_users = &upool;