12208eadfSEd Schouten /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
42208eadfSEd Schouten * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
52208eadfSEd Schouten * All rights reserved.
62208eadfSEd Schouten *
72208eadfSEd Schouten * Redistribution and use in source and binary forms, with or without
82208eadfSEd Schouten * modification, are permitted provided that the following conditions
92208eadfSEd Schouten * are met:
102208eadfSEd Schouten * 1. Redistributions of source code must retain the above copyright
112208eadfSEd Schouten * notice, this list of conditions and the following disclaimer.
122208eadfSEd Schouten * 2. Redistributions in binary form must reproduce the above copyright
132208eadfSEd Schouten * notice, this list of conditions and the following disclaimer in the
142208eadfSEd Schouten * documentation and/or other materials provided with the distribution.
152208eadfSEd Schouten *
162208eadfSEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
172208eadfSEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
182208eadfSEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
192208eadfSEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
202208eadfSEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
212208eadfSEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
222208eadfSEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
232208eadfSEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
242208eadfSEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
252208eadfSEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
262208eadfSEd Schouten * SUCH DAMAGE.
272208eadfSEd Schouten */
282208eadfSEd Schouten
292208eadfSEd Schouten #include <sys/cdefs.h>
302208eadfSEd Schouten #include <sys/wait.h>
312208eadfSEd Schouten
322208eadfSEd Schouten #include <errno.h>
332208eadfSEd Schouten #include <signal.h>
342208eadfSEd Schouten #include <stdlib.h>
352208eadfSEd Schouten #include <sysexits.h>
362208eadfSEd Schouten #include <unistd.h>
37691ac623SEd Schouten #include "ulog.h"
382208eadfSEd Schouten
392208eadfSEd Schouten #define _PATH_ULOG_HELPER "/usr/libexec/ulog-helper"
402208eadfSEd Schouten
412208eadfSEd Schouten /*
422208eadfSEd Schouten * Registering login sessions.
432208eadfSEd Schouten */
442208eadfSEd Schouten
452208eadfSEd Schouten static void
ulog_exec_helper(int fd,char const * const argv[])462208eadfSEd Schouten ulog_exec_helper(int fd, char const * const argv[])
472208eadfSEd Schouten {
482208eadfSEd Schouten sigset_t oblock, nblock;
492208eadfSEd Schouten pid_t pid, wpid;
502208eadfSEd Schouten int status;
512208eadfSEd Schouten
522208eadfSEd Schouten /* Block SIGCHLD. */
532208eadfSEd Schouten sigemptyset(&nblock);
542208eadfSEd Schouten sigaddset(&nblock, SIGCHLD);
552208eadfSEd Schouten sigprocmask(SIG_BLOCK, &nblock, &oblock);
562208eadfSEd Schouten
572208eadfSEd Schouten switch (pid = fork()) {
582208eadfSEd Schouten case -1:
592208eadfSEd Schouten break;
602208eadfSEd Schouten case 0:
612208eadfSEd Schouten /* Execute helper program. */
622208eadfSEd Schouten if (dup2(fd, STDIN_FILENO) == -1)
632208eadfSEd Schouten exit(EX_UNAVAILABLE);
642208eadfSEd Schouten sigprocmask(SIG_SETMASK, &oblock, NULL);
652208eadfSEd Schouten execv(_PATH_ULOG_HELPER, __DECONST(char * const *, argv));
662208eadfSEd Schouten exit(EX_UNAVAILABLE);
672208eadfSEd Schouten default:
682208eadfSEd Schouten /* Wait for helper to finish. */
692208eadfSEd Schouten do {
702208eadfSEd Schouten wpid = waitpid(pid, &status, 0);
712208eadfSEd Schouten } while (wpid == -1 && errno == EINTR);
722208eadfSEd Schouten break;
732208eadfSEd Schouten }
742208eadfSEd Schouten
752208eadfSEd Schouten sigprocmask(SIG_SETMASK, &oblock, NULL);
762208eadfSEd Schouten }
772208eadfSEd Schouten
782208eadfSEd Schouten void
ulog_login_pseudo(int fd,const char * host)792208eadfSEd Schouten ulog_login_pseudo(int fd, const char *host)
802208eadfSEd Schouten {
812208eadfSEd Schouten char const * const args[4] = { "ulog-helper", "login", host, NULL };
822208eadfSEd Schouten
832208eadfSEd Schouten ulog_exec_helper(fd, args);
842208eadfSEd Schouten }
852208eadfSEd Schouten
862208eadfSEd Schouten void
ulog_logout_pseudo(int fd)872208eadfSEd Schouten ulog_logout_pseudo(int fd)
882208eadfSEd Schouten {
892208eadfSEd Schouten char const * const args[3] = { "ulog-helper", "logout", NULL };
902208eadfSEd Schouten
912208eadfSEd Schouten ulog_exec_helper(fd, args);
922208eadfSEd Schouten }
93