<commit_before>/*
 * NTLM and Negotiate authentication mechanisms,
 * using Samba winbind daemon
 *
 * Copyright (c) 2007 <NAME> <<EMAIL>>
 *
 * This software is released under the MIT license.
 */

#include "common.h"
#include "lib-signals.h"
#include "mech.h"
#include "str.h"
#include "buffer.h"
#include "base64.h"
#include "istream.h"
#include "ostream.h"

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#define DEFAULT_WINBIND_HELPER_PATH "/usr/bin/ntlm_auth"

enum helper_result {
	HR_OK	= 0,	/* OK or continue */
	HR_FAIL	= -1,	/* authentication failed */
	HR_RESTART = -2	/* FAIL + try to restart helper */
};

struct winbind_helper {
	const char *param;
	pid_t pid;

	struct istream *in_pipe;
	struct ostream *out_pipe;
};

struct winbind_auth_request {
	struct auth_request auth_request;

	struct winbind_helper *winbind;
	bool continued;
};

static struct winbind_helper winbind_ntlm_context = {
	"--helper-protocol=squid-2.5-ntlmssp", -1, NULL, NULL
};
static struct winbind_helper winbind_spnego_context = {
	"--helper-protocol=gss-spnego", -1, NULL, NULL
};

static bool sigchld_handler_set = FALSE;

static void winbind_helper_disconnect(struct winbind_helper *winbind)
{
	if (winbind->in_pipe != NULL)
		i_stream_destroy(&winbind->in_pipe);
	if (winbind->out_pipe != NULL)
		o_stream_destroy(&winbind->out_pipe);
}

static void winbind_wait_pid(struct winbind_helper *winbind)
{
	int status, ret;

	if (winbind->pid == -1)
		return;

	if ((ret = waitpid(winbind->pid, &status, WNOHANG)) <= 0) {
		if (ret < 0 && errno != ECHILD && errno != EINTR)
			i_error("waitpid() failed: %m");
		return;
	}

	if (WIFSIGNALED(status)) {
		i_error("winbind: ntlm_auth died with signal %d",
			WTERMSIG(status));
	} else if (WIFEXITED(status)) {
		i_error("winbind: ntlm_auth exited with exit code %d",
			WEXITSTATUS(status));
	} else {
		/* shouldn't happen */
		i_error("winbind: ntlm_auth exited with status %d",
			status);
	}
	winbind->pid = -1;
}

static void sigchld_handler(int signo __attr_unused__,
			    void *context __attr_unused__)
{
	winbind_wait_pid(&winbind_ntlm_context);
	winbind_wait_pid(&winbind_spnego_context);
}

static void winbind_helper_connect(struct winbind_helper *winbind)
{
	int infd[2], outfd[2];
	pid_t pid;

	if (winbind->in_pipe != NULL || winbind->pid != -1)
		return;

	if (pipe(infd) < 0) {
		i_error("pipe() failed: %m");
		return;
	}
	if (pipe(outfd) < 0) {
		(void)close(infd[0]); (void)close(infd[1]);
		return;
	}

	pid = fork();
	if (pid < 0) {
		i_error("fork() failed: %m");
		(void)close(infd[0]); (void)close(infd[1]);
		(void)close(outfd[0]); (void)close(outfd[1]);
		return;
	}

	if (pid == 0) {
		/* child */
		const char *helper_path, *args[3];

		(void)close(infd[0]);
		(void)close(outfd[1]);

		if (dup2(outfd[0], STDIN_FILENO) < 0 ||
		    dup2(infd[1], STDOUT_FILENO) < 0)
			i_fatal("dup2() failed: %m");

		helper_path = getenv("WINBIND_HELPER_PATH");
		if (helper_path == NULL)
			helper_path = DEFAULT_WINBIND_HELPER_PATH;

		args[0] = helper_path;
		args[1] = winbind->param;
		args[2] = NULL;
		execv(args[0], (void *)args);
		i_fatal("execv(%s) failed: %m", args[0]);
	}

	/* parent */
	(void)close(infd[1]);
	(void)close(outfd[0]);

	winbind->in_pipe =
		i_stream_create_fd(infd[0], AUTH_CLIENT_MAX_LINE_LENGTH, TRUE);
	winbind->out_pipe =
		o_stream_create_fd(outfd[1], (size_t)-1, TRUE);

	if (!sigchld_handler_set) {
		sigchld_handler_set = TRUE;
		lib_signals_set_handler(SIGCHLD, TRUE, sigchld_handler, NULL);
	}
}

