~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/pam_ecryptfs/pam_ecryptfs.c

  • Committer: mhalcrow@us.ibm.com
  • Date: 2007-11-06 22:56:01 UTC
  • Revision ID: git-v1:f8357de9d554b274497b5cce9db4347254b7e7eb
Initial import of eCryptfs filesystem userspace utilities (mount helper, daemon component,
etc.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * pam_ecryptfs.c: PAM module that sends the user's authentication
 
3
 * tokens into the kernel keyring.
 
4
 *
 
5
 * Copyright (C) 2007 International Business Machines
 
6
 * Author(s): Michael Halcrow <mhalcrow@us.ibm.com>
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License as
 
10
 * published by the Free Software Foundation; either version 2 of the
 
11
 * License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful, but
 
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
16
 * General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 
21
 * 02111-1307, USA.
 
22
 */
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <stdint.h>
 
27
#include <stdarg.h>
 
28
#include <string.h>
 
29
#include <unistd.h>
 
30
#include <errno.h>
 
31
#include <syslog.h>
 
32
#include <pwd.h>
 
33
#include <sys/types.h>
 
34
#include <sys/wait.h>
 
35
#include <sys/types.h>
 
36
#include <sys/stat.h>
 
37
#include <fcntl.h>
 
38
#include <security/pam_modules.h>
 
39
#include "config.h"
 
40
#include "../include/ecryptfs.h"
 
41
 
 
42
static void error(const char *msg)
 
43
{
 
44
        syslog(LOG_ERR, "errno = [%i]; strerror = [%s]\n", errno,
 
45
               strerror(errno));
 
46
        switch (errno) {
 
47
        case ENOKEY:
 
48
                syslog(LOG_ERR, "%s: Requested key not available\n", msg);
 
49
                return;
 
50
 
 
51
        case EKEYEXPIRED:
 
52
                syslog(LOG_ERR, "%s: Key has expired\n", msg);
 
53
                return;
 
54
 
 
55
        case EKEYREVOKED:
 
56
                syslog(LOG_ERR, "%s: Key has been revoked\n", msg);
 
57
                return;
 
58
 
 
59
        case EKEYREJECTED:
 
60
                syslog(LOG_ERR, "%s: Key was rejected by service\n", msg);
 
61
                return;
 
62
        default:
 
63
                syslog(LOG_ERR, "%s: Unknown key error\n", msg);
 
64
                return;
 
65
        }
 
66
}
 
67
 
 
68
PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
 
69
                                   const char **argv)
 
