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