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.
38 #include <security/pam_appl.h>
53 ucontext_t mainctxt, pamctxt;
55 volatile int validctxt;
56 volatile int convdone, converr;
57 volatile char *passdata;
60 static int pamconv(int nmsg, const struct pam_message **msg, struct pam_response **resp, struct authhandle *auth)
65 data = auth->mechdata;
66 *resp = smalloc(sizeof(**resp) * nmsg);
67 for(i = 0; i < nmsg; i++)
69 switch(msg[i]->msg_style)
71 case PAM_PROMPT_ECHO_OFF:
72 auth->prompt = AUTH_PR_NOECHO;
74 case PAM_PROMPT_ECHO_ON:
75 auth->prompt = AUTH_PR_ECHO;
78 auth->prompt = AUTH_PR_ERROR;
81 auth->prompt = AUTH_PR_INFO;
84 if(auth->text != NULL)
86 if((auth->text = icmbstowcs((char *)msg[i]->msg, NULL)) == NULL)
88 flog(LOG_ERR, "could not convert PAM error %s into wcs: %s", msg[i]->msg, strerror(errno));
93 if(swapcontext(&data->pamctxt, &data->mainctxt))
95 flog(LOG_CRIT, "could not swap context in PAM conversation: %s", strerror(errno));
102 for(i--; i >= 0; i--)
103 free((*resp)[i].resp);
106 return(PAM_CONV_ERR);
108 (*resp)[i].resp_retcode = PAM_SUCCESS;
109 switch(msg[i]->msg_style)
111 case PAM_PROMPT_ECHO_OFF:
112 case PAM_PROMPT_ECHO_ON:
113 (*resp)[i].resp = sstrdup((char *)data->passdata);
114 memset((void *)data->passdata, 0, strlen((char *)data->passdata));
117 (*resp)[i].resp = NULL;
124 static void releasepam(struct pamdata *data)
126 if(data->pamh != NULL)
131 if(swapcontext(&data->mainctxt, &data->pamctxt))
133 flog(LOG_CRIT, "could not switch back to PAM context while releasing: %s", strerror(errno));
137 pam_end(data->pamh, data->pamret);
139 if(data->pamstack != NULL)
140 free(data->pamstack);
144 static void release(struct authhandle *auth)
146 releasepam((struct pamdata *)auth->mechdata);
149 static struct pamdata *newpamdata(void)
153 new = smalloc(sizeof(*new));
155 new->pamret = PAM_SUCCESS;
156 new->pamstack = NULL;
162 static int inithandle(struct authhandle *auth, char *username)
165 struct pamdata *data;
166 struct pam_conv conv;
169 conv.conv = (int (*)(int, const struct pam_message **, struct pam_response **, void *))pamconv;
170 conv.appdata_ptr = auth;
171 if((buf = icwcstombs(confgetstr("auth", "pamserv"), NULL)) == NULL)
173 flog(LOG_ERR, "could not initialize pam since auth.pamserv cannot be translated into the current locale: %s", strerror(errno));
177 if((data->pamret = pam_start(buf, username, &conv, &data->pamh)) != PAM_SUCCESS)
179 flog(LOG_CRIT, "could not pam_start: %s", pam_strerror(NULL, data->pamret));
182 errno = ENOTSUP; /* XXX */
186 auth->mechdata = data;
190 static void pamauththread(struct authhandle *auth)
192 struct pamdata *data;
194 data = (struct pamdata *)auth->mechdata;
196 data->pamret = pam_authenticate(data->pamh, 0);
200 static int pamauth(struct authhandle *auth, struct socket *sk, char *passdata)
202 struct pamdata *data;
204 data = auth->mechdata;
207 if(getcontext(&data->pamctxt))
209 flog(LOG_CRIT, "could not get context: %s", strerror(errno));
212 data->pamctxt.uc_link = &data->mainctxt;
213 if(data->pamstack == NULL)
214 data->pamstack = smalloc(65536);
215 data->pamctxt.uc_stack.ss_sp = data->pamstack;
216 data->pamctxt.uc_stack.ss_size = 65536;
217 makecontext(&data->pamctxt, (void (*)(void))pamauththread, 1, auth);
218 if(swapcontext(&data->mainctxt, &data->pamctxt))
220 flog(LOG_CRIT, "Could not switch to PAM context: %s", strerror(errno));
225 if(data->pamret == PAM_AUTHINFO_UNAVAIL)
227 else if(data->pamret == PAM_SUCCESS)
228 return(AUTH_SUCCESS);
234 data->passdata = passdata;
235 if(swapcontext(&data->mainctxt, &data->pamctxt))
237 flog(LOG_CRIT, "could not switch back to PAM context: %s", strerror(errno));
242 if(data->pamret == PAM_AUTHINFO_UNAVAIL)
244 else if(data->pamret == PAM_SUCCESS)
245 return(AUTH_SUCCESS);
253 static int renewcred(struct authhandle *auth)
255 struct pamdata *data;
257 data = auth->mechdata;
258 if(data->pamh == NULL)
259 return(AUTH_SUCCESS);
260 data->pamret = pam_setcred(data->pamh, PAM_REFRESH_CRED);
261 if(data->pamret != PAM_SUCCESS)
263 flog(LOG_INFO, "could not refresh credentials: %s", pam_strerror(data->pamh, data->pamret));
266 return(AUTH_SUCCESS);
269 static int opensess(struct authhandle *auth)
271 struct pamdata *data;
274 data = auth->mechdata;
275 if(data->pamh == NULL)
277 flog(LOG_ERR, "bug: in auth-pam.c:opensess: called with NULL pamh");
280 data->pamret = pam_setcred(data->pamh, PAM_ESTABLISH_CRED);
281 if(data->pamret != PAM_SUCCESS)
283 flog(LOG_INFO, "could not establish credentials: %s", pam_strerror(data->pamh, data->pamret));
286 data->pamret = pam_open_session(data->pamh, 0);
287 if(data->pamret != PAM_SUCCESS)
289 flog(LOG_INFO, "could not open session: %s", pam_strerror(data->pamh, data->pamret));
292 for(envp = pam_getenvlist(data->pamh); *envp; envp++)
294 return(AUTH_SUCCESS);
297 static int closesess(struct authhandle *auth)
300 struct pamdata *data;
302 data = auth->mechdata;
303 if(data->pamh == NULL)
305 flog(LOG_ERR, "bug: in auth-pam.c:closesess: called with NULL pamh");
309 data->pamret = pam_close_session(data->pamh, 0);
310 if(data->pamret != PAM_SUCCESS)
312 flog(LOG_INFO, "could not open session: %s", pam_strerror(data->pamh, data->pamret));
315 data->pamret = pam_setcred(data->pamh, PAM_DELETE_CRED);
316 if(data->pamret != PAM_SUCCESS)
318 flog(LOG_INFO, "could not establish credentials: %s", pam_strerror(data->pamh, data->pamret));
324 struct authmech authmech_pam =
326 .inithandle = inithandle,
328 .authenticate = pamauth,
329 .renewcred = renewcred,
330 .opensess = opensess,
331 .closesess = closesess,