webdav_authentication.c [plain text]
#include <sys/syslog.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <UNCUserNotification.h>
#include "webdav_authentication.h"
#include "webdav_authcache.h"
#define VALID_RESPONSES_MASK (kUNCAlternateResponse | kUNCOtherResponse | kUNCCancelResponse)
static void webdav_login_failed_warning(void)
{
int result;
int index;
const char *contents[11];
UNCUserNotificationRef notification;
unsigned response;
index = 0;
contents[index++] = kUNCLocalizationPathKey;
contents[index++] = WEBDAV_LOCALIZATION_BUNDLE;
contents[index++] = kUNCIconPathKey;
contents[index++] = WEBDAV_SERVER_ICON_PATH;
contents[index++] = kUNCAlertHeaderKey;
contents[index++] = WEBDAV_LOGIN_FAILED_HEADER_KEY;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = WEBDAV_LOGIN_FAILED_MSG_KEY;
contents[index++] = kUNCDefaultButtonTitleKey;
contents[index++] = WEBDAV_OK_KEY;
contents[index++] = 0;
notification = UNCUserNotificationCreate(WEBDAV_AUTHENTICATION_TIMEOUT,
kUNCStopAlertLevel, &result, contents);
if ( result == 0 )
{
(void) UNCUserNotificationReceiveResponse(notification, WEBDAV_AUTHENTICATION_TIMEOUT, &response);
UNCUserNotificationFree(notification);
}
else
{
syslog(LOG_ERR, "webdav_basic_auth_warning: UNCUserNotificationCreate() failed");
}
}
int webdav_get_authentication(char *namebuff, int namebuff_size, char *passbuff,
int passbuff_size,const char *urlStr, const char *realmStr, unsigned int level,
int *addtokeychain, int badlogin)
{
int error = 0, index;
unsigned response = 0;
UNCUserNotificationRef notification;
const char *username;
const char *password;
const char *contents[31];
*addtokeychain = 0;
if ( badlogin )
{
webdav_login_failed_warning();
}
index = 0;
contents[index++] = kUNCLocalizationPathKey;
contents[index++] = WEBDAV_LOCALIZATION_BUNDLE;
contents[index++] = kUNCIconPathKey;
contents[index++] = WEBDAV_SERVER_ICON_PATH;
contents[index++] = kUNCAlertHeaderKey;
contents[index++] = WEBDAV_AUTH_HEADER_KEY;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = WEBDAV_AUTH_MSG_KEY1;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = urlStr;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = WEBDAV_AUTH_MSG_KEY2;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = realmStr;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = WEBDAV_AUTH_MSG_KEY3;
contents[index++] = kUNCAlertMessageKey;
contents[index++] = (level == kChallengeSecurityLevelBasic) ?
WEBDAV_AUTH_MSG_INSECURE :
WEBDAV_AUTH_MSG_SECURE;
contents[index++] = kUNCTextFieldTitlesKey;
contents[index++] = WEBDAV_USERNAME_KEY;
contents[index++] = kUNCTextFieldTitlesKey;
contents[index++] = WEBDAV_PASSWORD_KEY;
if ( namebuff[0] )
{
contents[index++] = kUNCTextFieldValuesKey;
contents[index++] = namebuff;
}
contents[index++] = kUNCCheckBoxTitlesKey;
contents[index++] = WEBDAV_KEYCHAIN_KEY;
contents[index++] = kUNCDefaultButtonTitleKey;
contents[index++] = WEBDAV_OK_KEY;
contents[index++] = kUNCAlternateButtonTitleKey;
contents[index++] = WEBDAV_CANCEL_KEY;
contents[index++] = 0;
notification = UNCUserNotificationCreate(WEBDAV_AUTHENTICATION_TIMEOUT,
UNCSecureTextField(1) + kUNCPlainAlertLevel, &error, contents);
if (!error)
{
if (UNCUserNotificationReceiveResponse(notification, WEBDAV_AUTHENTICATION_TIMEOUT, &response) ||
(kUNCAlternateResponse == (response & VALID_RESPONSES_MASK)))
{
error = -1;
}
else
{
username = UNCUserNotificationGetResponseValue(notification, kUNCTextFieldValuesKey, 0);
password = UNCUserNotificationGetResponseValue(notification, kUNCTextFieldValuesKey, 1);
*addtokeychain = (response & UNCCheckBoxChecked(0));
#ifdef DEBUG
syslog(LOG_INFO,"Keychain checkBox is %s checked.\n", (*addtokeychain != 0) ? "" : "NOT");
#endif
if (!username || !strlen(username))
{
bzero(namebuff, (size_t)namebuff_size);
}
else
{
strncpy(namebuff, username, (size_t)namebuff_size);
namebuff[namebuff_size - 1] = '\0';
}
if (!password || !strlen(password))
{
bzero(passbuff, (size_t)passbuff_size);
}
else
{
strncpy(passbuff, password, (size_t)passbuff_size);
passbuff[passbuff_size - 1] = '\0';
}
}
UNCUserNotificationFree(notification);
}
else
{
syslog(LOG_ERR, "webdav_get_authentication: UNCUserNotificationCreate() failed");
}
return (error);
}