client-authenticate.c revision d1414c09cf0d58ac983054e2f4e1a1f329272dcf
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (C) 2002 Timo Sirainen */
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainenint cmd_capa(struct pop3_client *client, const char *args __attr_unused__)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen unsigned int i, count;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mech = auth_client_get_available_mechs(auth_client, &count);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen for (i = 0; i < count; i++) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* a) transport is secured
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen b) auth mechanism isn't plaintext
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen c) we allow insecure authentication
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen - but don't advertise AUTH=PLAIN, as RFC 2595 requires
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_send_line(client, t_strconcat("+OK\r\n" POP3_CAPABILITY_REPLY,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void client_auth_abort(struct pop3_client *client, const char *msg)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen auth_client_request_abort(client->common.auth_request);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_send_line(client, msg != NULL ? t_strconcat("-ERR ", msg, NULL) :
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen "-ERR Authentication failed.");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* get back to normal client input */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client->common.io = client->common.fd == -1 ? NULL :
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen io_add(client->common.fd, IO_READ, client_input, client);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void master_callback(struct client *_client, int success)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen struct pop3_client *client = (struct pop3_client *) _client;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen reason = t_strconcat("Login: ", client->common.virtual_user,
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen reason = t_strconcat("Internal login failure: ",
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen client_send_line(client, "* BYE Internal login failure.");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void client_send_auth_data(struct pop3_client *client,
a75d470c9223a75801418fcdda258885c36317e0Timo Sirainen buf = buffer_create_dynamic(pool_datastack_create(),
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen o_stream_send(client->output, buffer_get_data(buf, NULL),
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenclient_get_auth_flags(struct pop3_client *client)
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen enum auth_client_request_new_flags auth_flags = 0;
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen ssl_proxy_has_valid_client_cert(client->common.proxy))
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen auth_flags |= AUTH_CLIENT_FLAG_SSL_VALID_CLIENT_CERT;
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainenstatic void login_callback(struct auth_request *request,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen switch (auth_callback(request, reply, data, &client->common,
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen /* login failed */
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen /* success, we should be able to log in. if we fail, just
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen disconnect the client. */
f89eb8f2cda0bd6d40a9f96db1c92517f0593871Martti Rannanjärviint cmd_user(struct pop3_client *client, const char *args)
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen if (!client->secured && disable_plaintext_auth) {
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen "-ERR Plaintext authentication disabled.");
88339cce70862ea6c6fac5615975e1e94a52a72eTimo Sirainen /* authorization ID \0 authentication ID \0 pass */
3c3002ee03e2c20034f73cfec80c6647320bd27cTimo Sirainen buffer_append(client->plain_login, args, strlen(args));
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenint cmd_pass(struct pop3_client *client, const char *args)
055389c58fa3915e12fb4e72ec86782ce77c5c72Timo Sirainen if (buffer_get_used_size(client->plain_login) == 0) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_send_line(client, "-ERR No username given.");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen buffer_append(client->plain_login, args, strlen(args));
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen auth_client_request_new(auth_client, "PLAIN", "POP3",
e34d170f8f0e084bd94bfbc1a7085ece67e508dfTimo Sirainen /* don't read any input from client until login is finished */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen t_strconcat("-ERR Login failed: ", error, NULL));
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenstatic void authenticate_callback(struct auth_request *request,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen switch (auth_callback(request, reply, data, &client->common,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* login failed */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_send_auth_data(client, data, reply->data_size);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* success, we should be able to log in. if we fail, just
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen disconnect the client. */
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch /* @UNSAFE */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_auth_abort(client, "Authentication aborted");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen buf = buffer_create_static_hard(pool_datastack_create(), linelen);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (base64_decode((const unsigned char *) line, linelen,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_auth_abort(client, "Invalid base64 data");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen } else if (client->common.auth_request == NULL) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_auth_abort(client, "Don't send unrequested data");
6264b51bcce8ae98efdcda3e55a765d7a13d15edTimo Sirainen auth_client_request_continue(client->common.auth_request,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* clear sensitive data */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen safe_memset(buffer_free_without_data(buf), 0, bufsize);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainenint cmd_auth(struct pop3_client *client, const char *args)
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen /* <mechanism name> <initial response> */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen mech = auth_client_find_mech(auth_client, mech_name);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen "-ERR Unsupported authentication mechanism.");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (!client->secured && mech->plaintext && disable_plaintext_auth) {
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen "-ERR Plaintext authentication disabled.");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen buf = buffer_create_static_hard(pool_datastack_create(), argslen);
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen if (base64_decode((const unsigned char *)args, argslen,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client_send_line(client, "-ERR Invalid base64 data.");
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen auth_client_request_new(auth_client, mech->name, "POP3",
35283613d4c04ce18836e9fc431582c87b3710a0Timo Sirainen /* following input data will go to authentication */
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen client->common.io = io_add(client->common.fd, IO_READ,
f6d57a2c182f63cd52819f0abb3c3d9f828afe19Timo Sirainen "-ERR Authentication failed: ", error, NULL));