xref: /freebsd/lib/libulog/utempter.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
104b0c5bbSEd Schouten /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro 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>
30691ac623SEd Schouten #include "ulog.h"
3104b0c5bbSEd Schouten #include "utempter.h"
3204b0c5bbSEd Schouten 
3304b0c5bbSEd Schouten static int last_fd = -1;
3404b0c5bbSEd Schouten 
3504b0c5bbSEd Schouten int
utempter_add_record(int fd,const char * host)3604b0c5bbSEd Schouten utempter_add_record(int fd, const char *host)
3704b0c5bbSEd Schouten {
3804b0c5bbSEd Schouten 
3904b0c5bbSEd Schouten 	ulog_login_pseudo(fd, host);
4004b0c5bbSEd Schouten 	last_fd = fd;
4104b0c5bbSEd Schouten 	return (0);
4204b0c5bbSEd Schouten }
4304b0c5bbSEd Schouten 
4404b0c5bbSEd Schouten int
utempter_remove_added_record(void)4504b0c5bbSEd Schouten utempter_remove_added_record(void)
4604b0c5bbSEd Schouten {
4704b0c5bbSEd Schouten 
4804b0c5bbSEd Schouten 	if (last_fd < 0)
4904b0c5bbSEd Schouten 		return (0);
5004b0c5bbSEd Schouten 	ulog_logout_pseudo(last_fd);
5104b0c5bbSEd Schouten 	last_fd = -1;
5204b0c5bbSEd Schouten 	return (0);
5304b0c5bbSEd Schouten }
5404b0c5bbSEd Schouten 
5504b0c5bbSEd Schouten int
utempter_remove_record(int fd)5604b0c5bbSEd Schouten utempter_remove_record(int fd)
5704b0c5bbSEd Schouten {
5804b0c5bbSEd Schouten 
5904b0c5bbSEd Schouten 	ulog_logout_pseudo(fd);
6004b0c5bbSEd Schouten 	if (last_fd == fd)
6104b0c5bbSEd Schouten 		last_fd = -1;
6204b0c5bbSEd Schouten 	return (0);
6304b0c5bbSEd Schouten }
6404b0c5bbSEd Schouten 
6504b0c5bbSEd Schouten void
addToUtmp(const char * pty __unused,const char * host,int fd)6604b0c5bbSEd Schouten addToUtmp(const char *pty __unused, const char *host, int fd)
6704b0c5bbSEd Schouten {
6804b0c5bbSEd Schouten 
6904b0c5bbSEd Schouten 	utempter_add_record(fd, host);
7004b0c5bbSEd Schouten }
7104b0c5bbSEd Schouten 
7204b0c5bbSEd Schouten void
removeFromUtmp(void)7304b0c5bbSEd Schouten removeFromUtmp(void)
7404b0c5bbSEd Schouten {
7504b0c5bbSEd Schouten 
7604b0c5bbSEd Schouten 	utempter_remove_added_record();
7704b0c5bbSEd Schouten }
7804b0c5bbSEd Schouten 
7904b0c5bbSEd Schouten void
removeLineFromUtmp(const char * pty __unused,int fd)8004b0c5bbSEd Schouten removeLineFromUtmp(const char *pty __unused, int fd)
8104b0c5bbSEd Schouten {
8204b0c5bbSEd Schouten 
8304b0c5bbSEd Schouten 	utempter_remove_record(fd);
8404b0c5bbSEd Schouten }
85