ebf6c47b19e51a84c7673c3820cc64715f950e3b
[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 RCSID
18     ("$Header$");
19
20 #if defined(UKERNEL)
21 #include "afs/sysincludes.h"
22 #include "afs_usrops.h"
23 #include "afsincludes.h"
24 #include "afs/stds.h"
25 #include "rx/rx.h"
26 #include "rx/xdr.h"
27 #include "rx/rxkad.h"
28 #include "afs/auth.h"
29 #include "afs/cellconfig.h"
30 #include "afs/afsutil.h"
31 #include "afs/ptclient.h"
32 #include "afs/ptuser.h"
33 #include "afs/pterror.h"
34 #else /* defined(UKERNEL) */
35 #include <afs/stds.h>
36 #include <ctype.h>
37 #include <sys/types.h>
38 #ifdef AFS_NT40_ENV
39 #include <winsock2.h>
40 #else
41 #include <netinet/in.h>
42 #endif
43 #include <stdio.h>
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #else
47 #ifdef HAVE_STRINGS_H
48 #include <strings.h>
49 #endif
50 #endif
51 #include <rx/rx.h>
52 #include <rx/xdr.h>
53 #include <rx/rxkad.h>
54 #include <afs/auth.h>
55 #include <afs/cellconfig.h>
56 #include <afs/afsutil.h>
57 #include "ptclient.h"
58 #include "ptuser.h"
59 #include "pterror.h"
60 #endif /* defined(UKERNEL) */
61
62
63 struct ubik_client *pruclient = 0;
64 static afs_int32 lastLevel;     /* security level pruclient, if any */
65
66 static char *whoami = "libprot";
67
68 afs_int32
69 pr_Initialize(IN afs_int32 secLevel, IN const char *confDir, IN char *cell)
70 {
71     afs_int32 code;
72     struct rx_connection *serverconns[MAXSERVERS];
73     struct rx_securityClass *sc[3];
74     static struct afsconf_dir *tdir = (struct afsconf_dir *)NULL;       /* only do this once */
75     static char tconfDir[100] = "";
76     static char tcell[64] = "";
77     struct ktc_token ttoken;
78     afs_int32 scIndex;
79     static struct afsconf_cell info;
80     afs_int32 i;
81     char cellstr[64];
82     afs_int32 gottdir = 0;
83     afs_int32 refresh = 0;
84
85     initialize_PT_error_table();
86     initialize_RXK_error_table();
87     initialize_ACFG_error_table();
88     initialize_KTC_error_table();
89
90 #if defined(UKERNEL)
91     if (!cell) {
92         cell = afs_LclCellName;
93     }
94 #else /* defined(UKERNEL) */
95     if (!cell) {
96         if (!tdir) 
97             tdir = afsconf_Open(confDir);
98         if (!tdir) {
99             if (confDir && strcmp(confDir, ""))
100                 fprintf(stderr,
101                         "libprot: Could not open configuration directory: %s.\n",
102                         confDir);
103             else
104                 fprintf(stderr,
105                         "libprot: No configuration directory specified.\n");
106             return -1;
107         }
108         gottdir = 1;
109
110         code = afsconf_GetLocalCell(tdir, cellstr, sizeof(cellstr));
111         if (code) {
112             afsconf_Close(tdir);
113             fprintf(stderr,
114                      "libprot: Could not get local cell. [%d]\n", code);
115             return code;
116         }
117         cell = cellstr;
118     }
119 #endif /* defined(UKERNEL) */
120
121     if (tdir == NULL || strcmp(confDir, tconfDir) || strcmp(cell, tcell)) {
122         /*
123          * force re-evaluation.  we either don't have an afsconf_dir,
124          * the directory has changed or the cell has changed.
125          */
126         if (tdir && !gottdir) {
127             afsconf_Close(tdir);
128             tdir = (struct afsconf_dir *)NULL;
129         }
130         pruclient = (struct ubik_client *)NULL;
131         refresh = 1;
132     }
133
134     if (refresh) {
135         strncpy(tconfDir, confDir, sizeof(tconfDir));
136         strncpy(tcell, cell, sizeof(tcell));
137
138 #if defined(UKERNEL)
139         tdir = afs_cdir;
140 #else /* defined(UKERNEL) */
141         if (!gottdir)
142             tdir = afsconf_Open(confDir);
143         if (!tdir) {
144             if (confDir && strcmp(confDir, ""))
145                 fprintf(stderr,
146                         "libprot: Could not open configuration directory: %s.\n",
147                         confDir);
148             else
149                 fprintf(stderr,
150                         "libprot: No configuration directory specified.\n");
151             return -1;
152         }
153 #endif /* defined(UKERNEL) */
154
155         code = afsconf_GetCellInfo(tdir, cell, "afsprot", &info);
156         if (code) {
157             afsconf_Close(tdir);
158             fprintf(stderr, "libprot: Could not locate cell %s in %s/%s\n",
159                     cell, confDir, AFSDIR_CELLSERVDB_FILE);
160             return code;
161         }
162     }
163
164     /* If we already have a client and it is at the security level we
165      * want, don't get a new one. Unless the security level is 2 in
166      * which case we will get one (and re-read the key file).
167      */
168     if (pruclient && (lastLevel == secLevel) && (secLevel != 2)) {
169         afsconf_Close(tdir);
170         return 0;
171     }
172
173     code = rx_Init(0);
174     if (code) {
175         afsconf_Close(tdir);
176         fprintf(stderr, "libprot:  Could not initialize rx.\n");
177         return code;
178     }
179
180     scIndex = secLevel;
181     sc[0] = 0;
182     sc[1] = 0;
183     sc[2] = 0;
184     /* Most callers use secLevel==1, however, the fileserver uses secLevel==2
185      * to force use of the KeyFile.  secLevel == 0 implies -noauth was
186      * specified. */
187     if ((secLevel == 2) && (afsconf_GetLatestKey(tdir, 0, 0) == 0)) {
188         /* If secLevel is two assume we're on a file server and use
189          * ClientAuthSecure if possible. */
190         code = afsconf_ClientAuthSecure(tdir, &sc[2], &scIndex);
191         if (code)
192             fprintf(stderr,
193                     "libprot: clientauthsecure returns %d %s"
194                     " (so trying noauth)\n", code, error_message(code));
195         if (code)
196             scIndex = 0;        /* use noauth */
197         if (scIndex != 2)
198             /* if there was a problem, an unauthenticated conn is returned */
199             sc[scIndex] = sc[2];
200     } else if (secLevel > 0) {
201         struct ktc_principal sname;
202         strcpy(sname.cell, info.name);
203         sname.instance[0] = 0;
204         strcpy(sname.name, "afs");
205         code = ktc_GetToken(&sname, &ttoken, sizeof(ttoken), NULL);
206         if (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                         "libprot: funny kvno (%d) in ticket, proceeding\n",
215                         ttoken.kvno);
216                 scIndex = 2;
217             }
218             sc[2] =
219                 rxkad_NewClientSecurityObject(rxkad_clear, &ttoken.sessionKey,
220                                               ttoken.kvno, ttoken.ticketLen,
221                                               ttoken.ticket);
222         }
223     }
224     afsconf_Close(tdir);
225     tdir = NULL;
226
227     if (scIndex == 1)
228         return PRBADARG;
229     if ((scIndex == 0) && (sc[0] == 0))
230         sc[0] = rxnull_NewClientSecurityObject();
231     if ((scIndex == 0) && (secLevel != 0))
232         com_err(whoami, code,
233                 "Could not get afs tokens, running unauthenticated.");
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         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()
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       //code = ubik_PR_ListEntry(pruclient, 0, *id, &aentry);
635     if (code)
636         return code;
637     /* this should be done in one RPC, but I'm lazy. */
638     code = pr_SIdToName(aentry.owner, owner);
639     if (code)
640         return code;
641     code = pr_SIdToName(aentry.creator, creator);
642     if (code)
643         return code;
644     return PRSUCCESS;
645 }
646
647 int
648 pr_CheckEntryById(char *name, afs_int32 id, char *owner, char *creator)
649 {
650     /* struct prcheckentry returns other things, which aren't useful to show at this time. */
651     register afs_int32 code;
652     struct prcheckentry aentry;
653
654     code = pr_SIdToName(id, name);
655     if (code)
656         return code;
657     if (id == ANONYMOUSID)
658         return PRNOENT;
659     code = ubik_PR_ListEntry(pruclient, 0, id, &aentry);
660     if (code)
661         return code;
662     /* this should be done in one RPC, but I'm lazy. */
663     code = pr_SIdToName(aentry.owner, owner);
664     if (code)
665         return code;
666     code = pr_SIdToName(aentry.creator, creator);
667     if (code)
668         return code;
669     return PRSUCCESS;
670 }
671
672 int
673 pr_ChangeEntry(char *oldname, char *newname, afs_int32 *newid, char *newowner)
674 {
675     register afs_int32 code;
676     afs_int32 id;
677     afs_int32 oid = 0;
678
679     code = pr_SNameToId(oldname, &id);
680     if (code)
681         return code;
682     if (id == ANONYMOUSID)
683         return PRNOENT;
684     if (newowner && *newowner) {
685         code = pr_SNameToId(newowner, &oid);
686         if (code)
687             return code;
688         if (oid == ANONYMOUSID)
689             return PRNOENT;
690     }
691     code = ubik_PR_ChangeEntry(pruclient, 0, id, newname, oid, newid);
692     return code;
693 }
694
695 int
696 pr_IsAMemberOf(char *uname, char *gname, afs_int32 *flag)
697 {
698     register afs_int32 code;
699     namelist lnames;
700     idlist lids;
701
702     stolower(uname);
703     stolower(gname);
704     lnames.namelist_len = 2;
705     lnames.namelist_val = (prname *) malloc(2 * PR_MAXNAMELEN);
706     strncpy(lnames.namelist_val[0], uname, PR_MAXNAMELEN);
707     strncpy(lnames.namelist_val[1], gname, PR_MAXNAMELEN);
708     lids.idlist_val = 0;
709     lids.idlist_len = 0;
710     code = pr_NameToId(&lnames, &lids);
711     if (code) {
712         if (lnames.namelist_val)
713             free(lnames.namelist_val);
714         if (lids.idlist_val)
715             free(lids.idlist_val);
716         return code;
717     }
718     code =
719         ubik_PR_IsAMemberOf(pruclient, 0, lids.idlist_val[0],
720                   lids.idlist_val[1], flag);
721     if (lnames.namelist_val)
722         free(lnames.namelist_val);
723     if (lids.idlist_val)
724         free(lids.idlist_val);
725     return code;
726 }
727
728 int
729 pr_ListMaxUserId(afs_int32 *mid)
730 {
731     register afs_int32 code;
732     afs_int32 gid;
733     code = ubik_PR_ListMax(pruclient, 0, mid, &gid);
734     return code;
735 }
736
737 int
738 pr_SetMaxUserId(afs_int32 mid)
739 {
740     register afs_int32 code;
741     afs_int32 flag = 0;
742     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
743     return code;
744 }
745
746 int
747 pr_ListMaxGroupId(afs_int32 *mid)
748 {
749     register afs_int32 code;
750     afs_int32 id;
751     code = ubik_PR_ListMax(pruclient, 0, &id, mid);
752     return code;
753 }
754
755 int
756 pr_SetMaxGroupId(afs_int32 mid)
757 {
758     register afs_int32 code;
759     afs_int32 flag = 0;
760
761     flag |= PRGRP;
762     code = ubik_PR_SetMax(pruclient, 0, mid, flag);
763     return code;
764 }
765
766 afs_int32
767 pr_SetFieldsEntry(afs_int32 id, afs_int32 mask, afs_int32 flags, afs_int32 ngroups, afs_int32 nusers)
768 {
769     register afs_int32 code;
770
771     code =
772         ubik_PR_SetFieldsEntry(pruclient, 0, id, mask, flags, ngroups,
773                   nusers, 0, 0);
774     return code;
775 }