70
{
 
71
        uid_t uid = 0;
 
72
        char *homedir = NULL;
 
73
        uid_t saved_uid = 0;
 
74
        const char *username;
 
75
        char *passphrase = NULL;
 
76
        char salt[ECRYPTFS_SALT_SIZE];
 
77
        char salt_hex[ECRYPTFS_SALT_SIZE_HEX];
 
78
        char *auth_tok_sig;
 
79
        pid_t child_pid, tmp_pid;
 
80
        long rc;
 
81
 
 
82
        syslog(LOG_INFO, "%s: Called\n", __FUNCTION__);
 
83
        rc = pam_get_user(pamh, &username, NULL);
 
84
        if (rc == PAM_SUCCESS) {
 
85
                struct passwd *pwd;
 
86
 
 
87
                syslog(LOG_INFO, "%s: username = [%s]\n", __FUNCTION__,
 
88
                       username);
 
89
                pwd = getpwnam(username);
 
90
                if (pwd) {
 
91
                        uid = pwd->pw_uid;
 
92
                        homedir = pwd->pw_dir;
 
93
                }
 
94
        } else {
 
95
                syslog(LOG_ERR, "Error getting passwd info for user [%s]; "
 
96
                       "rc = [%ld]\n", username, rc);
 
97
                goto out;
 
98
        }
 
99
        saved_uid = geteuid();
 
100
        seteuid(uid);
 
101
        rc = pam_get_item(pamh, PAM_AUTHTOK, (const void **)&passphrase);
 
102
        seteuid(saved_uid);
 
103
        if (rc != PAM_SUCCESS) {
 
104
                syslog(LOG_ERR, "Error retrieving passphrase; rc = [%d]\n",
 
105
                       rc);
 
106
                goto out;
 
107
        }
 
108
        auth_tok_sig = malloc(ECRYPTFS_SIG_SIZE_HEX + 1);
 
109
        if (!auth_tok_sig) {
 
110
                rc = -ENOMEM;
 
111
                syslog(LOG_ERR, "Out of memory\n");
 
112
                goto out;
 
113
        }
 
114
        rc = ecryptfs_read_salt_hex_from_rc(salt_hex);
 
115
        if (rc) {
 
116
                syslog(LOG_WARNING, "Unable to read salt value from user's "
 
117
                       ".ecryptfsrc file; using default\n");
 
118
                from_hex(salt, ECRYPTFS_DEFAULT_SALT_HEX, ECRYPTFS_SALT_SIZE);
 
119
        } else
 
120
                from_hex(salt, salt_hex, ECRYPTFS_SALT_SIZE);
 
121
        if ((child_pid = fork()) == 0) {
 
122
                setuid(uid);
 
123
                if (passphrase == NULL) {
 
124
                        syslog(LOG_ERR, "NULL passphrase; aborting\n");
 
125
                        rc = -EINVAL;
 
126
                        goto out_child;
 
127
                }
 
128
                if ((rc = ecryptfs_validate_keyring())) {
 
129
                        syslog(LOG_WARNING,
 
130
                               "Cannot validate keyring integrity\n");
 
131
                }
 
132
                rc = 0;
 
133
                if ((argc == 1)
 
134
                    && (memcmp(argv[0], "unwrap\0", 7) == 0)) {
 
135
                        char *wrapped_pw_filename;
 
136
                        
 
137
                        rc = asprintf(
 
138
                                &wrapped_pw_filename, "%s/.ecryptfs/%s",
 
139
                                homedir,
 
140
                                ECRYPTFS_DEFAULT_WRAPPED_PASSPHRASE_FILENAME);
 
141
                        if (rc == -1) {
 
142
                                syslog(LOG_ERR, "Unable to allocate memory\n");
 
143
                                rc = -ENOMEM;
 
144
                                goto out_child;
 
145
                        }
 
146
                        rc = ecryptfs_insert_wrapped_passphrase_into_keyring(
 
147
                                auth_tok_sig, wrapped_pw_filename, passphrase,
 
148
                                salt);
 
149
                        free(wrapped_pw_filename);
 
150
                } else {
 
151
                        rc = ecryptfs_add_passphrase_key_to_keyring(
 
152
                                auth_tok_sig, passphrase, salt);
 
153
                }
 
154
                if (rc == 1) {
 
155
                        syslog(LOG_WARNING, "There is already a key in the "
 
156
                               "user session keyring for the given "
 
157
                               "passphrase.\n");
 
158
                        rc = 0;
 
159
                }
 
160
                if (rc) {
 
161
                        syslog(LOG_ERR, "Error adding passphrase key token to "
 
162
                               "user session keyring; rc = [%d]\n", rc);
 
163
                        goto out_child;
 
164
                }
 
165
                if (fork() == 0) {
 
166
                        if ((rc = ecryptfs_set_zombie_session_placeholder())) {
 
167
                                syslog(LOG_ERR, "Error attempting to create "
 
168
                                       "and register zombie process; "
 
169
                                       "rc = [%d]\n", rc);
 
170
                        }
 
171
                }
 
172
out_child:
 
173
                free(auth_tok_sig);
 
174
                exit(0);
 
175
        }
 
176
        tmp_pid = waitpid(child_pid, NULL, 0);
 
177
        if (tmp_pid == -1)
 
178
                syslog(LOG_WARNING,
 
179
                       "waitpid() returned with error condition\n");
 
180
out:
 
181
        return PAM_SUCCESS;
 
182
}
 
183
 
 
184
PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
 
185
                              const char **argv)
 
186
{
 
187
        return PAM_SUCCESS;
 
188
}
 
189
 
 
190
PAM_EXTERN int
 
191
pam_sm_open_session(pam_handle_t *pamh, int flags,
 
192
                    int argc, const char *argv[])
 
193
{
 
194
        return PAM_SUCCESS;
 
195
}
 
196
 
 
197
PAM_EXTERN int
 
198
pam_sm_close_session(pam_handle_t *pamh, int flags,
 
199
                     int argc, const char *argv[])
 
200
{
 
201
        return PAM_SUCCESS;
 
202
}
 
203
 
 
204
PAM_EXTERN int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
 
205
                                int argc, const char **argv)
 
