#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1989, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#if 0
#ifndef lint
static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
#endif
#endif
#include <sys/cdefs.h>
__FBSDID("$FreeBSD: src/usr.bin/finger/finger.c,v 1.36 2005/09/19 10:11:46 dds Exp $");
#include <sys/types.h>
#include <sys/socket.h>
#include <db.h>
#include <err.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <utmpx.h>
#include <locale.h>
#include "finger.h"
#include "pathnames.h"
DB *db;
time_t now;
int entries, gflag, kflag, lflag, mflag, pplan, sflag, oflag, Tflag;
sa_family_t family = PF_UNSPEC;
int d_first = -1;
char tbuf[1024];
int invoker_root = 0;
static void loginlist(void);
static int option(int, char **);
static void usage(void);
static void userlist(int, char **);
static int
option(int argc, char **argv)
{
int ch;
optind = 1;
#ifdef __APPLE__
while ((ch = getopt(argc, argv, "46gklmpsho")) != -1)
#else
while ((ch = getopt(argc, argv, "46gklmpshoT")) != -1)
#endif
switch(ch) {
case '4':
family = AF_INET;
break;
case '6':
family = AF_INET6;
break;
case 'g':
gflag = 1;
break;
case 'k':
kflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'm':
mflag = 1;
break;
case 'p':
pplan = 1;
break;
case 's':
sflag = 1;
break;
case 'h':
oflag = 0;
break;
case 'o':
oflag = 1;
break;
#ifndef __APPLE__
case 'T':
Tflag = 1;
break;
#endif
case '?':
default:
usage();
}
return optind;
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage: finger [-46gklmpshoT] [user ...] [user@host ...]\n");
exit(1);
}
int
main(int argc, char **argv)
{
int envargc, argcnt;
char *envargv[3];
struct passwd *pw;
static char myname[] = "finger";
if (getuid() == 0 || geteuid() == 0) {
invoker_root = 1;
if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
setgid(pw->pw_gid);
setuid(pw->pw_uid);
} else {
setgid(UNPRIV_UGID);
setuid(UNPRIV_UGID);
}
}
(void) setlocale(LC_ALL, "");
oflag = 1;
if ((envargv[1] = getenv("FINGER"))) {
envargc = 2;
envargv[0] = myname;
envargv[2] = NULL;
(void) option(envargc, envargv);
}
argcnt = option(argc, argv);
argc -= argcnt;
argv += argcnt;
(void)time(&now);
setpassent(1);
if (!*argv) {
if (!lflag)
sflag = 1;
loginlist();
if (entries == 0)
(void)printf("No one logged on.\n");
} else {
userlist(argc, argv);
if (!sflag)
lflag = 1;
}
if (entries) {
if (lflag)
lflag_print();
else
sflag_print();
}
return (0);
}
static void
loginlist(void)
{
PERSON *pn;
DBT data, key;
struct passwd *pw;
struct utmpx *user;
int r, sflag1;
char name[_UTX_USERSIZE + 1];
if (kflag)
errx(1, "can't list logins without reading utmpx");
setutxent();
name[_UTX_USERSIZE] = '\0';
while ((user = getutxent()) != NULL) {
if (!user->ut_user[0] || user->ut_type != USER_PROCESS)
continue;
if ((pn = find_person(user->ut_user)) == NULL) {
bcopy(user->ut_user, name, _UTX_USERSIZE);
if ((pw = getpwnam(name)) == NULL)
continue;
if (hide(pw))
continue;
pn = enter_person(pw);
}
enter_where(user, pn);
}
endutxent();
if (db && lflag)
for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
PERSON *tmp;
r = (*db->seq)(db, &key, &data, sflag1);
if (r == -1)
err(1, "db seq");
if (r == 1)
break;
memmove(&tmp, data.data, sizeof tmp);
enter_lastlog(tmp);
}
}
static void
userlist(int argc, char **argv)
{
PERSON *pn;
DBT data, key;
struct utmpx *user;
struct passwd *pw;
int r, sflag1, *used, *ip;
char **ap, **nargv, **np, **p;
FILE *conf_fp;
char conf_alias[LINE_MAX];
char *conf_realname;
int conf_length;
if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
(used = calloc(argc, sizeof(int))) == NULL)
err(1, NULL);
for (ap = p = argv, np = nargv; *p; ++p)
if (index(*p, '@'))
*np++ = *p;
else
*ap++ = *p;
*np++ = NULL;
*ap++ = NULL;
if (!*argv)
goto net;
for (p = argv, ip = used; *p; ++p, ++ip)
if (**p == '/') {
*ip = 1;
warnx("%s: no such user", *p);
}
if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
conf_length = strlen(conf_alias);
if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
continue;
conf_alias[conf_length] = '\0';
if ((conf_realname = strchr(conf_alias, ':')) == NULL)
continue;
*conf_realname = '\0';
for (p = argv; *p; ++p) {
if (strcmp(*p, conf_alias) == 0) {
if ((*p = strdup(conf_realname+1)) == NULL) {
err(1, NULL);
}
}
}
}
(void)fclose(conf_fp);
}
if (mflag)
for (p = argv, ip = used; *p; ++p, ++ip) {
if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
enter_person(pw);
else if (!*ip)
warnx("%s: no such user", *p);
}
}
else {
while ((pw = getpwent()) != NULL) {
for (p = argv, ip = used; *p; ++p, ++ip)
if (**p == '/' && *ip != 1
&& show_text("", *p, ""))
*ip = 1;
else if (match(pw, *p) && !hide(pw)) {
enter_person(pw);
*ip = 1;
}
}
for (p = argv, ip = used; *p; ++p, ++ip)
if (!*ip)
warnx("%s: no such user", *p);
}
net: for (p = nargv; *p;) {
netfinger(*p++);
if (*p || entries)
printf("\n");
}
if (entries == 0)
return;
if (kflag)
return;
setutxent();
while ((user = getutxent()) != NULL) {
if (!user->ut_user && user->ut_type != USER_PROCESS)
continue;
if ((pn = find_person(user->ut_user)) == NULL)
continue;
enter_where(user, pn);
}
endutxent();
if (db)
for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
PERSON *tmp;
r = (*db->seq)(db, &key, &data, sflag1);
if (r == -1)
err(1, "db seq");
if (r == 1)
break;
memmove(&tmp, data.data, sizeof tmp);
enter_lastlog(tmp);
}
}