#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <paths.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <vis.h>
#include "finger.h"
#include "extern.h"
char *estrdup(char *);
WHERE *walloc(PERSON *pn);
void find_idle_and_ttywrite(WHERE *);
void userinfo(PERSON *, struct passwd *);
void
find_idle_and_ttywrite(WHERE *w)
{
struct stat sb;
(void)snprintf(tbuf, sizeof(tbuf), "%s%s", _PATH_DEV, w->tty);
if (stat(tbuf, &sb) == -1) {
w->idletime = 0;
w->writable = 0;
return;
}
w->idletime = now < sb.st_atime ? 0 : now - sb.st_atime;
#define TALKABLE 0220
w->writable = ((sb.st_mode & TALKABLE) == TALKABLE);
}
char *
estrdup(char *s)
{
char *p = strdup(s);
if (!p)
err(1, "strdup");
return (p);
}
void
userinfo(PERSON *pn, struct passwd *pw)
{
char *p;
char *bp, name[1024];
struct stat sb;
int len;
pn->realname = pn->office = pn->officephone = pn->homephone = NULL;
pn->mailrecv = -1;
pn->uid = pw->pw_uid;
pn->name = estrdup(pw->pw_name);
pn->dir = estrdup(pw->pw_dir);
pn->shell = estrdup(pw->pw_shell);
(void)strlcpy(bp = tbuf, pw->pw_gecos, sizeof(tbuf));
if (!(p = strsep(&bp, ",")))
return;
expandusername(p, pw->pw_name, name, sizeof(name));
if (stravis(&pn->realname, name, VIS_SAFE|VIS_NOSLASH) == -1)
err(1, "stravis");
if ((p = strsep(&bp, ",")) && *p) {
if (stravis(&pn->office, p, VIS_SAFE|VIS_NOSLASH) == -1)
err(1, "stravis");
}
if ((p = strsep(&bp, ",")) && *p) {
if (stravis(&pn->officephone, p, VIS_SAFE|VIS_NOSLASH) == -1)
err(1, "stravis");
}
if ((p = strsep(&bp, ",")) && *p) {
if (stravis(&pn->homephone, p, VIS_SAFE|VIS_NOSLASH) == -1)
err(1, "stravis");
}
len = snprintf(tbuf, sizeof(tbuf), "%s/%s", _PATH_MAILSPOOL,
pw->pw_name);
if (len >= 0 && len < sizeof(tbuf)) {
if (stat(tbuf, &sb) < 0) {
if (errno != ENOENT) {
warn("%s", tbuf);
return;
}
} else if (sb.st_size != 0) {
pn->mailrecv = sb.st_mtime;
pn->mailread = sb.st_atime;
}
}
}
int
match(struct passwd *pw, char *user)
{
char *p, *t;
char name[1024];
(void)strlcpy(p = tbuf, pw->pw_gecos, sizeof(tbuf));
if (!(p = strtok(p, ",")))
return (0);
expandusername(p, pw->pw_name, name, sizeof(name));
for (t = name; (p = strtok(t, "\t ")) != NULL; t = NULL)
if (!strcasecmp(p, user))
return (1);
return (0);
}
void
expandusername(char *gecos, char *login, char *buf, size_t buflen)
{
char *p, *bp;
if (*gecos == '*')
gecos++;
bp = buf;
for (p = gecos; *p != '\0'; p++) {
if (bp >= &buf[buflen - 1]) {
strlcpy(buf, login, buflen);
buf[buflen - 1] = '\0';
return;
}
if (*p == '&') {
strlcpy(bp, login, buflen - (bp - buf));
*bp = toupper((unsigned char)*bp);
bp += strlen(bp);
}
else
*bp++ = *p;
}
*bp = '\0';
}
void
enter_lastlog(PERSON *pn)
{
WHERE *w;
static int opened, fd;
struct lastlog ll;
char doit = 0;
if (!opened) {
fd = open(_PATH_LASTLOG, O_RDONLY);
opened = 1;
}
if (fd == -1 ||
pread(fd, &ll, sizeof(ll), (off_t)pn->uid * sizeof(ll)) !=
sizeof(ll)) {
ll.ll_line[0] = ll.ll_host[0] = '\0';
ll.ll_time = 0;
}
if ((w = pn->whead) == NULL)
doit = 1;
else if (ll.ll_time != 0) {
for (; !doit && w != NULL; w = w->next)
if (w->info == LOGGEDIN && w->loginat < ll.ll_time)
doit = 1;
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;
bcopy(ll.ll_line, w->tty, UT_LINESIZE);
w->tty[UT_LINESIZE] = 0;
bcopy(ll.ll_host, w->host, UT_HOSTSIZE);
w->host[UT_HOSTSIZE] = 0;
w->loginat = ll.ll_time;
}
}
void
enter_where(struct utmp *ut, PERSON *pn)
{
WHERE *w = walloc(pn);
w->info = LOGGEDIN;
bcopy(ut->ut_line, w->tty, UT_LINESIZE);
w->tty[UT_LINESIZE] = 0;
bcopy(ut->ut_host, w->host, UT_HOSTSIZE);
w->host[UT_HOSTSIZE] = 0;
w->loginat = (time_t)ut->ut_time;
find_idle_and_ttywrite(w);
}
PERSON *
enter_person(struct passwd *pw)
{
PERSON *pn, **pp;
for (pp = htab + hash(pw->pw_name);
*pp != NULL && strcmp((*pp)->name, pw->pw_name) != 0;
pp = &(*pp)->hlink)
;
if ((pn = *pp) == NULL) {
pn = palloc();
entries++;
if (phead == NULL)
phead = ptail = pn;
else {
ptail->next = pn;
ptail = pn;
}
pn->next = NULL;
pn->hlink = NULL;
*pp = pn;
userinfo(pn, pw);
pn->whead = NULL;
}
return (pn);
}
PERSON *
find_person(char *name)
{
PERSON *pn;
for (pn = htab[hash(name)];
pn != NULL && strncmp(pn->name, name, UT_NAMESIZE) != 0;
pn = pn->hlink)
;
return (pn);
}
int
hash(char *name)
{
int h, i;
h = 0;
for (i = UT_NAMESIZE; --i >= 0 && *name;)
h = ((h << 2 | h >> (HBITS - 2)) ^ *name++) & HMASK;
return (h);
}
PERSON *
palloc(void)
{
PERSON *p;
if ((p = malloc((u_int) sizeof(PERSON))) == NULL)
err(1, "malloc");
return (p);
}
WHERE *
walloc(PERSON *pn)
{
WHERE *w;
if ((w = malloc((u_int) sizeof(WHERE))) == NULL)
err(1, "malloc");
if (pn->whead == NULL)
pn->whead = pn->wtail = w;
else {
pn->wtail->next = w;
pn->wtail = w;
}
w->next = NULL;
return (w);
}
char *
prphone(char *num)
{
char *p;
int len;
static char pbuf[15];
for (p = num; *p; ++p)
if (!isdigit((unsigned char)*p))
return (num);
len = p - num;
p = pbuf;
switch (len) {
case 11:
*p++ = '+';
*p++ = *num++;
*p++ = '-';
case 10:
*p++ = *num++;
*p++ = *num++;
*p++ = *num++;
*p++ = '-';
case 7:
*p++ = *num++;
*p++ = *num++;
*p++ = *num++;
break;
case 5:
case 4:
*p++ = 'x';
*p++ = *num++;
break;
default:
return (num);
}
if (len != 4) {
*p++ = '-';
*p++ = *num++;
}
*p++ = *num++;
*p++ = *num++;
*p++ = *num++;
*p = '\0';
return (pbuf);
}