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