206
{
 
207
        uid_t uid = 0;
 
208
        char *homedir = NULL;
 
209
        uid_t saved_uid = 0;
 
210
        const char *username;
 
211
        char *old_passphrase = NULL;
 
212
        char *new_passphrase = NULL;
 
213
        char *wrapped_pw_filename;
 
214
        char salt[ECRYPTFS_SALT_SIZE];
 
215
        char salt_hex[ECRYPTFS_SALT_SIZE_HEX];
 
216
        pid_t child_pid, tmp_pid;
 
217
        int rc = PAM_SUCCESS;
 
218
 
 
219
        rc = pam_get_user(pamh, &username, NULL);
 
220
        if (rc == PAM_SUCCESS) {
 
221
                struct passwd *pwd;
 
222
 
 
223
                pwd = getpwnam(username);
 
224
                if (pwd) {
 
225
                        uid = pwd->pw_uid;
 
226
                        homedir = pwd->pw_dir;
 
227
                }
 
228
        } else {
 
229
                syslog(LOG_ERR, "Error getting passwd info for user [%s]; "
 
230
                       "rc = [%ld]\n", username, rc);
 
231
                goto out;
 
232
        }
 
233
        saved_uid = geteuid();
 
234
        seteuid(uid);
 
235
        if ((rc = pam_get_item(pamh, PAM_OLDAUTHTOK,
 
236
                               (const void **)&old_passphrase))
 
237
            != PAM_SUCCESS) {
 
238
                syslog(LOG_ERR, "Error retrieving old passphrase; rc = [%d]\n",
 
239
                       rc);
 
240
                seteuid(saved_uid);
 
241
                goto out;
 
242
        }
 
243
        if ((rc = pam_get_item(pamh, PAM_AUTHTOK,
 
244
                               (const void **)&new_passphrase))
 
245
            != PAM_SUCCESS) {
 
246
                syslog(LOG_ERR, "Error retrieving new passphrase; rc = [%d]\n",
 
247
                       rc);
 
248
                seteuid(saved_uid);
 
249
                goto out;
 
250
        }
 
251
        seteuid(saved_uid);
 
252
        if (!old_passphrase || !new_passphrase) {
 
253
                syslog(LOG_WARNING, "eCryptfs PAM passphrase change module "
 
254
                       "retrieved at least one NULL passphrase; nothing to "
 
255
                       "do\n");
 
256
                goto out;
 
257
        }
 
258
        if ((rc = asprintf(&wrapped_pw_filename, "%s/.ecryptfs/%s", homedir,
 
259
                           ECRYPTFS_DEFAULT_WRAPPED_PASSPHRASE_FILENAME))
 
260
            == -1) {
 
261
                syslog(LOG_ERR, "Unable to allocate memory\n");
 
262
                rc = -ENOMEM;
 
263
                goto out;
 
264
        }
 
265
        if ((rc = ecryptfs_read_salt_hex_from_rc(salt_hex))) {
 
266
                syslog(LOG_WARNING, "Unable to read salt value from user's "
 
267
                       ".ecryptfsrc file; using default\n");
 
268
                from_hex(salt, ECRYPTFS_DEFAULT_SALT_HEX, ECRYPTFS_SALT_SIZE);
 
269
        } else
 
270
                from_hex(salt, salt_hex, ECRYPTFS_SALT_SIZE);
 
271
        rc = PAM_SUCCESS;
 
272
        if ((child_pid = fork()) == 0) {
 
273
                char passphrase[ECRYPTFS_MAX_PASSWORD_LENGTH + 1];
 
274
 
 
275
                setuid(uid);
 
276
                if ((rc = ecryptfs_unwrap_passphrase(passphrase,
 
277
                                                     wrapped_pw_filename,
 
278
                                                     old_passphrase, salt))) {
 
279
                        syslog(LOG_ERR, "Error attempting to unwrap "
 
280
                               "passphrase; rc = [%d]\n", rc);
 
281
                        goto out_child;
 
282
                }
 
283
                if ((rc = ecryptfs_wrap_passphrase(wrapped_pw_filename,
 
284
                                                   new_passphrase, salt,
 
285
                                                   passphrase))) {
 
286
                        syslog(LOG_ERR, "Error attempting to wrap passphrase; "
 
287
                               "rc = [%d]", rc);
 
288
                        goto out_child;
 
289
                }
 
290
out_child:
 
291
                exit(0);
 
292
        }
 
293
        if ((tmp_pid = waitpid(child_pid, NULL, 0)) == -1)
 
294
                syslog(LOG_WARNING,
 
295
                       "waitpid() returned with error condition\n");
 
296
        free(wrapped_pw_filename);
 
297
out:
 
298
        return rc;
 
299
}