Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / kafs / afskrb.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "kafs_locl.h"
35
36 RCSID("$Id: afskrb.c,v 1.13.2.1 2000/06/23 03:26:53 assar Exp $");
37
38 struct krb_kafs_data {
39     const char *realm;
40 };
41
42 static int
43 get_cred(kafs_data *data, const char *name, const char *inst, 
44          const char *realm, CREDENTIALS *c)
45 {
46     KTEXT_ST tkt;
47     int ret = krb_get_cred((char*)name, (char*)inst, (char*)realm, c);
48     
49     if (ret) {
50         ret = krb_mk_req(&tkt, (char*)name, (char*)inst, (char*)realm, 0);
51         if (ret == KSUCCESS)
52             ret = krb_get_cred((char*)name, (char*)inst, (char*)realm, c);
53     }
54     return ret;
55 }
56
57 static int
58 afslog_uid_int(kafs_data *data,
59                const char *cell,
60                const char *realm_hint,
61                uid_t uid,
62                const char *homedir)
63 {
64     int ret;
65     CREDENTIALS c;
66     char realm[REALM_SZ];
67     
68     if (cell == 0 || cell[0] == 0)
69         return _kafs_afslog_all_local_cells (data, uid, homedir);
70
71     /* Extract realm from ticket file. */
72     ret = krb_get_tf_fullname(tkt_string(), NULL, NULL, realm);
73     if (ret != KSUCCESS)
74         return ret;
75
76     ret = _kafs_get_cred(data, cell, realm_hint, realm, &c);
77     
78     if (ret == 0)
79         ret = kafs_settoken(cell, uid, &c);
80     return ret;
81 }
82
83 static char *
84 get_realm(kafs_data *data, const char *host)
85 {
86     char *r = krb_realmofhost(host);
87     if(r != NULL)
88         return strdup(r);
89     else
90         return NULL;
91 }
92
93 int
94 krb_afslog_uid_home(const char *cell, const char *realm_hint, uid_t uid,
95                     const char *homedir)
96 {
97     kafs_data kd;
98
99     kd.afslog_uid = afslog_uid_int;
100     kd.get_cred = get_cred;
101     kd.get_realm = get_realm;
102     kd.data = 0;
103     return afslog_uid_int(&kd, cell, realm_hint, uid, homedir);
104 }
105
106 int
107 krb_afslog_uid(const char *cell, const char *realm_hint, uid_t uid)
108 {
109     return krb_afslog_uid_home(cell, realm_hint, uid, NULL);
110 }
111
112 int
113 krb_afslog(const char *cell, const char *realm_hint)
114 {
115     return krb_afslog_uid(cell, realm_hint, getuid());
116 }
117
118 int
119 krb_afslog_home(const char *cell, const char *realm_hint, const char *homedir)
120 {
121     return krb_afslog_uid_home(cell, realm_hint, getuid(), homedir);
122 }
123
124 /*
125  *
126  */
127
128 int
129 krb_realm_of_cell(const char *cell, char **realm)
130 {
131     kafs_data kd;
132
133     kd.get_realm = get_realm;
134     return _kafs_realm_of_cell(&kd, cell, realm);
135 }