1*84d9c625SLionel Sambuc /* $NetBSD: k5login.c,v 1.33 2012/04/24 16:52:26 christos Exp $ */
2a2d13726SBen Gras
3a2d13726SBen Gras /*-
4a2d13726SBen Gras * Copyright (c) 1990 The Regents of the University of California.
5a2d13726SBen Gras * All rights reserved.
6a2d13726SBen Gras *
7a2d13726SBen Gras * Redistribution and use in source and binary forms, with or without
8a2d13726SBen Gras * modification, are permitted provided that the following conditions
9a2d13726SBen Gras * are met:
10a2d13726SBen Gras * 1. Redistributions of source code must retain the above copyright
11a2d13726SBen Gras * notice, this list of conditions and the following disclaimer.
12a2d13726SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13a2d13726SBen Gras * notice, this list of conditions and the following disclaimer in the
14a2d13726SBen Gras * documentation and/or other materials provided with the distribution.
15a2d13726SBen Gras * 3. Neither the name of the University nor the names of its contributors
16a2d13726SBen Gras * may be used to endorse or promote products derived from this software
17a2d13726SBen Gras * without specific prior written permission.
18a2d13726SBen Gras *
19a2d13726SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20a2d13726SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a2d13726SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a2d13726SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23a2d13726SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a2d13726SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a2d13726SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a2d13726SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a2d13726SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a2d13726SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a2d13726SBen Gras * SUCH DAMAGE.
30a2d13726SBen Gras */
31a2d13726SBen Gras
32a2d13726SBen Gras /*
33a2d13726SBen Gras * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
34a2d13726SBen Gras * All rights reserved.
35a2d13726SBen Gras *
36a2d13726SBen Gras * Redistribution and use in source and binary forms are permitted
37a2d13726SBen Gras * provided that the above copyright notice and this paragraph are
38a2d13726SBen Gras * duplicated in all such forms and that any documentation,
39a2d13726SBen Gras * advertising materials, and other materials related to such
40a2d13726SBen Gras * distribution and use acknowledge that the software was developed
41a2d13726SBen Gras * by the University of California, Berkeley. The name of the
42a2d13726SBen Gras * University may not be used to endorse or promote products derived
43a2d13726SBen Gras * from this software without specific prior written permission.
44a2d13726SBen Gras * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
45a2d13726SBen Gras * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
46a2d13726SBen Gras * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
47a2d13726SBen Gras */
48a2d13726SBen Gras
49a2d13726SBen Gras #include <sys/cdefs.h>
50a2d13726SBen Gras #ifndef lint
51a2d13726SBen Gras #if 0
52a2d13726SBen Gras static char sccsid[] = "@(#)klogin.c 5.11 (Berkeley) 7/12/92";
53a2d13726SBen Gras #endif
54*84d9c625SLionel Sambuc __RCSID("$NetBSD: k5login.c,v 1.33 2012/04/24 16:52:26 christos Exp $");
55a2d13726SBen Gras #endif /* not lint */
56a2d13726SBen Gras
57a2d13726SBen Gras #ifdef KERBEROS5
58a2d13726SBen Gras #include <sys/param.h>
59a2d13726SBen Gras #include <sys/syslog.h>
60a2d13726SBen Gras #include <krb5/krb5.h>
61a2d13726SBen Gras #include <pwd.h>
62a2d13726SBen Gras #include <netdb.h>
63a2d13726SBen Gras #include <stdio.h>
64a2d13726SBen Gras #include <stdlib.h>
65a2d13726SBen Gras #include <string.h>
66a2d13726SBen Gras #include <unistd.h>
67a2d13726SBen Gras #include <errno.h>
68a2d13726SBen Gras
69a2d13726SBen Gras #define KRB5_DEFAULT_OPTIONS 0
70a2d13726SBen Gras #define KRB5_DEFAULT_LIFE 60*60*10 /* 10 hours */
71a2d13726SBen Gras
72a2d13726SBen Gras krb5_context kcontext;
73a2d13726SBen Gras
74*84d9c625SLionel Sambuc extern int notickets;
75a2d13726SBen Gras int krb5_configured;
76a2d13726SBen Gras char *krb5tkfile_env;
77a2d13726SBen Gras extern char *tty;
78a2d13726SBen Gras extern int login_krb5_forwardable_tgt;
79a2d13726SBen Gras extern int has_ccache;
80a2d13726SBen Gras
81a2d13726SBen Gras static char tkt_location[MAXPATHLEN];
82a2d13726SBen Gras static krb5_creds forw_creds;
83a2d13726SBen Gras int have_forward;
84*84d9c625SLionel Sambuc static krb5_principal me;
85a2d13726SBen Gras
86a2d13726SBen Gras int k5_read_creds(char *);
87a2d13726SBen Gras int k5_write_creds(void);
88a2d13726SBen Gras int k5_verify_creds(krb5_context, krb5_ccache);
89a2d13726SBen Gras int k5login(struct passwd *, char *, char *, char *);
90a2d13726SBen Gras void k5destroy(void);
91a2d13726SBen Gras
92*84d9c625SLionel Sambuc static void __printflike(3, 4)
k5_log(krb5_context context,krb5_error_code kerror,const char * fmt,...)93*84d9c625SLionel Sambuc k5_log(krb5_context context, krb5_error_code kerror, const char *fmt, ...)
94*84d9c625SLionel Sambuc {
95*84d9c625SLionel Sambuc const char *msg = krb5_get_error_message(context, kerror);
96*84d9c625SLionel Sambuc char *str;
97*84d9c625SLionel Sambuc va_list ap;
98*84d9c625SLionel Sambuc
99*84d9c625SLionel Sambuc va_start(ap, fmt);
100*84d9c625SLionel Sambuc if (vasprintf(&str, fmt, ap) == -1) {
101*84d9c625SLionel Sambuc va_end(ap);
102*84d9c625SLionel Sambuc syslog(LOG_NOTICE, "Cannot allocate memory for error %s: %s",
103*84d9c625SLionel Sambuc fmt, msg);
104*84d9c625SLionel Sambuc return;
105*84d9c625SLionel Sambuc }
106*84d9c625SLionel Sambuc va_end(ap);
107*84d9c625SLionel Sambuc
108*84d9c625SLionel Sambuc syslog(LOG_NOTICE, "warning: %s: %s", str, msg);
109*84d9c625SLionel Sambuc krb5_free_error_message(kcontext, msg);
110*84d9c625SLionel Sambuc free(str);
111*84d9c625SLionel Sambuc }
112a2d13726SBen Gras
113a2d13726SBen Gras /*
114a2d13726SBen Gras * Verify the Kerberos ticket-granting ticket just retrieved for the
115a2d13726SBen Gras * user. If the Kerberos server doesn't respond, assume the user is
116a2d13726SBen Gras * trying to fake us out (since we DID just get a TGT from what is
117a2d13726SBen Gras * supposedly our KDC). If the host/<host> service is unknown (i.e.,
118a2d13726SBen Gras * the local keytab doesn't have it), let her in.
119a2d13726SBen Gras *
120a2d13726SBen Gras * Returns 1 for confirmation, -1 for failure, 0 for uncertainty.
121a2d13726SBen Gras */
122a2d13726SBen Gras int
k5_verify_creds(krb5_context c,krb5_ccache ccache)123a2d13726SBen Gras k5_verify_creds(krb5_context c, krb5_ccache ccache)
124a2d13726SBen Gras {
125a2d13726SBen Gras char phost[MAXHOSTNAMELEN];
126a2d13726SBen Gras int retval, have_keys;
127a2d13726SBen Gras krb5_principal princ;
128a2d13726SBen Gras krb5_keyblock *kb = 0;
129a2d13726SBen Gras krb5_error_code kerror;
130a2d13726SBen Gras krb5_data packet;
131a2d13726SBen Gras krb5_auth_context auth_context = NULL;
132a2d13726SBen Gras krb5_ticket *ticket = NULL;
133a2d13726SBen Gras
134a2d13726SBen Gras kerror = krb5_sname_to_principal(c, 0, 0, KRB5_NT_SRV_HST, &princ);
135a2d13726SBen Gras if (kerror) {
136a2d13726SBen Gras krb5_warn(kcontext, kerror, "constructing local service name");
137a2d13726SBen Gras return (-1);
138a2d13726SBen Gras }
139a2d13726SBen Gras
140a2d13726SBen Gras /* Do we have host/<host> keys? */
141a2d13726SBen Gras /* (use default keytab, kvno IGNORE_VNO to get the first match,
142a2d13726SBen Gras * and default enctype.) */
143a2d13726SBen Gras kerror = krb5_kt_read_service_key(c, NULL, princ, 0, 0, &kb);
144a2d13726SBen Gras if (kb)
145a2d13726SBen Gras krb5_free_keyblock(c, kb);
146a2d13726SBen Gras /* any failure means we don't have keys at all. */
147a2d13726SBen Gras have_keys = kerror ? 0 : 1;
148a2d13726SBen Gras
149a2d13726SBen Gras /* XXX there should be a krb5 function like mk_req, but taking a full
150a2d13726SBen Gras * principal, instead of a service/hostname. (Did I miss one?) */
151a2d13726SBen Gras gethostname(phost, sizeof(phost));
152a2d13726SBen Gras phost[sizeof(phost) - 1] = '\0';
153a2d13726SBen Gras
154a2d13726SBen Gras /* talk to the kdc and construct the ticket */
155a2d13726SBen Gras kerror = krb5_mk_req(c, &auth_context, 0, "host", phost,
156a2d13726SBen Gras 0, ccache, &packet);
157a2d13726SBen Gras /* wipe the auth context for rd_req */
158a2d13726SBen Gras if (auth_context) {
159a2d13726SBen Gras krb5_auth_con_free(c, auth_context);
160a2d13726SBen Gras auth_context = NULL;
161a2d13726SBen Gras }
162a2d13726SBen Gras if (kerror == KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN) {
163a2d13726SBen Gras /* we have a service key, so something should be
164a2d13726SBen Gras * in the database, therefore this error packet could
165a2d13726SBen Gras * have come from an attacker. */
166a2d13726SBen Gras if (have_keys) {
167a2d13726SBen Gras retval = -1;
168a2d13726SBen Gras goto EGRESS;
169a2d13726SBen Gras }
170a2d13726SBen Gras /* but if it is unknown and we've got no key, we don't
171a2d13726SBen Gras * have any security anyhow, so it is ok. */
172a2d13726SBen Gras else {
173a2d13726SBen Gras retval = 0;
174a2d13726SBen Gras goto EGRESS;
175a2d13726SBen Gras }
176a2d13726SBen Gras }
177a2d13726SBen Gras else if (kerror) {
178a2d13726SBen Gras krb5_warn(kcontext, kerror,
179a2d13726SBen Gras "Unable to verify Kerberos V5 TGT: %s", phost);
180*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "Kerberos V5 TGT bad");
181a2d13726SBen Gras retval = -1;
182a2d13726SBen Gras goto EGRESS;
183a2d13726SBen Gras }
184a2d13726SBen Gras /* got ticket, try to use it */
185a2d13726SBen Gras kerror = krb5_rd_req(c, &auth_context, &packet,
186a2d13726SBen Gras princ, NULL, NULL, &ticket);
187a2d13726SBen Gras if (kerror) {
188a2d13726SBen Gras if (!have_keys) {
189a2d13726SBen Gras /* The krb5 errors aren't specified well, but I think
190a2d13726SBen Gras * these values cover the cases we expect. */
191a2d13726SBen Gras switch (kerror) {
192a2d13726SBen Gras case ENOENT: /* no keytab */
193a2d13726SBen Gras case KRB5_KT_NOTFOUND:
194a2d13726SBen Gras retval = 0;
195a2d13726SBen Gras break;
196a2d13726SBen Gras default:
197a2d13726SBen Gras /* unexpected error: fail */
198a2d13726SBen Gras retval = -1;
199a2d13726SBen Gras break;
200a2d13726SBen Gras }
201a2d13726SBen Gras }
202a2d13726SBen Gras else {
203a2d13726SBen Gras /* we have keys, so if we got any error, we could be
204a2d13726SBen Gras * under attack. */
205a2d13726SBen Gras retval = -1;
206a2d13726SBen Gras }
207a2d13726SBen Gras krb5_warn(kcontext, kerror, "Unable to verify host ticket");
208*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "can't verify v5 ticket (%s)",
209*84d9c625SLionel Sambuc retval ? "keytab found, assuming failure" :
210*84d9c625SLionel Sambuc "no keytab found, assuming success");
211a2d13726SBen Gras goto EGRESS;
212a2d13726SBen Gras }
213a2d13726SBen Gras /*
214a2d13726SBen Gras * The host/<host> ticket has been received _and_ verified.
215a2d13726SBen Gras */
216a2d13726SBen Gras retval = 1;
217a2d13726SBen Gras
218a2d13726SBen Gras /* do cleanup and return */
219a2d13726SBen Gras EGRESS:
220a2d13726SBen Gras if (auth_context)
221a2d13726SBen Gras krb5_auth_con_free(c, auth_context);
222a2d13726SBen Gras krb5_free_principal(c, princ);
223a2d13726SBen Gras /* possibly ticket and packet need freeing here as well */
224a2d13726SBen Gras return (retval);
225a2d13726SBen Gras }
226a2d13726SBen Gras
227a2d13726SBen Gras /*
228a2d13726SBen Gras * Attempt to read forwarded kerberos creds
229a2d13726SBen Gras *
230a2d13726SBen Gras * return 0 on success (forwarded creds in memory)
231a2d13726SBen Gras * 1 if no forwarded creds.
232a2d13726SBen Gras */
233a2d13726SBen Gras int
k5_read_creds(char * username)234a2d13726SBen Gras k5_read_creds(char *username)
235a2d13726SBen Gras {
236a2d13726SBen Gras krb5_error_code kerror;
237a2d13726SBen Gras krb5_creds mcreds;
238a2d13726SBen Gras krb5_ccache ccache;
239a2d13726SBen Gras
240a2d13726SBen Gras have_forward = 0;
241a2d13726SBen Gras memset((char*) &mcreds, 0, sizeof(forw_creds));
242a2d13726SBen Gras memset((char*) &forw_creds, 0, sizeof(forw_creds));
243a2d13726SBen Gras
244a2d13726SBen Gras kerror = krb5_cc_default(kcontext, &ccache);
245a2d13726SBen Gras if (kerror) {
246a2d13726SBen Gras krb5_warn(kcontext, kerror, "while getting default ccache");
247a2d13726SBen Gras return(1);
248a2d13726SBen Gras }
249a2d13726SBen Gras
250a2d13726SBen Gras kerror = krb5_parse_name(kcontext, username, &me);
251a2d13726SBen Gras if (kerror) {
252a2d13726SBen Gras krb5_warn(kcontext, kerror, "when parsing name %s", username);
253a2d13726SBen Gras return(1);
254a2d13726SBen Gras }
255a2d13726SBen Gras
256a2d13726SBen Gras mcreds.client = me;
257*84d9c625SLionel Sambuc const char *realm = krb5_principal_get_realm(kcontext, me);
258*84d9c625SLionel Sambuc size_t rlen = strlen(realm);
259a2d13726SBen Gras kerror = krb5_build_principal_ext(kcontext, &mcreds.server,
260*84d9c625SLionel Sambuc rlen, realm,
261a2d13726SBen Gras KRB5_TGS_NAME_SIZE,
262a2d13726SBen Gras KRB5_TGS_NAME,
263*84d9c625SLionel Sambuc rlen, realm,
264a2d13726SBen Gras 0);
265a2d13726SBen Gras if (kerror) {
266a2d13726SBen Gras krb5_warn(kcontext, kerror, "while building server name");
267a2d13726SBen Gras goto nuke_ccache;
268a2d13726SBen Gras }
269a2d13726SBen Gras
270a2d13726SBen Gras kerror = krb5_cc_retrieve_cred(kcontext, ccache, 0,
271a2d13726SBen Gras &mcreds, &forw_creds);
272a2d13726SBen Gras if (kerror) {
273a2d13726SBen Gras krb5_warn(kcontext, kerror,
274a2d13726SBen Gras "while retrieving V5 initial ticket for copy");
275a2d13726SBen Gras goto nuke_ccache;
276a2d13726SBen Gras }
277a2d13726SBen Gras
278a2d13726SBen Gras have_forward = 1;
279a2d13726SBen Gras
280a2d13726SBen Gras strlcpy(tkt_location, getenv("KRB5CCNAME"), sizeof(tkt_location));
281a2d13726SBen Gras krb5tkfile_env = tkt_location;
282a2d13726SBen Gras has_ccache = 1;
283a2d13726SBen Gras notickets = 0;
284a2d13726SBen Gras
285a2d13726SBen Gras nuke_ccache:
286a2d13726SBen Gras krb5_cc_destroy(kcontext, ccache);
287a2d13726SBen Gras return(!have_forward);
288a2d13726SBen Gras }
289a2d13726SBen Gras
290a2d13726SBen Gras int
k5_write_creds(void)291a2d13726SBen Gras k5_write_creds(void)
292a2d13726SBen Gras {
293a2d13726SBen Gras krb5_error_code kerror;
294a2d13726SBen Gras krb5_ccache ccache;
295a2d13726SBen Gras
296a2d13726SBen Gras if (!have_forward)
297a2d13726SBen Gras return (1);
298a2d13726SBen Gras
299a2d13726SBen Gras kerror = krb5_cc_default(kcontext, &ccache);
300a2d13726SBen Gras if (kerror) {
301a2d13726SBen Gras krb5_warn(kcontext, kerror, "while getting default ccache");
302a2d13726SBen Gras return (1);
303a2d13726SBen Gras }
304a2d13726SBen Gras
305a2d13726SBen Gras kerror = krb5_cc_initialize(kcontext, ccache, me);
306a2d13726SBen Gras if (kerror) {
307a2d13726SBen Gras krb5_warn(kcontext, kerror,
308a2d13726SBen Gras "while re-initializing V5 ccache as user");
309a2d13726SBen Gras goto nuke_ccache_contents;
310a2d13726SBen Gras }
311a2d13726SBen Gras
312a2d13726SBen Gras kerror = krb5_cc_store_cred(kcontext, ccache, &forw_creds);
313a2d13726SBen Gras if (kerror) {
314a2d13726SBen Gras krb5_warn(kcontext, kerror,
315a2d13726SBen Gras "while re-storing V5 ccache as user");
316a2d13726SBen Gras goto nuke_ccache_contents;
317a2d13726SBen Gras }
318a2d13726SBen Gras
319a2d13726SBen Gras nuke_ccache_contents:
320a2d13726SBen Gras krb5_free_cred_contents(kcontext, &forw_creds);
321a2d13726SBen Gras return (kerror != 0);
322a2d13726SBen Gras }
323a2d13726SBen Gras
324a2d13726SBen Gras /*
325a2d13726SBen Gras * Attempt to log the user in using Kerberos authentication
326a2d13726SBen Gras *
327a2d13726SBen Gras * return 0 on success (will be logged in)
328a2d13726SBen Gras * 1 if Kerberos failed (try local password in login)
329a2d13726SBen Gras */
330a2d13726SBen Gras int
k5login(struct passwd * pw,char * instance,char * localhost,char * password)331a2d13726SBen Gras k5login(struct passwd *pw, char *instance, char *localhost, char *password)
332a2d13726SBen Gras {
333a2d13726SBen Gras krb5_error_code kerror;
334a2d13726SBen Gras krb5_creds my_creds;
335a2d13726SBen Gras krb5_ccache ccache = NULL;
336a2d13726SBen Gras char *realm, *client_name;
337a2d13726SBen Gras char *principal;
338a2d13726SBen Gras
339a2d13726SBen Gras krb5_configured = 1;
340a2d13726SBen Gras
341a2d13726SBen Gras
342a2d13726SBen Gras /*
343a2d13726SBen Gras * Root logins don't use Kerberos.
344a2d13726SBen Gras * If we have a realm, try getting a ticket-granting ticket
345a2d13726SBen Gras * and using it to authenticate. Otherwise, return
346a2d13726SBen Gras * failure so that we can try the normal passwd file
347a2d13726SBen Gras * for a password. If that's ok, log the user in
348a2d13726SBen Gras * without issuing any tickets.
349a2d13726SBen Gras */
350a2d13726SBen Gras if (strcmp(pw->pw_name, "root") == 0 ||
351a2d13726SBen Gras krb5_get_default_realm(kcontext, &realm)) {
352a2d13726SBen Gras krb5_configured = 0;
353a2d13726SBen Gras return (1);
354a2d13726SBen Gras }
355a2d13726SBen Gras
356a2d13726SBen Gras /*
357a2d13726SBen Gras * get TGT for local realm
358a2d13726SBen Gras * tickets are stored in a file named TKT_ROOT plus uid
359a2d13726SBen Gras * except for user.root tickets.
360a2d13726SBen Gras */
361a2d13726SBen Gras
362a2d13726SBen Gras if (strcmp(instance, "root") != 0)
363a2d13726SBen Gras (void)snprintf(tkt_location, sizeof tkt_location,
364*84d9c625SLionel Sambuc "FILE:/tmp/krb5cc_%d", pw->pw_uid);
365a2d13726SBen Gras else
366a2d13726SBen Gras (void)snprintf(tkt_location, sizeof tkt_location,
367*84d9c625SLionel Sambuc "FILE:/tmp/krb5cc_root_%d", pw->pw_uid);
368a2d13726SBen Gras krb5tkfile_env = tkt_location;
369a2d13726SBen Gras has_ccache = 1;
370a2d13726SBen Gras
371a2d13726SBen Gras if (strlen(instance))
372a2d13726SBen Gras asprintf(&principal, "%s/%s", pw->pw_name, instance);
373a2d13726SBen Gras else
374a2d13726SBen Gras principal = strdup(pw->pw_name);
375a2d13726SBen Gras if (!principal) {
376a2d13726SBen Gras syslog(LOG_NOTICE, "fatal: %s", strerror(errno));
377a2d13726SBen Gras return (1);
378a2d13726SBen Gras }
379a2d13726SBen Gras
380a2d13726SBen Gras if ((kerror = krb5_cc_resolve(kcontext, tkt_location, &ccache)) != 0) {
381*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "while getting default ccache");
382a2d13726SBen Gras return (1);
383a2d13726SBen Gras }
384a2d13726SBen Gras
385a2d13726SBen Gras if ((kerror = krb5_parse_name(kcontext, principal, &me)) != 0) {
386*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "when parsing name %s", principal);
387a2d13726SBen Gras return (1);
388a2d13726SBen Gras }
389a2d13726SBen Gras
390a2d13726SBen Gras if ((kerror = krb5_unparse_name(kcontext, me, &client_name)) != 0) {
391*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "when unparsing name %s", principal);
392a2d13726SBen Gras return (1);
393a2d13726SBen Gras }
394a2d13726SBen Gras
395a2d13726SBen Gras kerror = krb5_cc_initialize(kcontext, ccache, me);
396a2d13726SBen Gras if (kerror != 0) {
397*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "when initializing cache %s",
398*84d9c625SLionel Sambuc tkt_location);
399a2d13726SBen Gras return (1);
400a2d13726SBen Gras }
401a2d13726SBen Gras
402*84d9c625SLionel Sambuc memset(&my_creds, 0, sizeof(my_creds));
403*84d9c625SLionel Sambuc krb5_get_init_creds_opt *opt;
404a2d13726SBen Gras
405*84d9c625SLionel Sambuc if ((kerror = krb5_get_init_creds_opt_alloc(kcontext, &opt)) != 0) {
406*84d9c625SLionel Sambuc k5_log(kcontext, kerror, "while getting options");
407a2d13726SBen Gras return (1);
408a2d13726SBen Gras }
409*84d9c625SLionel Sambuc if (login_krb5_forwardable_tgt)
410*84d9c625SLionel Sambuc krb5_get_init_creds_opt_set_forwardable(opt, 1);
411a2d13726SBen Gras
412*84d9c625SLionel Sambuc kerror = krb5_get_init_creds_password(kcontext, &my_creds, me, password,
413*84d9c625SLionel Sambuc NULL, NULL, 0, NULL, opt);
414a2d13726SBen Gras
415*84d9c625SLionel Sambuc krb5_get_init_creds_opt_free(kcontext, opt);
416*84d9c625SLionel Sambuc if (kerror == 0)
417*84d9c625SLionel Sambuc kerror = krb5_cc_store_cred(kcontext, ccache, &my_creds);
418a2d13726SBen Gras
419a2d13726SBen Gras if (chown(&tkt_location[5], pw->pw_uid, pw->pw_gid) < 0)
420a2d13726SBen Gras syslog(LOG_ERR, "chown tkfile (%s): %m", &tkt_location[5]);
421a2d13726SBen Gras
422a2d13726SBen Gras if (kerror) {
423a2d13726SBen Gras if (kerror == KRB5KRB_AP_ERR_BAD_INTEGRITY)
424a2d13726SBen Gras printf("%s: Kerberos Password incorrect\n", principal);
425a2d13726SBen Gras else
426a2d13726SBen Gras krb5_warn(kcontext, kerror,
427a2d13726SBen Gras "while getting initial credentials");
428a2d13726SBen Gras
429a2d13726SBen Gras return (1);
430a2d13726SBen Gras }
431a2d13726SBen Gras
432a2d13726SBen Gras if (k5_verify_creds(kcontext, ccache) < 0)
433a2d13726SBen Gras return (1);
434a2d13726SBen Gras
435a2d13726SBen Gras /* Success */
436a2d13726SBen Gras notickets = 0;
437a2d13726SBen Gras return (0);
438a2d13726SBen Gras }
439a2d13726SBen Gras
440a2d13726SBen Gras /*
441a2d13726SBen Gras * Remove any credentials
442a2d13726SBen Gras */
443a2d13726SBen Gras void
k5destroy(void)444a2d13726SBen Gras k5destroy(void)
445a2d13726SBen Gras {
446a2d13726SBen Gras krb5_error_code kerror;
447a2d13726SBen Gras krb5_ccache ccache = NULL;
448a2d13726SBen Gras
449a2d13726SBen Gras if (krb5tkfile_env == NULL)
450a2d13726SBen Gras return;
451a2d13726SBen Gras
452a2d13726SBen Gras kerror = krb5_cc_resolve(kcontext, krb5tkfile_env, &ccache);
453a2d13726SBen Gras if (kerror == 0)
454a2d13726SBen Gras (void)krb5_cc_destroy(kcontext, ccache);
455a2d13726SBen Gras }
456a2d13726SBen Gras #endif /* KERBEROS5 */
457