59a0f021648288c206360c475404dec574d384a2
[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 = 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
362     xdr_free((xdrproc_t) xdr_idlist, &lids);
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 = 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
395     xdr_free((xdrproc_t) xdr_idlist, &lids);
396
397     return code;
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 = 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         xdr_free((xdrproc_t) xdr_idlist, &lids);
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 = 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
458     if (lnames.namelist_val)
459         strncpy(name, lnames.namelist_val[0], PR_MAXNAMELEN);
460
461     if (lids.idlist_val)
462         free(lids.idlist_val);
463
464     xdr_free((xdrproc_t) xdr_namelist, &lnames);
465
466     return code;
467 }
468
469 int
470 pr_GetCPS(afs_int32 id, prlist *CPS)
471 {
472     register afs_int32 code;
473     afs_int32 over;
474
475     over = 0;
476     code = ubik_PR_GetCPS(pruclient, 0, id, CPS, &over);
477     if (code != PRSUCCESS)
478         return code;
479     if (over) {
480         /* do something about this, probably make a new call */
481         /* don't forget there's a hard limit in the interface */
482         fprintf(stderr, "membership list for id %d exceeds display limit\n",
483                 id);
484     }
485     return 0;
486 }
487
488 int
489 pr_GetCPS2(afs_int32 id, afs_int32 host, prlist *CPS)
490 {
491     register afs_int32 code;
492     afs_int32 over;
493
494     over = 0;
495     code = ubik_PR_GetCPS2(pruclient, 0, id, host, CPS, &over);
496     if (code != PRSUCCESS)
497         return code;
498     if (over) {
499         /* do something about this, probably make a new call */
500         /* don't forget there's a hard limit in the interface */
501         fprintf(stderr, "membership list for id %d exceeds display limit\n",
502                 id);
503     }
504     return 0;
505 }
506
507 int
508 pr_GetHostCPS(afs_int32 host, prlist *CPS)
509 {
510     register afs_int32 code;
511     afs_int32 over;
512
513     over = 0;
514     code = ubik_PR_GetHostCPS(pruclient, 0, host, CPS, &over);
515     if (code != PRSUCCESS)
516         return code;
517     if (over) {
518         /* do something about this, probably make a new call */
519         /* don't forget there's a hard limit in the interface */
520         fprintf(stderr,
521                 "membership list for host id %d exceeds display limit\n",
522                 host);
523     }
524     return 0;
525 }
526
527 int
528 pr_ListMembers(char *group, namelist *lnames)
529 {
530     register afs_int32 code;
531     afs_int32 gid;
532
533     code = pr_SNameToId(group, &gid);
534     if (code)
535         return code;
536     if (gid == ANONYMOUSID)
537         return PRNOENT;
538     code = pr_IDListMembers(gid, lnames);
539     return code;
540 }
541
542 int
543 pr_ListOwned(afs_int32 oid, namelist *lnames, afs_int32 *moreP)
544 {
545     register afs_int32 code;
546     prlist alist;
547     idlist *lids;
548
549     alist.prlist_len = 0;
550     alist.prlist_val = 0;
551     code = ubik_PR_ListOwned(pruclient, 0, oid, &alist, moreP);
552     if (code)
553         return code;
554     if (*moreP == 1) {
555         /* Remain backwards compatible when moreP was a T/F bit */
556         fprintf(stderr, "membership list for id %d exceeds display limit\n",
557                 oid);
558         *moreP = 0;
559     }
560     lids = (idlist *) &alist;
561     code = pr_IdToName(lids, lnames);
562
563     xdr_free((xdrproc_t) xdr_prlist, &alist);
564
565     if (code)
566         return code;
567
568     return PRSUCCESS;
569 }
570
571 int
572 pr_IDListMembers(afs_int32 gid, namelist *lnames)
573 {
574     register afs_int32 code;
575     prlist alist;
576     idlist *lids;
577     afs_int32 over;
578
579     alist.prlist_len = 0;
580     alist.prlist_val = 0;
581     code = ubik_PR_ListElements(pruclient, 0, gid, &alist, &over);
582     if (code)
583         return code;
584     if (over) {
585         fprintf(stderr, "membership list for id %d exceeds display limit\n",
586                 gid);
587     }
588     lids = (idlist *) &alist;
589     code = pr_IdToName(lids, lnames);
590
591     xdr_free((xdrproc_t) xdr_prlist, &alist);
592
593     if (code)
594         return code;
595     return PRSUCCESS;
596 }
597
598 int
599 pr_ListEntry(afs_int32 id, struct prcheckentry *aentry)
600 {
601     register afs_int32 code;
602
603     code = ubik_PR_ListEntry(pruclient, 0, id, aentry);
604     return code;
605 }
606
607 afs_int32
608 pr_ListEntries(int flag, afs_int32 startindex, afs_int32 *nentries, struct prlistentries **entries, afs_int32 *nextstartindex)
609 {
610     afs_int32 code;
611     prentries bulkentries;
612
613     *nentries = 0;
614     *entries = NULL;
615     *nextstartindex = -1;
616     bulkentries.prentries_val = 0;
617     bulkentries.prentries_len = 0;
618
619     code =
620         ubik_PR_ListEntries(pruclient, 0, flag, startindex,
621                   &bulkentries, nextstartindex);
622     *nentries = bulkentries.prentries_len;
623     *entries = bulkentries.prentries_val;
624     return code;
625 }
626
627 int
628 pr_CheckEntryByName(char *name, afs_int32 *id, char *owner, char *creator)
629 {
630     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
631     register afs_int32 code;
632     struct prcheckentry aentry;
633
634     code = pr_SNameToId(name, id);
635     if (code)
636         return code;
637     if (*id == ANONYMOUSID)
638         return PRNOENT;
639     code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
640     if (code)
641         return code;
642     /* this should be done in one RPC, but I'm lazy. */
643     code = pr_SIdToName(aentry.owner, owner);
644     if (code)
645         return code;
646     code = pr_SIdToName(aentry.creator, creator);
647     if (code)
648         return code;
649     return PRSUCCESS;
650 }
651
652 int
653 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
654 {
655     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
656     register afs_int32 code;
657     struct prcheckentry aentry;
658
659     code = pr_SIdToName(id, name);
660     if (code)
661         return code;
662     if (id == ANONYMOUSID)
663         return PRNOENT;
664     code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
665     if (code)
666         return code;
667     /* this should be done in one RPC, but I'm lazy. */
668     code = pr_SIdToName(aentry.owner, owner);
669     if (code)
670         return code;
671     code = pr_SIdToName(aentry.creator, creator);
672     if (code)
673         return code;
674     return PRSUCCESS;
675 }
676
677 int
678 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
679 {
680     register afs_int32 code;
681     afs_int32 id;
682     afs_int32 oid = 0;
683
684     code = pr_SNameToId(oldname, &id);
685     if (code)
686         return code;
687     if (id == ANONYMOUSID)
688         return PRNOENT;
689     if (newowner && *newowner) {
690         code = pr_SNameToId(newowner, &oid);
691         if (code)
692             return code;
693         if (oid == ANONYMOUSID)
694             return PRNOENT;
695     }
696     if (newid)
697         code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, *newid);
698     else
699         code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, 0);
700     return code;
701 }
702
703 int
704 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
705 {
706     register afs_int32 code;
707     namelist lnames;
708     idlist lids;
709
710     stolower(uname);
711     stolower(gname);
712     lnames.namelist_len = 2;
713     lnames.namelist_val = malloc(2 * PR_MAXNAMELEN);
714     strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
715     strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
716     lids.idlist_val = 0;
717     lids.idlist_len = 0;
718     code = pr_NameToId(&lnames, &lids);
719     if (code) {
720         if (lnames.namelist_val)
721             free(lnames.namelist_val);
722         xdr_free((xdrproc_t) xdr_idlist, &lids);
723         return code;
724     }
725     code =
726         ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
727                   lids.idlist_val[1], flag);
728     if (lnames.namelist_val)
729         free(lnames.namelist_val);
730     xdr_free((xdrproc_t) xdr_idlist, &lids);
731     return code;
732 }
733
734 int
735 pr_ListMaxUserId(afs_int32 *mid)
736 {
737     register afs_int32 code;
738     afs_int32 gid;
739     code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
740     return code;
741 }
742
743 int
744 pr_SetMaxUserId(afs_int32 mid)
745 {
746     register afs_int32 code;
747     afs_int32 flag = 0;
748     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
749     return code;
750 }
751
752 int
753 pr_ListMaxGroupId(afs_int32 *mid)
754 {
755     register afs_int32 code;
756     afs_int32 id;
757     code = ubik_PR_ListMax(pruclient, 0, &id, mid);
758     return code;
759 }
760
761 int
762 pr_SetMaxGroupId(afs_int32 mid)
763 {
764     register afs_int32 code;
765     afs_int32 flag = 0;
766
767     flag |= PRGRP;
768     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
769     return code;
770 }
771
772 afs_int32
773 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
774 {
775     register afs_int32 code;
776
777     code =
778         ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
779                   nusers, 0, 0);
780     return code;
781 }