afs: afs_osi_Alloc_NoSleep() cleanup
[openafs.git] / src / afs / afs_pag_cred.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include "afs/param.h"
12
13
14 #include "afs/sysincludes.h"    /* Standard vendor system headers */
15 #include "afsincludes.h"        /* Afs-based standard headers */
16 #include "afs/afs_stats.h"
17 #include "afs/unified_afs.h"
18 #include "rx/rx_globals.h"
19 #include "pagcb.h"
20
21
22 struct afspag_cell {
23     struct afspag_cell *next;
24     char *cellname;
25     afs_int32 cellnum;
26 };
27
28 afs_rwlock_t afs_xpagcell;
29 afs_rwlock_t afs_xpagsys;
30 static int lastcell = 0;
31 static struct afspag_cell *cells = 0;
32 static struct afspag_cell *primary_cell = 0;
33
34
35 struct afspag_cell *
36 afspag_GetCell(char *acell)
37 {
38     struct afspag_cell *tcell;
39
40     ObtainWriteLock(&afs_xpagcell, 820);
41
42     for (tcell = cells; tcell; tcell = tcell->next) {
43         if (!strcmp(acell, tcell->cellname))
44             break;
45     }
46
47     if (!tcell) {
48         tcell = (struct afspag_cell *)afs_osi_Alloc(sizeof(struct afspag_cell));
49         if (!tcell)
50             goto out;
51         tcell->cellname = (char *)afs_osi_Alloc(strlen(acell) + 1);
52         if (!tcell->cellname) {
53             afs_osi_Free(tcell, sizeof(struct afspag_cell));
54             tcell = 0;
55             goto out;
56         }
57         strcpy(tcell->cellname, acell);
58         tcell->cellnum = ++lastcell;
59         tcell->next = cells;
60         cells = tcell;
61         if (!primary_cell) primary_cell = tcell;
62     }
63
64 out:
65     ReleaseWriteLock(&afs_xpagcell);
66     return tcell;
67 }
68
69
70 struct afspag_cell *
71 afspag_GetPrimaryCell(void)
72 {
73     struct afspag_cell *tcell;
74
75     ObtainWriteLock(&afs_xpagcell, 821);
76     tcell = primary_cell;
77     ReleaseWriteLock(&afs_xpagcell);
78     return tcell;
79 }
80
81
82 void
83 afspag_SetPrimaryCell(char *acell)
84 {
85     struct afspag_cell *tcell;
86
87     tcell = afspag_GetCell(acell);
88     ObtainWriteLock(&afs_xpagcell, 822);
89     primary_cell = tcell;
90     ReleaseWriteLock(&afs_xpagcell);
91 }
92
93
94 int
95 afspag_PUnlog(char *ain, afs_int32 ainSize, afs_ucred_t **acred)
96 {
97     afs_int32 i;
98     struct unixuser *tu;
99     afs_int32 pag, uid;
100
101     AFS_STATCNT(PUnlog);
102     if (!afs_resourceinit_flag) /* afs daemons haven't started yet */
103         return EIO;             /* Inappropriate ioctl for device */
104
105     pag = PagInCred(*acred);
106     uid = (pag == NOPAG) ? afs_cr_uid(*acred) : pag;
107     i = UHash(uid);
108     ObtainWriteLock(&afs_xuser, 823);
109     for (tu = afs_users[i]; tu; tu = tu->next) {
110         if (tu->uid == uid) {
111             tu->states &= ~UHasTokens;
112             tu->viceId = UNDEFVID;
113             afs_FreeTokens(&tu->tokens);
114 #ifdef UKERNEL
115             /* set the expire times to 0, causes
116              * afs_GCUserData to remove this entry
117              */
118             tu->tokenTime = 0;
119 #endif /* UKERNEL */
120         }
121     }
122     ReleaseWriteLock(&afs_xuser);
123     return 0;
124 }
125
126
127 int
128 afspag_PSetTokens(char *ain, afs_int32 ainSize, afs_ucred_t **acred)
129 {
130     afs_int32 i;
131     struct unixuser *tu;
132     struct afspag_cell *tcell;
133     struct ClearToken clear;
134     char *stp;
135     int stLen;
136     afs_int32 flag, set_parent_pag = 0;
137     afs_int32 pag, uid;
138
139     AFS_STATCNT(PSetTokens);
140     if (!afs_resourceinit_flag) {
141         return EIO;
142     }
143     memcpy((char *)&i, ain, sizeof(afs_int32));
144     ain += sizeof(afs_int32);
145     stp = ain;                  /* remember where the ticket is */
146     if (i < 0 || i > MAXKTCTICKETLEN)
147         return EINVAL;          /* malloc may fail */
148     stLen = i;
149     ain += i;                   /* skip over ticket */
150     memcpy((char *)&i, ain, sizeof(afs_int32));
151     ain += sizeof(afs_int32);
152     if (i != sizeof(struct ClearToken)) {
153         return EINVAL;
154     }
155     memcpy((char *)&clear, ain, sizeof(struct ClearToken));
156     if (clear.AuthHandle == -1)
157         clear.AuthHandle = 999; /* more rxvab compat stuff */
158     ain += sizeof(struct ClearToken);
159     if (ainSize != 2 * sizeof(afs_int32) + stLen + sizeof(struct ClearToken)) {
160         /* still stuff left?  we've got primary flag and cell name.  Set these */
161         memcpy((char *)&flag, ain, sizeof(afs_int32));  /* primary id flag */
162         ain += sizeof(afs_int32);       /* skip id field */
163         /* rest is cell name, look it up */
164         /* some versions of gcc appear to need != 0 in order to get this right */
165         if ((flag & 0x8000) != 0) {     /* XXX Use Constant XXX */
166             flag &= ~0x8000;
167             set_parent_pag = 1;
168         }
169         tcell = afspag_GetCell(ain);
170     } else {
171         /* default to primary cell, primary id */
172         flag = 1;               /* primary id */
173         tcell = afspag_GetPrimaryCell();
174     }
175     if (!tcell) return ESRCH;
176     if (set_parent_pag) {
177 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
178         char procname[256];
179         osi_procname(procname, 256);
180
181         afs_warnuser("Process %d (%s) tried to change pags in PSetTokens\n",
182                      MyPidxx2Pid(MyPidxx), procname);
183         setpag(osi_curproc(), acred, -1, &pag, 1);
184 #else
185         setpag(acred, -1, &pag, 1);
186 #endif
187     }
188     pag = PagInCred(*acred);
189     uid = (pag == NOPAG) ? afs_cr_uid(*acred) : pag;
190     /* now we just set the tokens */
191     tu = afs_GetUser(uid, tcell->cellnum, WRITE_LOCK);
192     if (!tu->cellinfo)
193         tu->cellinfo = (void *)tcell;
194     afs_FreeTokens(&tu->tokens);
195     afs_AddRxkadToken(&tu->tokens, stp, stLen, &clear);
196 #ifndef AFS_NOSTATS
197     afs_stats_cmfullperf.authent.TicketUpdates++;
198     afs_ComputePAGStats();
199 #endif /* AFS_NOSTATS */
200     tu->states |= UHasTokens;
201     tu->states &= ~UTokensBad;
202     afs_SetPrimary(tu, flag);
203     tu->tokenTime = osi_Time();
204     afs_PutUser(tu, WRITE_LOCK);
205
206     return 0;
207 }
208
209
210 int
211 SPAGCB_GetCreds(struct rx_call *a_call, afs_int32 a_uid,
212                 CredInfos *a_creds)
213 {
214     struct unixuser *tu;
215     union tokenUnion *token;
216     CredInfo *tci;
217     int bucket, count, i = 0, clen;
218     char *cellname;
219
220     RX_AFS_GLOCK();
221
222     memset(a_creds, 0, sizeof(struct CredInfos));
223     if ((rx_HostOf(rx_PeerOf(rx_ConnectionOf(a_call))) != afs_nfs_server_addr
224         ||  rx_PortOf(rx_PeerOf(rx_ConnectionOf(a_call))) != htons(7001))
225 #if 0 /* for debugging ONLY! */
226         &&  rx_PortOf(rx_PeerOf(rx_ConnectionOf(a_call))) != htons(7901)
227 #endif
228         ) {
229         RX_AFS_GUNLOCK();
230         return UAEPERM;
231     }
232
233     ObtainWriteLock(&afs_xuser, 823);
234
235     /* count them first */
236     bucket = UHash(a_uid);
237     for (count = 0, tu = afs_users[bucket]; tu; tu = tu->next) {
238         if (tu->uid == a_uid) count++;
239     }
240
241     if (!count) {
242         ReleaseWriteLock(&afs_xuser);
243         RX_AFS_GUNLOCK();
244         return UAESRCH;
245     }
246
247     a_creds->CredInfos_val =
248         (CredInfo *)afs_osi_Alloc(count * sizeof(CredInfo));
249     if (!a_creds->CredInfos_val)
250         goto out;
251     a_creds->CredInfos_len = count;
252     memset(a_creds->CredInfos_val, 0, count * sizeof(CredInfo));
253
254     for (i = 0, tu = afs_users[bucket]; tu; tu = tu->next, i++) {
255         if (tu->uid == a_uid && tu->cellinfo &&
256             (tu->states & UHasTokens) && !(tu->states & UTokensBad)) {
257
258             token = afs_FindToken(tu->tokens, RX_SECIDX_KAD);
259
260             tci = &a_creds->CredInfos_val[i];
261             tci->vid               = token->rxkad.clearToken.ViceId;
262             tci->ct.AuthHandle     = token->rxkad.clearToken.AuthHandle;
263             memcpy(tci->ct.HandShakeKey,
264                    token->rxkad.clearToken.HandShakeKey, 8);
265             tci->ct.ViceId         = token->rxkad.clearToken.ViceId;
266             tci->ct.BeginTimestamp = token->rxkad.clearToken.BeginTimestamp;
267             tci->ct.EndTimestamp   = token->rxkad.clearToken.EndTimestamp;
268
269             cellname = ((struct afspag_cell *)(tu->cellinfo))->cellname;
270             clen = strlen(cellname) + 1;
271             tci->cellname = afs_osi_Alloc(clen);
272             if (!tci->cellname)
273                 goto out;
274             memcpy(tci->cellname, cellname, clen);
275
276             tci->st.st_len = token->rxkad.ticketLen;
277             tci->st.st_val = afs_osi_Alloc(token->rxkad.ticketLen);
278             if (!tci->st.st_val) {
279                 afs_osi_Free(tci->cellname, clen);
280                 goto out;
281             }
282             memcpy(tci->st.st_val,
283                    token->rxkad.ticket, token->rxkad.ticketLen);
284             if (tu->states & UPrimary)
285                 tci->states |= UPrimary;
286         }
287     }
288
289     ReleaseWriteLock(&afs_xuser);
290     RX_AFS_GUNLOCK();
291     return 0;
292
293 out:
294     if (a_creds->CredInfos_val) {
295         while (i-- > 0) {
296             afs_osi_Free(a_creds->CredInfos_val[i].st.st_val,
297                          a_creds->CredInfos_val[i].st.st_len);
298             afs_osi_Free(a_creds->CredInfos_val[i].cellname,
299                          strlen(a_creds->CredInfos_val[i].cellname) + 1);
300         }
301         afs_osi_Free(a_creds->CredInfos_val, count * sizeof(CredInfo));
302     }
303
304     ReleaseWriteLock(&afs_xuser);
305     RX_AFS_GUNLOCK();
306     return UAENOMEM;
307 }
308
309
310 int
311 afspag_PSetSysName(char *ain, afs_int32 ainSize, afs_ucred_t **acred)
312 {
313     int setsysname, count, t;
314     char *cp, *setp;
315
316     setp = ain;
317     memcpy((char *)&setsysname, ain, sizeof(afs_int32));
318     ain += sizeof(afs_int32);
319     if (!setsysname)
320         return 0; /* nothing to do locally */
321
322     /* Check my args */
323     if (setsysname < 0 || setsysname > MAXNUMSYSNAMES)
324         return EINVAL;
325     if (!afs_osi_suser(*acred))
326         return EACCES;
327     for (cp = ain, count = 0; count < setsysname; count++) {
328         /* won't go past end of ain since maxsysname*num < ain length */
329         t = strlen(cp);
330         if (t >= MAXSYSNAME || t <= 0)
331             return EINVAL;
332         /* check for names that can shoot us in the foot */
333         if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
334             return EINVAL;
335         cp += t + 1;
336     }
337
338     ObtainWriteLock(&afs_xpagsys, 824);
339     for (cp = ain, count = 0; count < setsysname; count++) {
340         t = strlen(cp);
341         memcpy(afs_sysnamelist[count], cp, t + 1);
342         cp += t + 1;
343     }
344     afs_sysnamecount = setsysname;
345     afs_sysnamegen++;
346     ReleaseWriteLock(&afs_xpagsys);
347
348     /* Change the arguments so we pass the allpags flag to the server */
349     setsysname |= 0x8000;
350     memcpy(setp, (char *)&setsysname, sizeof(afs_int32));
351     return 0;
352 }
353
354
355 int
356 SPAGCB_GetSysName(struct rx_call *a_call, afs_int32 a_uid,
357                   SysNameList *a_sysnames)
358 {
359     int i = 0;
360
361     RX_AFS_GLOCK();
362
363     ObtainReadLock(&afs_xpagsys);
364     memset(a_sysnames, 0, sizeof(struct SysNameList));
365
366     a_sysnames->SysNameList_len = afs_sysnamecount;
367     a_sysnames->SysNameList_val =
368         afs_osi_Alloc(afs_sysnamecount * sizeof(SysNameEnt));
369     if (!a_sysnames->SysNameList_val)
370         goto out;
371
372     for (i = 0; i < afs_sysnamecount; i++) {
373         a_sysnames->SysNameList_val[i].sysname =
374             afs_osi_Alloc(strlen(afs_sysnamelist[i]) + 1);
375         if (!a_sysnames->SysNameList_val[i].sysname)
376             goto out;
377         strcpy(a_sysnames->SysNameList_val[i].sysname, afs_sysnamelist[i]);
378     }
379
380     ReleaseReadLock(&afs_xpagsys);
381     RX_AFS_GUNLOCK();
382     return 0;
383
384 out:
385     if (a_sysnames->SysNameList_val) {
386         while (i-- > 0) {
387             afs_osi_Free(a_sysnames->SysNameList_val[i].sysname,
388                          strlen(a_sysnames->SysNameList_val[i].sysname) + 1);
389         }
390         afs_osi_Free(a_sysnames->SysNameList_val,
391                      afs_sysnamecount * sizeof(SysNameEnt));
392     }
393
394     ReleaseWriteLock(&afs_xpagsys);
395     RX_AFS_GUNLOCK();
396     return UAENOMEM;
397 }