2 * Copyright (c) 1995-2000, 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
36 RCSID("$Id: verify.c,v 1.25.12.1 2004/09/08 09:14:26 joda Exp $");
39 #include <sys/types.h>
51 static char krb5ccname
[128];
54 static char krbtkfile
[128];
58 In some cases is afs_gettktstring called twice (once before
59 afs_verify and once after afs_verify).
60 In some cases (rlogin with access allowed via .rhosts)
61 afs_verify is not called!
62 So we can't rely on correct value in krbtkfile in some
66 static int correct_tkfilename
=0;
71 set_krbtkfile(uid_t uid
)
73 snprintf (krbtkfile
, sizeof(krbtkfile
), "%s%d", TKT_ROOT
, (unsigned)uid
);
74 krb_set_tkt_string (krbtkfile
);
75 correct_tkfilename
= 1;
79 /* XXX this has to be the default cache name, since the KRB5CCNAME
80 * environment variable isn't exported by login/xdm
85 set_krb5ccname(uid_t uid
)
87 snprintf (krb5ccname
, sizeof(krb5ccname
), "FILE:/tmp/krb5cc_%d", uid
);
89 snprintf (krbtkfile
, sizeof(krbtkfile
), "%s%d", TKT_ROOT
, (unsigned)uid
);
91 correct_tkfilename
= 1;
96 set_spec_krbtkfile(void)
100 snprintf (krbtkfile
, sizeof(krbtkfile
), "%s_XXXXXX", TKT_ROOT
);
101 fd
= mkstemp(krbtkfile
);
104 krb_set_tkt_string (krbtkfile
);
107 snprintf(krb5ccname
, sizeof(krb5ccname
),"FILE:/tmp/krb5cc_XXXXXX");
108 fd
=mkstemp(krb5ccname
+5);
110 unlink(krb5ccname
+5);
116 verify_krb5(struct passwd
*pwd
,
121 krb5_context context
;
124 krb5_principal principal
;
126 ret
= krb5_init_context(&context
);
128 syslog(LOG_AUTH
|LOG_DEBUG
, "krb5_init_context failed: %d", ret
);
132 ret
= krb5_parse_name (context
, pwd
->pw_name
, &principal
);
134 syslog(LOG_AUTH
|LOG_DEBUG
, "krb5_parse_name: %s",
135 krb5_get_err_text(context
, ret
));
139 set_krb5ccname(pwd
->pw_uid
);
140 ret
= krb5_cc_resolve(context
, krb5ccname
, &ccache
);
142 syslog(LOG_AUTH
|LOG_DEBUG
, "krb5_cc_resolve: %s",
143 krb5_get_err_text(context
, ret
));
147 ret
= krb5_verify_user_lrealm(context
,
154 syslog(LOG_AUTH
|LOG_DEBUG
, "krb5_verify_user: %s",
155 krb5_get_err_text(context
, ret
));
159 if(chown(krb5_cc_get_name(context
, ccache
), pwd
->pw_uid
, pwd
->pw_gid
)) {
160 syslog(LOG_AUTH
|LOG_DEBUG
, "chown: %s",
161 krb5_get_err_text(context
, errno
));
167 krb5_realm realm
= NULL
;
168 krb5_boolean get_v4_tgt
;
170 krb5_get_default_realm(context
, &realm
);
171 krb5_appdefault_boolean(context
, "afskauthlib",
173 "krb4_get_tickets", FALSE
, &get_v4_tgt
);
176 krb5_creds mcred
, cred
;
178 krb5_make_principal(context
, &mcred
.server
, realm
,
182 ret
= krb5_cc_retrieve_cred(context
, ccache
, 0, &mcred
, &cred
);
184 ret
= krb524_convert_creds_kdc_ccache(context
, ccache
, &cred
, &c
);
186 krb5_warn(context
, ret
, "converting creds");
188 set_krbtkfile(pwd
->pw_uid
);
189 tf_setup(&c
, c
.pname
, c
.pinst
);
191 memset(&c
, 0, sizeof(c
));
192 krb5_free_creds_contents(context
, &cred
);
194 syslog(LOG_AUTH
|LOG_DEBUG
, "krb5_cc_retrieve_cred: %s",
195 krb5_get_err_text(context
, ret
));
197 krb5_free_principal(context
, mcred
.server
);
200 if (!pag_set
&& k_hasafs()) {
206 krb5_afslog_uid_home(context
, ccache
, NULL
, NULL
,
207 pwd
->pw_uid
, pwd
->pw_dir
);
212 printf ("%s\n", krb5_get_err_text (context
, ret
));
219 verify_krb4(struct passwd
*pwd
,
225 char lrealm
[REALM_SZ
];
227 if (krb_get_lrealm (lrealm
, 1) != KFAILURE
) {
228 set_krbtkfile(pwd
->pw_uid
);
229 ret
= krb_verify_user (pwd
->pw_name
, "", lrealm
, password
,
230 KRB_VERIFY_SECURE
, NULL
);
231 if (ret
== KSUCCESS
) {
232 if (!pag_set
&& k_hasafs()) {
237 krb_afslog_uid_home (0, 0, pwd
->pw_uid
, pwd
->pw_dir
);
239 printf ("%s\n", krb_get_err_text (ret
));
246 afs_verify(char *name
,
252 struct passwd
*pwd
= k_getpwnam (name
);
257 if (!pag_set
&& k_hasafs()) {
263 ret
= unix_verify_user (name
, password
);
266 ret
= verify_krb5(pwd
, password
, exp
, quiet
);
270 ret
= verify_krb4(pwd
, password
, exp
, quiet
);
276 afs_gettktstring (void)
281 if (!correct_tkfilename
) {
282 ptr
= getenv("LOGNAME");
283 if (ptr
!= NULL
&& ((pwd
= getpwnam(ptr
)) != NULL
)) {
284 set_krb5ccname(pwd
->pw_uid
);
286 set_krbtkfile(pwd
->pw_uid
);
287 if (!pag_set
&& k_hasafs()) {
293 set_spec_krbtkfile();
297 esetenv("KRB5CCNAME",krb5ccname
,1);
300 esetenv("KRBTKFILE",krbtkfile
,1);