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
30202215Sed#include <sys/param.h>
31200153Sed#include <sys/time.h>
32200062Sed#include <paths.h>
33202215Sed#include <sha.h>
34200062Sed#include <string.h>
35202215Sed#include <unistd.h>
36202215Sed#include <utmpx.h>
37202215Sed#include "ulog.h"
38200062Sed
39202215Sedstatic void
40202215Sedulog_fill(struct utmpx *utx, const char *line)
41200062Sed{
42202215Sed	SHA_CTX c;
43202215Sed	char id[SHA_DIGEST_LENGTH];
44200062Sed
45200062Sed	/* Remove /dev/ component. */
46200062Sed	if (strncmp(line, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
47200062Sed		line += sizeof _PATH_DEV - 1;
48200062Sed
49202215Sed	memset(utx, 0, sizeof *utx);
50200062Sed
51202215Sed	utx->ut_pid = getpid();
52202215Sed	gettimeofday(&utx->ut_tv, NULL);
53202215Sed	strncpy(utx->ut_line, line, sizeof utx->ut_line);
54202215Sed
55202215Sed	SHA1_Init(&c);
56202215Sed	SHA1_Update(&c, "libulog", 7);
57202215Sed	SHA1_Update(&c, utx->ut_line, sizeof utx->ut_line);
58234462Sed	SHA1_Final(id, &c);
59202215Sed
60202215Sed	memcpy(utx->ut_id, id, MIN(sizeof utx->ut_id, sizeof id));
61202215Sed}
62202215Sed
63202215Sedvoid
64202215Sedulog_login(const char *line, const char *user, const char *host)
65202215Sed{
66202215Sed	struct utmpx utx;
67202215Sed
68202215Sed	ulog_fill(&utx, line);
69200153Sed	utx.ut_type = USER_PROCESS;
70200153Sed	strncpy(utx.ut_user, user, sizeof utx.ut_user);
71200421Sed	if (host != NULL)
72200421Sed		strncpy(utx.ut_host, host, sizeof utx.ut_host);
73202215Sed	pututxline(&utx);
74200062Sed}
75200062Sed
76200062Sedvoid
77200062Sedulog_logout(const char *line)
78200062Sed{
79202215Sed	struct utmpx utx;
80200062Sed
81202215Sed	ulog_fill(&utx, line);
82200153Sed	utx.ut_type = DEAD_PROCESS;
83202215Sed	pututxline(&utx);
84200062Sed}
85