11 #include <security/pam_modules.h>
13 #define DEF_INSTANCE "autologin"
30 static void log(int prio, char *format, ...)
35 va_start(args, format);
36 snprintf(buf, sizeof(buf), "pam_krb5auto[%i]: %s", getpid(), format);
37 vsyslog(prio, buf, args);
41 static struct options *parseopts(int argc, const char **argv)
46 opts = malloc(sizeof(*opts));
47 memset(opts, 0, sizeof(*opts));
48 for(i = 0; i < argc; i++) {
49 if(!strncmp(argv[i], "realm=", 6))
50 opts->realm = strdup(argv[i] + 6);
51 if(!strncmp(argv[i], "instance=", 9))
52 opts->instance = strdup(argv[i] + 9);
53 if(!strncmp(argv[i], "keytab=", 7))
54 opts->keytab = strdup(argv[i] + 7);
55 if(!strcmp(argv[i], "debug"))
61 static void freeopts(struct options *opts)
63 if(opts->realm != NULL)
65 if(opts->instance != NULL)
67 if(opts->keytab != NULL)
72 static void freedata(struct data *data)
75 krb5_free_principal(data->ctx, data->me);
77 krb5_free_context(data->ctx);
81 static void cleanupdata(pam_handle_t *pamh, struct data *data, int error_status)
86 static struct data *getdata(pam_handle_t *pamh, struct options *opts)
91 const char *user, *instance;
94 pam_get_data(pamh, "krb5auto-data", (const void **)&data);
97 log(LOG_DEBUG, "creating new instance");
98 data = malloc(sizeof(*data));
99 memset(data, 0, sizeof(*data));
100 if((ret = krb5_init_context(&data->ctx)) != 0) {
101 log(LOG_CRIT, "could not create krb5 context: %s", error_message(ret));
105 pam_get_user(pamh, &user, NULL);
107 instance = opts->instance;
109 instance = DEF_INSTANCE;
111 snprintf(buf, sizeof(buf), "%s/%s@%s", user, instance, opts->realm);
113 snprintf(buf, sizeof(buf), "%s/%s", user, instance);
114 if((ret = krb5_parse_name(data->ctx, buf, &data->me)) != 0) {
115 log(LOG_ERR, "could not parse principal name `%s': %s", buf, error_message(ret));
119 pam_set_data(pamh, "krb5auto-data", data, (void (*)(pam_handle_t *, void *, int))cleanupdata);
124 static void savecreds(struct options *opts, struct data *data)
128 PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc, const char **argv)
133 PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc, const char **argv)
135 struct options *opts;
138 opts = parseopts(argc, argv);
139 data = getdata(pamh, opts);
141 log(LOG_ERR, "could not get data, erroring out");
142 return(PAM_SERVICE_ERR);
144 if(flags & PAM_ESTABLISH_CRED) {
145 savecreds(opts, data);
153 * compile-command: "gcc -Wall -g --shared -fPIC -o pam_krb5auto.so pam_krb5auto.c"