Deleted Added
full compact
finger.c (24360) finger.c (27169)
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/*
38 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
39 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
40 * Unread since ...".)
41 * - 4 digit phone extensions (3210 is printed as x3210.)
42 * - host/office toggling in short format with -h & -o.
43 * - short day names (`Tue' printed instead of `Jun 21' if the
44 * login time is < 6 days.
45 */
46
47#ifndef lint
48static char copyright[] =
49"@(#) Copyright (c) 1989, 1993\n\
50 The Regents of the University of California. All rights reserved.\n";
51#endif /* not lint */
52
53#ifndef lint
1/*
2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37/*
38 * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
39 * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
40 * Unread since ...".)
41 * - 4 digit phone extensions (3210 is printed as x3210.)
42 * - host/office toggling in short format with -h & -o.
43 * - short day names (`Tue' printed instead of `Jun 21' if the
44 * login time is < 6 days.
45 */
46
47#ifndef lint
48static char copyright[] =
49"@(#) Copyright (c) 1989, 1993\n\
50 The Regents of the University of California. All rights reserved.\n";
51#endif /* not lint */
52
53#ifndef lint
54#if 0
54static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
55static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
56#else
57static const char rcsid[] =
58 "$Id$";
59#endif
55#endif /* not lint */
56
57/*
58 * Finger prints out information about users. It is not portable since
59 * certain fields (e.g. the full user name, office, and phone numbers) are
60 * extracted from the gecos field of the passwd file which other UNIXes
61 * may not have or may use for other things.
62 *
63 * There are currently two output formats; the short format is one line
64 * per user and displays login name, tty, login time, real name, idle time,
65 * and either remote host information (default) or office location/phone
66 * number, depending on if -h or -o is used respectively.
67 * The long format gives the same information (in a more legible format) as
68 * well as home directory, shell, mail info, and .plan/.project files.
69 */
70
71#include <sys/param.h>
72
73#include <db.h>
74#include <err.h>
75#include <errno.h>
76#include <fcntl.h>
77#include <pwd.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <string.h>
81#include <time.h>
82#include <unistd.h>
83#include <utmp.h>
84#include <db.h>
85#include <locale.h>
86
87#include "finger.h"
88
89DB *db;
90time_t now;
91int entries, lflag, mflag, pplan, sflag, oflag, Tflag;
92char tbuf[1024];
93
94static void loginlist __P((void));
60#endif /* not lint */
61
62/*
63 * Finger prints out information about users. It is not portable since
64 * certain fields (e.g. the full user name, office, and phone numbers) are
65 * extracted from the gecos field of the passwd file which other UNIXes
66 * may not have or may use for other things.
67 *
68 * There are currently two output formats; the short format is one line
69 * per user and displays login name, tty, login time, real name, idle time,
70 * and either remote host information (default) or office location/phone
71 * number, depending on if -h or -o is used respectively.
72 * The long format gives the same information (in a more legible format) as
73 * well as home directory, shell, mail info, and .plan/.project files.
74 */
75
76#include <sys/param.h>
77
78#include <db.h>
79#include <err.h>
80#include <errno.h>
81#include <fcntl.h>
82#include <pwd.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <time.h>
87#include <unistd.h>
88#include <utmp.h>
89#include <db.h>
90#include <locale.h>
91
92#include "finger.h"
93
94DB *db;
95time_t now;
96int entries, lflag, mflag, pplan, sflag, oflag, Tflag;
97char tbuf[1024];
98
99static void loginlist __P((void));
100static void usage __P((void));
95static void userlist __P((int, char **));
96
97int
98option(argc, argv)
99 int argc;
100 char **argv;
101{
102 int ch;
103
104 optind = 1; /* reset getopt */
105
106 while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
107 switch(ch) {
108 case 'l':
109 lflag = 1; /* long format */
110 break;
111 case 'm':
112 mflag = 1; /* force exact match of names */
113 break;
114 case 'p':
115 pplan = 1; /* don't show .plan/.project */
116 break;
117 case 's':
118 sflag = 1; /* short format */
119 break;
120 case 'h':
121 oflag = 0; /* remote host info */
122 break;
123 case 'o':
124 oflag = 1; /* office info */
125 break;
126 case 'T':
127 Tflag = 1; /* disable T/TCP */
128 break;
129 case '?':
130 default:
101static void userlist __P((int, char **));
102
103int
104option(argc, argv)
105 int argc;
106 char **argv;
107{
108 int ch;
109
110 optind = 1; /* reset getopt */
111
112 while ((ch = getopt(argc, argv, "lmpshoT")) != -1)
113 switch(ch) {
114 case 'l':
115 lflag = 1; /* long format */
116 break;
117 case 'm':
118 mflag = 1; /* force exact match of names */
119 break;
120 case 'p':
121 pplan = 1; /* don't show .plan/.project */
122 break;
123 case 's':
124 sflag = 1; /* short format */
125 break;
126 case 'h':
127 oflag = 0; /* remote host info */
128 break;
129 case 'o':
130 oflag = 1; /* office info */
131 break;
132 case 'T':
133 Tflag = 1; /* disable T/TCP */
134 break;
135 case '?':
136 default:
131 (void)fprintf(stderr,
132 "usage: finger [-lmpshoT] [login ...]\n");
133 exit(1);
137 usage();
134 }
135
136 return optind;
137}
138
138 }
139
140 return optind;
141}
142
143static void
144usage()
145{
146 (void)fprintf(stderr, "usage: finger [-lmpshoT] [login ...]\n");
147 exit(1);
148}
149
150int
139main(argc, argv)
140 int argc;
141 char **argv;
142{
151main(argc, argv)
152 int argc;
153 char **argv;
154{
143 int ch, envargc, argcnt;
155 int envargc, argcnt;
144 char *envargv[3];
145
146 (void) setlocale(LC_ALL, "");
147
148 /* remove this line to get remote host */
149 oflag = 1; /* default to old "office" behavior */
150
151 /*
152 * Process environment variables followed by command line arguments.
153 */
154 if ((envargv[1] = getenv("FINGER"))) {
155 envargc = 2;
156 envargv[0] = "finger";
157 envargv[2] = NULL;
158 (void) option(envargc, envargv);
159 }
160
161 argcnt = option(argc, argv);
162 argc -= argcnt;
163 argv += argcnt;
164
165 (void)time(&now);
166 setpassent(1);
167 if (!*argv) {
168 /*
169 * Assign explicit "small" format if no names given and -l
170 * not selected. Force the -s BEFORE we get names so proper
171 * screening will be done.
172 */
173 if (!lflag)
174 sflag = 1; /* if -l not explicit, force -s */
175 loginlist();
176 if (entries == 0)
177 (void)printf("No one logged on.\n");
178 } else {
179 userlist(argc, argv);
180 /*
181 * Assign explicit "large" format if names given and -s not
182 * explicitly stated. Force the -l AFTER we get names so any
183 * remote finger attempts specified won't be mishandled.
184 */
185 if (!sflag)
186 lflag = 1; /* if -s not explicit, force -l */
187 }
188 if (entries)
189 if (lflag)
190 lflag_print();
191 else
192 sflag_print();
193 return (0);
194}
195
196static void
197loginlist()
198{
199 register PERSON *pn;
200 DBT data, key;
201 struct passwd *pw;
202 struct utmp user;
203 int r, sflag;
204 char name[UT_NAMESIZE + 1];
205
206 if (!freopen(_PATH_UTMP, "r", stdin))
207 err(1, "%s", _PATH_UTMP);
156 char *envargv[3];
157
158 (void) setlocale(LC_ALL, "");
159
160 /* remove this line to get remote host */
161 oflag = 1; /* default to old "office" behavior */
162
163 /*
164 * Process environment variables followed by command line arguments.
165 */
166 if ((envargv[1] = getenv("FINGER"))) {
167 envargc = 2;
168 envargv[0] = "finger";
169 envargv[2] = NULL;
170 (void) option(envargc, envargv);
171 }
172
173 argcnt = option(argc, argv);
174 argc -= argcnt;
175 argv += argcnt;
176
177 (void)time(&now);
178 setpassent(1);
179 if (!*argv) {
180 /*
181 * Assign explicit "small" format if no names given and -l
182 * not selected. Force the -s BEFORE we get names so proper
183 * screening will be done.
184 */
185 if (!lflag)
186 sflag = 1; /* if -l not explicit, force -s */
187 loginlist();
188 if (entries == 0)
189 (void)printf("No one logged on.\n");
190 } else {
191 userlist(argc, argv);
192 /*
193 * Assign explicit "large" format if names given and -s not
194 * explicitly stated. Force the -l AFTER we get names so any
195 * remote finger attempts specified won't be mishandled.
196 */
197 if (!sflag)
198 lflag = 1; /* if -s not explicit, force -l */
199 }
200 if (entries)
201 if (lflag)
202 lflag_print();
203 else
204 sflag_print();
205 return (0);
206}
207
208static void
209loginlist()
210{
211 register PERSON *pn;
212 DBT data, key;
213 struct passwd *pw;
214 struct utmp user;
215 int r, sflag;
216 char name[UT_NAMESIZE + 1];
217
218 if (!freopen(_PATH_UTMP, "r", stdin))
219 err(1, "%s", _PATH_UTMP);
208 name[UT_NAMESIZE] = NULL;
220 name[UT_NAMESIZE] = '\0';
209 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
210 if (!user.ut_name[0])
211 continue;
212 if ((pn = find_person(user.ut_name)) == NULL) {
213 bcopy(user.ut_name, name, UT_NAMESIZE);
214 if ((pw = getpwnam(name)) == NULL)
215 continue;
216 if (hide(pw))
217 continue;
218 pn = enter_person(pw);
219 }
220 enter_where(&user, pn);
221 }
222 if (db && lflag)
223 for (sflag = R_FIRST;; sflag = R_NEXT) {
224 PERSON *tmp;
225
226 r = (*db->seq)(db, &key, &data, sflag);
227 if (r == -1)
228 err(1, "db seq");
229 if (r == 1)
230 break;
231 memmove(&tmp, data.data, sizeof tmp);
232 enter_lastlog(tmp);
233 }
234}
235
236static void
237userlist(argc, argv)
238 register int argc;
239 register char **argv;
240{
241 register PERSON *pn;
242 DBT data, key;
243 struct utmp user;
244 struct passwd *pw;
245 int r, sflag, *used, *ip;
246 char **ap, **nargv, **np, **p;
247
248 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
249 (used = calloc(argc, sizeof(int))) == NULL)
250 err(1, NULL);
251
252 /* Pull out all network requests. */
253 for (ap = p = argv, np = nargv; *p; ++p)
254 if (index(*p, '@'))
255 *np++ = *p;
256 else
257 *ap++ = *p;
258
259 *np++ = NULL;
260 *ap++ = NULL;
261
262 if (!*argv)
263 goto net;
264
265 /*
266 * Traverse the list of possible login names and check the login name
267 * and real name against the name specified by the user.
268 */
269 if (mflag)
270 for (p = argv; *p; ++p)
271 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
272 enter_person(pw);
273 else
221 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
222 if (!user.ut_name[0])
223 continue;
224 if ((pn = find_person(user.ut_name)) == NULL) {
225 bcopy(user.ut_name, name, UT_NAMESIZE);
226 if ((pw = getpwnam(name)) == NULL)
227 continue;
228 if (hide(pw))
229 continue;
230 pn = enter_person(pw);
231 }
232 enter_where(&user, pn);
233 }
234 if (db && lflag)
235 for (sflag = R_FIRST;; sflag = R_NEXT) {
236 PERSON *tmp;
237
238 r = (*db->seq)(db, &key, &data, sflag);
239 if (r == -1)
240 err(1, "db seq");
241 if (r == 1)
242 break;
243 memmove(&tmp, data.data, sizeof tmp);
244 enter_lastlog(tmp);
245 }
246}
247
248static void
249userlist(argc, argv)
250 register int argc;
251 register char **argv;
252{
253 register PERSON *pn;
254 DBT data, key;
255 struct utmp user;
256 struct passwd *pw;
257 int r, sflag, *used, *ip;
258 char **ap, **nargv, **np, **p;
259
260 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
261 (used = calloc(argc, sizeof(int))) == NULL)
262 err(1, NULL);
263
264 /* Pull out all network requests. */
265 for (ap = p = argv, np = nargv; *p; ++p)
266 if (index(*p, '@'))
267 *np++ = *p;
268 else
269 *ap++ = *p;
270
271 *np++ = NULL;
272 *ap++ = NULL;
273
274 if (!*argv)
275 goto net;
276
277 /*
278 * Traverse the list of possible login names and check the login name
279 * and real name against the name specified by the user.
280 */
281 if (mflag)
282 for (p = argv; *p; ++p)
283 if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
284 enter_person(pw);
285 else
274 (void)fprintf(stderr,
275 "finger: %s: no such user\n", *p);
286 warnx("%s: no such user", *p);
276 else {
277 while ((pw = getpwent()) != NULL) {
278 for (p = argv, ip = used; *p; ++p, ++ip)
279 if (match(pw, *p) && !hide(pw)) {
280 enter_person(pw);
281 *ip = 1;
282 }
283 }
284 for (p = argv, ip = used; *p; ++p, ++ip)
285 if (!*ip)
287 else {
288 while ((pw = getpwent()) != NULL) {
289 for (p = argv, ip = used; *p; ++p, ++ip)
290 if (match(pw, *p) && !hide(pw)) {
291 enter_person(pw);
292 *ip = 1;
293 }
294 }
295 for (p = argv, ip = used; *p; ++p, ++ip)
296 if (!*ip)
286 (void)fprintf(stderr,
287 "finger: %s: no such user\n", *p);
297 warnx("%s: no such user", *p);
288 }
289
290 /* Handle network requests. */
291net: for (p = nargv; *p;) {
292 netfinger(*p++);
293 if (*p || entries)
294 printf("\n");
295 }
296
297 if (entries == 0)
298 return;
299
300 /*
301 * Scan thru the list of users currently logged in, saving
302 * appropriate data whenever a match occurs.
303 */
304 if (!freopen(_PATH_UTMP, "r", stdin))
305 err(1, "%s", _PATH_UTMP);
306 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
307 if (!user.ut_name[0])
308 continue;
309 if ((pn = find_person(user.ut_name)) == NULL)
310 continue;
311 enter_where(&user, pn);
312 }
313 if (db)
314 for (sflag = R_FIRST;; sflag = R_NEXT) {
315 PERSON *tmp;
316
317 r = (*db->seq)(db, &key, &data, sflag);
318 if (r == -1)
319 err(1, "db seq");
320 if (r == 1)
321 break;
322 memmove(&tmp, data.data, sizeof tmp);
323 enter_lastlog(tmp);
324 }
325}
298 }
299
300 /* Handle network requests. */
301net: for (p = nargv; *p;) {
302 netfinger(*p++);
303 if (*p || entries)
304 printf("\n");
305 }
306
307 if (entries == 0)
308 return;
309
310 /*
311 * Scan thru the list of users currently logged in, saving
312 * appropriate data whenever a match occurs.
313 */
314 if (!freopen(_PATH_UTMP, "r", stdin))
315 err(1, "%s", _PATH_UTMP);
316 while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
317 if (!user.ut_name[0])
318 continue;
319 if ((pn = find_person(user.ut_name)) == NULL)
320 continue;
321 enter_where(&user, pn);
322 }
323 if (db)
324 for (sflag = R_FIRST;; sflag = R_NEXT) {
325 PERSON *tmp;
326
327 r = (*db->seq)(db, &key, &data, sflag);
328 if (r == -1)
329 err(1, "db seq");
330 if (r == 1)
331 break;
332 memmove(&tmp, data.data, sizeof tmp);
333 enter_lastlog(tmp);
334 }
335}