/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Portions Copyright (c) 1983, 1995, 1996 Eric P. Allman
*
* This code is derived from software contributed to Berkeley by
* Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* why do we skip asterisks!?!? */
if (*gecos == '*')
gecos++;
bp = buf;
/* copy gecos, interpolating & to be full name */
for (p = gecos; *p != '\0'; p++) {
if (bp >= &buf[buflen - 1]) {
/* buffer overflow - just use login name */
snprintf(buf, buflen, "%s", login);
buf[buflen - 1] = '\0';
return;
}
if (*p == '&') {
/* interpolate full name */
snprintf(bp, buflen - (bp - buf), "%s", login);
*bp = toupper((unsigned char)*bp);
bp += strlen(bp);
}
else
*bp++ = *p;
}
*bp = '\0';
}
void
enter_lastlog(PERSON *pn)
{
WHERE *w;
static int opened;
#ifdef SUPPORT_UTMPX
# define ll_time ll_tv.tv_sec
# define UT_LINESIZE _UTX_LINESIZE
# define UT_HOSTSIZE _UTX_HOSTSIZE
static DB *lldb = NULL;
DBT key, data;
struct lastlogx ll;
#else
static int fd;
struct lastlog ll;
#endif
char doit = 0;
(void)memset(&ll, 0, sizeof(ll));
/* some systems may not maintain lastlog, don't report errors. */
if (!opened) {
#ifdef SUPPORT_UTMPX
lldb = dbopen(_PATH_LASTLOGX, O_RDONLY|O_SHLOCK, 0, DB_HASH, NULL);
#else
fd = open(_PATH_LASTLOG, O_RDONLY, 0);
#endif
opened = 1;
}
#ifdef SUPPORT_UTMPX
if (lldb != NULL) {
key.data = &pn->uid;
key.size = sizeof(pn->uid);
if ((*lldb->get)(lldb, &key, &data, 0) == 0 &&
data.size == sizeof(ll))
(void)memcpy(&ll, data.data, sizeof(ll));
}
#else
if (fd == -1 ||
lseek(fd, (off_t)pn->uid * sizeof(ll), SEEK_SET) !=
(off_t)pn->uid * (off_t)sizeof(ll) ||
read(fd, (char *)&ll, sizeof(ll)) != sizeof(ll)) {
/* as if never logged in */
ll.ll_line[0] = ll.ll_host[0] = '\0';
ll.ll_time = 0;
}
#endif
if ((w = pn->whead) == NULL)
doit = 1;
else if (ll.ll_time != 0) {
/* if last login is earlier than some current login */
for (; !doit && w != NULL; w = w->next)
if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
doit = 1;
/*
* and if it's not any of the current logins
* can't use time comparison because there may be a small
* discrepancy since login calls time() twice
*/
for (w = pn->whead; doit && w != NULL; w = w->next)
if (w->info == LOGGEDIN &&
strncmp(w->tty, ll.ll_line, UT_LINESIZE) == 0)
doit = 0;
}
if (doit) {
w = walloc(pn);
w->info = LASTLOG;
if ((w->tty = malloc(UT_LINESIZE + 1)) == NULL)
err(1, NULL);
memcpy(w->tty, ll.ll_line, UT_LINESIZE);
w->tty[UT_LINESIZE] = '\0';
if ((w->host = malloc(UT_HOSTSIZE + 1)) == NULL)
err(1, NULL);
memcpy(w->host, ll.ll_host, UT_HOSTSIZE);
w->host[UT_HOSTSIZE] = '\0';
w->loginat = ll.ll_time;
}
}
void
enter_where(struct utmpentry *ep, PERSON *pn)
{
WHERE *w = walloc(pn);