static enum helper_result
do_auth_continue(struct auth_request *auth_request,
		 const unsigned char *data, size_t data_size)
{
	struct winbind_auth_request *request =
		(struct winbind_auth_request *)auth_request;
	struct istream *in_pipe = request->winbind->in_pipe;
	string_t *str;
	char *answer;<commit_msg>One more pid fix.<commit_after>/*
 * NTLM and Negotiate authentication mechanisms,
 * using Samba winbind daemon
 *
 * Copyright (c) 2007 <NAME> <<EMAIL>>
 *
 * This software is released under the MIT license.
 */

#include "common.h"
#include "lib-signals.h"
#include "mech.h"
#include "str.h"
#include "buffer.h"
#include "base64.h"
#include "istream.h"
#include "ostream.h"

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

#define DEFAULT_WINBIND_HELPER_PATH "/usr/bin/ntlm_auth"

enum helper_result {
	HR_OK	= 0,	/* OK or continue */
	HR_FAIL	= -1,	/* authentication failed */
	HR_RESTART = -2	/* FAIL + try to restart helper */
};

struct winbind_helper {
	const char *param;
	pid_t pid;

	struct istream *in_pipe;
	struct ostream *out_pipe;
};

struct winbind_auth_request {
	struct auth_request auth_request;

	struct winbind_helper *winbind;
	bool continued;
};

static struct winbind_helper winbind_ntlm_context = {
	"--helper-protocol=squid-2.5-ntlmssp", -1, NULL, NULL
};
static struct winbind_helper winbind_spnego_context = {
	"--helper-protocol=gss-spnego", -1, NULL, NULL
};

static bool sigchld_handler_set = FALSE;

static void winbind_helper_disconnect(struct winbind_helper *winbind)
{
	if (winbind->in_pipe != NULL)
		i_stream_destroy(&winbind->in_pipe);
	if (winbind->out_pipe != NULL)
		o_stream_destroy(&winbind->out_pipe);
}

static void winbind_wait_pid(struct winbind_helper *winbind)
{
	int status, ret;

	if (winbind->pid == -1)
		return;

	if ((ret = waitpid(winbind->pid, &status, WNOHANG)) <= 0) {
		if (ret < 0 && errno != ECHILD && errno != EINTR)
			i_error("waitpid() failed: %m");
		return;
	}

	if (WIFSIGNALED(status)) {
		i_error("winbind: ntlm_auth died with signal %d",
			WTERMSIG(status));
	} else if (WIFEXITED(status)) {
		i_error("winbind: ntlm_auth exited with exit code %d",
			WEXITSTATUS(status));
	} else {
		/* shouldn't happen */
		i_error("winbind: ntlm_auth exited with status %d",
			status);
	}
	winbind->pid = -1;
}

static void sigchld_handler(int signo __attr_unused__,
			    void *context __attr_unused__)
{
	winbind_wait_pid(&winbind_ntlm_context);
	winbind_wait_pid(&winbind_spnego_context);
}

static void winbind_helper_connect(struct winbind_helper *winbind)
{
	int infd[2], outfd[2];
	pid_t pid;

	if (winbind->in_pipe != NULL || winbind->pid != -1)
		return;

	if (pipe(infd) < 0) {
		i_error("pipe() failed: %m");
		return;
	}
	if (pipe(outfd) < 0) {
		(void)close(infd[0]); (void)close(infd[1]);
		return;
	}

	pid = fork();
	if (pid < 0) {
		i_error("fork() failed: %m");
		(void)close(infd[0]); (void)close(infd[1]);
		(void)close(outfd[0]); (void)close(outfd[1]);
		return;
	}

	if (pid == 0) {
		/* child */
		const char *helper_path, *args[3];

		(void)close(infd[0]);
		(void)close(outfd[1]);

		if (dup2(outfd[0], STDIN_FILENO) < 0 ||
		    dup2(infd[1], STDOUT_FILENO) < 0)
			i_fatal("dup2() failed: %m");

		helper_path = getenv("WINBIND_HELPER_PATH");
		if (helper_path == NULL)
			helper_path = DEFAULT_WINBIND_HELPER_PATH;

		args[0] = helper_path;
		args[1] = winbind->param;
		args[2] = NULL;
		execv(args[0], (void *)args);
		i_fatal("execv(%s) failed: %m", args[0]);
	}

	/* parent */
	(void)close(infd[1]);
	(void)close(outfd[0]);

	winbind->pid = pid;
	winbind->in_pipe =
		i_stream_create_fd(infd[0], AUTH_CLIENT_MAX_LINE_LENGTH, TRUE);
	winbind->out_pipe =
		o_stream_create_fd(outfd[1], (size_t)-1, TRUE);

	if (!sigchld_handler_set) {
		sigchld_handler_set = TRUE;
		lib_signals_set_handler(SIGCHLD, TRUE, sigchld_handler, NULL);
	}
}

static enum helper_result
do_auth_continue(struct auth_request *auth_request,
		 const unsigned char *data, size_t data_size)
{
	struct winbind_auth_request *request =
		(struct winbind_auth_request *)auth_request;
	struct istream *in_pipe = request->winbind->in_pipe;
	string_t *str;
	char *answer;
{'fraction_non_alphanumeric': 0.15437818086911131, 'fraction_numerical': 0.013049719431032232, 'mean_word_length': 4.710879284649776, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7356221', 'n_tokens_mistral': 3231, 'n_tokens_neox': 3035, 'n_words': 644}