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
|
/*
** Copyright 1998 - 2005 Double Precision, Inc. See COPYING for
** distribution information.
*/
#include "auth.h"
#include "courierauth.h"
#include "courierauthdebug.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
static int badstr(const char *p)
{
while (p && *p)
{
if ((int)(unsigned char)*p < ' ')
return 1;
++p;
}
return 0;
}
/* Create a new string consisting of:
** - username
** - DEFDOMAIN if username does not contain any characters from DOMAINSEP
** (or if DOMAINSEP not set, then if username does not contain
** the first char of DEFDOMAIN)
** - strings s1, s2, s3
*/
char *strdupdefdomain(const char *userid, const char *s1, const char *s2,
const char *s3)
{
char *p, *q, *r;
q=getenv("DEFDOMAIN");
if (q && q[0])
{
r=getenv("DOMAINSEP");
if (r ? strpbrk(userid, r) : strchr(userid, q[0])) q = "";
}
else
q = "";
p=malloc(strlen(userid)+strlen(q)+strlen(s1)+strlen(s2)+strlen(s3)+1);
if (p)
strcat(strcat(strcat(strcat(strcpy(p, userid), q), s1), s2), s3);
return p;
}
int auth_login(const char *service,
const char *userid,
const char *passwd,
int (*callback_func)(struct authinfo *, void *),
void *callback_arg)
{
struct auth_meta dummy;
memset(&dummy, 0, sizeof(dummy));
return auth_login_meta(&dummy, service, userid, passwd, callback_func,
callback_arg);
}
int auth_login_meta(struct auth_meta *meta,
const char *service,
const char *userid,
const char *passwd,
int (*callback_func)(struct authinfo *, void *),
void *callback_arg)
{
char *p;
int rc;
if (badstr(userid) || badstr(passwd))
{
errno=EINVAL;
return -1;
}
courier_authdebug_login_init();
courier_authdebug_login( 1, "username=%s", userid );
courier_authdebug_login( 2, "password=%s", passwd );
p = strdupdefdomain(userid, "\n", passwd, "\n");
if (!p)
return (-1);
rc=auth_generic_meta(meta, service, AUTHTYPE_LOGIN, p,
callback_func,
callback_arg);
free(p);
return rc;
}
|