2 * Dolda Connect - Modular multiuser Direct Connect-style client
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
20 * I have decided that I don't like PAM. Maybe I'm inexperienced, so
21 * please correct me if I'm wrong, but is it not so that
22 * pam_authenticate blocks until the user has fully authenticated
23 * herself? That isn't very good in a program that wants to do other
24 * things at the same time. In my mind, pam_authenticate should return
25 * with a conversation struct every time it wants data.
27 * My solution here, for now, is to use the ucontext context switching
28 * functions to get back and forth from the conversation
29 * function. Ugly? Yes indeed, it most certainly is, but what am I to
30 * do, then? If there actually is a good way to do this that is built
31 * into PAM, _please_, do mail me about it.
50 #include <security/pam_appl.h>
56 ucontext_t mainctxt, pamctxt;
58 volatile int validctxt;
59 volatile int convdone, converr;
60 volatile char *passdata;
63 static int pamconv(int nmsg, const struct pam_message **msg, struct pam_response **resp, struct authhandle *auth)
68 data = auth->mechdata;
69 *resp = smalloc(sizeof(**resp) * nmsg);
70 for(i = 0; i < nmsg; i++)
72 switch(msg[i]->msg_style)
74 case PAM_PROMPT_ECHO_OFF:
75 auth->prompt = AUTH_PR_NOECHO;
77 case PAM_PROMPT_ECHO_ON:
78 auth->prompt = AUTH_PR_ECHO;
81 auth->prompt = AUTH_PR_ERROR;
84 auth->prompt = AUTH_PR_INFO;
87 if(auth->text != NULL)
89 if((auth->text = icmbstowcs((char *)msg[i]->msg, NULL)) == NULL)
91 flog(LOG_ERR, "could not convert PAM error %s into wcs: %s", msg[i]->msg, strerror(errno));
96 if(swapcontext(&data->pamctxt, &data->mainctxt))
98 flog(LOG_CRIT, "could not swap context in PAM conversation: %s", strerror(errno));
101 return(PAM_CONV_ERR);
105 for(i--; i >= 0; i--)
106 free((*resp)[i].resp);
109 return(PAM_CONV_ERR);
111 (*resp)[i].resp_retcode = PAM_SUCCESS;
112 switch(msg[i]->msg_style)
114 case PAM_PROMPT_ECHO_OFF:
115 case PAM_PROMPT_ECHO_ON:
116 (*resp)[i].resp = sstrdup((char *)data->passdata);
117 memset((void *)data->passdata, 0, strlen((char *)data->passdata));
120 (*resp)[i].resp = NULL;
127 static void releasepam(struct pamdata *data)
129 if(data->pamh != NULL)
134 if(swapcontext(&data->mainctxt, &data->pamctxt))
136 flog(LOG_CRIT, "could not switch back to PAM context while releasing: %s", strerror(errno));
140 pam_end(data->pamh, data->pamret);
142 if(data->pamstack != NULL)
143 free(data->pamstack);
147 static void release(struct authhandle *auth)
149 releasepam((struct pamdata *)auth->mechdata);
152 static struct pamdata *newpamdata(void)
156 new = smalloc(sizeof(*new));
158 new->pamret = PAM_SUCCESS;
159 new->pamstack = NULL;
165 static int inithandle(struct authhandle *auth, char *username)
168 struct pamdata *data;
169 struct pam_conv conv;
172 conv.conv = (int (*)(int, const struct pam_message **, struct pam_response **, void *))pamconv;
173 conv.appdata_ptr = auth;
174 if((buf = icwcstombs(confgetstr("auth-pam", "pamserv"), NULL)) == NULL)
176 flog(LOG_ERR, "could not initialize pam since auth-pam.pamserv cannot be translated into the current locale: %s", strerror(errno));
180 if((data->pamret = pam_start(buf, username, &conv, &data->pamh)) != PAM_SUCCESS)
182 flog(LOG_CRIT, "could not pam_start: %s", pam_strerror(NULL, data->pamret));
185 errno = ENOTSUP; /* XXX */
189 auth->mechdata = data;
193 static void pamauththread(struct authhandle *auth)
195 struct pamdata *data;
197 data = (struct pamdata *)auth->mechdata;
199 data->pamret = pam_authenticate(data->pamh, 0);
203 static int pamauth(struct authhandle *auth, struct socket *sk, char *passdata)
205 struct pamdata *data;
207 data = auth->mechdata;
210 if(getcontext(&data->pamctxt))
212 flog(LOG_CRIT, "could not get context: %s", strerror(errno));
215 data->pamctxt.uc_link = &data->mainctxt;
216 if(data->pamstack == NULL)
217 data->pamstack = smalloc(65536);
218 data->pamctxt.uc_stack.ss_sp = data->pamstack;
219 data->pamctxt.uc_stack.ss_size = 65536;
220 makecontext(&data->pamctxt, (void (*)(void))pamauththread, 1, auth);
221 if(swapcontext(&data->mainctxt, &data->pamctxt))
223 flog(LOG_CRIT, "Could not switch to PAM context: %s", strerror(errno));
228 if(data->pamret == PAM_AUTHINFO_UNAVAIL)
230 else if(data->pamret == PAM_SUCCESS)
231 return(AUTH_SUCCESS);
237 data->passdata = passdata;
238 if(swapcontext(&data->mainctxt, &data->pamctxt))
240 flog(LOG_CRIT, "could not switch back to PAM context: %s", strerror(errno));
245 if(data->pamret == PAM_AUTHINFO_UNAVAIL)
247 else if(data->pamret == PAM_SUCCESS)
248 return(AUTH_SUCCESS);
256 static int renewcred(struct authhandle *auth)
258 struct pamdata *data;
260 data = auth->mechdata;
261 if(data->pamh == NULL)
262 return(AUTH_SUCCESS);
263 data->pamret = pam_setcred(data->pamh, PAM_REFRESH_CRED);
264 if(data->pamret != PAM_SUCCESS)
266 flog(LOG_INFO, "could not refresh credentials: %s", pam_strerror(data->pamh, data->pamret));
269 return(AUTH_SUCCESS);
272 static int opensess(struct authhandle *auth)
274 struct pamdata *data;
277 data = auth->mechdata;
278 if(data->pamh == NULL)
280 flog(LOG_ERR, "bug: in auth-pam.c:opensess: called with NULL pamh");
283 data->pamret = pam_setcred(data->pamh, PAM_ESTABLISH_CRED);
284 if(data->pamret != PAM_SUCCESS)
286 flog(LOG_INFO, "could not establish credentials: %s", pam_strerror(data->pamh, data->pamret));
289 data->pamret = pam_open_session(data->pamh, 0);
290 if(data->pamret != PAM_SUCCESS)
292 flog(LOG_INFO, "could not open session: %s", pam_strerror(data->pamh, data->pamret));
295 for(envp = pam_getenvlist(data->pamh); *envp; envp++)
297 return(AUTH_SUCCESS);
300 static int closesess(struct authhandle *auth)
303 struct pamdata *data;
305 data = auth->mechdata;
306 if(data->pamh == NULL)
308 flog(LOG_ERR, "bug: in auth-pam.c:closesess: called with NULL pamh");
312 data->pamret = pam_close_session(data->pamh, 0);
313 if(data->pamret != PAM_SUCCESS)
315 flog(LOG_INFO, "could not open session: %s", pam_strerror(data->pamh, data->pamret));
318 data->pamret = pam_setcred(data->pamh, PAM_DELETE_CRED);
319 if(data->pamret != PAM_SUCCESS)
321 flog(LOG_INFO, "could not establish credentials: %s", pam_strerror(data->pamh, data->pamret));
327 static struct authmech authmech_pam =
329 .inithandle = inithandle,
331 .authenticate = pamauth,
332 .renewcred = renewcred,
333 .opensess = opensess,
334 .closesess = closesess,
339 static int init(int hup)
342 regmech(&authmech_pam);
346 static struct configvar myvars[] =
348 /** The name of the PAM service file to use. */
349 {CONF_VAR_STRING, "pamserv", {.str = L"doldacond"}},
353 static struct module me =
365 #endif /* HAVE_PAM */