finger.c revision 1.4 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * This code is derived from software contributed to Berkeley by
6 1.1 cgd * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.3 brezak /*
38 1.3 brezak * Mail status reporting added 931007 by Luke Mewburn, <zak (at) rmit.edu.au>.
39 1.3 brezak */
40 1.3 brezak
41 1.1 cgd #ifndef lint
42 1.1 cgd char copyright[] =
43 1.1 cgd "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
44 1.1 cgd All rights reserved.\n";
45 1.1 cgd #endif /* not lint */
46 1.1 cgd
47 1.1 cgd #ifndef lint
48 1.2 mycroft /*static char sccsid[] = "from: @(#)finger.c 5.22 (Berkeley) 6/29/90";*/
49 1.4 cgd static char rcsid[] = "$Id: finger.c,v 1.4 1994/12/24 16:33:46 cgd Exp $";
50 1.1 cgd #endif /* not lint */
51 1.1 cgd
52 1.1 cgd /*
53 1.1 cgd * Finger prints out information about users. It is not portable since
54 1.1 cgd * certain fields (e.g. the full user name, office, and phone numbers) are
55 1.1 cgd * extracted from the gecos field of the passwd file which other UNIXes
56 1.1 cgd * may not have or may use for other things.
57 1.1 cgd *
58 1.1 cgd * There are currently two output formats; the short format is one line
59 1.1 cgd * per user and displays login name, tty, login time, real name, idle time,
60 1.1 cgd * and office location/phone number. The long format gives the same
61 1.1 cgd * information (in a more legible format) as well as home directory, shell,
62 1.1 cgd * mail info, and .plan/.project files.
63 1.1 cgd */
64 1.1 cgd
65 1.1 cgd #include <sys/param.h>
66 1.1 cgd #include <sys/file.h>
67 1.1 cgd #include <stdio.h>
68 1.4 cgd #include <stdlib.h>
69 1.1 cgd #include "finger.h"
70 1.1 cgd
71 1.1 cgd time_t now;
72 1.1 cgd int lflag, sflag, mflag, pplan;
73 1.1 cgd char tbuf[1024];
74 1.1 cgd
75 1.1 cgd main(argc, argv)
76 1.1 cgd int argc;
77 1.1 cgd char **argv;
78 1.1 cgd {
79 1.1 cgd extern int optind;
80 1.1 cgd int ch;
81 1.1 cgd time_t time();
82 1.1 cgd
83 1.1 cgd while ((ch = getopt(argc, argv, "lmps")) != EOF)
84 1.1 cgd switch(ch) {
85 1.1 cgd case 'l':
86 1.1 cgd lflag = 1; /* long format */
87 1.1 cgd break;
88 1.1 cgd case 'm':
89 1.1 cgd mflag = 1; /* force exact match of names */
90 1.1 cgd break;
91 1.1 cgd case 'p':
92 1.1 cgd pplan = 1; /* don't show .plan/.project */
93 1.1 cgd break;
94 1.1 cgd case 's':
95 1.1 cgd sflag = 1; /* short format */
96 1.1 cgd break;
97 1.1 cgd case '?':
98 1.1 cgd default:
99 1.1 cgd (void)fprintf(stderr,
100 1.1 cgd "usage: finger [-lmps] [login ...]\n");
101 1.1 cgd exit(1);
102 1.1 cgd }
103 1.1 cgd argc -= optind;
104 1.1 cgd argv += optind;
105 1.1 cgd
106 1.1 cgd (void)time(&now);
107 1.1 cgd setpassent(1);
108 1.1 cgd if (!*argv) {
109 1.1 cgd /*
110 1.1 cgd * Assign explicit "small" format if no names given and -l
111 1.1 cgd * not selected. Force the -s BEFORE we get names so proper
112 1.1 cgd * screening will be done.
113 1.1 cgd */
114 1.1 cgd if (!lflag)
115 1.1 cgd sflag = 1; /* if -l not explicit, force -s */
116 1.1 cgd loginlist();
117 1.1 cgd if (entries == 0)
118 1.1 cgd (void)printf("No one logged on.\n");
119 1.1 cgd } else {
120 1.1 cgd userlist(argc, argv);
121 1.1 cgd /*
122 1.1 cgd * Assign explicit "large" format if names given and -s not
123 1.1 cgd * explicitly stated. Force the -l AFTER we get names so any
124 1.1 cgd * remote finger attempts specified won't be mishandled.
125 1.1 cgd */
126 1.1 cgd if (!sflag)
127 1.1 cgd lflag = 1; /* if -s not explicit, force -l */
128 1.1 cgd }
129 1.1 cgd if (entries != 0) {
130 1.1 cgd if (lflag)
131 1.1 cgd lflag_print();
132 1.1 cgd else
133 1.1 cgd sflag_print();
134 1.1 cgd }
135 1.1 cgd exit(0);
136 1.1 cgd }
137 1.1 cgd
138 1.1 cgd loginlist()
139 1.1 cgd {
140 1.1 cgd register PERSON *pn;
141 1.1 cgd struct passwd *pw;
142 1.1 cgd struct utmp user;
143 1.1 cgd char name[UT_NAMESIZE + 1];
144 1.1 cgd
145 1.1 cgd if (!freopen(_PATH_UTMP, "r", stdin)) {
146 1.1 cgd (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP);
147 1.1 cgd exit(2);
148 1.1 cgd }
149 1.1 cgd name[UT_NAMESIZE] = NULL;
150 1.1 cgd while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
151 1.1 cgd if (!user.ut_name[0])
152 1.1 cgd continue;
153 1.1 cgd if ((pn = find_person(user.ut_name)) == NULL) {
154 1.1 cgd bcopy(user.ut_name, name, UT_NAMESIZE);
155 1.1 cgd if ((pw = getpwnam(name)) == NULL)
156 1.1 cgd continue;
157 1.1 cgd pn = enter_person(pw);
158 1.1 cgd }
159 1.1 cgd enter_where(&user, pn);
160 1.1 cgd }
161 1.1 cgd for (pn = phead; lflag && pn != NULL; pn = pn->next)
162 1.1 cgd enter_lastlog(pn);
163 1.1 cgd }
164 1.1 cgd
165 1.1 cgd userlist(argc, argv)
166 1.1 cgd register argc;
167 1.1 cgd register char **argv;
168 1.1 cgd {
169 1.1 cgd register i;
170 1.1 cgd register PERSON *pn;
171 1.1 cgd PERSON *nethead, **nettail;
172 1.1 cgd struct utmp user;
173 1.1 cgd struct passwd *pw;
174 1.1 cgd int dolocal, *used;
175 1.1 cgd char *index();
176 1.1 cgd
177 1.1 cgd if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) {
178 1.1 cgd (void)fprintf(stderr, "finger: out of space.\n");
179 1.1 cgd exit(1);
180 1.1 cgd }
181 1.1 cgd
182 1.1 cgd /* pull out all network requests */
183 1.1 cgd for (i = 0, dolocal = 0, nettail = &nethead; i < argc; i++) {
184 1.1 cgd if (!index(argv[i], '@')) {
185 1.1 cgd dolocal = 1;
186 1.1 cgd continue;
187 1.1 cgd }
188 1.1 cgd pn = palloc();
189 1.1 cgd *nettail = pn;
190 1.1 cgd nettail = &pn->next;
191 1.1 cgd pn->name = argv[i];
192 1.1 cgd used[i] = -1;
193 1.1 cgd }
194 1.1 cgd *nettail = NULL;
195 1.1 cgd
196 1.1 cgd if (!dolocal)
197 1.1 cgd goto net;
198 1.1 cgd
199 1.1 cgd /*
200 1.1 cgd * traverse the list of possible login names and check the login name
201 1.1 cgd * and real name against the name specified by the user.
202 1.1 cgd */
203 1.1 cgd if (mflag) {
204 1.1 cgd for (i = 0; i < argc; i++)
205 1.1 cgd if (used[i] >= 0 && (pw = getpwnam(argv[i]))) {
206 1.1 cgd enter_person(pw);
207 1.1 cgd used[i] = 1;
208 1.1 cgd }
209 1.1 cgd } else while (pw = getpwent())
210 1.1 cgd for (i = 0; i < argc; i++)
211 1.1 cgd if (used[i] >= 0 &&
212 1.1 cgd (!strcasecmp(pw->pw_name, argv[i]) ||
213 1.1 cgd match(pw, argv[i]))) {
214 1.1 cgd enter_person(pw);
215 1.1 cgd used[i] = 1;
216 1.1 cgd }
217 1.1 cgd
218 1.1 cgd /* list errors */
219 1.1 cgd for (i = 0; i < argc; i++)
220 1.1 cgd if (!used[i])
221 1.1 cgd (void)fprintf(stderr,
222 1.1 cgd "finger: %s: no such user.\n", argv[i]);
223 1.1 cgd
224 1.1 cgd /* handle network requests */
225 1.1 cgd net: for (pn = nethead; pn; pn = pn->next) {
226 1.1 cgd netfinger(pn->name);
227 1.1 cgd if (pn->next || entries)
228 1.1 cgd putchar('\n');
229 1.1 cgd }
230 1.1 cgd
231 1.1 cgd if (entries == 0)
232 1.1 cgd return;
233 1.1 cgd
234 1.1 cgd /*
235 1.1 cgd * Scan thru the list of users currently logged in, saving
236 1.1 cgd * appropriate data whenever a match occurs.
237 1.1 cgd */
238 1.1 cgd if (!freopen(_PATH_UTMP, "r", stdin)) {
239 1.1 cgd (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP);
240 1.1 cgd exit(1);
241 1.1 cgd }
242 1.1 cgd while (fread((char *)&user, sizeof(user), 1, stdin) == 1) {
243 1.1 cgd if (!user.ut_name[0])
244 1.1 cgd continue;
245 1.1 cgd if ((pn = find_person(user.ut_name)) == NULL)
246 1.1 cgd continue;
247 1.1 cgd enter_where(&user, pn);
248 1.1 cgd }
249 1.1 cgd for (pn = phead; pn != NULL; pn = pn->next)
250 1.1 cgd enter_lastlog(pn);
251 1.1 cgd }
252