#include <stdio.h>
#include <syslog.h>
#include <string.h>
#include <stdlib.h>
#include "auth_event.h"
#include <CoreDaemon/CoreDaemon.h>
void auth_event_data_init(auth_event_data_t *data, char *client_ip, unsigned int client_port, char *mech)
{
if (client_ip == NULL || mech == NULL) {
*data = NULL;
return;
}
*data = (auth_event_data_t) calloc(1, sizeof(struct auth_event_data_st));
auth_event_data_t d = *data;
d->client_ip = strdup(client_ip);
d->client_port = client_port;
d->mech = strdup(mech);
d->username = NULL;
d->status = eUnknownEvent;
return;
}
void auth_event_data_dispose(auth_event_data_t *data)
{
if (data == NULL)
return;
auth_event_data_t d = *data;
if (d->username != NULL) free(d->username);
if (d->mech != NULL) free(d->mech);
if (d->client_ip != NULL) free(d->client_ip);
free(d);
return;
}
void auth_event_log_simple(char *username, char *client_ip, unsigned int client_port, char *mech, int status)
{
char log_buf[1024];
if (status == eAuthFailure)
snprintf(log_buf, sizeof(log_buf), "Authentication failed, mech: %s client IP: %s client port: %d username: %s",
(mech == NULL) ? "?" : mech,
(client_ip == NULL) ? "?" : client_ip,
client_port,
(username == NULL) ? "?" : username);
else if (status == eAuthSuccess)
snprintf(log_buf, sizeof(log_buf), "Authentication succeeded, mech: %s client IP: %s client port: %d username: %s",
(mech == NULL) ? "?" : mech,
(client_ip == NULL) ? "?" : client_ip,
client_port,
(username == NULL) ? "?" : username);
else
return;
syslog(LOG_NOTICE, "%s", log_buf);
send_server_event(status, client_ip);
return;
}
void auth_event_log(auth_event_data_t data)
{
char log_buf[1024];
if (data->status == eAuthFailure)
snprintf(log_buf, sizeof(log_buf), "Authentication failed, mech: %s client IP: %s client port: %d username: %s",
(data->mech == NULL) ? "?" : data->mech,
(data->client_ip == NULL) ? "?" : data->client_ip,
data->client_port,
(data->username == NULL) ? "?" : data->username);
else if (data->status == eAuthSuccess)
snprintf(log_buf, sizeof(log_buf), "Authentication succeeded, mech: %s client IP: %s client port: %d username: %s",
(data->mech == NULL) ? "?" : data->mech,
(data->client_ip == NULL) ? "?" : data->client_ip,
data->client_port,
(data->username == NULL) ? "?" : data->username);
else
return;
syslog(LOG_NOTICE, "%s", log_buf);
send_server_event(data->status, data->client_ip);
return;
}
char g_client_addr[16] = "";
XSEventPortRef gEventPort = NULL;
void send_server_event ( const eEventCode in_event_code, const char *in_addr )
{
CFTypeRef keys[2];
CFTypeRef values[2];
CFStringRef cfstr_addr = NULL;
CFStringRef cfstr_event = NULL;
if (in_addr == NULL)
return;
if ( !strlen(g_client_addr) || (strcmp(g_client_addr, in_addr) != 0) ) {
strlcpy(g_client_addr, in_addr, sizeof g_client_addr);
}
if ( gEventPort == NULL )
gEventPort = XSEventPortCreate(nil);
keys[0] = CFSTR("eventType");
keys[1] = CFSTR("host_address");
switch ( in_event_code ) {
case eAuthFailure:
cfstr_event = CFStringCreateWithCString(NULL, "auth.failure", kCFStringEncodingMacRoman);
break;
case eAuthSuccess:
cfstr_event = CFStringCreateWithCString(NULL, "auth.success", kCFStringEncodingMacRoman);
break;
default:
syslog(LOG_WARNING, "Warning: unknown sever event: %d", in_event_code);
return;
}
cfstr_addr = CFStringCreateWithCString(NULL, in_addr, kCFStringEncodingMacRoman);
values[0] = cfstr_event;
values[1] = cfstr_addr;
CFDictionaryRef dict_event = CFDictionaryCreate(NULL, keys, values,
sizeof(keys) / sizeof(keys[0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
(void)XSEventPortPostEvent(gEventPort, cfstr_event, dict_event);
CFRelease(cfstr_addr);
CFRelease(cfstr_event);
CFRelease(dict_event);
}
void close_server_event_port ( void )
{
if ( gEventPort != NULL )
XSEventPortDelete(gEventPort);
}