libuafs: Add uafs_Init compat function
[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     register afs_int32 i;
98     register 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->vid = UNDEFVID;
112             tu->states &= ~UHasTokens;
113             /* security is not having to say you're sorry */
114             memset(&tu->ct, 0, sizeof(struct ClearToken));
115 #ifdef UKERNEL
116             /* set the expire times to 0, causes
117              * afs_GCUserData to remove this entry
118              */
119             tu->ct.EndTimestamp = 0;
120             tu->tokenTime = 0;
121 #endif /* UKERNEL */
122         }
123     }
124     ReleaseWriteLock(&afs_xuser);
125     return 0;
126 }
127
128
129 int
130 afspag_PSetTokens(char *ain, afs_int32 ainSize, afs_ucred_t **acred)
131 {
132     afs_int32 i;
133     register struct unixuser *tu;
134     struct afspag_cell *tcell;
135     struct ClearToken clear;
136     char *stp;
137     int stLen;
138     afs_int32 flag, set_parent_pag = 0;
139     afs_int32 pag, uid;
140
141     AFS_STATCNT(PSetTokens);
142     if (!afs_resourceinit_flag) {
143         return EIO;
144     }
145     memcpy((char *)&i, ain, sizeof(afs_int32));
146     ain += sizeof(afs_int32);
147     stp = ain;                  /* remember where the ticket is */
148     if (i < 0 || i > MAXKTCTICKETLEN)
149         return EINVAL;          /* malloc may fail */
150     stLen = i;
151     ain += i;                   /* skip over ticket */
152     memcpy((char *)&i, ain, sizeof(afs_int32));
153     ain += sizeof(afs_int32);
154     if (i != sizeof(struct ClearToken)) {
155         return EINVAL;
156     }
157     memcpy((char *)&clear, ain, sizeof(struct ClearToken));
158     if (clear.AuthHandle == -1)
159         clear.AuthHandle = 999; /* more rxvab compat stuff */
160     ain += sizeof(struct ClearToken);
161     if (ainSize != 2 * sizeof(afs_int32) + stLen + sizeof(struct ClearToken)) {
162         /* still stuff left?  we've got primary flag and cell name.  Set these */
163         memcpy((char *)&flag, ain, sizeof(afs_int32));  /* primary id flag */
164         ain += sizeof(afs_int32);       /* skip id field */
165         /* rest is cell name, look it up */
166         /* some versions of gcc appear to need != 0 in order to get this right */
167         if ((flag & 0x8000) != 0) {     /* XXX Use Constant XXX */
168             flag &= ~0x8000;
169             set_parent_pag = 1;
170         }
171         tcell = afspag_GetCell(ain);
172     } else {
173         /* default to primary cell, primary id */
174         flag = 1;               /* primary id */
175         tcell = afspag_GetPrimaryCell();
176     }
177     if (!tcell) return ESRCH;
178     if (set_parent_pag) {
179 #if defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
180         char procname[256];
181         osi_procname(procname, 256);
182
183         afs_warnuser("Process %d (%s) tried to change pags in PSetTokens\n",
184                      MyPidxx2Pid(MyPidxx), procname);
185         setpag(osi_curproc(), acred, -1, &pag, 1);
186 #else
187         setpag(acred, -1, &pag, 1);
188 #endif
189     }
190     pag = PagInCred(*acred);
191     uid = (pag == NOPAG) ? afs_cr_uid(*acred) : pag;
192     /* now we just set the tokens */
193     tu = afs_GetUser(uid, tcell->cellnum, WRITE_LOCK);
194     if (!tu->cellinfo)
195         tu->cellinfo = (void *)tcell;
196     tu->vid = clear.ViceId;
197     if (tu->stp != NULL) {
198         afs_osi_Free(tu->stp, tu->stLen);
199     }
200     tu->stp = (char *)afs_osi_Alloc(stLen);
201     tu->stLen = stLen;
202     memcpy(tu->stp, stp, stLen);
203     tu->ct = clear;
204 #ifndef AFS_NOSTATS
205     afs_stats_cmfullperf.authent.TicketUpdates++;
206     afs_ComputePAGStats();
207 #endif /* AFS_NOSTATS */
208     tu->states |= UHasTokens;
209     tu->states &= ~UTokensBad;
210     afs_SetPrimary(tu, flag);
211     tu->tokenTime = osi_Time();
212     afs_PutUser(tu, WRITE_LOCK);
213
214     return 0;
215 }
216
217
218 int
219 SPAGCB_GetCreds(struct rx_call *a_call, afs_int32 a_uid,
220                 CredInfos *a_creds)
221 {
222     struct unixuser *tu;
223     CredInfo *tci;
224     int bucket, count, i = 0, clen;
225     char *cellname;
226
227     RX_AFS_GLOCK();
228
229     memset(a_creds, 0, sizeof(struct CredInfos));
230     if ((rx_HostOf(rx_PeerOf(rx_ConnectionOf(a_call))) != afs_nfs_server_addr
231         ||  rx_PortOf(rx_PeerOf(rx_ConnectionOf(a_call))) != htons(7001))
232 #if 0 /* for debugging ONLY! */
233         &&  rx_PortOf(rx_PeerOf(rx_ConnectionOf(a_call))) != htons(7901)
234 #endif
235         ) {
236         RX_AFS_GUNLOCK();
237         return UAEPERM;
238     }
239
240     ObtainWriteLock(&afs_xuser, 823);
241
242     /* count them first */
243     bucket = UHash(a_uid);
244     for (count = 0, tu = afs_users[bucket]; tu; tu = tu->next) {
245         if (tu->uid == a_uid) count++;
246     }
247
248     if (!count) {
249         ReleaseWriteLock(&afs_xuser);
250         RX_AFS_GUNLOCK();
251         return UAESRCH;
252     }
253
254     a_creds->CredInfos_val =
255         (CredInfo *)afs_osi_Alloc(count * sizeof(CredInfo));
256     if (!a_creds->CredInfos_val)
257         goto out;
258     a_creds->CredInfos_len = count;
259     memset(a_creds->CredInfos_val, 0, count * sizeof(CredInfo));
260
261     for (i = 0, tu = afs_users[bucket]; tu; tu = tu->next, i++) {
262         if (tu->uid == a_uid && tu->cellinfo &&
263             (tu->states & UHasTokens) && !(tu->states & UTokensBad)) {
264
265             tci = &a_creds->CredInfos_val[i];
266             tci->vid               = tu->vid;
267             tci->ct.AuthHandle     = tu->ct.AuthHandle;
268             memcpy(tci->ct.HandShakeKey, tu->ct.HandShakeKey, 8);
269             tci->ct.ViceId         = tu->ct.ViceId;
270             tci->ct.BeginTimestamp = tu->ct.BeginTimestamp;
271             tci->ct.EndTimestamp   = tu->ct.EndTimestamp;
272
273             cellname = ((struct afspag_cell *)(tu->cellinfo))->cellname;
274             clen = strlen(cellname) + 1;
275             tci->cellname = afs_osi_Alloc(clen);
276             if (!tci->cellname)
277                 goto out;
278             memcpy(tci->cellname, cellname, clen);
279
280             tci->st.st_len = tu->stLen;
281             tci->st.st_val = afs_osi_Alloc(tu->stLen);
282             if (!tci->st.st_val) {
283                 afs_osi_Free(tci->cellname, clen);
284                 goto out;
285             }
286             memcpy(tci->st.st_val, tu->stp, tu->stLen);
287             if (tu->states & UPrimary)
288                 tci->states |= UPrimary;
289         }
290     }
291
292     ReleaseWriteLock(&afs_xuser);
293     RX_AFS_GUNLOCK();
294     return 0;
295
296 out:
297     if (a_creds->CredInfos_val) {
298         while (i-- > 0) {
299             afs_osi_Free(a_creds->CredInfos_val[i].st.st_val,
300                          a_creds->CredInfos_val[i].st.st_len);
301             afs_osi_Free(a_creds->CredInfos_val[i].cellname,
302                          strlen(a_creds->CredInfos_val[i].cellname) + 1);
303         }
304         afs_osi_Free(a_creds->CredInfos_val, count * sizeof(CredInfo));
305     }
306
307     ReleaseWriteLock(&afs_xuser);
308     RX_AFS_GUNLOCK();
309     return UAENOMEM;
310 }
311
312
313 int
314 afspag_PSetSysName(char *ain, afs_int32 ainSize, afs_ucred_t **acred)
315 {
316     int setsysname, count, t;
317     char *cp, *setp;
318
319     setp = ain;
320     memcpy((char *)&setsysname, ain, sizeof(afs_int32));
321     ain += sizeof(afs_int32);
322     if (!setsysname)
323         return 0; /* nothing to do locally */
324
325     /* Check my args */
326     if (setsysname < 0 || setsysname > MAXNUMSYSNAMES)
327         return EINVAL;
328     if (!afs_osi_suser(*acred))
329         return EACCES;
330     for (cp = ain, count = 0; count < setsysname; count++) {
331         /* won't go past end of ain since maxsysname*num < ain length */
332         t = strlen(cp);
333         if (t >= MAXSYSNAME || t <= 0)
334             return EINVAL;
335         /* check for names that can shoot us in the foot */
336         if (*cp == '.' && (cp[1] == 0 || (cp[1] == '.' && cp[2] == 0)))
337             return EINVAL;
338         cp += t + 1;
339     }
340
341     ObtainWriteLock(&afs_xpagsys, 824);
342     for (cp = ain, count = 0; count < setsysname; count++) {
343         t = strlen(cp);
344         memcpy(afs_sysnamelist[count], cp, t + 1);
345         cp += t + 1;
346     }
347     afs_sysnamecount = setsysname;
348     afs_sysnamegen++;
349     ReleaseWriteLock(&afs_xpagsys);
350
351     /* Change the arguments so we pass the allpags flag to the server */
352     setsysname |= 0x8000;
353     memcpy(setp, (char *)&setsysname, sizeof(afs_int32));
354     return 0;
355 }
356
357
358 int
359 SPAGCB_GetSysName(struct rx_call *a_call, afs_int32 a_uid,
360                   SysNameList *a_sysnames)
361 {
362     int i = 0;
363
364     RX_AFS_GLOCK();
365
366     ObtainReadLock(&afs_xpagsys);
367     memset(a_sysnames, 0, sizeof(struct SysNameList));
368
369     a_sysnames->SysNameList_len = afs_sysnamecount;
370     a_sysnames->SysNameList_val =
371         afs_osi_Alloc(afs_sysnamecount * sizeof(SysNameEnt));
372     if (!a_sysnames->SysNameList_val)
373         goto out;
374
375     for (i = 0; i < afs_sysnamecount; i++) {
376         a_sysnames->SysNameList_val[i].sysname =
377             afs_osi_Alloc(strlen(afs_sysnamelist[i]) + 1);
378         if (!a_sysnames->SysNameList_val[i].sysname)
379             goto out;
380         strcpy(a_sysnames->SysNameList_val[i].sysname, afs_sysnamelist[i]);
381     }
382
383     ReleaseReadLock(&afs_xpagsys);
384     RX_AFS_GUNLOCK();
385     return 0;
386
387 out:
388     if (a_sysnames->SysNameList_val) {
389         while (i-- > 0) {
390             afs_osi_Free(a_sysnames->SysNameList_val[i].sysname,
391                          strlen(a_sysnames->SysNameList_val[i].sysname) + 1);
392         }
393         afs_osi_Free(a_sysnames->SysNameList_val,
394                      afs_sysnamecount * sizeof(SysNameEnt));
395     }
396
397     ReleaseWriteLock(&afs_xpagsys);
398     RX_AFS_GUNLOCK();
399     return UAENOMEM;
400 }