1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
/*
** Copyright 1998 - 2004 Double Precision, Inc. See COPYING for
** distribution information.
*/
#if HAVE_CONFIG_H
#include "courier_auth_config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "auth.h"
#include "authwait.h"
#include "authstaticlist.h"
#include "courierauthdebug.h"
#if HAVE_SECURITY_PAM_APPL_H
#include <security/pam_appl.h>
#endif
#if HAVE_PAM_PAM_APPL_H
#include <Pam/pam_appl.h>
#endif
static const char rcsid[]="$Id: authpam.c,v 1.22 2005/12/08 23:15:55 mrsam Exp $";
static const char *pam_username, *pam_password, *pam_service;
extern void auth_pwd_enumerate( void(*cb_func)(const char *name,
uid_t uid,
gid_t gid,
const char *homedir,
const char *maildir,
const char *options,
void *void_arg),
void *void_arg);
static int pam_conv(int num_msg, const struct pam_message **msg,
struct pam_response **resp, void *appdata_ptr)
{
int i = 0;
struct pam_response *repl = NULL;
repl = malloc(sizeof(struct pam_response) * num_msg);
if (!repl) return PAM_CONV_ERR;
for (i=0; i<num_msg; i++)
switch (msg[i]->msg_style) {
case PAM_PROMPT_ECHO_ON:
repl[i].resp_retcode = PAM_SUCCESS;
repl[i].resp = strdup(pam_username);
if (!repl[i].resp)
{
perror("strdup");
exit(1);
}
break;
case PAM_PROMPT_ECHO_OFF:
repl[i].resp_retcode = PAM_SUCCESS;
repl[i].resp = strdup(pam_password);
if (!repl[i].resp)
{
perror("strdup");
exit(1);
}
break;
case PAM_TEXT_INFO:
case PAM_ERROR_MSG:
write(2, msg[i]->msg, strlen(msg[i]->msg));
write(2, "\n", 1);
repl[i].resp_retcode = PAM_SUCCESS;
repl[i].resp = NULL;
break;
default:
free (repl);
return PAM_CONV_ERR;
}
*resp=repl;
return PAM_SUCCESS;
}
static struct pam_conv conv = {
pam_conv,
NULL
};
static int dopam(pam_handle_t **pamh)
{
int retval;
DPRINTF("pam_service=%s, pam_username=%s",
pam_service ? pam_service : "<null>",
pam_username ? pam_username : "<null>");
retval=pam_start(pam_service, pam_username, &conv, pamh);
if (retval != PAM_SUCCESS) DPRINTF("pam_start failed, result %d [Hint: bad PAM configuration?]", retval);
#if 0
if (retval == PAM_SUCCESS)
{
retval=pam_set_item(*pamh, PAM_AUTHTOK, pam_password);
if (retval != PAM_SUCCESS) DPRINTF("pam_set_item failed, result %d", retval);
}
#endif
if (retval == PAM_SUCCESS)
{
retval=pam_authenticate(*pamh, 0);
if (retval != PAM_SUCCESS) DPRINTF("pam_authenticate failed, result %d", retval);
}
#if 0
#if HAVE_PAM_SETCRED
if (retval == PAM_SUCCESS)
{
retval=pam_setcred(*pamh, PAM_ESTABLISH_CRED);
if (retval != PAM_SUCCESS) DPRINTF("pam_setcred failed, result %d", retval);
}
#endif
#endif
if (retval == PAM_SUCCESS)
{
retval=pam_acct_mgmt(*pamh, 0);
if (retval != PAM_SUCCESS) DPRINTF("pam_acct_mgmt failed, result %d", retval);
}
if (retval == PAM_SUCCESS)
DPRINTF("dopam successful");
return (retval);
}
struct callback_info {
int (*callback_func)(struct authinfo *, void *);
void *callback_arg;
} ;
static int callback_pam(struct authinfo *a, void *argptr)
{
struct callback_info *ci=(struct callback_info *)argptr;
pam_handle_t *pamh=NULL;
int pipefd[2];
int retval;
pid_t p;
int waitstat;
char *s;
char buf[1];
a->clearpasswd=pam_password;
s=strdup(a->sysusername);
if (!s)
{
perror("malloc");
return (1);
}
/*
** OK, in order to transparently support PAM sessions inside this
** authentication module, what we need to do is to fork(), and let
** the child run in its parent place. Once the child process exits,
** the parent calls pam_end_session, and clears the PAM library.
**
** This means that upon return from auth_pam(), your process ID
** might've changed!!!
**
** However, if the authentication fails, we can simply exit, without
** running as a child process.
**
** Additionally, the PAM library might allocate some resources that
** authenticated clients should not access. Therefore, we fork
** *before* we try to authenticate. If the authentication succeeds,
** the child process will run in the parent's place. The child
** process waits until the parent tells it whether the authentication
** worked. If it worked, the child keeps running. If not, the child
** exits, which the parent waits for.
**
** The authentication status is communicated to the child process via
** a pipe.
*/
if (pipe(pipefd) < 0)
{
perror("pipe");
free(s);
return (1);
}
if ((p=fork()) == -1)
{
perror("fork");
free(s);
return (1);
}
if (p == 0)
{
close(pipefd[0]);
retval=dopam(&pamh);
if (retval == PAM_SUCCESS)
write(pipefd[1], "", 1);
close(pipefd[1]);
_exit(0);
}
close(pipefd[1]);
while (wait(&waitstat) != p)
;
if (read(pipefd[0], buf, 1) > 0)
{
int rc;
close(pipefd[0]);
a->address=s;
rc=(*ci->callback_func)(a, ci->callback_arg);
free(s);
return rc;
}
close(pipefd[0]);
free(s);
errno=EPERM;
return (-1);
#if 0
free(s);
close(pipefd[0]);
retval=dopam(&pamh);
if (retval == PAM_SUCCESS)
retval=pam_open_session(pamh, 0);
if (retval != PAM_SUCCESS)
{
if (pam_end(pamh, retval) != PAM_SUCCESS)
perror("Unable to release PAM tokens");
/* Wait for child to terminate */
close(pipefd[1]); /* Tell the child to shut down */
while (wait(&waitstat) != p)
;
return (-1);
}
/* Tell child process to run in authenticated state */
write(pipefd[1], "", 1);
close(pipefd[1]);
/* Wait for child process to finish */
while (wait(&waitstat) != p)
;
retval=pam_close_session(pamh, 0);
if (retval != PAM_SUCCESS)
perror("pam_close_session");
if (pam_end(pamh, retval) != PAM_SUCCESS)
perror("Unable to release PAM tokens");
if (WIFEXITED(waitstat))
exit(WEXITSTATUS(waitstat));
exit(255);
return (1);
#endif
}
extern int auth_pam_pre(const char *userid, const char *service,
int (*callback)(struct authinfo *, void *),
void *arg);
int auth_pam(const char *service, const char *type, char *authdata,
int (*callback_func)(struct authinfo *, void *),
void *callback_arg)
{
struct callback_info ci;
if (strcmp(type, AUTHTYPE_LOGIN))
{
DPRINTF("authpam only handles authtype=" AUTHTYPE_LOGIN);
errno=EPERM;
return (-1);
}
if ((pam_username=strtok(authdata, "\n")) == 0 ||
(pam_password=strtok(0, "\n")) == 0)
{
DPRINTF("incomplete username or missing password");
errno=EPERM;
return (-1);
}
pam_service=service;
ci.callback_func=callback_func;
ci.callback_arg=callback_arg;
return auth_pam_pre(pam_username, service, &callback_pam, &ci);
}
static void auth_pam_cleanup()
{
}
static struct authstaticinfo authpam_info={
"authpam",
auth_pam,
auth_pam_pre,
auth_pam_cleanup,
auth_syspasswd,
auth_pam_cleanup,
auth_pwd_enumerate};
struct authstaticinfo *courier_authpam_init()
{
return &authpam_info;
}
|