1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/* $OpenBSD: finger.c,v 1.3 1996/08/30 11:39:36 deraadt Exp $ */
/*
* Copyright (c) 1989 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. 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.
*/
/*
* Mail status reporting added 931007 by Luke Mewburn, <zak@rmit.edu.au>.
*/
#ifndef lint
char copyright[] =
"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
All rights reserved.\n";
#endif /* not lint */
#ifndef lint
/*static char sccsid[] = "from: @(#)finger.c 5.22 (Berkeley) 6/29/90";*/
static char rcsid[] = "$OpenBSD: finger.c,v 1.3 1996/08/30 11:39:36 deraadt Exp $";
#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,
* and office location/phone number. The long format gives the same
* information (in a more legible format) as well as home directory, shell,
* mail info, and .plan/.project files.
*/
#include <sys/param.h>
#include <sys/file.h>
#include <stdio.h>
#include <stdlib.h>
#include "finger.h"
time_t now;
int lflag, sflag, mflag, pplan;
char tbuf[1024];
int loginlist __P((void));
void userlist __P((int, char **));
int
main(argc, argv)
int argc;
char **argv;
{
extern int optind;
int ch;
time_t time();
while ((ch = getopt(argc, argv, "lmps")) != EOF)
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;
case '?':
default:
(void)fprintf(stderr,
"usage: finger [-lmps] [login ...]\n");
exit(1);
}
argc -= optind;
argv += optind;
(void)time(&now);
setpassent(1);
if (!*argv) {
/*
* 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 */
}
if (entries != 0) {
if (lflag)
lflag_print();
else
sflag_print();
}
exit(0);
}
int
loginlist()
{
register PERSON *pn;
struct passwd *pw;
struct utmp user;
char name[UT_NAMESIZE + 1];
if (!freopen(_PATH_UTMP, "r", stdin)) {
(void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
exit(2);
}
name[UT_NAMESIZE] = NULL;
while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
if (!user.ut_name[0])
continue;
if ((pn = find_person(user.ut_name)) == NULL) {
bcopy(user.ut_name, name, UT_NAMESIZE);
if ((pw = getpwnam(name)) == NULL)
continue;
pn = enter_person(pw);
}
enter_where(&user, pn);
}
for (pn = phead; lflag && pn != NULL; pn = pn->next)
enter_lastlog(pn);
}
void
userlist(argc, argv)
register argc;
register char **argv;
{
register i;
register PERSON *pn;
PERSON *nethead, **nettail;
struct utmp user;
struct passwd *pw;
int dolocal, *used;
char *index();
if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
(void)fprintf(stderr, "finger: out of space.\n");
exit(1);
}
/* pull out all network requests */
for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
if (!index(argv[i], '@')) {
dolocal = 1;
continue;
}
pn = palloc();
*nettail = pn;
nettail = &pn->next;
pn->name = argv[i];
used[i] = -1;
}
*nettail = NULL;
if (!dolocal)
goto net;
/*
* traverse the list of possible login names and check the login name
* and real name against the name specified by the user.
*/
if (mflag) {
for (i = 0; i < argc; i++)
if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
enter_person(pw);
used[i] = 1;
}
} else while (pw = getpwent())
for (i = 0; i < argc; i++)
if (used[i] >= 0 &&
(!strcasecmp(pw->pw_name, argv[i]) ||
match(pw, argv[i]))) {
enter_person(pw);
used[i] = 1;
}
/* list errors */
for (i = 0; i < argc; i++)
if (!used[i])
(void)fprintf(stderr,
"finger: %s: no such user.\n", argv[i]);
/* handle network requests */
net: for (pn = nethead; pn; pn = pn->next) {
netfinger(pn->name);
if (pn->next || entries)
putchar('\n');
}
if (entries == 0)
return;
/*
* Scan thru the list of users currently logged in, saving
* appropriate data whenever a match occurs.
*/
if (!freopen(_PATH_UTMP, "r", stdin)) {
(void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
exit(1);
}
while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
if (!user.ut_name[0])
continue;
if ((pn = find_person(user.ut_name)) == NULL)
continue;
enter_where(&user, pn);
}
for (pn = phead; pn != NULL; pn = pn->next)
enter_lastlog(pn);
}
|