finger.c revision 1.16 1 /* $NetBSD: finger.c,v 1.16 2002/08/02 00:10:40 christos 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.16 2002/08/02 00:10:40 christos 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
90 #include "utmpentry.h"
91
92 #include "finger.h"
93 #include "extern.h"
94
95 DB *db;
96 time_t now;
97 int entries, gflag, lflag, mflag, oflag, sflag, pplan;
98 char tbuf[1024];
99 struct utmpentry *ehead;
100
101 static void loginlist __P((void));
102 static void userlist __P((int, char **));
103 int main __P((int, char **));
104
105 int
106 main(argc, argv)
107 int argc;
108 char **argv;
109 {
110 int ch;
111
112 oflag = 1; /* default to old "office" behavior */
113
114 while ((ch = getopt(argc, argv, "lmpshog")) != -1)
115 switch(ch) {
116 case 'l':
117 lflag = 1; /* long format */
118 break;
119 case 'm':
120 mflag = 1; /* force exact match of names */
121 break;
122 case 'p':
123 pplan = 1; /* don't show .plan/.project */
124 break;
125 case 's':
126 sflag = 1; /* short format */
127 break;
128 case 'h':
129 oflag = 0; /* remote host info */
130 break;
131 case 'o':
132 oflag = 1; /* office info */
133 break;
134 case 'g':
135 gflag = 1; /* no gecos info, besides name */
136 break;
137 case '?':
138 default:
139 (void)fprintf(stderr,
140 "usage: finger [-lmpshog] [login ...]\n");
141 exit(1);
142 }
143 argc -= optind;
144 argv += optind;
145
146 (void)time(&now);
147 setpassent(1);
148 entries = getutentries(NULL, &ehead);
149 if (!*argv) {
150 /*
151 * Assign explicit "small" format if no names given and -l
152 * not selected. Force the -s BEFORE we get names so proper
153 * screening will be done.
154 */
155 if (!lflag)
156 sflag = 1; /* if -l not explicit, force -s */
157 loginlist();
158 if (entries == 0)
159 (void)printf("No one logged on.\n");
160 } else {
161 userlist(argc, argv);
162 /*
163 * Assign explicit "large" format if names given and -s not
164 * explicitly stated. Force the -l AFTER we get names so any
165 * remote finger attempts specified won't be mishandled.
166 */
167 if (!sflag)
168 lflag = 1; /* if -s not explicit, force -l */
169 }
170 if (entries) {
171 if (lflag)
172 lflag_print();
173 else
174 sflag_print();
175 }
176 return (0);
177 }
178
179 static void
180 loginlist()
181 {
182 PERSON *pn;
183 DBT data, key;
184 struct passwd *pw;
185 int r, sflag;
186 struct utmpentry *ep;
187
188 for (ep = ehead; ep; ep = ep->next) {
189 if ((pn = find_person(ep->name)) == NULL) {
190 if ((pw = getpwnam(ep->name)) == NULL)
191 continue;
192 pn = enter_person(pw);
193 }
194 enter_where(ep, pn);
195 }
196 if (db && lflag)
197 for (sflag = R_FIRST;; sflag = R_NEXT) {
198 PERSON *tmp;
199
200 r = (*db->seq)(db, &key, &data, sflag);
201 if (r == -1)
202 err(1, "db seq");
203 if (r == 1)
204 break;
205 memmove(&tmp, data.data, sizeof tmp);
206 enter_lastlog(tmp);
207 }
208 }
209
210 static void
211 userlist(argc, argv)
212 int argc;
213 char **argv;
214 {
215 register PERSON *pn;
216 DBT data, key;
217 struct passwd *pw;
218 int r, sflag, *used, *ip;
219 char **ap, **nargv, **np, **p;
220 struct utmpentry *ep;
221
222 if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
223 (used = calloc(argc, sizeof(int))) == NULL)
224 err(1, NULL);
225
226 /* Pull out all network requests. */
227 for (ap = p = argv, np = nargv; *p; ++p)
228 if (strchr(*p, '@'))
229 *np++ = *p;
230 else
231 *ap++ = *p;
232
233 *np++ = NULL;
234 *ap++ = NULL;
235
236 if (!*argv)
237 goto net;
238
239 /*
240 * Traverse the list of possible login names and check the login name
241 * and real name against the name specified by the user.
242 */
243 if (mflag) {
244 for (p = argv; *p; ++p)
245 if ((pw = getpwnam(*p)) != NULL)
246 enter_person(pw);
247 else
248 (void)fprintf(stderr,
249 "finger: %s: no such user\n", *p);
250 } else {
251 while ((pw = getpwent()) != NULL)
252 for (p = argv, ip = used; *p; ++p, ++ip)
253 if (match(pw, *p)) {
254 enter_person(pw);
255 *ip = 1;
256 }
257 for (p = argv, ip = used; *p; ++p, ++ip)
258 if (!*ip)
259 (void)fprintf(stderr,
260 "finger: %s: no such user\n", *p);
261 }
262
263 /* Handle network requests. */
264 net:
265 for (p = nargv; *p;)
266 netfinger(*p++);
267
268 if (entries == 0)
269 return;
270
271 /*
272 * Scan thru the list of users currently logged in, saving
273 * appropriate data whenever a match occurs.
274 */
275 for (ep = ehead; ep; ep = ep->next) {
276 if ((pn = find_person(ep->name)) == NULL)
277 continue;
278 enter_where(ep, pn);
279 }
280 if (db)
281 for (sflag = R_FIRST;; sflag = R_NEXT) {
282 PERSON *tmp;
283
284 r = (*db->seq)(db, &key, &data, sflag);
285 if (r == -1)
286 err(1, "db seq");
287 if (r == 1)
288 break;
289 memmove(&tmp, data.data, sizeof tmp);
290 enter_lastlog(tmp);
291 }
292 }
293