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