File: public.c

package info (click to toggle)
libpam-afs-session 2.5-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,300 kB
  • ctags: 662
  • sloc: sh: 11,361; ansic: 6,442; makefile: 170; perl: 58
file content (231 lines) | stat: -rw-r--r-- 6,965 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * The public APIs of the pam-afs-session PAM module.
 *
 * Provides the public pam_sm_setcred, pam_sm_open_session, and
 * pam_sm_close_session functions, plus whatever other stubs we need to
 * satisfy PAM.
 *
 * Written by Russ Allbery <rra@stanford.edu>
 * Copyright 2006, 2007, 2008, 2010
 *     The Board of Trustees of the Leland Stanford Junior University
 *
 * See LICENSE for licensing terms.
 */

#include <config.h>
#include <portable/kafs.h>
#include <portable/pam.h>
#include <portable/system.h>

#include <errno.h>

#include <internal.h>
#include <pam-util/args.h>
#include <pam-util/logging.h>


/*
 * Open a new session.  Create a new PAG with k_setpag and then fork the aklog
 * binary as the user.  A Kerberos v5 PAM module should have previously run to
 * obtain Kerberos tickets (or ticket forwarding should have already
 * happened).
 */
int
pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
                    const char *argv[])
{
    struct pam_args *args;
    int pamret = PAM_SUCCESS;
    const void *dummy;

    args = pamafs_init(pamh, flags, argc, argv);
    if (args == NULL) {
        pamret = PAM_SESSION_ERR;
        goto done;
    }
    ENTRY(args, flags);

    /* Do nothing unless AFS is available. */
    if (!k_hasafs()) {
        putil_err(args, "skipping, AFS apparently not available");
        pamret = PAM_IGNORE;
        goto done;
    }

    /*
     * Unless nopag is set or we've already created a PAG, always create a
     * PAG.  Do this even if we're otherwise ignoring the user.
     */
    if (pam_get_data(pamh, "pam_afs_session", &dummy) == PAM_SUCCESS) {
        if (!k_haspag() && !args->config->nopag)
            putil_notice(args, "PAG apparently lost, recreating");
        else {
            putil_debug(args, "skipping, apparently already ran");
            pamret = PAM_SUCCESS;
            goto done;
        }
    }
    if (!args->config->nopag && k_setpag() != 0) {
        putil_err(args, "PAG creation failed: %s", strerror(errno));
        pamret = PAM_SESSION_ERR;
        goto done;
    }

    /* Get tokens. */
    if (!args->config->notokens)
        pamret = pamafs_token_get(args);

    /* Error codes are returned for pam_setcred.  Map to pam_open_sesssion. */
    if (pamret != PAM_SUCCESS && pamret != PAM_IGNORE)
        pamret = PAM_SESSION_ERR;

done:
    EXIT(args, pamret);
    pamafs_free(args);
    return pamret;
}


/*
 * Don't do anything for authenticate.  We're only an auth module so that we
 * can supply a pam_setcred implementation.
 */
int
pam_sm_authenticate(pam_handle_t *pamh UNUSED, int flags UNUSED,
                    int argc UNUSED, const char *argv[] UNUSED)
{
    /*
     * We want to return PAM_IGNORE here, but Linux PAM 0.99.7.1 (at least)
     * has a bug that causes PAM_IGNORE to result in authentication failure
     * when the module is marked [default=done].  So we return PAM_SUCCESS,
     * which is dangerous but works in that case.
     */
    return PAM_SUCCESS;
}


/*
 * Calling pam_setcred with PAM_ESTABLISH_CRED is equivalent to opening a new
 * session for our purposes.  With PAM_REFRESH_CRED, we don't call setpag,
 * just run aklog again.  PAM_DELETE_CRED calls unlog.
 */
int 
pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
               const char *argv[])
{
    struct pam_args *args;
    int status;
    int pamret = PAM_SUCCESS;
    const void *dummy;

    args = pamafs_init(pamh, flags, argc, argv);
    if (args == NULL) {
        pamret = PAM_CRED_ERR;
        goto done;
    }
    ENTRY(args, flags);

    /*
     * Do nothing unless AFS is available.  We need to return success here
     * rather than PAM_IGNORE (which would be the more correct return status)
     * since PAM_IGNORE can confuse the Linux PAM library, at least for
     * applications that call pam_setcred without pam_authenticate (possibly
     * because authentication was done some other way), when used with jumps
     * with the [] syntax.  Since we do nothing in this case, and since the
     * stack is already frozen from the auth group, success makes sense.
     */
    if (!k_hasafs()) {
        putil_err(args, "skipping, AFS apparently not available");
        pamret = PAM_SUCCESS;
        goto done;
    }

    /*
     * If DELETE_CRED was specified, delete the tokens (if any).  Similarly
     * return PAM_SUCCESS here instead of PAM_IGNORE.  Map the error code for
     * pam_setcred, since normally this call is made by pam_close_session.
     */
    if (flags & PAM_DELETE_CRED) {
        if (args->config->retain_after_close || args->config->notokens) {
            pamret = PAM_SUCCESS;
            putil_debug(args, "skipping as configured");
        } else {
            pamret = pamafs_token_delete(args);
            if (pamret == PAM_SESSION_ERR)
                pamret = PAM_CRED_ERR;
        }
        goto done;
    }

    /*
     * We're acquiring tokens.  See if we already have done this and don't do
     * it again if we have unless we were explicitly told to reinitialize.  If
     * we're reinitializing, we may be running in a screen saver or the like
     * and should use the existing PAG, so don't create a new PAG.
     */
    if (!(flags & (PAM_REINITIALIZE_CRED | PAM_REFRESH_CRED))) {
        status = pam_get_data(pamh, "pam_afs_session", &dummy);
        if (status == PAM_SUCCESS) {
            if (!k_haspag() && !args->config->nopag)
                putil_notice(args, "PAG apparently lost, recreating");
            else {
                putil_debug(args, "skipping, apparently already ran");
                goto done;
            }
        }
        if (!args->config->nopag && k_setpag() != 0) {
            putil_err(args, "PAG creation failed: %s", strerror(errno));
            pamret = PAM_CRED_ERR;
            goto done;
        }
    }
    if (!args->config->notokens)
        pamret = pamafs_token_get(args);

done:
    EXIT(args, pamret);
    pamafs_free(args);
    return pamret;
}


/*
 * Close a session.  Normally, what we do here is call unlog, but we can be
 * configured not to do so.
 */
int
pam_sm_close_session(pam_handle_t *pamh, int flags, int argc,
                     const char *argv[])
{
    struct pam_args *args;
    int pamret = PAM_SUCCESS;

    args = pamafs_init(pamh, flags, argc, argv);
    if (args == NULL) {
        pamret = PAM_SESSION_ERR;
        goto done;
    }
    ENTRY(args, flags);

    /* Do nothing if so configured. */
    if (args->config->retain_after_close || args->config->notokens) {
        pamret = PAM_IGNORE;
        putil_debug(args, "skipping as configured");
        goto done;
    }

    /* Do nothing unless AFS is available. */
    if (!k_hasafs()) {
        pamret = PAM_IGNORE;
        putil_err(args, "skipping, AFS apparently not available");
        goto done;
    }

    /* Delete tokens. */
    pamret = pamafs_token_delete(args);

done:
    EXIT(args, pamret);
    pamafs_free(args);
    return pamret;
}