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