client-common.c revision d3d769026fae5d21c2d29614d3bc4579e8d79e81
6781N/A/* Copyright (c) 2002-2008 Dovecot authors, see the included COPYING file */
6781N/A
6781N/A#include "common.h"
6781N/A#include "hostpid.h"
6781N/A#include "llist.h"
6781N/A#include "str.h"
6781N/A#include "str-sanitize.h"
6781N/A#include "var-expand.h"
6781N/A#include "ssl-proxy.h"
6781N/A#include "client-common.h"
6781N/A
6781N/A#include <stdlib.h>
6781N/A
6781N/Astruct client *clients = NULL;
6781N/Aunsigned int clients_count = 0;
6781N/A
6781N/Avoid client_link(struct client *client)
6781N/A{
6781N/A DLLIST_PREPEND(&clients, client);
6781N/A clients_count++;
6781N/A}
6781N/A
6781N/Avoid client_unlink(struct client *client)
6781N/A{
6781N/A i_assert(clients_count > 0);
6781N/A
6781N/A clients_count--;
6781N/A DLLIST_REMOVE(&clients, client);
6781N/A}
6781N/A
6781N/Aunsigned int clients_get_count(void)
6781N/A{
6781N/A return clients_count;
6781N/A}
6781N/A
6781N/Astatic const struct var_expand_table *
6781N/Aget_var_expand_table(struct client *client)
6781N/A{
6781N/A static struct var_expand_table static_tab[] = {
6781N/A { 'u', NULL },
6781N/A { 'n', NULL },
6781N/A { 'd', NULL },
6781N/A { 's', NULL },
6781N/A { 'h', NULL },
6781N/A { 'l', NULL },
6781N/A { 'r', NULL },
6781N/A { 'p', NULL },
6781N/A { 'm', NULL },
6781N/A { 'a', NULL },
6781N/A { 'b', NULL },
6781N/A { 'c', NULL },
6781N/A { '\0', NULL }
6781N/A };
6781N/A struct var_expand_table *tab;
6781N/A unsigned int i;
6781N/A
6781N/A tab = t_malloc(sizeof(static_tab));
6781N/A memcpy(tab, static_tab, sizeof(static_tab));
6781N/A
6781N/A if (client->virtual_user != NULL) {
6781N/A tab[0].value = client->virtual_user;
6781N/A tab[1].value = t_strcut(client->virtual_user, '@');
6781N/A tab[2].value = strchr(client->virtual_user, '@');
6781N/A if (tab[2].value != NULL) tab[2].value++;
6781N/A
6781N/A for (i = 0; i < 3; i++)
6781N/A tab[i].value = str_sanitize(tab[i].value, 80);
6781N/A }
6781N/A tab[3].value = login_protocol;
6781N/A tab[4].value = getenv("HOME");
6781N/A tab[5].value = net_ip2addr(&client->local_ip);
6781N/A tab[6].value = net_ip2addr(&client->ip);
6781N/A tab[7].value = my_pid;
6781N/A tab[8].value = client->auth_mech_name == NULL ? NULL :
6781N/A str_sanitize(client->auth_mech_name, MAX_MECH_NAME);
6781N/A tab[9].value = dec2str(client->local_port);
6781N/A tab[10].value = dec2str(client->remote_port);
6781N/A if (!client->tls) {
6781N/A tab[11].value = client->secured ? "secured" : NULL;
6781N/A } else {
6781N/A tab[11].value = client->proxy != NULL &&
6781N/A ssl_proxy_is_handshaked(client->proxy) ? "TLS" :
6781N/A "TLS handshaking";
6781N/A }
6781N/A
6781N/A return tab;
6781N/A}
6781N/A
6781N/Astatic bool have_key(const struct var_expand_table *table, const char *str)
6781N/A{
6781N/A char key;
6781N/A unsigned int i;
6781N/A
6781N/A key = var_get_key(str);
6781N/A for (i = 0; table[i].key != '\0'; i++) {
6781N/A if (table[i].key == key) {
6781N/A return table[i].value != NULL &&
6781N/A table[i].value[0] != '\0';
6781N/A }
6781N/A }
6781N/A return FALSE;
6781N/A}
6781N/A
6781N/Astatic void client_syslog_real(struct client *client, const char *msg)
6781N/A{
6781N/A static struct var_expand_table static_tab[3] = {
6781N/A { 's', NULL },
6781N/A { '$', NULL },
6781N/A { '\0', NULL }
6781N/A };
6781N/A const struct var_expand_table *var_expand_table;
6781N/A struct var_expand_table *tab;
6781N/A const char *p, *const *e;
6781N/A string_t *str;
6781N/A
6781N/A var_expand_table = get_var_expand_table(client);
6781N/A
6781N/A tab = t_malloc(sizeof(static_tab));
6781N/A memcpy(tab, static_tab, sizeof(static_tab));
6781N/A
6781N/A str = t_str_new(256);
6781N/A for (e = log_format_elements; *e != NULL; e++) {
6781N/A for (p = *e; *p != '\0'; p++) {
6781N/A if (*p != '%' || p[1] == '\0')
6781N/A continue;
6781N/A
6781N/A p++;
6781N/A if (have_key(var_expand_table, p)) {
6781N/A if (str_len(str) > 0)
6781N/A str_append(str, ", ");
6781N/A var_expand(str, *e, var_expand_table);
6781N/A break;
6781N/A }
6781N/A }
6781N/A }
6781N/A
6781N/A tab[0].value = t_strdup(str_c(str));
6781N/A tab[1].value = msg;
6781N/A str_truncate(str, 0);
6781N/A
6781N/A var_expand(str, log_format, tab);
6781N/A i_info("%s", str_c(str));
6781N/A}
6781N/A
6781N/Avoid client_syslog(struct client *client, const char *msg)
6781N/A{
6781N/A T_FRAME(
6781N/A client_syslog_real(client, msg);
6781N/A );
6781N/A}
6781N/A