2006-04-28 23:53:56 +04:00
|
|
|
/* $NetBSD: finger.c,v 1.26 2006/04/28 19:53:56 christos Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1997-10-19 12:13:23 +04:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1993-10-07 22:58:28 +03:00
|
|
|
/*
|
2003-07-27 00:34:11 +04:00
|
|
|
* Luke Mewburn <lukem@NetBSD.org> added the following on 961121:
|
1996-11-21 09:01:47 +03:00
|
|
|
* - mail status ("No Mail", "Mail read:...", or "New Mail ...,
|
|
|
|
* Unread since ...".)
|
|
|
|
* - 4 digit phone extensions (3210 is printed as x3210.)
|
|
|
|
* - host/office toggling in short format with -h & -o.
|
|
|
|
* - short day names (`Tue' printed instead of `Jun 21' if the
|
|
|
|
* login time is < 6 days.
|
1993-10-07 22:58:28 +03:00
|
|
|
*/
|
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-19 12:13:23 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-10-19 12:13:23 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
|
|
|
|
#else
|
2006-04-28 23:53:56 +04:00
|
|
|
__RCSID("$NetBSD: finger.c,v 1.26 2006/04/28 19:53:56 christos Exp $");
|
1997-10-19 12:13:23 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Finger prints out information about users. It is not portable since
|
|
|
|
* certain fields (e.g. the full user name, office, and phone numbers) are
|
|
|
|
* extracted from the gecos field of the passwd file which other UNIXes
|
|
|
|
* may not have or may use for other things.
|
|
|
|
*
|
|
|
|
* There are currently two output formats; the short format is one line
|
|
|
|
* per user and displays login name, tty, login time, real name, idle time,
|
1996-11-21 09:01:47 +03:00
|
|
|
* and either remote host information (default) or office location/phone
|
|
|
|
* number, depending on if -h or -o is used respectively.
|
|
|
|
* The long format gives the same information (in a more legible format) as
|
|
|
|
* well as home directory, shell, mail info, and .plan/.project files.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
1997-10-19 12:13:23 +04:00
|
|
|
|
|
|
|
#include <db.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-12-24 19:33:46 +03:00
|
|
|
#include <stdlib.h>
|
1996-11-21 09:01:47 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
1997-10-19 12:13:23 +04:00
|
|
|
#include <unistd.h>
|
2002-08-02 04:10:40 +04:00
|
|
|
|
2002-09-10 07:02:40 +04:00
|
|
|
#include <locale.h>
|
|
|
|
#include <langinfo.h>
|
|
|
|
|
2002-08-02 04:10:40 +04:00
|
|
|
#include "utmpentry.h"
|
1997-10-19 12:13:23 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "finger.h"
|
1996-11-21 09:01:47 +03:00
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
DB *db;
|
1993-03-21 12:45:37 +03:00
|
|
|
time_t now;
|
2002-09-12 05:31:41 +04:00
|
|
|
int entries, gflag, lflag, mflag, oflag, sflag, eightflag, pplan;
|
1993-03-21 12:45:37 +03:00
|
|
|
char tbuf[1024];
|
2002-08-02 04:10:40 +04:00
|
|
|
struct utmpentry *ehead;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2006-01-04 04:17:54 +03:00
|
|
|
static void loginlist(void);
|
|
|
|
static void userlist(int, char **);
|
|
|
|
int main(int, char **);
|
1997-10-19 12:13:23 +04:00
|
|
|
|
1996-11-21 09:01:47 +03:00
|
|
|
int
|
2006-01-04 04:17:54 +03:00
|
|
|
main(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
2002-09-10 07:02:40 +04:00
|
|
|
/* Allow user's locale settings to affect character output. */
|
|
|
|
(void *) setlocale(LC_CTYPE, "");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset back to the C locale, unless we are using a known
|
|
|
|
* single-byte 8-bit locale.
|
|
|
|
*/
|
|
|
|
if (strncmp(nl_langinfo(CODESET), "ISO8859-", 8))
|
|
|
|
(void *) setlocale(LC_CTYPE, "C");
|
|
|
|
|
1996-11-21 09:01:47 +03:00
|
|
|
oflag = 1; /* default to old "office" behavior */
|
|
|
|
|
2002-09-12 05:31:41 +04:00
|
|
|
while ((ch = getopt(argc, argv, "lmpshog8")) != -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
switch(ch) {
|
|
|
|
case 'l':
|
|
|
|
lflag = 1; /* long format */
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
mflag = 1; /* force exact match of names */
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
pplan = 1; /* don't show .plan/.project */
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
sflag = 1; /* short format */
|
|
|
|
break;
|
1996-11-21 09:01:47 +03:00
|
|
|
case 'h':
|
|
|
|
oflag = 0; /* remote host info */
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
oflag = 1; /* office info */
|
|
|
|
break;
|
1997-09-09 06:41:06 +04:00
|
|
|
case 'g':
|
|
|
|
gflag = 1; /* no gecos info, besides name */
|
|
|
|
break;
|
2002-09-12 05:31:41 +04:00
|
|
|
case '8':
|
|
|
|
eightflag = 1; /* 8-bit pass-through */
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
(void)fprintf(stderr,
|
2002-09-12 05:31:41 +04:00
|
|
|
"usage: finger [-lmpshog8] [login ...]\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
(void)time(&now);
|
|
|
|
setpassent(1);
|
2002-08-02 04:10:40 +04:00
|
|
|
entries = getutentries(NULL, &ehead);
|
2002-08-05 12:04:03 +04:00
|
|
|
if (argc == 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* Assign explicit "small" format if no names given and -l
|
|
|
|
* not selected. Force the -s BEFORE we get names so proper
|
|
|
|
* screening will be done.
|
|
|
|
*/
|
|
|
|
if (!lflag)
|
|
|
|
sflag = 1; /* if -l not explicit, force -s */
|
|
|
|
loginlist();
|
|
|
|
if (entries == 0)
|
|
|
|
(void)printf("No one logged on.\n");
|
|
|
|
} else {
|
|
|
|
userlist(argc, argv);
|
|
|
|
/*
|
|
|
|
* Assign explicit "large" format if names given and -s not
|
|
|
|
* explicitly stated. Force the -l AFTER we get names so any
|
|
|
|
* remote finger attempts specified won't be mishandled.
|
|
|
|
*/
|
|
|
|
if (!sflag)
|
|
|
|
lflag = 1; /* if -s not explicit, force -l */
|
|
|
|
}
|
1998-08-26 00:59:36 +04:00
|
|
|
if (entries) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (lflag)
|
|
|
|
lflag_print();
|
|
|
|
else
|
|
|
|
sflag_print();
|
1998-08-26 00:59:36 +04:00
|
|
|
}
|
1997-10-19 12:13:23 +04:00
|
|
|
return (0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
static void
|
2006-01-04 04:17:54 +03:00
|
|
|
loginlist(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1996-11-21 09:01:47 +03:00
|
|
|
PERSON *pn;
|
1997-10-19 12:13:23 +04:00
|
|
|
DBT data, key;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct passwd *pw;
|
1997-10-19 12:13:23 +04:00
|
|
|
int r, sflag;
|
2002-08-02 04:10:40 +04:00
|
|
|
struct utmpentry *ep;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-08-02 04:10:40 +04:00
|
|
|
for (ep = ehead; ep; ep = ep->next) {
|
|
|
|
if ((pn = find_person(ep->name)) == NULL) {
|
|
|
|
if ((pw = getpwnam(ep->name)) == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
pn = enter_person(pw);
|
|
|
|
}
|
2002-08-02 04:10:40 +04:00
|
|
|
enter_where(ep, pn);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-19 12:13:23 +04:00
|
|
|
if (db && lflag)
|
|
|
|
for (sflag = R_FIRST;; sflag = R_NEXT) {
|
|
|
|
PERSON *tmp;
|
|
|
|
|
|
|
|
r = (*db->seq)(db, &key, &data, sflag);
|
|
|
|
if (r == -1)
|
|
|
|
err(1, "db seq");
|
|
|
|
if (r == 1)
|
|
|
|
break;
|
|
|
|
memmove(&tmp, data.data, sizeof tmp);
|
|
|
|
enter_lastlog(tmp);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
static void
|
2006-01-04 04:17:54 +03:00
|
|
|
userlist(int argc, char **argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2006-01-04 04:17:54 +03:00
|
|
|
PERSON *pn;
|
1997-10-19 12:13:23 +04:00
|
|
|
DBT data, key;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct passwd *pw;
|
1997-10-19 12:13:23 +04:00
|
|
|
int r, sflag, *used, *ip;
|
|
|
|
char **ap, **nargv, **np, **p;
|
2002-08-02 04:10:40 +04:00
|
|
|
struct utmpentry *ep;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
|
|
|
|
(used = calloc(argc, sizeof(int))) == NULL)
|
|
|
|
err(1, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
/* Pull out all network requests. */
|
|
|
|
for (ap = p = argv, np = nargv; *p; ++p)
|
1998-12-19 18:59:50 +03:00
|
|
|
if (strchr(*p, '@'))
|
1997-10-19 12:13:23 +04:00
|
|
|
*np++ = *p;
|
|
|
|
else
|
|
|
|
*ap++ = *p;
|
|
|
|
|
|
|
|
*np++ = NULL;
|
|
|
|
*ap++ = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
if (!*argv)
|
1993-03-21 12:45:37 +03:00
|
|
|
goto net;
|
|
|
|
|
|
|
|
/*
|
1997-10-19 12:13:23 +04:00
|
|
|
* Traverse the list of possible login names and check the login name
|
1993-03-21 12:45:37 +03:00
|
|
|
* and real name against the name specified by the user.
|
|
|
|
*/
|
|
|
|
if (mflag) {
|
1997-10-19 12:13:23 +04:00
|
|
|
for (p = argv; *p; ++p)
|
|
|
|
if ((pw = getpwnam(*p)) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
enter_person(pw);
|
1997-10-19 12:13:23 +04:00
|
|
|
else
|
2006-04-28 23:53:56 +04:00
|
|
|
warnx("%s: no such user", *p);
|
1997-10-19 12:13:23 +04:00
|
|
|
} else {
|
|
|
|
while ((pw = getpwent()) != NULL)
|
|
|
|
for (p = argv, ip = used; *p; ++p, ++ip)
|
|
|
|
if (match(pw, *p)) {
|
|
|
|
enter_person(pw);
|
|
|
|
*ip = 1;
|
|
|
|
}
|
|
|
|
for (p = argv, ip = used; *p; ++p, ++ip)
|
|
|
|
if (!*ip)
|
2006-04-28 23:53:56 +04:00
|
|
|
warnx("%s: no such user", *p);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1997-10-19 12:13:23 +04:00
|
|
|
/* Handle network requests. */
|
|
|
|
net:
|
|
|
|
for (p = nargv; *p;)
|
|
|
|
netfinger(*p++);
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (entries == 0)
|
2006-04-28 23:53:56 +04:00
|
|
|
goto done;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan thru the list of users currently logged in, saving
|
|
|
|
* appropriate data whenever a match occurs.
|
|
|
|
*/
|
2002-08-02 04:10:40 +04:00
|
|
|
for (ep = ehead; ep; ep = ep->next) {
|
|
|
|
if ((pn = find_person(ep->name)) == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
2002-08-02 04:10:40 +04:00
|
|
|
enter_where(ep, pn);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-08-05 12:04:03 +04:00
|
|
|
if (db != NULL)
|
1997-10-19 12:13:23 +04:00
|
|
|
for (sflag = R_FIRST;; sflag = R_NEXT) {
|
|
|
|
PERSON *tmp;
|
|
|
|
|
|
|
|
r = (*db->seq)(db, &key, &data, sflag);
|
|
|
|
if (r == -1)
|
|
|
|
err(1, "db seq");
|
|
|
|
if (r == 1)
|
|
|
|
break;
|
|
|
|
memmove(&tmp, data.data, sizeof tmp);
|
|
|
|
enter_lastlog(tmp);
|
|
|
|
}
|
2006-04-28 23:53:56 +04:00
|
|
|
done:
|
|
|
|
free(nargv);
|
|
|
|
free(used);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|