13 #include <security/pam_modules.h>
15 #define DEF_INSTANCE "autologin"
37 static void log(int prio, char *format, ...)
42 va_start(args, format);
43 snprintf(buf, sizeof(buf), "pam_krb5auto[%i]: %s", getpid(), format);
44 vsyslog(prio, buf, args);
48 static struct options *parseopts(int argc, const char **argv)
53 opts = malloc(sizeof(*opts));
54 memset(opts, 0, sizeof(*opts));
55 for(i = 0; i < argc; i++) {
56 if(!strncmp(argv[i], "realm=", 6))
57 opts->realm = strdup(argv[i] + 6);
58 if(!strncmp(argv[i], "instance=", 9))
59 opts->instance = strdup(argv[i] + 9);
60 if(!strncmp(argv[i], "keytab=", 7))
61 opts->keytab = strdup(argv[i] + 7);
62 if(!strncmp(argv[i], "renew=", 6))
63 opts->renewable = atoi(argv[i] + 6);
64 if(!strcmp(argv[i], "forwardable"))
65 opts->forwardable = 1;
66 if(!strcmp(argv[i], "debug"))
72 static void freeopts(struct options *opts)
74 if(opts->realm != NULL)
76 if(opts->instance != NULL)
78 if(opts->keytab != NULL)
83 static void freedata(struct data *data)
86 krb5_free_cred_contents(data->ctx, &data->initcreds);
88 krb5_cc_close(data->ctx, data->cc);
90 krb5_free_principal(data->ctx, data->me);
92 krb5_free_context(data->ctx);
96 static void cleanupdata(pam_handle_t *pamh, struct data *data, int error_status)
101 static struct data *getdata(pam_handle_t *pamh, struct options *opts)
106 const char *user, *instance;
107 struct passwd *pwent;
110 pam_get_data(pamh, "krb5auto-data", (const void **)&data);
113 log(LOG_DEBUG, "creating new instance");
114 data = malloc(sizeof(*data));
115 memset(data, 0, sizeof(*data));
116 pam_get_user(pamh, &user, NULL);
118 log(LOG_ERR, "could not get user name");
123 if((pwent = getpwnam(user)) == NULL) {
124 log(LOG_ERR, "could not user information for `%s': %s", user, (errno == 0)?"user not found":strerror(errno));
128 data->uid = pwent->pw_uid;
129 if((ret = krb5_init_context(&data->ctx)) != 0) {
130 log(LOG_CRIT, "could not create krb5 context: %s", error_message(ret));
135 instance = opts->instance;
137 instance = DEF_INSTANCE;
139 snprintf(buf, sizeof(buf), "%s/%s@%s", user, instance, opts->realm);
141 snprintf(buf, sizeof(buf), "%s/%s", user, instance);
142 if((ret = krb5_parse_name(data->ctx, buf, &data->me)) != 0) {
143 log(LOG_ERR, "could not parse principal name `%s': %s", buf, error_message(ret));
147 pam_set_data(pamh, "krb5auto-data", data, (void (*)(pam_handle_t *, void *, int))cleanupdata);
152 static int savecreds(pam_handle_t *pamh, struct options *opts, struct data *data)
156 krb5_get_init_creds_opt icopts;
157 char buf[1024], *ccname, *filename;
159 krb5_get_init_creds_opt_init(&icopts);
163 if((ret = krb5_kt_resolve(data->ctx, opts->keytab, &kt)) != 0) {
164 log(LOG_ERR, "could not resolve keytab `%s': %s", opts->keytab, error_message(ret));
165 ret = PAM_SERVICE_ERR;
169 log(LOG_DEBUG, "using keytab `%s'", opts->keytab);
171 krb5_get_init_creds_opt_set_forwardable(&icopts, opts->forwardable);
172 krb5_get_init_creds_opt_set_renew_life(&icopts, opts->renewable);
174 krb5_free_cred_contents(data->ctx, &data->initcreds);
177 if((ret = krb5_get_init_creds_keytab(data->ctx, &data->initcreds, data->me, kt, 0, NULL, &icopts)) != 0) {
178 log(LOG_ERR, "could not get credentials: %s", error_message(ret));
179 ret = PAM_SERVICE_ERR;
184 log(LOG_DEBUG, "got creds successfully");
185 snprintf(buf, sizeof(buf), "KRB5CCNAME=FILE:/tmp/krb5cc_%i_XXXXXX", data->uid);
186 ccname = buf + sizeof("KRB5CCNAME=");
187 filename = ccname + sizeof("FILE:");
188 if((fd = mkstemp(filename)) < 0) {
189 log(LOG_ERR, "could not create tempfile for credentials cache: %s", strerror(errno));
190 ret = PAM_SERVICE_ERR;
195 log(LOG_DEBUG, "created ccache `%s'", filename);
196 if((ret = krb5_cc_resolve(data->ctx, ccname, &data->cc)) != 0) {
197 log(LOG_ERR, "could not resolve ccache `%s': %s", ccname, error_message(ret));
199 ret = PAM_SERVICE_ERR;
202 if((ret = krb5_cc_initialize(data->ctx, data->cc, data->me)) != 0) {
203 log(LOG_ERR, "could not initialize credentials cache `%s': %s", ccname, error_message(ret));
205 ret = PAM_SERVICE_ERR;
208 if((ret = krb5_cc_store_cred(data->ctx, data->cc, &data->initcreds)) != 0) {
209 log(LOG_ERR, "could not store credentials: %s", error_message(ret));
211 ret = PAM_SERVICE_ERR;
214 pam_putenv(pamh, strdup(buf));
216 log(LOG_DEBUG, "successfully initialized ccache");
221 krb5_kt_close(data->ctx, kt);
225 static int delcreds(pam_handle_t *pamh, struct options *opts, struct data *data)
228 log(LOG_DEBUG, "deleting credentials");
230 krb5_free_cred_contents(data->ctx, &data->initcreds);
233 log(LOG_DEBUG, "freed internal creds");
235 if(data->cc != NULL) {
236 krb5_cc_destroy(data->ctx, data->cc);
239 log(LOG_DEBUG, "destroyed ccache");
244 PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
246 struct options *opts;
248 opts = parseopts(argc, argv);
250 log(LOG_DEBUG, "pam_sm_authenticate called");
255 PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
257 struct options *opts;
261 opts = parseopts(argc, argv);
262 data = getdata(pamh, opts);
264 log(LOG_ERR, "could not get data, erroring out");
265 return(PAM_SERVICE_ERR);
267 ret = PAM_SERVICE_ERR;
268 if(flags & PAM_ESTABLISH_CRED) {
269 ret = savecreds(pamh, opts, data);
270 } else if(flags & PAM_DELETE_CRED) {
271 ret = delcreds(pamh, opts, data);
279 * compile-command: "gcc -Wall -g --shared -fPIC -o pam_krb5auto.so pam_krb5auto.c -lkrb5"