1200062Sed/*-
2200062Sed * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
3200062Sed * All rights reserved.
4200062Sed *
5200062Sed * Redistribution and use in source and binary forms, with or without
6200062Sed * modification, are permitted provided that the following conditions
7200062Sed * are met:
8200062Sed * 1. Redistributions of source code must retain the above copyright
9200062Sed *    notice, this list of conditions and the following disclaimer.
10200062Sed * 2. Redistributions in binary form must reproduce the above copyright
11200062Sed *    notice, this list of conditions and the following disclaimer in the
12200062Sed *    documentation and/or other materials provided with the distribution.
13200062Sed *
14200062Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15200062Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16200062Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17200062Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18200062Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19200062Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20200062Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21200062Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22200062Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23200062Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24200062Sed * SUCH DAMAGE.
25200062Sed */
26200062Sed
27200062Sed#include <sys/cdefs.h>
28200062Sed__FBSDID("$FreeBSD$");
29200062Sed
30200062Sed#include <sys/wait.h>
31200062Sed
32200062Sed#include <errno.h>
33200062Sed#include <signal.h>
34200062Sed#include <stdlib.h>
35200062Sed#include <sysexits.h>
36200062Sed#include <unistd.h>
37202215Sed#include "ulog.h"
38200062Sed
39200062Sed#define	_PATH_ULOG_HELPER	"/usr/libexec/ulog-helper"
40200062Sed
41200062Sed/*
42200062Sed * Registering login sessions.
43200062Sed */
44200062Sed
45200062Sedstatic void
46200062Sedulog_exec_helper(int fd, char const * const argv[])
47200062Sed{
48200062Sed	sigset_t oblock, nblock;
49200062Sed	pid_t pid, wpid;
50200062Sed	int status;
51200062Sed
52200062Sed	/* Block SIGCHLD. */
53200062Sed	sigemptyset(&nblock);
54200062Sed	sigaddset(&nblock, SIGCHLD);
55200062Sed	sigprocmask(SIG_BLOCK, &nblock, &oblock);
56200062Sed
57200062Sed	switch (pid = fork()) {
58200062Sed	case -1:
59200062Sed		break;
60200062Sed	case 0:
61200062Sed		/* Execute helper program. */
62200062Sed		if (dup2(fd, STDIN_FILENO) == -1)
63200062Sed			exit(EX_UNAVAILABLE);
64200062Sed		sigprocmask(SIG_SETMASK, &oblock, NULL);
65200062Sed		execv(_PATH_ULOG_HELPER, __DECONST(char * const *, argv));
66200062Sed		exit(EX_UNAVAILABLE);
67200062Sed	default:
68200062Sed		/* Wait for helper to finish. */
69200062Sed		do {
70200062Sed			wpid = waitpid(pid, &status, 0);
71200062Sed		} while (wpid == -1 && errno == EINTR);
72200062Sed		break;
73200062Sed	}
74200062Sed
75200062Sed	sigprocmask(SIG_SETMASK, &oblock, NULL);
76200062Sed}
77200062Sed
78200062Sedvoid
79200062Sedulog_login_pseudo(int fd, const char *host)
80200062Sed{
81200062Sed	char const * const args[4] = { "ulog-helper", "login", host, NULL };
82200062Sed
83200062Sed	ulog_exec_helper(fd, args);
84200062Sed}
85200062Sed
86200062Sedvoid
87200062Sedulog_logout_pseudo(int fd)
88200062Sed{
89200062Sed	char const * const args[3] = { "ulog-helper", "logout", NULL };
90200062Sed
91200062Sed	ulog_exec_helper(fd, args);
92200062Sed}
93