#include "c2s.h"
#ifdef STORAGE_SQLITE
#define APPLE_ENABLE_OD_AUTH 1
#ifdef APPLE_ENABLE_OD_AUTH
#include <apple_authenticate.h>
#include <apple_authorize.h>
#define APPLE_CHAT_SACL_NAME "chat"
#endif
#ifdef APPLE_ENABLE_OD_AUTH
static int _ar_od_user_exists(authreg_t ar, char *username, char *realm)
{
log_debug( ZONE, "_ar_od_user_exists()." );
if (NULL != username) log_debug( ZONE, "_ar_od_user_exists(): username = %s.", username);
if (NULL != realm) log_debug( ZONE, "_ar_od_user_exists(): realm = %s.", realm);
int iResult = od_auth_check_user_exists((const char *) username);
log_debug( ZONE, "_ar_od_user_exists(): od_auth_check_user_exists returned %d", iResult );
if (0 > iResult)
iResult = 0;
return iResult;
}
static int _ar_od_check_password(authreg_t ar, char *username, char *realm, char password[257])
{
log_debug( ZONE, "_ar_od_check_password()." );
if (NULL != username) log_debug( ZONE, "_ar_od_check_password(): username = %s.", username);
if (NULL != realm) log_debug( ZONE, "_ar_od_check_password(): realm = %s.", realm);
if ((NULL != password) && (0 < strlen(password)))
log_debug( ZONE, "_ar_od_check_password(): password = %s.", password);
int iResult = od_auth_check_plain_password(username, password);
log_debug( ZONE, "_ar_od_check_password(): od_auth_check_plain_password returned %d", iResult );
if (0 != iResult)
iResult = 1;
else {
int iErr = od_auth_check_service_membership(username, APPLE_CHAT_SACL_NAME);
log_debug( ZONE, "_ar_od_check_password(): od_auth_check_service_membership returned %d", iErr );
iResult = (1 == iErr) ? 0 : 1;
}
return iResult;
}
static int _ar_od_create_challenge(authreg_t ar, char *username, char *challenge, int maxlen)
{
log_debug( ZONE, "_ar_od_create_challenge()." );
int iResult = od_auth_supports_cram_md5(username);
log_debug( ZONE, "_ar_od_create_challenge(): od_auth_supports_cram_md5 returned %d", iResult );
if (0 == iResult)
iResult = -1;
iResult = od_auth_create_crammd5_challenge(challenge, maxlen);
log_debug( ZONE, "_ar_od_create_challenge(): od_auth_create_crammd5_challenge returned %d", iResult );
if (0 < iResult)
iResult = 1;
return iResult;
}
static int _ar_od_check_response(authreg_t ar, char *username, char *realm, char *challenge, char *response)
{
log_debug( ZONE, "_ar_od_check_response()." );
if (NULL != username) log_debug( ZONE, "_ar_od_check_response(): username = %s.", username);
if (NULL != realm) log_debug( ZONE, "_ar_od_check_response(): realm = %s.", realm);
if ((NULL != challenge) && (0 < strlen(challenge)))
log_debug( ZONE, "_ar_od_check_response(): challenge = %s.", challenge);
if ((NULL != response) && (0 < strlen(response)))
log_debug( ZONE, "_ar_od_check_response(): response = %s.", response);
int iResult = od_auth_check_crammd5_response(username, challenge, response);
log_debug( ZONE, "_ar_od_check_response(): od_auth_check_crammd5_response returned %d", iResult );
if (0 != iResult)
iResult = 1;
else {
int iErr = od_auth_check_service_membership(username, APPLE_CHAT_SACL_NAME);
log_debug( ZONE, "_ar_od_check_response(): od_auth_check_service_membership returned %d", iErr );
iResult = (1 == iErr) ? 0 : 1;
}
return iResult;
}
#endif
static int _ar_anon_user_exists(authreg_t ar, char *username, char *realm)
{
return 1;
}
static int _ar_anon_check_password(authreg_t ar, char *username, char *realm, char password[257])
{
return 0;
}
int ar_sqlite_init(authreg_t ar)
{
#ifdef APPLE_ENABLE_OD_AUTH
log_debug( ZONE, "APPLE: initializing OD auth functions." );
ar->user_exists = _ar_od_user_exists;
ar->check_password = _ar_od_check_password;
ar->create_challenge = _ar_od_create_challenge;
ar->check_response = _ar_od_check_response;
#else
ar->user_exists = _ar_anon_user_exists;
ar->check_password = _ar_anon_check_password;
#endif
return 0;
}
#endif