xref: /freebsd/lib/libulog/utempter.c (revision 5e53a4f90f82c4345f277dd87cc9292f26e04a29)
104b0c5bbSEd Schouten /*-
2*5e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*5e53a4f9SPedro F. Giffuni  *
404b0c5bbSEd Schouten  * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
504b0c5bbSEd Schouten  * All rights reserved.
604b0c5bbSEd Schouten  *
704b0c5bbSEd Schouten  * Redistribution and use in source and binary forms, with or without
804b0c5bbSEd Schouten  * modification, are permitted provided that the following conditions
904b0c5bbSEd Schouten  * are met:
1004b0c5bbSEd Schouten  * 1. Redistributions of source code must retain the above copyright
1104b0c5bbSEd Schouten  *    notice, this list of conditions and the following disclaimer.
1204b0c5bbSEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
1304b0c5bbSEd Schouten  *    notice, this list of conditions and the following disclaimer in the
1404b0c5bbSEd Schouten  *    documentation and/or other materials provided with the distribution.
1504b0c5bbSEd Schouten  *
1604b0c5bbSEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1704b0c5bbSEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1804b0c5bbSEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1904b0c5bbSEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2004b0c5bbSEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2104b0c5bbSEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2204b0c5bbSEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2304b0c5bbSEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2404b0c5bbSEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2504b0c5bbSEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2604b0c5bbSEd Schouten  * SUCH DAMAGE.
2704b0c5bbSEd Schouten  */
2804b0c5bbSEd Schouten 
2904b0c5bbSEd Schouten #include <sys/cdefs.h>
3004b0c5bbSEd Schouten __FBSDID("$FreeBSD$");
3104b0c5bbSEd Schouten 
32691ac623SEd Schouten #include "ulog.h"
3304b0c5bbSEd Schouten #include "utempter.h"
3404b0c5bbSEd Schouten 
3504b0c5bbSEd Schouten static int last_fd = -1;
3604b0c5bbSEd Schouten 
3704b0c5bbSEd Schouten int
3804b0c5bbSEd Schouten utempter_add_record(int fd, const char *host)
3904b0c5bbSEd Schouten {
4004b0c5bbSEd Schouten 
4104b0c5bbSEd Schouten 	ulog_login_pseudo(fd, host);
4204b0c5bbSEd Schouten 	last_fd = fd;
4304b0c5bbSEd Schouten 	return (0);
4404b0c5bbSEd Schouten }
4504b0c5bbSEd Schouten 
4604b0c5bbSEd Schouten int
4704b0c5bbSEd Schouten utempter_remove_added_record(void)
4804b0c5bbSEd Schouten {
4904b0c5bbSEd Schouten 
5004b0c5bbSEd Schouten 	if (last_fd < 0)
5104b0c5bbSEd Schouten 		return (0);
5204b0c5bbSEd Schouten 	ulog_logout_pseudo(last_fd);
5304b0c5bbSEd Schouten 	last_fd = -1;
5404b0c5bbSEd Schouten 	return (0);
5504b0c5bbSEd Schouten }
5604b0c5bbSEd Schouten 
5704b0c5bbSEd Schouten int
5804b0c5bbSEd Schouten utempter_remove_record(int fd)
5904b0c5bbSEd Schouten {
6004b0c5bbSEd Schouten 
6104b0c5bbSEd Schouten 	ulog_logout_pseudo(fd);
6204b0c5bbSEd Schouten 	if (last_fd == fd)
6304b0c5bbSEd Schouten 		last_fd = -1;
6404b0c5bbSEd Schouten 	return (0);
6504b0c5bbSEd Schouten }
6604b0c5bbSEd Schouten 
6704b0c5bbSEd Schouten void
6804b0c5bbSEd Schouten addToUtmp(const char *pty __unused, const char *host, int fd)
6904b0c5bbSEd Schouten {
7004b0c5bbSEd Schouten 
7104b0c5bbSEd Schouten 	utempter_add_record(fd, host);
7204b0c5bbSEd Schouten }
7304b0c5bbSEd Schouten 
7404b0c5bbSEd Schouten void
7504b0c5bbSEd Schouten removeFromUtmp(void)
7604b0c5bbSEd Schouten {
7704b0c5bbSEd Schouten 
7804b0c5bbSEd Schouten 	utempter_remove_added_record();
7904b0c5bbSEd Schouten }
8004b0c5bbSEd Schouten 
8104b0c5bbSEd Schouten void
8204b0c5bbSEd Schouten removeLineFromUtmp(const char *pty __unused, int fd)
8304b0c5bbSEd Schouten {
8404b0c5bbSEd Schouten 
8504b0c5bbSEd Schouten 	utempter_remove_record(fd);
8604b0c5bbSEd Schouten }
87