pam_afslog.c revision 1.2 1 1.2 christos /* $NetBSD: pam_afslog.c,v 1.2 2006/01/20 16:51:15 christos Exp $ */
2 1.1 tsarna
3 1.1 tsarna /*-
4 1.1 tsarna * Copyright 2005 Tyler C. Sarna <tsarna (at) netbsd.org>
5 1.1 tsarna *
6 1.1 tsarna * This code is derived from software contributed to The NetBSD Foundation
7 1.1 tsarna * by Tyler C. Sarna
8 1.1 tsarna *
9 1.1 tsarna * Redistribution and use in source and binary forms, with or without
10 1.1 tsarna * modification, are permitted provided that the following conditions
11 1.1 tsarna * are met:
12 1.1 tsarna * 1. Redistributions of source code must retain the above copyright
13 1.1 tsarna * notice, this list of conditions and the following disclaimer.
14 1.1 tsarna * 2. Neither the name of The NetBSD Foundation nor the names of its
15 1.1 tsarna * contributors may be used to endorse or promote products derived
16 1.1 tsarna * from this software without specific prior written permission.
17 1.1 tsarna *
18 1.1 tsarna * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1 tsarna * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1 tsarna * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1 tsarna * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1 tsarna * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1 tsarna * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1 tsarna * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1 tsarna * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1 tsarna * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1 tsarna * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1 tsarna * POSSIBILITY OF SUCH DAMAGE.
29 1.1 tsarna */
30 1.1 tsarna
31 1.1 tsarna #include <sys/cdefs.h>
32 1.1 tsarna
33 1.2 christos __RCSID("$NetBSD: pam_afslog.c,v 1.2 2006/01/20 16:51:15 christos Exp $");
34 1.1 tsarna
35 1.1 tsarna #include <krb5/krb5.h>
36 1.1 tsarna #include <krb5/kafs.h>
37 1.1 tsarna
38 1.2 christos #define PAM_SM_AUTH
39 1.2 christos #define PAM_SM_CRED
40 1.1 tsarna #include <security/pam_appl.h>
41 1.1 tsarna #include <security/pam_modules.h>
42 1.1 tsarna #include <security/pam_mod_misc.h>
43 1.1 tsarna
44 1.1 tsarna PAM_EXTERN int
45 1.1 tsarna pam_sm_authenticate(pam_handle_t *pamh, int flags __unused,
46 1.1 tsarna int argc __unused, const char *argv[] __unused)
47 1.1 tsarna {
48 1.1 tsarna return PAM_IGNORE;
49 1.1 tsarna }
50 1.1 tsarna
51 1.1 tsarna PAM_EXTERN int
52 1.1 tsarna pam_sm_setcred(pam_handle_t *pamh, int flags,
53 1.1 tsarna int argc __unused, const char *argv[] __unused)
54 1.1 tsarna {
55 1.1 tsarna krb5_context ctx;
56 1.1 tsarna krb5_ccache ccache;
57 1.1 tsarna krb5_principal principal;
58 1.1 tsarna krb5_error_code kret;
59 1.1 tsarna const void *service = NULL;
60 1.1 tsarna const char *ccname = NULL;
61 1.1 tsarna int do_afslog = 0, ret = PAM_SUCCESS;
62 1.1 tsarna
63 1.1 tsarna pam_get_item(pamh, PAM_SERVICE, &service);
64 1.1 tsarna if (service == NULL)
65 1.1 tsarna service = "pam_afslog";
66 1.1 tsarna
67 1.1 tsarna kret = krb5_init_context(&ctx);
68 1.1 tsarna if (kret != 0) {
69 1.1 tsarna PAM_LOG("Error: krb5_init_context() failed");
70 1.1 tsarna ret = PAM_SERVICE_ERR;
71 1.1 tsarna } else {
72 1.1 tsarna ccname = pam_getenv(pamh, "KRB5CCNAME");
73 1.1 tsarna if (ccname)
74 1.1 tsarna kret = krb5_cc_resolve(ctx, ccname, &ccache);
75 1.1 tsarna else
76 1.1 tsarna kret = krb5_cc_default(ctx, &ccache);
77 1.1 tsarna if (kret != 0) {
78 1.1 tsarna PAM_LOG("Error: failed to open ccache");
79 1.1 tsarna ret = PAM_SERVICE_ERR;
80 1.1 tsarna } else {
81 1.1 tsarna kret = krb5_cc_get_principal(ctx, ccache, &principal);
82 1.1 tsarna if (kret != 0) {
83 1.1 tsarna PAM_LOG("Error: krb5_cc_get_principal() failed");
84 1.1 tsarna ret = PAM_SERVICE_ERR;
85 1.1 tsarna } else {
86 1.1 tsarna krb5_appdefault_boolean(ctx,
87 1.1 tsarna (const char *)service,
88 1.1 tsarna krb5_principal_get_realm(
89 1.1 tsarna ctx, principal),
90 1.1 tsarna "afslog", FALSE, &do_afslog);
91 1.1 tsarna
92 1.1 tsarna /* silently bail if not enabled */
93 1.1 tsarna
94 1.1 tsarna if (do_afslog && k_hasafs()) {
95 1.1 tsarna switch (flags & ~PAM_SILENT) {
96 1.1 tsarna case 0:
97 1.1 tsarna case PAM_ESTABLISH_CRED:
98 1.1 tsarna k_setpag();
99 1.1 tsarna
100 1.1 tsarna /* FALLTHROUGH */
101 1.1 tsarna
102 1.1 tsarna case PAM_REINITIALIZE_CRED:
103 1.1 tsarna case PAM_REFRESH_CRED:
104 1.1 tsarna krb5_afslog(ctx, ccache,
105 1.1 tsarna NULL, NULL);
106 1.1 tsarna break;
107 1.1 tsarna
108 1.1 tsarna case PAM_DELETE_CRED:
109 1.1 tsarna k_unlog();
110 1.1 tsarna break;
111 1.1 tsarna }
112 1.1 tsarna }
113 1.1 tsarna
114 1.1 tsarna krb5_free_principal(ctx, principal);
115 1.1 tsarna }
116 1.1 tsarna
117 1.1 tsarna krb5_cc_close(ctx, ccache);
118 1.1 tsarna }
119 1.1 tsarna
120 1.1 tsarna krb5_free_context(ctx);
121 1.1 tsarna }
122 1.1 tsarna
123 1.1 tsarna return ret;
124 1.1 tsarna }
125 1.1 tsarna
126 1.1 tsarna PAM_MODULE_ENTRY("pam_afslog");
127