2 * pam_krb5auto - Gets initial credentials non-interactively
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <security/pam_modules.h>
33 #define DEF_INSTANCE "autologin"
56 static void log(int prio, char *format, ...)
61 va_start(args, format);
62 snprintf(buf, sizeof(buf), "pam_krb5auto[%i]: %s", getpid(), format);
63 vsyslog(prio, buf, args);
67 static struct options *parseopts(int argc, const char **argv)
74 opts = malloc(sizeof(*opts));
75 memset(opts, 0, sizeof(*opts));
76 for(i = 0; i < argc; i++) {
77 if(!strncmp(argv[i], "realm=", 6))
78 opts->realm = strdup(argv[i] + 6);
79 if(!strncmp(argv[i], "instance=", 9))
80 opts->instance = strdup(argv[i] + 9);
81 if(!strncmp(argv[i], "keytab=", 7))
82 opts->keytab = strdup(argv[i] + 7);
83 if(!strncmp(argv[i], "renew=", 6)) {
84 p = argv[i] + strlen(argv[i]) - 1;
86 if((*p >= 'a') && (*p <= 'z')) {
96 opts->renewable = atoi(argv[i] + 6) * unit;
98 if(!strcmp(argv[i], "forwardable"))
99 opts->forwardable = 1;
100 if(!strcmp(argv[i], "debug"))
106 static void freeopts(struct options *opts)
108 if(opts->realm != NULL)
110 if(opts->instance != NULL)
111 free(opts->instance);
112 if(opts->keytab != NULL)
117 static void freedata(struct data *data)
120 krb5_free_cred_contents(data->ctx, &data->initcreds);
122 krb5_cc_close(data->ctx, data->cc);
124 krb5_free_principal(data->ctx, data->me);
125 if(data->ctx != NULL)
126 krb5_free_context(data->ctx);
130 static void cleanupdata(pam_handle_t *pamh, struct data *data, int error_status)
135 static struct data *getdata(pam_handle_t *pamh, struct options *opts)
140 const char *user, *instance;
141 struct passwd *pwent;
144 pam_get_data(pamh, "krb5auto-data", (const void **)&data);
147 log(LOG_DEBUG, "creating new instance");
148 data = malloc(sizeof(*data));
149 memset(data, 0, sizeof(*data));
150 pam_get_user(pamh, &user, NULL);
152 log(LOG_ERR, "could not get user name");
157 if((pwent = getpwnam(user)) == NULL) {
158 log(LOG_ERR, "could not user information for `%s': %s", user, (errno == 0)?"user not found":strerror(errno));
162 data->uid = pwent->pw_uid;
163 data->gid = pwent->pw_gid;
164 if((ret = krb5_init_context(&data->ctx)) != 0) {
165 log(LOG_CRIT, "could not create krb5 context: %s", error_message(ret));
170 instance = opts->instance;
172 instance = DEF_INSTANCE;
174 snprintf(buf, sizeof(buf), "%s/%s@%s", user, instance, opts->realm);
176 snprintf(buf, sizeof(buf), "%s/%s", user, instance);
177 if((ret = krb5_parse_name(data->ctx, buf, &data->me)) != 0) {
178 log(LOG_ERR, "could not parse principal name `%s': %s", buf, error_message(ret));
182 pam_set_data(pamh, "krb5auto-data", data, (void (*)(pam_handle_t *, void *, int))cleanupdata);
187 static int savecreds(pam_handle_t *pamh, struct options *opts, struct data *data)
191 krb5_get_init_creds_opt icopts;
192 char buf[1024], *ccname, *filename;
194 krb5_get_init_creds_opt_init(&icopts);
198 if((ret = krb5_kt_resolve(data->ctx, opts->keytab, &kt)) != 0) {
199 log(LOG_ERR, "could not resolve keytab `%s': %s", opts->keytab, error_message(ret));
200 ret = PAM_SERVICE_ERR;
204 log(LOG_DEBUG, "using keytab `%s'", opts->keytab);
206 krb5_get_init_creds_opt_set_forwardable(&icopts, opts->forwardable);
207 krb5_get_init_creds_opt_set_renew_life(&icopts, opts->renewable);
209 krb5_free_cred_contents(data->ctx, &data->initcreds);
212 if((ret = krb5_get_init_creds_keytab(data->ctx, &data->initcreds, data->me, kt, 0, NULL, &icopts)) != 0) {
213 log(LOG_ERR, "could not get credentials: %s", error_message(ret));
214 ret = PAM_SERVICE_ERR;
219 log(LOG_DEBUG, "got creds successfully");
220 snprintf(buf, sizeof(buf), "KRB5CCNAME=FILE:/tmp/krb5cc_%i_XXXXXX", data->uid);
221 ccname = buf + sizeof("KRB5CCNAME=") - 1;
222 filename = ccname + sizeof("FILE:") - 1;
223 if((fd = mkstemp(filename)) < 0) {
224 log(LOG_ERR, "could not create tempfile for credentials cache: %s", strerror(errno));
225 ret = PAM_SERVICE_ERR;
230 log(LOG_DEBUG, "created ccache `%s'", filename);
231 if((ret = krb5_cc_resolve(data->ctx, ccname, &data->cc)) != 0) {
232 log(LOG_ERR, "could not resolve ccache `%s': %s", ccname, error_message(ret));
234 ret = PAM_SERVICE_ERR;
237 if((ret = krb5_cc_initialize(data->ctx, data->cc, data->me)) != 0) {
238 log(LOG_ERR, "could not initialize credentials cache `%s': %s", ccname, error_message(ret));
240 ret = PAM_SERVICE_ERR;
243 if((ret = krb5_cc_store_cred(data->ctx, data->cc, &data->initcreds)) != 0) {
244 log(LOG_ERR, "could not store credentials: %s", error_message(ret));
246 ret = PAM_SERVICE_ERR;
249 chown(filename, data->uid, data->gid);
250 pam_putenv(pamh, strdup(buf));
252 log(LOG_DEBUG, "successfully initialized ccache");
257 krb5_kt_close(data->ctx, kt);
261 static int delcreds(pam_handle_t *pamh, struct options *opts, struct data *data)
264 log(LOG_DEBUG, "deleting credentials");
266 krb5_free_cred_contents(data->ctx, &data->initcreds);
269 log(LOG_DEBUG, "freed internal creds");
271 if(data->cc != NULL) {
272 krb5_cc_destroy(data->ctx, data->cc);
275 log(LOG_DEBUG, "destroyed ccache");
280 PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
282 struct options *opts;
284 opts = parseopts(argc, argv);
286 log(LOG_DEBUG, "pam_sm_authenticate called");
291 PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
293 struct options *opts;
297 opts = parseopts(argc, argv);
299 log(LOG_DEBUG, "pam_sm_setcred called");
300 data = getdata(pamh, opts);
302 log(LOG_ERR, "could not get data, erroring out");
303 return(PAM_SERVICE_ERR);
305 ret = PAM_SERVICE_ERR;
306 if(flags & PAM_ESTABLISH_CRED) {
307 ret = savecreds(pamh, opts, data);
308 } else if(flags & PAM_DELETE_CRED) {
309 ret = delcreds(pamh, opts, data);
317 * compile-command: "gcc -Wall -g --shared -fPIC -o pam_krb5auto.so pam_krb5auto.c -lkrb5"