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
32 #include <security/pam_modules.h>
34 #define DEF_INSTANCE "autologin"
57 static void log(int prio, char *format, ...)
62 va_start(args, format);
63 snprintf(buf, sizeof(buf), "pam_krb5auto[%i]: %s", getpid(), format);
64 vsyslog(prio, buf, args);
68 static struct options *parseopts(int argc, const char **argv)
75 opts = malloc(sizeof(*opts));
76 memset(opts, 0, sizeof(*opts));
77 for(i = 0; i < argc; i++) {
78 if(!strncmp(argv[i], "realm=", 6))
79 opts->realm = strdup(argv[i] + 6);
80 if(!strncmp(argv[i], "instance=", 9))
81 opts->instance = strdup(argv[i] + 9);
82 if(!strncmp(argv[i], "keytab=", 7))
83 opts->keytab = strdup(argv[i] + 7);
84 if(!strncmp(argv[i], "renew=", 6)) {
85 p = argv[i] + strlen(argv[i]) - 1;
87 if((*p >= 'a') && (*p <= 'z')) {
97 opts->renewable = atoi(argv[i] + 6) * unit;
99 if(!strcmp(argv[i], "forwardable"))
100 opts->forwardable = 1;
101 if(!strcmp(argv[i], "debug"))
107 static void freeopts(struct options *opts)
109 if(opts->realm != NULL)
111 if(opts->instance != NULL)
112 free(opts->instance);
113 if(opts->keytab != NULL)
118 static void freedata(struct data *data)
121 krb5_free_cred_contents(data->ctx, &data->initcreds);
123 krb5_cc_close(data->ctx, data->cc);
125 krb5_free_principal(data->ctx, data->me);
126 if(data->ctx != NULL)
127 krb5_free_context(data->ctx);
131 static void cleanupdata(pam_handle_t *pamh, struct data *data, int error_status)
136 static struct data *getdata(pam_handle_t *pamh, struct options *opts)
141 const char *user, *instance;
142 struct passwd *pwent;
145 pam_get_data(pamh, "krb5auto-data", (const void **)&data);
148 log(LOG_DEBUG, "creating new instance");
149 data = malloc(sizeof(*data));
150 memset(data, 0, sizeof(*data));
151 pam_get_user(pamh, &user, NULL);
153 log(LOG_ERR, "could not get user name");
158 if((pwent = getpwnam(user)) == NULL) {
159 log(LOG_ERR, "could not user information for `%s': %s", user, (errno == 0)?"user not found":strerror(errno));
163 data->uid = pwent->pw_uid;
164 data->gid = pwent->pw_gid;
165 if((ret = krb5_init_context(&data->ctx)) != 0) {
166 log(LOG_CRIT, "could not create krb5 context: %s", error_message(ret));
171 instance = opts->instance;
173 instance = DEF_INSTANCE;
175 snprintf(buf, sizeof(buf), "%s/%s@%s", user, instance, opts->realm);
177 snprintf(buf, sizeof(buf), "%s/%s", user, instance);
178 if((ret = krb5_parse_name(data->ctx, buf, &data->me)) != 0) {
179 log(LOG_ERR, "could not parse principal name `%s': %s", buf, error_message(ret));
183 pam_set_data(pamh, "krb5auto-data", data, (void (*)(pam_handle_t *, void *, int))cleanupdata);
188 static int savecreds(pam_handle_t *pamh, struct options *opts, struct data *data)
192 krb5_get_init_creds_opt icopts;
193 char buf[1024], *ccname, *filename;
195 krb5_get_init_creds_opt_init(&icopts);
199 if((ret = krb5_kt_resolve(data->ctx, opts->keytab, &kt)) != 0) {
200 log(LOG_ERR, "could not resolve keytab `%s': %s", opts->keytab, error_message(ret));
201 ret = PAM_SERVICE_ERR;
205 log(LOG_DEBUG, "using keytab `%s'", opts->keytab);
207 krb5_get_init_creds_opt_set_forwardable(&icopts, opts->forwardable);
208 krb5_get_init_creds_opt_set_renew_life(&icopts, opts->renewable);
210 krb5_free_cred_contents(data->ctx, &data->initcreds);
213 if((ret = krb5_get_init_creds_keytab(data->ctx, &data->initcreds, data->me, kt, 0, NULL, &icopts)) != 0) {
214 log(LOG_ERR, "could not get credentials: %s", error_message(ret));
215 ret = PAM_SERVICE_ERR;
220 log(LOG_DEBUG, "got creds successfully");
221 snprintf(buf, sizeof(buf), "KRB5CCNAME=FILE:/tmp/krb5cc_%i_XXXXXX", data->uid);
222 ccname = buf + sizeof("KRB5CCNAME=") - 1;
223 filename = ccname + sizeof("FILE:") - 1;
224 if((fd = mkstemp(filename)) < 0) {
225 log(LOG_ERR, "could not create tempfile for credentials cache: %s", strerror(errno));
226 ret = PAM_SERVICE_ERR;
231 log(LOG_DEBUG, "created ccache `%s'", filename);
232 if((ret = krb5_cc_resolve(data->ctx, ccname, &data->cc)) != 0) {
233 log(LOG_ERR, "could not resolve ccache `%s': %s", ccname, error_message(ret));
235 ret = PAM_SERVICE_ERR;
238 if((ret = krb5_cc_initialize(data->ctx, data->cc, data->me)) != 0) {
239 log(LOG_ERR, "could not initialize credentials cache `%s': %s", ccname, error_message(ret));
241 ret = PAM_SERVICE_ERR;
244 if((ret = krb5_cc_store_cred(data->ctx, data->cc, &data->initcreds)) != 0) {
245 log(LOG_ERR, "could not store credentials: %s", error_message(ret));
247 ret = PAM_SERVICE_ERR;
250 chown(filename, data->uid, data->gid);
251 pam_putenv(pamh, strdup(buf));
253 log(LOG_DEBUG, "successfully initialized ccache");
258 krb5_kt_close(data->ctx, kt);
262 static int delcreds(pam_handle_t *pamh, struct options *opts, struct data *data)
265 log(LOG_DEBUG, "deleting credentials");
267 krb5_free_cred_contents(data->ctx, &data->initcreds);
270 log(LOG_DEBUG, "freed internal creds");
272 if(data->cc != NULL) {
273 krb5_cc_destroy(data->ctx, data->cc);
276 log(LOG_DEBUG, "destroyed ccache");
281 PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
283 struct options *opts;
285 opts = parseopts(argc, argv);
287 log(LOG_DEBUG, "pam_sm_authenticate called");
292 PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
294 struct options *opts;
298 opts = parseopts(argc, argv);
300 log(LOG_DEBUG, "pam_sm_setcred called");
301 data = getdata(pamh, opts);
303 log(LOG_ERR, "could not get data, erroring out");
304 return(PAM_SERVICE_ERR);
306 ret = PAM_SERVICE_ERR;
307 if(flags & PAM_ESTABLISH_CRED) {
308 ret = savecreds(pamh, opts, data);
309 } else if(flags & PAM_DELETE_CRED) {
310 ret = delcreds(pamh, opts, data);
318 * compile-command: "gcc -Wall -g --shared -fPIC -o pam_krb5auto.so pam_krb5auto.c -lkrb5"