Additional UKERNEL headers, prototyping and other fixes
[openafs.git] / src / ptserver / ptuser.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 #if defined(UKERNEL)
12 #include "afs/param.h"
13 #else
14 #include <afs/param.h>
15 #endif
16
17
18 #if defined(UKERNEL)
19 #include "afs/sysincludes.h"
20 #include "afs_usrops.h"
21 #include "afsincludes.h"
22 #include "afs/stds.h"
23 #include "rx/rx.h"
24 #include "rx/xdr.h"
25 #include "afs/auth.h"
26 #include "afs/cellconfig.h"
27 #include "afs/afsutil.h"
28 #include "afs/ptclient.h"
29 #include "afs/ptuser.h"
30 #include "afs/pterror.h"
31 #include "afs/com_err.h"
32 #else /* defined(UKERNEL) */
33 #include <afs/stds.h>
34 #include <ctype.h>
35 #include <sys/types.h>
36 #ifdef AFS_NT40_ENV
37 #include <winsock2.h>
38 #else
39 #include <netinet/in.h>
40 #endif
41 #include <stdio.h>
42 #include <string.h>
43 #include <rx/rx.h>
44 #include <rx/xdr.h>
45 #include <afs/auth.h>
46 #include <afs/cellconfig.h>
47 #include <afs/afsutil.h>
48 #include <afs/com_err.h>
49 #include "ptclient.h"
50 #include "ptuser.h"
51 #include "pterror.h"
52 #endif /* defined(UKERNEL) */
53
54
55 struct ubik_client *pruclient = 0;
56 static afs_int32 lastLevel;     /* security level pruclient, if any */
57
58 static char *whoami = "libprot";
59
60 afs_int32
61 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
62 {
63     afs_int32 code;
64     struct rx_connection *serverconns[MAXSERVERS];
65     struct rx_securityClass *sc[3];
66     static struct afsconf_dir *tdir = (struct afsconf_dir *)NULL;       /* only do this once */
67     static char tconfDir[100] = "";
68     static char tcell[64] = "";
69     struct ktc_token ttoken;
70     afs_int32 scIndex;
71     static struct afsconf_cell info;
72     afs_int32 i;
73 #if !defined(UKERNEL)
74     char cellstr[64];
75 #endif
76     afs_int32 gottdir = 0;
77     afs_int32 refresh = 0;
78
79     initialize_PT_error_table();
80     initialize_RXK_error_table();
81     initialize_ACFG_error_table();
82     initialize_KTC_error_table();
83
84 #if defined(UKERNEL)
85     if (!cell) {
86         cell = afs_LclCellName;
87     }
88 #else /* defined(UKERNEL) */
89     if (!cell) {
90         if (!tdir) 
91             tdir = afsconf_Open(confDir);
92         if (!tdir) {
93             if (confDir && strcmp(confDir, ""))
94                 fprintf(stderr,
95                         "%s: Could not open configuration directory: %s.\n",
96                         whoami, confDir);
97             else
98                 fprintf(stderr,
99                         "%s: No configuration directory specified.\n",
100                         whoami);
101             return -1;
102         }
103         gottdir = 1;
104
105         code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
106         if (code) {
107             fprintf(stderr,
108                      "libprot: Could not get local cell. [%d]\n", code);
109             return code;
110         }
111         cell = cellstr;
112     }
113 #endif /* defined(UKERNEL) */
114
115     if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
116         /*
117          * force re-evaluation.  we either don't have an afsconf_dir,
118          * the directory has changed or the cell has changed.
119          */
120         if (tdir && !gottdir) {
121             afsconf_Close(tdir);
122             tdir = (struct afsconf_dir *)NULL;
123         }
124         pruclient = (struct ubik_client *)NULL;
125         refresh = 1;
126     }
127
128     if (refresh) {
129         strncpy(tconfDir, confDir, sizeof(tconfDir));
130         strncpy(tcell, cell, sizeof(tcell));
131
132 #if defined(UKERNEL)
133         tdir = afs_cdir;
134 #else /* defined(UKERNEL) */
135         if (!gottdir)
136             tdir = afsconf_Open(confDir);
137         if (!tdir) {
138             if (confDir && strcmp(confDir, ""))
139                 fprintf(stderr,
140                         "libprot: Could not open configuration directory: %s.\n",
141                         confDir);
142             else
143                 fprintf(stderr,
144                         "libprot: No configuration directory specified.\n");
145             return -1;
146         }
147 #endif /* defined(UKERNEL) */
148
149         code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
150         if (code) {
151             fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
152                     cell, confDir, AFSDIR_CELLSERVDB_FILE);
153             return code;
154         }
155     }
156
157     /* If we already have a client and it is at the security level we
158      * want, don't get a new one. Unless the security level is 2 in
159      * which case we will get one (and re-read the key file).
160      */
161     if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
162         return 0;
163     }
164
165     code = rx_Init(0);
166     if (code) {
167         fprintf(stderr, "libprot:  Could not initialize rx.\n");
168         return code;
169     }
170
171     scIndex = secLevel;
172     sc[0] = 0;
173     sc[1] = 0;
174     sc[2] = 0;
175     /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
176      * to force use of the KeyFile.  secLevel == 0 implies -noauth was
177      * specified. */
178     if (secLevel == 2) {
179         code = afsconf_GetLatestKey(tdir, 0, 0);
180         if (code) {
181             afs_com_err(whoami, code, 
182                         "(getting key from local KeyFile)\n");
183             scIndex = 0; /* use noauth */
184         } else {
185             /* If secLevel is two assume we're on a file server and use
186              * ClientAuthSecure if possible. */
187             code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
188             if (code) {
189                 afs_com_err(whoami, code,
190                             "(calling client secure)\n");
191                 scIndex = 0;    /* use noauth */
192             }
193         }
194         if (scIndex != 2)
195             /* if there was a problem, an unauthenticated conn is returned */
196             sc[scIndex] = sc[2];
197     } else if (secLevel > 0) {
198         struct ktc_principal sname;
199         strcpy(sname.cell, info.name);
200         sname.instance[0] = 0;
201         strcpy(sname.name, "afs");
202         code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
203         if (code) {
204             afs_com_err(whoami, code, "(getting token)");
205             if (secLevel > 1)
206                 return code;
207             scIndex = 0;
208         } else {
209             if (ttoken.kvno >= 0 && ttoken.kvno <= 256)
210                 /* this is a kerberos ticket, set scIndex accordingly */
211                 scIndex = 2;
212             else {
213                 fprintf(stderr,
214                         "%s: funny kvno (%d) in ticket, proceeding\n",
215                         whoami, ttoken.kvno);
216                 scIndex = 2;
217             }
218             sc[2] =
219                 rxkad_NewClientSecurityObject((secLevel > 1) ? rxkad_crypt :
220                                               rxkad_clear, &ttoken.sessionKey,
221                                               ttoken.kvno, ttoken.ticketLen,
222                                               ttoken.ticket);
223         }
224     }
225
226     if (scIndex == 1)
227         return PRBADARG;
228     if ((scIndex == 0) && (sc[0] == 0))
229         sc[0] = rxnull_NewClientSecurityObject();
230     if ((scIndex == 0) && (secLevel != 0))
231         fprintf(stderr,
232                 "%s: Could not get afs tokens, running unauthenticated\n",
233                 whoami);
234
235     memset(serverconns, 0, sizeof(serverconns));        /* terminate list!!! */
236     for (i = 0; i < info.numServers; i++)
237         serverconns[i] =
238             rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
239                              info.hostAddr[i].sin_port, PRSRV, sc[scIndex],
240                              scIndex);
241
242     code = ubik_ClientInit(serverconns, &pruclient);
243     if (code) {
244         afs_com_err(whoami, code, "ubik client init failed.");
245         return code;
246     }
247     lastLevel = scIndex;
248
249     code = rxs_Release(sc[scIndex]);
250     return code;
251 }
252
253 int
254 pr_End(void)
255 {
256     int code = 0;
257
258     if (pruclient) {
259         code = ubik_ClientDestroy(pruclient);
260         pruclient = 0;
261     }
262     return code;
263 }
264
265
266
267 int
268 pr_CreateUser(char name[PR_MAXNAMELEN], afs_int32 *id)
269 {
270     register afs_int32 code;
271
272     stolower(name);
273     if (*id) {
274         code = ubik_PR_INewEntry(pruclient, 0, name, *id, 0);
275         return code;
276     } else {
277         code = ubik_PR_NewEntry(pruclient, 0, name, 0, 0, id);
278         return code;
279     }
280
281 }
282
283 int 
284 pr_CreateGroup(char name[PR_MAXNAMELEN], char owner[PR_MAXNAMELEN], afs_int32 *id)
285 {
286     register afs_int32 code;
287     afs_int32 oid = 0;
288     afs_int32 flags = 0;
289
290     stolower(name);
291     if (owner) {
292         code = pr_SNameToId(owner, &oid);
293         if (code)
294             return code;
295         if (oid == ANONYMOUSID)
296             return PRNOENT;
297     }
298     flags |= PRGRP;
299     if (*id) {
300         code = ubik_PR_INewEntry(pruclient, 0, name, *id, oid);
301         return code;
302     } else {
303         code = ubik_PR_NewEntry(pruclient, 0, name, flags, oid, id);
304         return code;
305     }
306 }
307
308 int
309 pr_Delete(char *name)
310 {
311     register afs_int32 code;
312     afs_int32 id;
313
314     stolower(name);
315     code = pr_SNameToId(name, &id);
316     if (code)
317         return code;
318     if (id == ANONYMOUSID)
319         return PRNOENT;
320     code = ubik_PR_Delete(pruclient, 0, id);
321     return code;
322 }
323
324 int
325 pr_DeleteByID(afs_int32 id)
326 {
327     register afs_int32 code;
328
329     code = ubik_PR_Delete(pruclient, 0, id);
330     return code;
331 }
332
333 int
334 pr_AddToGroup(char *user, char *group)
335 {
336     register afs_int32 code;
337     namelist lnames;
338     idlist lids;
339
340     lnames.namelist_len = 2;
341     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
342     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
343     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
344     lids.idlist_val = 0;
345     lids.idlist_len = 0;
346     code = pr_NameToId(&lnames, &lids);
347     if (code)
348         goto done;
349     /* if here, still could be missing an entry */
350     if (lids.idlist_val[0] == ANONYMOUSID
351         || lids.idlist_val[1] == ANONYMOUSID) {
352         code = PRNOENT;
353         goto done;
354     }
355     code =
356         ubik_PR_AddToGroup(pruclient, 0, lids.idlist_val[0],
357                   lids.idlist_val[1]);
358   done:
359     if (lnames.namelist_val)
360         free(lnames.namelist_val);
361     if (lids.idlist_val)
362         free(lids.idlist_val);
363     return code;
364 }
365
366 int
367 pr_RemoveUserFromGroup(char *user, char *group)
368 {
369     register afs_int32 code;
370     namelist lnames;
371     idlist lids;
372
373     lnames.namelist_len = 2;
374     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
375     strncpy(lnames.namelist_val[0], user, PR_MAXNAMELEN);
376     strncpy(lnames.namelist_val[1], group, PR_MAXNAMELEN);
377     lids.idlist_val = 0;
378     lids.idlist_len = 0;
379     code = pr_NameToId(&lnames, &lids);
380     if (code)
381         goto done;
382
383     if (lids.idlist_val[0] == ANONYMOUSID
384         || lids.idlist_val[1] == ANONYMOUSID) {
385         code = PRNOENT;
386         goto done;
387     }
388     code =
389         ubik_PR_RemoveFromGroup(pruclient, 0, lids.idlist_val[0],
390                   lids.idlist_val[1]);
391   done:
392     if (lnames.namelist_val)
393         free(lnames.namelist_val);
394     if (lids.idlist_val)
395         free(lids.idlist_val);
396     return code;
397
398 }
399
400 int
401 pr_NameToId(namelist *names, idlist *ids)
402 {
403     register afs_int32 code;
404     register afs_int32 i;
405
406     for (i = 0; i < names->namelist_len; i++)
407         stolower(names->namelist_val[i]);
408     code = ubik_PR_NameToID(pruclient, 0, names, ids);
409     return code;
410 }
411
412 int
413 pr_SNameToId(char name[PR_MAXNAMELEN], afs_int32 *id)
414 {
415     namelist lnames;
416     idlist lids;
417     register afs_int32 code;
418
419     lids.idlist_len = 0;
420     lids.idlist_val = 0;
421     lnames.namelist_len = 1;
422     lnames.namelist_val = (prname *) malloc(PR_MAXNAMELEN);
423     stolower(name);
424     strncpy(lnames.namelist_val[0], name, PR_MAXNAMELEN);
425     code = ubik_PR_NameToID(pruclient, 0, &lnames, &lids);
426     if (lids.idlist_val) {
427         *id = *lids.idlist_val;
428         free(lids.idlist_val);
429     }
430     if (lnames.namelist_val)
431         free(lnames.namelist_val);
432     return code;
433 }
434
435 int
436 pr_IdToName(idlist *ids, namelist *names)
437 {
438     register afs_int32 code;
439
440     code = ubik_PR_IDToName(pruclient, 0, ids, names);
441     return code;
442 }
443
444 int
445 pr_SIdToName(afs_int32 id, char name[PR_MAXNAMELEN])
446 {
447     namelist lnames;
448     idlist lids;
449     register afs_int32 code;
450
451     lids.idlist_len = 1;
452     lids.idlist_val = (afs_int32 *) malloc(sizeof(afs_int32));
453     *lids.idlist_val = id;
454     lnames.namelist_len = 0;
455     lnames.namelist_val = 0;
456     code = ubik_PR_IDToName(pruclient, 0, &lids, &lnames);
457     if (lnames.namelist_val) {
458         strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
459         free(lnames.namelist_val);
460     }
461     if (lids.idlist_val)
462         free(lids.idlist_val);
463     return code;
464 }
465
466 int
467 pr_GetCPS(afs_int32 id, prlist *CPS)
468 {
469     register afs_int32 code;
470     afs_int32 over;
471
472     over = 0;
473     code = ubik_PR_GetCPS(pruclient, 0, id, CPS, &over);
474     if (code != PRSUCCESS)
475         return code;
476     if (over) {
477         /* do something about this, probably make a new call */
478         /* don't forget there's a hard limit in the interface */
479         fprintf(stderr, "membership list for id %d exceeds display limit\n",
480                 id);
481     }
482     return 0;
483 }
484
485 int
486 pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
487 {
488     register afs_int32 code;
489     afs_int32 over;
490
491     over = 0;
492     code = ubik_PR_GetCPS2(pruclient, 0, id, host, CPS, &over);
493     if (code != PRSUCCESS)
494         return code;
495     if (over) {
496         /* do something about this, probably make a new call */
497         /* don't forget there's a hard limit in the interface */
498         fprintf(stderr, "membership list for id %d exceeds display limit\n",
499                 id);
500     }
501     return 0;
502 }
503
504 int
505 pr_GetHostCPS(afs_int32 host, prlist *CPS)
506 {
507     register afs_int32 code;
508     afs_int32 over;
509
510     over = 0;
511     code = ubik_PR_GetHostCPS(pruclient, 0, host, CPS, &over);
512     if (code != PRSUCCESS)
513         return code;
514     if (over) {
515         /* do something about this, probably make a new call */
516         /* don't forget there's a hard limit in the interface */
517         fprintf(stderr,
518                 "membership list for host id %d exceeds display limit\n",
519                 host);
520     }
521     return 0;
522 }
523
524 int
525 pr_ListMembers(char *group, namelist *lnames)
526 {
527     register afs_int32 code;
528     afs_int32 gid;
529
530     code = pr_SNameToId(group, &gid);
531     if (code)
532         return code;
533     if (gid == ANONYMOUSID)
534         return PRNOENT;
535     code = pr_IDListMembers(gid, lnames);
536     return code;
537 }
538
539 int
540 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
541 {
542     register afs_int32 code;
543     prlist alist;
544     idlist *lids;
545
546     alist.prlist_len = 0;
547     alist.prlist_val = 0;
548     code = ubik_PR_ListOwned(pruclient, 0, oid, &alist, moreP);
549     if (code)
550         return code;
551     if (*moreP == 1) {
552         /* Remain backwards compatible when moreP was a T/F bit */
553         fprintf(stderr, "membership list for id %d exceeds display limit\n",
554                 oid);
555         *moreP = 0;
556     }
557     lids = (idlist *) & alist;
558     code = pr_IdToName(lids, lnames);
559     if (code)
560         return code;
561     if (alist.prlist_val)
562         free(alist.prlist_val);
563     return PRSUCCESS;
564 }
565
566 int
567 pr_IDListMembers(afs_int32 gid, namelist *lnames)
568 {
569     register afs_int32 code;
570     prlist alist;
571     idlist *lids;
572     afs_int32 over;
573
574     alist.prlist_len = 0;
575     alist.prlist_val = 0;
576     code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
577     if (code)
578         return code;
579     if (over) {
580         fprintf(stderr, "membership list for id %d exceeds display limit\n",
581                 gid);
582     }
583     lids = (idlist *) & alist;
584     code = pr_IdToName(lids, lnames);
585     if (code)
586         return code;
587     if (alist.prlist_val)
588         free(alist.prlist_val);
589     return PRSUCCESS;
590 }
591
592 int
593 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
594 {
595     register afs_int32 code;
596
597     code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
598     return code;
599 }
600
601 afs_int32
602 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
603 {
604     afs_int32 code;
605     prentries bulkentries;
606
607     *nentries = 0;
608     *entries = NULL;
609     *nextstartindex = -1;
610     bulkentries.prentries_val = 0;
611     bulkentries.prentries_len = 0;
612
613     code =
614         ubik_PR_ListEntries(pruclient, 0, flag, startindex,
615                   &bulkentries, nextstartindex);
616     *nentries = bulkentries.prentries_len;
617     *entries = bulkentries.prentries_val;
618     return code;
619 }
620
621 int
622 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
623 {
624     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
625     register afs_int32 code;
626     struct prcheckentry aentry;
627
628     code = pr_SNameToId(name, id);
629     if (code)
630         return code;
631     if (*id == ANONYMOUSID)
632         return PRNOENT;
633     code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
634     if (code)
635         return code;
636     /* this should be done in one RPC, but I'm lazy. */
637     code = pr_SIdToName(aentry.owner, owner);
638     if (code)
639         return code;
640     code = pr_SIdToName(aentry.creator, creator);
641     if (code)
642         return code;
643     return PRSUCCESS;
644 }
645
646 int
647 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
648 {
649     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
650     register afs_int32 code;
651     struct prcheckentry aentry;
652
653     code = pr_SIdToName(id, name);
654     if (code)
655         return code;
656     if (id == ANONYMOUSID)
657         return PRNOENT;
658     code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
659     if (code)
660         return code;
661     /* this should be done in one RPC, but I'm lazy. */
662     code = pr_SIdToName(aentry.owner, owner);
663     if (code)
664         return code;
665     code = pr_SIdToName(aentry.creator, creator);
666     if (code)
667         return code;
668     return PRSUCCESS;
669 }
670
671 int
672 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
673 {
674     register afs_int32 code;
675     afs_int32 id;
676     afs_int32 oid = 0;
677
678     code = pr_SNameToId(oldname, &id);
679     if (code)
680         return code;
681     if (id == ANONYMOUSID)
682         return PRNOENT;
683     if (newowner && *newowner) {
684         code = pr_SNameToId(newowner, &oid);
685         if (code)
686             return code;
687         if (oid == ANONYMOUSID)
688             return PRNOENT;
689     }
690     if (newid)
691         code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
692     else
693         code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
694     return code;
695 }
696
697 int
698 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
699 {
700     register afs_int32 code;
701     namelist lnames;
702     idlist lids;
703
704     stolower(uname);
705     stolower(gname);
706     lnames.namelist_len = 2;
707     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
708     strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
709     strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
710     lids.idlist_val = 0;
711     lids.idlist_len = 0;
712     code = pr_NameToId(&lnames, &lids);
713     if (code) {
714         if (lnames.namelist_val)
715             free(lnames.namelist_val);
716         if (lids.idlist_val)
717             free(lids.idlist_val);
718         return code;
719     }
720     code =
721         ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
722                   lids.idlist_val[1], flag);
723     if (lnames.namelist_val)
724         free(lnames.namelist_val);
725     if (lids.idlist_val)
726         free(lids.idlist_val);
727     return code;
728 }
729
730 int
731 pr_ListMaxUserId(afs_int32 *mid)
732 {
733     register afs_int32 code;
734     afs_int32 gid;
735     code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
736     return code;
737 }
738
739 int
740 pr_SetMaxUserId(afs_int32 mid)
741 {
742     register afs_int32 code;
743     afs_int32 flag = 0;
744     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
745     return code;
746 }
747
748 int
749 pr_ListMaxGroupId(afs_int32 *mid)
750 {
751     register afs_int32 code;
752     afs_int32 id;
753     code = ubik_PR_ListMax(pruclient, 0, &id, mid);
754     return code;
755 }
756
757 int
758 pr_SetMaxGroupId(afs_int32 mid)
759 {
760     register afs_int32 code;
761     afs_int32 flag = 0;
762
763     flag |= PRGRP;
764     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
765     return code;
766 }
767
768 afs_int32
769 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
770 {
771     register afs_int32 code;
772
773     code =
774         ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
775                   nusers, 0, 0);
776     return code;
777 }