login-process.c revision 0cb57ee35d4cab9c03434d7abf312c081ed554d4
/* Copyright (C) 2002 Timo Sirainen */
#include "common.h"
#include "ioloop.h"
#include "network.h"
#include "ostream.h"
#include "fdpass.h"
#include "fd-close-on-exec.h"
#include "env-util.h"
#include "restrict-access.h"
#include "restrict-process-size.h"
#include "login-process.h"
#include "auth-process.h"
#include "mail-process.h"
#include "master-login-interface.h"
#include <unistd.h>
#include <syslog.h>
struct login_process {
struct login_group *group;
int refcount;
int fd;
unsigned int initialized:1;
unsigned int listening:1;
unsigned int destroyed:1;
};
struct login_auth_request {
struct login_process *process;
unsigned int tag;
unsigned int login_tag;
int fd;
};
static unsigned int auth_id_counter, login_pid_counter;
static struct hash_table *processes;
static struct login_group *login_groups;
static void login_process_destroy(struct login_process *p);
static void login_process_unref(struct login_process *p);
static int login_process_init_group(struct login_process *p);
{
struct login_group *group;
}
{
}
{
struct master_login_reply master_reply;
else {
}
/* reply to login */
sizeof(master_reply)) < 0)
i_error("close(mail client) failed: %m");
}
static void login_process_mark_nonlistening(struct login_process *p)
{
if (!p->listening) {
i_error("login: received another \"not listening\" "
"notification (if you can't login at all, "
return;
}
p->group->listening_processes--;
p->group->newest_nonlisten_process = p;
p->group->oldest_nonlisten_process = p;
}
}
static void login_process_groups_create(void)
{
struct server_settings *server;
}
}
static struct login_group *
{
struct login_group *group;
if (login_groups == NULL)
return group;
}
return NULL;
}
static int login_process_read_group(struct login_process *p)
{
struct login_group *group;
char buf[256];
enum mail_protocol protocol;
unsigned int len;
/* read length */
if (ret != 1)
len = 0;
else {
i_error("login: Server name length too large");
return FALSE;
}
}
if (ret < 0)
i_error("login: read() failed: %m");
i_error("login: Server name wasn't sent");
else {
i_error("login: Missing protocol from server name '%s'",
name);
return FALSE;
}
else {
return FALSE;
}
return FALSE;
}
return login_process_init_group(p);
}
return FALSE;
}
static void login_process_input(void *context)
{
struct login_process *p = context;
struct auth_process *auth_process;
struct login_auth_request *authreq;
struct master_login_request req;
int client_fd;
/* we want to read the group */
if (!login_process_read_group(p))
return;
}
if (ret == 0) {
/* disconnected, ie. the login process died */
} else if (ret > 0) {
/* req wasn't fully read */
i_error("login: fd_read() couldn't read all req");
} else {
i_error("login: fd_read() failed: %m");
}
if (client_fd != -1) {
i_error("close(mail client) failed: %m");
}
return;
}
if (client_fd == -1) {
/* just a notification that the login process */
if (!p->initialized) {
/* initialization notify */
p->initialized = TRUE;;
} else {
/* not listening for new connections anymore */
}
return;
}
/* ask the cookie from the auth process */
p->refcount++;
if (auth_process == NULL) {
i_error("login: Authentication process %u doesn't exist",
} else {
}
}
static struct login_process *
{
struct login_process *p;
p->refcount = 1;
sizeof(struct master_login_reply)*10,
FALSE);
p->group->listening_processes++;
}
return p;
}
static void login_process_remove_from_lists(struct login_process *p)
{
return;
if (p == p->group->oldest_nonlisten_process)
else
if (p == p->group->newest_nonlisten_process)
else
}
static void login_process_destroy(struct login_process *p)
{
if (p->destroyed)
return;
i_error("Login process died too early - shutting down");
}
p->group->listening_processes--;
o_stream_close(p->output);
i_error("close(login) failed: %m");
if (!p->listening)
if (p->pid != 0)
}
static void login_process_unref(struct login_process *p)
{
if (--p->refcount > 0)
return;
o_stream_unref(p->output);
i_free(p);
}
{
/* setup access environment - needs to be done after
clean_child_process() since it clears environment */
0, 0);
env_put("DOVECOT_MASTER=1");
if (!set->ssl_disable) {
}
if (set->disable_plaintext_auth)
env_put("DISABLE_PLAINTEXT_AUTH=1");
if (set->verbose_proctitle)
env_put("VERBOSE_PROCTITLE=1");
if (set->verbose_ssl)
env_put("VERBOSE_SSL=1");
if (set->login_process_per_connection) {
env_put("PROCESS_PER_CONNECTION=1");
env_put("MAX_LOGGING_USERS=1");
} else {
}
}
{
int fd[2];
}
i_fatal("Login process must not run as root");
/* create communication to process with a socket pair */
i_error("socketpair() failed: %m");
return -1;
}
if (pid < 0) {
i_error("fork() failed: %m");
return -1;
}
if (pid != 0) {
/* master */
return pid;
}
/* move the listen handle */
i_fatal("login: dup2(listen_fd) failed: %m");
/* move the SSL listen handle */
i_fatal("login: dup2(ssl_listen_fd) failed: %m");
/* move communication handle */
i_fatal("login: dup2(master) failed: %m");
/* no chrooting, but still change to the directory */
i_fatal("chdir(%s) failed: %m",
}
}
/* make sure we don't leak syslog fd, but do it last so that
any errors above will be logged */
closelog();
/* hide the path, it's ugly */
else
argv[0]++;
return -1;
}
{
struct login_process *p;
/* don't start raising the process count if they're dying all
the time */
p->group->wanted_processes_count = 0;
}
void *context __attr_unused__)
{
}
void login_processes_destroy_all(void)
{
while (login_groups != NULL) {
}
}
{
/* create max. one process every second, that way if it keeps
dying all the time we don't eat all cpu with fork()ing. */
if (group->listening_processes <
(void)create_login_process(group);
return;
}
/* we want to respond fast when multiple clients are connecting
at once, but we also want to prevent fork-bombing. use the
same method as apache: check once a second if we need new
processes. if yes and we've used all the existing processes,
double their amount (unless we've hit the high limit).
Then for each second that didn't use all existing processes,
drop the max. process count by one. */
} else if (group->listening_processes == 0)
else if (group->wanted_processes_count >
if (group->wanted_processes_count >
}
(void)create_login_process(group);
}
static void
{
struct login_group *group;
if (login_groups == NULL)
}
static int login_process_send_env(struct login_process *p)
{
extern char **environ;
char **env;
int ret = 0;
/* this will clear our environment. luckily we don't need it. */
ret = -1;
break;
}
}
ret = -1;
env_clean();
return ret;
}
static int login_process_init_group(struct login_process *p)
{
p->group->listening_processes++;
if (login_process_send_env(p) < 0) {
i_error("login: Couldn't send environment");
return FALSE;
}
return TRUE;
}
{
struct login_process *p;
int fd;
if (fd < 0) {
if (fd < -1)
i_fatal("accept(inetd_login_fd) failed: %m");
} else {
p->initialized = TRUE;
}
}
void login_processes_init(void)
{
auth_id_counter = 0;
login_pid_counter = 0;
login_groups = NULL;
if (!IS_INETD()) {
} else {
}
}
void login_processes_deinit(void)
{
}