Skip to content

Commit

Permalink
lib-master: master-login-auth - Add support for event API and use it …
Browse files Browse the repository at this point in the history
…for logging.
  • Loading branch information
stephanbosch committed Feb 3, 2019
1 parent 76a385b commit 23fb881
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/lib-master/master-login-auth.c
Expand Up @@ -22,8 +22,13 @@

#define AUTH_MAX_INBUF_SIZE 8192

static struct event_category event_category_auth_master_client_login = {
.name = "auth-master-client-login"
};

struct master_login_auth_request {
struct master_login_auth_request *prev, *next;
struct event *event;

unsigned int id;
struct timeval create_stamp;
Expand All @@ -42,6 +47,7 @@ struct master_login_auth_request {
struct master_login_auth {
struct connection conn;
struct connection_list *clist;
struct event *event;
pool_t pool;
int refcount;

Expand Down Expand Up @@ -110,6 +116,13 @@ master_login_auth_init(const char *auth_socket_path, bool request_auth_token)

auth->clist = connection_list_init(&master_login_auth_set,
&master_login_auth_vfuncs);

auth->event = event_create(NULL);
event_add_category(auth->event,
&event_category_auth_master_client_login);
event_set_append_log_prefix(auth->event, "auth-master: login: ");

auth->conn.event_parent = auth->event;
connection_init_client_unix(auth->clist, &auth->conn,
auth->auth_socket_path);

Expand All @@ -133,7 +146,7 @@ static void request_failure(struct master_login_auth *auth,
timeval_diff_msecs(&ioloop_timeval, &request->create_stamp),
request->client_pid, request->auth_id);

i_error("%s (%s)", log_reason, str_c(str));
e_error(request->event, "%s (%s)", log_reason, str_c(str));
request->callback(NULL, client_reason, request->context);
}

Expand All @@ -150,6 +163,8 @@ static void request_free(struct master_login_auth_request **_request)
struct master_login_auth_request *request = *_request;

*_request = NULL;

event_unref(&request->event);
i_free(request);
}

Expand Down Expand Up @@ -199,6 +214,7 @@ static void master_login_auth_unref(struct master_login_auth **_auth)
hash_table_destroy(&auth->requests);
connection_deinit(&auth->conn);
connection_list_deinit(&clist);
event_unref(&auth->event);
pool_unref(&auth->pool);
}

Expand Down Expand Up @@ -232,7 +248,7 @@ static void master_login_auth_destroy(struct connection *_conn)
break;
case CONNECTION_DISCONNECT_BUFFER_FULL:
/* buffer full */
i_error("Auth server sent us too long line");
e_error(auth->event, "Auth server sent us too long line");
master_login_auth_fail(auth, NULL);
break;
default:
Expand Down Expand Up @@ -305,7 +321,8 @@ master_login_auth_handshake_line(struct connection *_conn, const char *line)
tmp[1] != NULL && tmp[2] != NULL) {
if (str_to_uint(tmp[1], &major_version) < 0 ||
str_to_uint(tmp[2], &minor_version) < 0) {
i_error("Auth server sent invalid version line: %s",
e_error(auth->event,
"Auth server sent invalid version line: %s",
line);
return -1;
}
Expand All @@ -318,7 +335,8 @@ master_login_auth_handshake_line(struct connection *_conn, const char *line)
}
if (strcmp(tmp[0], "SPID") != 0 ||
str_to_pid(tmp[1], &auth->auth_server_pid) < 0) {
i_error("Auth server did not send valid SPID: %s", line);
e_error(auth->event,
"Auth server did not send valid SPID: %s", line);
return -1;
}

Expand Down Expand Up @@ -351,7 +369,8 @@ master_login_auth_lookup_request(struct master_login_auth *auth,

request = hash_table_lookup(auth->requests, POINTER_CAST(id));
if (request == NULL) {
i_error("Auth server sent reply with unknown ID %u", id);
e_error(auth->event,
"Auth server sent reply with unknown ID %u", id);
return NULL;
}
master_login_auth_request_remove(auth, request);
Expand Down Expand Up @@ -432,15 +451,15 @@ master_login_auth_input_args(struct connection *_conn, const char *const *args)
unsigned int id;

if (args[0] != NULL && strcmp(args[0], "CUID") == 0) {
i_error("%s is an auth client socket. "
e_error(auth->event, "%s is an auth client socket. "
"It should be a master socket.",
auth->auth_socket_path);
return -1;
}

if (args[0] == NULL || args[1] == NULL ||
str_to_uint(args[1], &id) < 0) {
i_error("BUG: Unexpected input: %s",
e_error(auth->event, "BUG: Unexpected input: %s",
t_strarray_join(args, "\t"));
return -1;
}
Expand Down Expand Up @@ -475,11 +494,11 @@ master_login_auth_connect(struct master_login_auth *auth)

if (connection_client_connect(&auth->conn) < 0) {
if (errno == EACCES) {
i_error("%s",
e_error(auth->event, "%s",
eacces_error_get("connect",
auth->auth_socket_path));
} else {
i_error("connect(%s) failed: %m",
e_error(auth->event, "connect(%s) failed: %m",
auth->auth_socket_path);;
}
return -1;
Expand All @@ -496,7 +515,8 @@ auth_request_check_spid(struct master_login_auth *auth,
if (auth->auth_server_pid != req->auth_pid &&
auth->conn.handshake_received) {
/* auth server was restarted. don't even attempt a login. */
i_warning("Auth server restarted (pid %u -> %u), aborting auth",
e_warning(auth->event,
"Auth server restarted (pid %u -> %u), aborting auth",
(unsigned int)req->auth_pid,
(unsigned int)auth->auth_server_pid);
return FALSE;
Expand Down Expand Up @@ -581,6 +601,12 @@ void master_login_auth_request(struct master_login_auth *auth,
hash_table_insert(auth->requests, POINTER_CAST(id), login_req);
DLLIST2_APPEND(&auth->request_head, &auth->request_tail, login_req);

login_req->event = event_create(auth->event);
event_add_int(login_req->event, "id", login_req->id);
event_set_append_log_prefix(login_req->event,
t_strdup_printf("request [%u]: ",
login_req->id));

if (auth->to == NULL)
master_login_auth_update_timeout(auth);

Expand Down

0 comments on commit 23fb881

Please sign in to comment.