Move /var/svc/log to /var/log/svc
[unleashed/lotheac.git] / usr / src / cmd / cmd-inet / usr.bin / finger.c
blob207c71d28ec19858564aa85df8e7c80de4e2187a
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
33 * All Rights Reserved
35 * Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
37 * contributors.
41 * This is a finger program. It prints out useful information about users
42 * by digging it up from various system files.
44 * There are three output formats, all of which give login name, teletype
45 * line number, and login time. The short output format is reminiscent
46 * of finger on ITS, and gives one line of information per user containing
47 * in addition to the minimum basic requirements (MBR), the user's full name,
48 * idle time and location.
49 * The quick style output is UNIX who-like, giving only name, teletype and
50 * login time. Finally, the long style output give the same information
51 * as the short (in more legible format), the home directory and shell
52 * of the user, and, if it exits, a copy of the file .plan in the users
53 * home directory. Finger may be called with or without a list of people
54 * to finger -- if no list is given, all the people currently logged in
55 * are fingered.
57 * The program is validly called by one of the following:
59 * finger {short form list of users}
60 * finger -l {long form list of users}
61 * finger -b {briefer long form list of users}
62 * finger -q {quick list of users}
63 * finger -i {quick list of users with idle times}
64 * finger -m {matches arguments against only username}
65 * finger -f {suppress header in non-long form}
66 * finger -p {suppress printing of .plan file}
67 * finger -h {suppress printing of .project file}
68 * finger -i {forces "idle" output format}
69 * finger namelist {long format list of specified users}
70 * finger -s namelist {short format list of specified users}
71 * finger -w namelist {narrow short format list of specified users}
73 * where 'namelist' is a list of users login names.
74 * The other options can all be given after one '-', or each can have its
75 * own '-'. The -f option disables the printing of headers for short and
76 * quick outputs. The -b option briefens long format outputs. The -p
77 * option turns off plans for long format outputs.
80 #include <sys/types.h>
81 #include <sys/stat.h>
82 #include <utmpx.h>
83 #include <sys/signal.h>
84 #include <pwd.h>
85 #include <stdio.h>
86 #include <lastlog.h>
87 #include <ctype.h>
88 #include <sys/time.h>
89 #include <time.h>
90 #include <sys/socket.h>
91 #include <netinet/in.h>
92 #include <netdb.h>
93 #include <locale.h>
94 #include <sys/select.h>
95 #include <stdlib.h>
96 #include <strings.h>
97 #include <fcntl.h>
98 #include <curses.h>
99 #include <maillock.h>
100 #include <deflt.h>
101 #include <unistd.h>
102 #include <arpa/inet.h>
103 #include <macros.h>
105 static char gecos_ignore_c = '*'; /* ignore this in real name */
106 static char gecos_sep_c = ','; /* separator in pw_gecos field */
107 static char gecos_samename = '&'; /* repeat login name in real name */
109 #define TALKABLE 0220 /* tty is writable if this mode */
111 #define NMAX sizeof (((struct utmpx *)0)->ut_name)
112 #define LMAX sizeof (((struct utmpx *)0)->ut_line)
113 #define HMAX sizeof (((struct utmpx *)0)->ut_host)
115 struct person { /* one for each person fingered */
116 char *name; /* name */
117 char tty[LMAX+1]; /* null terminated tty line */
118 char host[HMAX+1]; /* null terminated remote host name */
119 char *ttyloc; /* location of tty line, if any */
120 time_t loginat; /* time of (last) login */
121 time_t idletime; /* how long idle (if logged in) */
122 char *realname; /* pointer to full name */
123 struct passwd *pwd; /* structure of /etc/passwd stuff */
124 char loggedin; /* person is logged in */
125 char writable; /* tty is writable */
126 char original; /* this is not a duplicate entry */
127 struct person *link; /* link to next person */
130 char PLAN[] = "/.plan"; /* what plan file is */
131 char PROJ[] = "/.project"; /* what project file */
133 int unbrief = 1; /* -b option default */
134 int header = 1; /* -f option default */
135 int hack = 1; /* -h option default */
136 int idle = 0; /* -i option default */
137 int large = 0; /* -l option default */
138 int match = 1; /* -m option default */
139 int plan = 1; /* -p option default */
140 int unquick = 1; /* -q option default */
141 int small = 0; /* -s option default */
142 int wide = 1; /* -w option default */
145 * RFC 1288 says that system administrators should have the option of
146 * separately allowing ASCII characters less than 32 or greater than
147 * 126. The termpass variable keeps track of this.
149 char defaultfile[] = "/etc/default/finger";
150 char passvar[] = "PASS=";
151 int termpass = 0; /* default is ASCII only */
152 char *termopts[] = {
153 #define TERM_LOW 0
154 "low",
155 #define TERM_HIGH 1
156 "high",
157 NULL
159 #define TS_LOW (1 << TERM_LOW) /* print characters less than 32 */
160 #define TS_HIGH (1 << TERM_HIGH) /* print characters greater than 126 */
163 int unshort;
164 FILE *lf; /* LASTLOG file pointer */
165 struct person *person1; /* list of people */
166 size_t nperson; /* number of people */
167 time_t tloc; /* current time */
169 char usagestr[] = "Usage: "
170 "finger [-bfhilmpqsw] [name1 [name2 ...] ]\n";
172 int AlreadyPrinted(uid_t uid);
173 void AnyMail(char *name);
174 void catfile(char *s, mode_t mode, int trunc_at_nl);
175 void decode(struct person *pers);
176 void doall(void);
177 void donames(char **argv);
178 void findidle(struct person *pers);
179 void findwhen(struct person *pers);
180 void fwclose(void);
181 void fwopen(void);
182 void initscreening(void);
183 void ltimeprint(char *before, time_t *dt, char *after);
184 int matchcmp(char *gname, char *login, char *given);
185 int namecmp(char *name1, char *name2);
186 int netfinger(char *name);
187 void personprint(struct person *pers);
188 void print(void);
189 struct passwd *pwdcopy(const struct passwd *pfrom);
190 void quickprint(struct person *pers);
191 void shortprint(struct person *pers);
192 void stimeprint(time_t *dt);
193 void sort_by_username(void);
197 main(int argc, char **argv)
199 int c;
201 (void) setlocale(LC_ALL, "");
202 /* parse command line for (optional) arguments */
203 while ((c = getopt(argc, argv, "bfhilmpqsw")) != EOF)
204 switch (c) {
205 case 'b':
206 unbrief = 0;
207 break;
208 case 'f':
209 header = 0;
210 break;
211 case 'h':
212 hack = 0;
213 break;
214 case 'i':
215 idle = 1;
216 unquick = 0;
217 break;
218 case 'l':
219 large = 1;
220 break;
221 case 'm':
222 match = 0;
223 break;
224 case 'p':
225 plan = 0;
226 break;
227 case 'q':
228 unquick = 0;
229 break;
230 case 's':
231 small = 1;
232 break;
233 case 'w':
234 wide = 0;
235 break;
236 default:
237 (void) fprintf(stderr, usagestr);
238 exit(1);
240 if (unquick || idle)
241 tloc = time(NULL);
243 /* find out what filtering on .plan/.project files we should do */
244 initscreening();
247 * optind == argc means no names given
249 if (optind == argc)
250 doall();
251 else
252 donames(&argv[optind]);
254 sort_by_username();
256 if (nperson > 0)
257 print();
258 return (0);
259 /* NOTREACHED */
262 void
263 doall(void)
265 struct person *p;
266 struct passwd *pw;
267 struct utmpx *u;
268 char name[NMAX + 1];
270 unshort = large;
271 setutxent();
272 if (unquick) {
273 setpwent();
274 fwopen();
276 while ((u = getutxent()) != NULL) {
277 if (u->ut_name[0] == 0 ||
278 nonuserx(*u) ||
279 u->ut_type != USER_PROCESS)
280 continue;
281 if (person1 == NULL)
282 p = person1 = malloc(sizeof (*p));
283 else {
284 p->link = malloc(sizeof (*p));
285 p = p->link;
287 bcopy(u->ut_name, name, NMAX);
288 name[NMAX] = 0;
289 bcopy(u->ut_line, p->tty, LMAX);
290 p->tty[LMAX] = 0;
291 bcopy(u->ut_host, p->host, HMAX);
292 p->host[HMAX] = 0;
293 p->loginat = u->ut_tv.tv_sec;
294 p->pwd = NULL;
295 p->loggedin = 1;
296 if (unquick && (pw = getpwnam(name))) {
297 p->pwd = pwdcopy(pw);
298 decode(p);
299 p->name = p->pwd->pw_name;
300 } else
301 p->name = strdup(name);
302 p->ttyloc = NULL;
304 nperson++;
306 if (unquick) {
307 fwclose();
308 endpwent();
310 endutxent();
311 if (nperson == 0) {
312 (void) printf("No one logged on\n");
313 return;
315 p->link = NULL;
318 void
319 donames(char **argv)
321 struct person *p;
322 struct passwd *pw;
323 struct utmpx *u;
326 * get names from command line and check to see if they're
327 * logged in
329 unshort = !small;
330 for (; *argv != NULL; argv++) {
331 if (netfinger(*argv))
332 continue;
333 if (person1 == NULL)
334 p = person1 = malloc(sizeof (*p));
335 else {
336 p->link = malloc(sizeof (*p));
337 p = p->link;
339 p->name = *argv;
340 p->loggedin = 0;
341 p->original = 1;
342 p->pwd = NULL;
344 nperson++;
346 if (nperson == 0)
347 return;
348 p->link = NULL;
350 * if we are doing it, read /etc/passwd for the useful info
352 if (unquick) {
353 setpwent();
354 if (!match) {
355 for (p = person1; p != NULL; p = p->link) {
356 if ((pw = getpwnam(p->name)) != NULL)
357 p->pwd = pwdcopy(pw);
359 } else {
360 while ((pw = getpwent()) != NULL) {
361 for (p = person1; p != NULL; p = p->link) {
362 if (!p->original)
363 continue;
364 if (strcmp(p->name, pw->pw_name) != 0 &&
365 !matchcmp(pw->pw_gecos, pw->pw_name,
366 p->name)) {
367 continue;
369 if (p->pwd == NULL) {
370 p->pwd = pwdcopy(pw);
371 } else {
372 struct person *new;
374 * Handle multiple login names.
375 * Insert new "duplicate" entry
376 * behind.
378 new = malloc(sizeof (*new));
379 new->pwd = pwdcopy(pw);
380 new->name = p->name;
381 new->original = 1;
382 new->loggedin = 0;
383 new->ttyloc = NULL;
384 new->link = p->link;
385 p->original = 0;
386 p->link = new;
387 p = new;
389 nperson++;
394 endpwent();
396 /* Now get login information */
397 setutxent();
398 while ((u = getutxent()) != NULL) {
399 if (u->ut_name[0] == 0 || u->ut_type != USER_PROCESS)
400 continue;
401 for (p = person1; p != NULL; p = p->link) {
402 p->ttyloc = NULL;
403 if (p->loggedin == 2)
404 continue;
405 if (strncmp((p->pwd != NULL) ?
406 p->pwd->pw_name : p->name,
407 u->ut_name, NMAX) != 0)
408 continue;
409 if (p->loggedin == 0) {
410 bcopy(u->ut_line, p->tty, LMAX);
411 p->tty[LMAX] = 0;
412 bcopy(u->ut_host, p->host, HMAX);
413 p->host[HMAX] = 0;
414 p->loginat = u->ut_tv.tv_sec;
415 p->loggedin = 1;
416 } else { /* p->loggedin == 1 */
417 struct person *new;
418 new = malloc(sizeof (*new));
419 new->name = p->name;
420 bcopy(u->ut_line, new->tty, LMAX);
421 new->tty[LMAX] = 0;
422 bcopy(u->ut_host, new->host, HMAX);
423 new->host[HMAX] = 0;
424 new->loginat = u->ut_tv.tv_sec;
425 new->pwd = p->pwd;
426 new->loggedin = 1;
427 new->original = 0;
428 new->link = p->link;
429 p->loggedin = 2;
430 p->link = new;
431 p = new;
433 nperson++;
437 endutxent();
438 if (unquick) {
439 fwopen();
440 for (p = person1; p != NULL; p = p->link)
441 decode(p);
442 fwclose();
446 void
447 print(void)
449 struct person *p;
450 char *s;
453 * print out what we got
455 if (header) {
456 if (unquick) {
457 if (!unshort) {
458 if (wide) {
459 (void) printf("Login "
460 "Name TTY "
461 "Idle When Where\n");
462 } else {
463 (void) printf("Login TTY Idle "
464 "When Where\n");
467 } else {
468 (void) printf("Login TTY When");
469 if (idle)
470 (void) printf(" Idle");
471 (void) putchar('\n');
474 for (p = person1; p != NULL; p = p->link) {
475 if (!unquick) {
476 quickprint(p);
477 continue;
479 if (!unshort) {
480 shortprint(p);
481 continue;
483 personprint(p);
484 if (p->pwd != NULL && !AlreadyPrinted(p->pwd->pw_uid)) {
485 AnyMail(p->pwd->pw_name);
486 if (hack) {
487 struct stat sbuf;
489 s = malloc(strlen(p->pwd->pw_dir) +
490 sizeof (PROJ));
491 if (s != NULL) {
492 (void) strcpy(s, p->pwd->pw_dir);
493 (void) strcat(s, PROJ);
494 if (stat(s, &sbuf) != -1 &&
495 (S_ISREG(sbuf.st_mode) ||
496 S_ISFIFO(sbuf.st_mode)) &&
497 (sbuf.st_mode & S_IROTH)) {
498 (void) printf("Project: ");
499 catfile(s, sbuf.st_mode, 1);
500 (void) putchar('\n');
502 free(s);
505 if (plan) {
506 struct stat sbuf;
508 s = malloc(strlen(p->pwd->pw_dir) +
509 sizeof (PLAN));
510 if (s != NULL) {
511 (void) strcpy(s, p->pwd->pw_dir);
512 (void) strcat(s, PLAN);
513 if (stat(s, &sbuf) == -1 ||
514 (!S_ISREG(sbuf.st_mode) &&
515 !S_ISFIFO(sbuf.st_mode)) ||
516 ((sbuf.st_mode & S_IROTH) == 0))
517 (void) printf("No Plan.\n");
518 else {
519 (void) printf("Plan:\n");
520 catfile(s, sbuf.st_mode, 0);
522 free(s);
526 if (p->link != NULL)
527 (void) putchar('\n');
532 * Duplicate a pwd entry.
533 * Note: Only the useful things (what the program currently uses) are copied.
535 struct passwd *
536 pwdcopy(const struct passwd *pfrom)
538 struct passwd *pto;
540 pto = malloc(sizeof (*pto));
541 pto->pw_name = strdup(pfrom->pw_name);
542 pto->pw_uid = pfrom->pw_uid;
543 pto->pw_gecos = strdup(pfrom->pw_gecos);
544 pto->pw_dir = strdup(pfrom->pw_dir);
545 pto->pw_shell = strdup(pfrom->pw_shell);
546 return (pto);
550 * print out information on quick format giving just name, tty, login time
551 * and idle time if idle is set.
553 void
554 quickprint(struct person *pers)
556 (void) printf("%-8.8s ", pers->name);
557 if (pers->loggedin) {
558 if (idle) {
559 findidle(pers);
560 (void) printf("%c%-12s %-16.16s",
561 pers->writable ? ' ' : '*',
562 pers->tty, ctime(&pers->loginat));
563 ltimeprint(" ", &pers->idletime, "");
564 } else {
565 (void) printf(" %-12s %-16.16s",
566 pers->tty, ctime(&pers->loginat));
568 (void) putchar('\n');
569 } else {
570 (void) printf(" Not Logged In\n");
575 * print out information in short format, giving login name, full name,
576 * tty, idle time, login time, and host.
578 void
579 shortprint(struct person *pers)
581 char *p;
583 if (pers->pwd == NULL) {
584 (void) printf("%-15s ???\n", pers->name);
585 return;
587 (void) printf("%-8s", pers->pwd->pw_name);
588 if (wide) {
589 if (pers->realname != NULL) {
590 (void) printf(" %-20.20s", pers->realname);
591 } else {
592 (void) printf(" ??? ");
595 (void) putchar(' ');
596 if (pers->loggedin && !pers->writable) {
597 (void) putchar('*');
598 } else {
599 (void) putchar(' ');
601 if (*pers->tty) {
602 (void) printf("%-11.11s ", pers->tty);
603 } else {
604 (void) printf(" "); /* 12 spaces */
606 p = ctime(&pers->loginat);
607 if (pers->loggedin) {
608 stimeprint(&pers->idletime);
609 (void) printf(" %3.3s %-5.5s ", p, p + 11);
610 } else if (pers->loginat == 0) {
611 (void) printf(" < . . . . >");
612 } else if (tloc - pers->loginat >= 180 * 24 * 60 * 60) {
613 (void) printf(" <%-6.6s, %-4.4s>", p + 4, p + 20);
614 } else {
615 (void) printf(" <%-12.12s>", p + 4);
617 if (*pers->host) {
618 (void) printf(" %-20.20s", pers->host);
619 } else {
620 if (pers->ttyloc != NULL)
621 (void) printf(" %-20.20s", pers->ttyloc);
623 (void) putchar('\n');
628 * print out a person in long format giving all possible information.
629 * directory and shell are inhibited if unbrief is clear.
631 void
632 personprint(struct person *pers)
634 if (pers->pwd == NULL) {
635 (void) printf("Login name: %-10s\t\t\tIn real life: ???\n",
636 pers->name);
637 return;
639 (void) printf("Login name: %-10s", pers->pwd->pw_name);
640 if (pers->loggedin && !pers->writable) {
641 (void) printf(" (messages off) ");
642 } else {
643 (void) printf(" ");
645 if (pers->realname != NULL) {
646 (void) printf("In real life: %s", pers->realname);
648 if (unbrief) {
649 (void) printf("\nDirectory: %-25s", pers->pwd->pw_dir);
650 if (*pers->pwd->pw_shell)
651 (void) printf("\tShell: %-s", pers->pwd->pw_shell);
653 if (pers->loggedin) {
654 char *ep = ctime(&pers->loginat);
655 if (*pers->host) {
656 (void) printf("\nOn since %15.15s on %s from %s",
657 &ep[4], pers->tty, pers->host);
658 ltimeprint("\n", &pers->idletime, " Idle Time");
659 } else {
660 (void) printf("\nOn since %15.15s on %-12s",
661 &ep[4], pers->tty);
662 ltimeprint("\n", &pers->idletime, " Idle Time");
664 } else if (pers->loginat == 0) {
665 (void) printf("\nNever logged in.");
666 } else if (tloc - pers->loginat > 180 * 24 * 60 * 60) {
667 char *ep = ctime(&pers->loginat);
668 (void) printf("\nLast login %10.10s, %4.4s on %s",
669 ep, ep+20, pers->tty);
670 if (*pers->host) {
671 (void) printf(" from %s", pers->host);
673 } else {
674 char *ep = ctime(&pers->loginat);
675 (void) printf("\nLast login %16.16s on %s", ep, pers->tty);
676 if (*pers->host) {
677 (void) printf(" from %s", pers->host);
680 (void) putchar('\n');
685 * decode the information in the gecos field of /etc/passwd
687 void
688 decode(struct person *pers)
690 char buffer[256];
691 char *bp, *gp, *lp;
693 pers->realname = NULL;
694 if (pers->pwd == NULL)
695 return;
696 gp = pers->pwd->pw_gecos;
697 bp = buffer;
699 if (gecos_ignore_c != '\0' &&
700 *gp == gecos_ignore_c) {
701 gp++;
703 while (*gp != '\0' &&
704 *gp != gecos_sep_c) { /* name */
705 if (*gp == gecos_samename) {
706 lp = pers->pwd->pw_name;
707 if (islower(*lp))
708 *bp++ = toupper(*lp++);
709 while (*bp++ = *lp++)
711 bp--;
712 gp++;
713 } else {
714 *bp++ = *gp++;
717 *bp++ = 0;
718 if (bp > (buffer + 1))
719 pers->realname = strdup(buffer);
720 if (pers->loggedin)
721 findidle(pers);
722 else
723 findwhen(pers);
727 * find the last log in of a user by checking the LASTLOG file.
728 * the entry is indexed by the uid, so this can only be done if
729 * the uid is known (which it isn't in quick mode)
731 void
732 fwopen(void)
734 if ((lf = fopen(_PATH_LASTLOG, "r")) == NULL)
735 (void) fprintf(stderr, "finger: %s open error\n",
736 _PATH_LASTLOG);
739 void
740 findwhen(struct person *pers)
742 struct lastlog ll;
744 if (lf != NULL) {
745 if (fseeko(lf, (off_t)pers->pwd->pw_uid * (off_t)sizeof (ll),
746 SEEK_SET) == 0) {
747 if (fread((char *)&ll, sizeof (ll), 1, lf) == 1) {
748 int l_max, h_max;
750 l_max = min(LMAX, sizeof (ll.ll_line));
751 h_max = min(HMAX, sizeof (ll.ll_host));
753 bcopy(ll.ll_line, pers->tty, l_max);
754 pers->tty[l_max] = '\0';
755 bcopy(ll.ll_host, pers->host, h_max);
756 pers->host[h_max] = '\0';
757 pers->loginat = ll.ll_time;
758 } else {
759 if (ferror(lf))
760 (void) fprintf(stderr,
761 "finger: %s read error\n",
762 _PATH_LASTLOG);
763 pers->tty[0] = 0;
764 pers->host[0] = 0;
765 pers->loginat = 0L;
767 } else {
768 (void) fprintf(stderr, "finger: %s fseeko error\n",
769 _PATH_LASTLOG);
771 } else {
772 pers->tty[0] = 0;
773 pers->host[0] = 0;
774 pers->loginat = 0L;
778 void
779 fwclose(void)
781 if (lf != NULL)
782 (void) fclose(lf);
786 * find the idle time of a user by doing a stat on /dev/tty??,
787 * where tty?? has been gotten from UTMPX_FILE, supposedly.
789 void
790 findidle(struct person *pers)
792 struct stat ttystatus;
793 struct stat inputdevstatus;
794 #define TTYLEN (sizeof ("/dev/") - 1)
795 static char buffer[TTYLEN + LMAX + 1] = "/dev/";
796 time_t t;
797 time_t lastinputtime;
799 (void) strcpy(buffer + TTYLEN, pers->tty);
800 buffer[TTYLEN+LMAX] = 0;
801 if (stat(buffer, &ttystatus) < 0) {
802 (void) fprintf(stderr, "finger: Can't stat %s\n", buffer);
803 exit(4);
805 lastinputtime = ttystatus.st_atime;
806 if (strcmp(pers->tty, "console") == 0) {
808 * On the console, the user may be running a window system; if
809 * so, their activity will show up in the last-access times of
810 * "/dev/kbd" and "/dev/mouse", so take the minimum of the idle
811 * times on those two devices and "/dev/console" and treat that
812 * as the idle time.
814 if (stat("/dev/kbd", &inputdevstatus) == 0) {
815 if (lastinputtime < inputdevstatus.st_atime)
816 lastinputtime = inputdevstatus.st_atime;
818 if (stat("/dev/mouse", &inputdevstatus) == 0) {
819 if (lastinputtime < inputdevstatus.st_atime)
820 lastinputtime = inputdevstatus.st_atime;
823 t = time(NULL);
824 if (t < lastinputtime)
825 pers->idletime = (time_t)0;
826 else
827 pers->idletime = t - lastinputtime;
828 pers->writable = (ttystatus.st_mode & TALKABLE) == TALKABLE;
832 * print idle time in short format; this program always prints 4 characters;
833 * if the idle time is zero, it prints 4 blanks.
835 void
836 stimeprint(time_t *dt)
838 struct tm *delta;
840 delta = gmtime(dt);
841 if (delta->tm_yday == 0)
842 if (delta->tm_hour == 0)
843 if (delta->tm_min == 0)
844 (void) printf(" ");
845 else
846 (void) printf(" %2d", delta->tm_min);
847 else
848 if (delta->tm_hour >= 10)
849 (void) printf("%3d:", delta->tm_hour);
850 else
851 (void) printf("%1d:%02d",
852 delta->tm_hour, delta->tm_min);
853 else
854 (void) printf("%3dd", delta->tm_yday);
858 * print idle time in long format with care being taken not to pluralize
859 * 1 minutes or 1 hours or 1 days.
860 * print "prefix" first.
862 void
863 ltimeprint(char *before, time_t *dt, char *after)
865 struct tm *delta;
867 delta = gmtime(dt);
868 if (delta->tm_yday == 0 && delta->tm_hour == 0 && delta->tm_min == 0 &&
869 delta->tm_sec <= 10)
870 return;
871 (void) printf("%s", before);
872 if (delta->tm_yday >= 10)
873 (void) printf("%d days", delta->tm_yday);
874 else if (delta->tm_yday > 0)
875 (void) printf("%d day%s %d hour%s",
876 delta->tm_yday, delta->tm_yday == 1 ? "" : "s",
877 delta->tm_hour, delta->tm_hour == 1 ? "" : "s");
878 else
879 if (delta->tm_hour >= 10)
880 (void) printf("%d hours", delta->tm_hour);
881 else if (delta->tm_hour > 0)
882 (void) printf("%d hour%s %d minute%s",
883 delta->tm_hour, delta->tm_hour == 1 ? "" : "s",
884 delta->tm_min, delta->tm_min == 1 ? "" : "s");
885 else
886 if (delta->tm_min >= 10)
887 (void) printf("%2d minutes", delta->tm_min);
888 else if (delta->tm_min == 0)
889 (void) printf("%2d seconds", delta->tm_sec);
890 else
891 (void) printf("%d minute%s %d second%s",
892 delta->tm_min,
893 delta->tm_min == 1 ? "" : "s",
894 delta->tm_sec,
895 delta->tm_sec == 1 ? "" : "s");
896 (void) printf("%s", after);
900 * The grammar of the pw_gecos field is sufficiently complex that the
901 * best way to parse it is by using an explicit finite-state machine,
902 * in which a table defines the rules of interpretation.
904 * Some special rules are necessary to handle the fact that names
905 * may contain certain punctuation characters. At this writing,
906 * the possible punctuation characters are '.', '-', and '_'.
908 * Other rules are needed to account for characters that require special
909 * processing when they appear in the pw_gecos field. At present, there
910 * are three such characters, with these default values and effects:
912 * gecos_ignore_c '*' This character is ignored.
913 * gecos_sep_c ',' Delimits displayed and nondisplayed contents.
914 * gecos_samename '&' Copies the login name into the output.
916 * As the program examines each successive character in the returned
917 * pw_gecos value, it fetches (from the table) the FSM rule applicable
918 * for that character in the current machine state, and thus determines
919 * the next state.
921 * The possible states are:
922 * S0 start
923 * S1 in a word
924 * S2 not in a word
925 * S3 copy login name into output
926 * S4 end of GECOS field
928 * Here follows a depiction of the state transitions.
931 * gecos_ignore_c OR isspace OR any other character
932 * +--+
933 * | |
934 * | V
935 * +-----+
936 * NULL OR | S0 | isalpha OR isdigit
937 * +---------------|start|------------------------+
938 * | gecos_sep_c +-----+ | isalpha OR isdigit
939 * | | | | +---------------------+
940 * | | | | | OR '.' '-' '_' |
941 * | | |isspace | | |
942 * | | +-------+ V V |
943 * | | | +-----------+ |
944 * | | | | S1 |<--+ |
945 * | | | | in a word | | isalpha OR |
946 * | | | +-----------+ | isdigit OR |
947 * | | | | | | | | '.' '-' '_' |
948 * | | +----- ---------------+ | | +-----+ |
949 * | | | | | | |
950 * | | | | gecos_ignore_c | | |
951 * | | | | isspace | | |
952 * | | | | ispunct/other | | |
953 * | | | | any other char | | |
954 * | | | | +---------------+ | |
955 * | | | | | |NULL OR gecos_sep_c |
956 * | | | | | +------------------+ |
957 * | gecos_samename| | V V | |
958 * | +-------------+ | +---------------+ | |
959 * | | | | S2 | isspace OR '.' '-' '_' | |
960 * | | gecos_samename | | not in a word |<---------------------+ | |
961 * | | +---------------+ +---------------+ OR gecos_ignore_c | | |
962 * | | | | ^ | | OR ispunct OR other | | |
963 * | | | | | | | | | |
964 * | | | gecos_samename | | | +-----------------------+ | |
965 * | | | +---------------------+ | | | |
966 * | | | | | | | |
967 * | | | | gecos_ignore_c| | NULL OR gecos_sep_c | |
968 * | | | | gecos_samename| +-----------------------+ | |
969 * | | | | ispunct/other | | | |
970 * | V V V isspace | | | |
971 * | +-----------------+ any other char| | | |
972 * | | S3 |---------------+ isalpha OR isdigit OR | | |
973 * | |insert login name|------------------------------------------ ----- ---+
974 * | +-----------------+ '.' '-' '_' | |
975 * | | NULL OR gecos_sep_c | |
976 * | +------------------------------------------+ | |
977 * | | | |
978 * | V V V
979 * | +------------+
980 * | NULL OR gecos_sep_c | S4 |
981 * +-------------------------------------------------------->|end of gecos|<--+
982 * +------------+ |
983 * | all |
984 * +-----+
987 * The transitions from the above diagram are summarized in
988 * the following table of target states, which is implemented
989 * in code as the gecos_fsm array.
991 * Input:
992 * +--gecos_ignore_c
993 * | +--gecos_sep_c
994 * | | +--gecos_samename
995 * | | | +--isalpha
996 * | | | | +--isdigit
997 * | | | | | +--isspace
998 * | | | | | | +--punctuation possible in name
999 * | | | | | | | +--other punctuation
1000 * | | | | | | | | +--NULL character
1001 * | | | | | | | | | +--any other character
1002 * | | | | | | | | | |
1003 * V V V V V V V V V V
1004 * From: ---------------------------------------------------
1005 * S0 | S0 | S4 | S3 | S1 | S1 | S0 | S1 | S2 | S4 | S0 |
1006 * S1 | S2 | S4 | S3 | S1 | S1 | S2 | S1 | S2 | S4 | S2 |
1007 * S2 | S2 | S4 | S3 | S1 | S1 | S2 | S2 | S2 | S4 | S2 |
1008 * S3 | S2 | S4 | S2 | S1 | S1 | S2 | S1 | S2 | S4 | S2 |
1009 * S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 | S4 |
1014 * Data types and structures for scanning the pw_gecos field.
1016 typedef enum gecos_state {
1017 S0, /* start */
1018 S1, /* in a word */
1019 S2, /* not in a word */
1020 S3, /* copy login */
1021 S4 /* end of gecos */
1022 } gecos_state_t;
1024 #define GFSM_ROWS 5
1025 #define GFSM_COLS 10
1027 gecos_state_t gecos_fsm[GFSM_ROWS][GFSM_COLS] = {
1028 {S0, S4, S3, S1, S1, S0, S1, S2, S4, S0}, /* S0 */
1029 {S2, S4, S3, S1, S1, S2, S1, S2, S4, S2}, /* S1 */
1030 {S2, S4, S3, S1, S1, S2, S2, S2, S4, S2}, /* S2 */
1031 {S2, S4, S2, S1, S1, S2, S1, S2, S4, S2}, /* S3 */
1032 {S4, S4, S4, S4, S4, S4, S4, S4, S4, S4} /* S4 */
1036 * Scan the pw_gecos field according to defined state table;
1037 * return the next state according the the rules.
1039 gecos_state_t
1040 gecos_scan_state(gecos_state_t instate, char ch)
1042 if (ch == gecos_ignore_c) {
1043 return (gecos_fsm[instate][0]);
1044 } else if (ch == gecos_sep_c) {
1045 return (gecos_fsm[instate][1]);
1046 } else if (ch == gecos_samename) {
1047 return (gecos_fsm[instate][2]);
1048 } else if (isalpha(ch)) {
1049 return (gecos_fsm[instate][3]);
1050 } else if (isdigit(ch)) {
1051 return (gecos_fsm[instate][4]);
1052 } else if (isspace(ch)) {
1053 return (gecos_fsm[instate][5]);
1054 } else if (ch == '.' || ch == '-' || ch == '_') {
1055 return (gecos_fsm[instate][6]);
1056 } else if (ispunct(ch)) {
1057 return (gecos_fsm[instate][7]);
1058 } else if (ch == '\0') {
1059 return (gecos_fsm[instate][8]);
1061 return (gecos_fsm[instate][9]);
1066 * Compare the given argument, which is taken to be a username, with
1067 * the login name and with strings in the the pw_gecos field.
1070 matchcmp(char *gname, char *login, char *given)
1072 char buffer[100];
1073 char *bp, *lp, *gp;
1075 gecos_state_t kstate = S0;
1076 gecos_state_t kstate_next = S0;
1078 if (*gname == '\0' && *given == '\0')
1079 return (1);
1081 bp = buffer;
1082 gp = gname;
1084 do {
1085 kstate_next = gecos_scan_state(kstate, *gp);
1087 switch (kstate_next) {
1089 case S0:
1090 gp++;
1091 break;
1092 case S1:
1093 if (bp < buffer + sizeof (buffer)) {
1094 *bp++ = *gp++;
1096 break;
1097 case S2:
1098 if (kstate == S1 || kstate == S3) {
1099 *bp++ = ' ';
1101 gp++;
1102 break;
1103 case S3:
1104 lp = login;
1105 do {
1106 *bp++ = *lp++;
1107 } while (*bp != '\0' && bp < buffer + sizeof (buffer));
1108 bp--;
1109 break;
1110 case S4:
1111 *bp++ = '\0';
1112 break;
1113 default:
1114 *bp++ = '\0';
1115 break;
1117 kstate = kstate_next;
1119 } while ((bp < buffer + sizeof (buffer)) && kstate != S4);
1121 gp = strtok(buffer, " ");
1123 while (gp != NULL) {
1124 if (namecmp(gp, given) > 0) {
1125 return (1);
1127 gp = strtok(NULL, " ");
1129 return (0);
1133 * Perform the character-by-character comparison.
1134 * It is intended that "finger foo" should match "foo2", but an argument
1135 * consisting entirely of digits should not be matched too broadly.
1136 * Also, we do not want "finger foo123" to match "Mr. Foo" in the gecos.
1139 namecmp(char *name1, char *name2)
1141 char c1, c2;
1142 boolean_t alphaseen = B_FALSE;
1143 boolean_t digitseen = B_FALSE;
1145 for (;;) {
1146 c1 = *name1++;
1147 if (isalpha(c1))
1148 alphaseen = B_TRUE;
1149 if (isdigit(c1))
1150 digitseen = B_TRUE;
1151 if (isupper(c1))
1152 c1 = tolower(c1);
1154 c2 = *name2++;
1155 if (isupper(c2))
1156 c2 = tolower(c2);
1158 if (c1 != c2)
1159 break;
1160 if (c1 == '\0')
1161 return (1);
1163 if (!c1) {
1164 for (name2--; isdigit(*name2); name2++)
1166 if (*name2 == '\0' && digitseen) {
1167 return (1);
1169 } else if (!c2) {
1170 for (name1--; isdigit(*name1); name1++)
1172 if (*name1 == '\0' && alphaseen) {
1173 return (1);
1176 return (0);
1181 netfinger(char *name)
1183 char *host;
1184 struct hostent *hp;
1185 struct sockaddr_in6 sin6;
1186 struct in6_addr ipv6addr;
1187 struct in_addr ipv4addr;
1188 int s;
1189 FILE *f;
1190 int c;
1191 int lastc;
1192 char abuf[INET6_ADDRSTRLEN];
1193 int error_num;
1195 if (name == NULL)
1196 return (0);
1197 host = strrchr(name, '@');
1198 if (host == NULL)
1199 return (0);
1200 *host++ = 0;
1202 if ((hp = getipnodebyname(host, AF_INET6, AI_ALL | AI_ADDRCONFIG |
1203 AI_V4MAPPED, &error_num)) == NULL) {
1204 if (error_num == TRY_AGAIN) {
1205 (void) fprintf(stderr,
1206 "unknown host: %s (try again later)\n", host);
1207 } else {
1208 (void) fprintf(stderr, "unknown host: %s\n", host);
1210 return (1);
1214 * If hp->h_name is a IPv4-mapped IPv6 literal, we'll convert it to
1215 * IPv4 literal address.
1217 if ((inet_pton(AF_INET6, hp->h_name, &ipv6addr) > 0) &&
1218 IN6_IS_ADDR_V4MAPPED(&ipv6addr)) {
1219 IN6_V4MAPPED_TO_INADDR(&ipv6addr, &ipv4addr);
1220 (void) printf("[%s] ", inet_ntop(AF_INET, &ipv4addr, abuf,
1221 sizeof (abuf)));
1222 } else {
1223 (void) printf("[%s] ", hp->h_name);
1225 bzero(&sin6, sizeof (sin6));
1226 sin6.sin6_family = hp->h_addrtype;
1227 bcopy(hp->h_addr_list[0], (char *)&sin6.sin6_addr, hp->h_length);
1228 sin6.sin6_port = htons(IPPORT_FINGER);
1229 s = socket(sin6.sin6_family, SOCK_STREAM, 0);
1230 if (s < 0) {
1231 (void) fflush(stdout);
1232 perror("socket");
1233 freehostent(hp);
1234 return (1);
1236 while (connect(s, (struct sockaddr *)&sin6, sizeof (sin6)) < 0) {
1238 if (hp && hp->h_addr_list[1]) {
1240 hp->h_addr_list++;
1241 bcopy(hp->h_addr_list[0],
1242 (caddr_t)&sin6.sin6_addr, hp->h_length);
1243 (void) close(s);
1244 s = socket(sin6.sin6_family, SOCK_STREAM, 0);
1245 if (s < 0) {
1246 (void) fflush(stdout);
1247 perror("socket");
1248 freehostent(hp);
1249 return (0);
1251 continue;
1254 (void) fflush(stdout);
1255 perror("connect");
1256 (void) close(s);
1257 freehostent(hp);
1258 return (1);
1260 freehostent(hp);
1261 hp = NULL;
1263 (void) printf("\n");
1264 if (large)
1265 (void) write(s, "/W ", 3);
1266 (void) write(s, name, strlen(name));
1267 (void) write(s, "\r\n", 2);
1268 f = fdopen(s, "r");
1270 lastc = '\n';
1271 while ((c = getc(f)) != EOF) {
1272 /* map CRLF -> newline */
1273 if ((lastc == '\r') && (c != '\n'))
1274 /* print out saved CR */
1275 (void) putchar('\r');
1276 lastc = c;
1277 if (c == '\r')
1278 continue;
1279 (void) putchar(c);
1282 if (lastc != '\n')
1283 (void) putchar('\n');
1284 (void) fclose(f);
1285 return (1);
1289 * AnyMail - takes a username (string pointer thereto), and
1290 * prints on standard output whether there is any unread mail,
1291 * and if so, how old it is. (JCM@Shasta 15 March 80)
1293 void
1294 AnyMail(char *name)
1296 struct stat buf; /* space for file status buffer */
1297 char *mbxdir = MAILDIR; /* string with path preamble */
1298 char *mbxpath; /* space for entire pathname */
1300 char *timestr;
1302 mbxpath = malloc(strlen(name) + strlen(MAILDIR) + 1);
1303 if (mbxpath == NULL)
1304 return;
1306 (void) strcpy(mbxpath, mbxdir); /* copy preamble into path name */
1307 (void) strcat(mbxpath, name); /* concatenate user name to path */
1309 if (stat(mbxpath, &buf) == -1 || buf.st_size == 0) {
1310 /* Mailbox is empty or nonexistent */
1311 (void) printf("No unread mail\n");
1312 } else {
1313 if (buf.st_mtime < buf.st_atime) {
1315 * No new mail since the last time the user read it.
1317 (void) printf("Mail last read ");
1318 (void) printf("%s", ctime(&buf.st_atime));
1319 } else if (buf.st_mtime > buf.st_atime) {
1321 * New mail has definitely arrived since the last time
1322 * mail was read. mtime is the time the most recent
1323 * message arrived; atime is either the time the oldest
1324 * unread message arrived, or the last time the mail
1325 * was read.
1327 (void) printf("New mail received ");
1328 timestr = ctime(&buf.st_mtime); /* time last modified */
1329 timestr[24] = '\0'; /* suppress newline (ugh) */
1330 (void) printf("%s", timestr);
1331 (void) printf(";\n unread since ");
1332 (void) printf("%s", ctime(&buf.st_atime));
1333 } else {
1335 * There is something in mailbox, but we can't really
1336 * be sure whether it is mail held there by the user
1337 * or a (single) new message that was placed in a newly
1338 * recreated mailbox, so punt and call it "unread mail."
1340 (void) printf("Unread mail since ");
1341 (void) printf("%s", ctime(&buf.st_mtime));
1344 free(mbxpath);
1348 * return true iff we've already printed project/plan for this uid;
1349 * if not, enter this uid into table (so this function has a side-effect.)
1351 #define PPMAX 4096 /* assume no more than 4096 logged-in users */
1352 uid_t PlanPrinted[PPMAX+1];
1353 int PPIndex = 0; /* index of next unused table entry */
1356 AlreadyPrinted(uid_t uid)
1358 int i = 0;
1360 while (i++ < PPIndex) {
1361 if (PlanPrinted[i] == uid)
1362 return (1);
1364 if (i < PPMAX) {
1365 PlanPrinted[i] = uid;
1366 PPIndex++;
1368 return (0);
1371 #define FIFOREADTIMEOUT (60) /* read timeout on select */
1372 /* BEGIN CSTYLED */
1373 #define PRINT_CHAR(c) \
1375 ((termpass & TS_HIGH) && ((int)c) > 126) \
1376 || \
1377 (isascii((int)c) && \
1378 (isprint((int)c) || isspace((int)c)) \
1380 || \
1381 ((termpass & TS_LOW) && ((int)c) < 32) \
1383 /* END CSTYLED */
1386 void
1387 catfile(char *s, mode_t mode, int trunc_at_nl)
1389 if (S_ISFIFO(mode)) {
1390 int fd;
1392 fd = open(s, O_RDONLY | O_NONBLOCK);
1393 if (fd != -1) {
1394 fd_set readfds, exceptfds;
1395 struct timeval tv;
1397 FD_ZERO(&readfds);
1398 FD_ZERO(&exceptfds);
1399 FD_SET(fd, &readfds);
1400 FD_SET(fd, &exceptfds);
1402 timerclear(&tv);
1403 tv.tv_sec = FIFOREADTIMEOUT;
1405 (void) fflush(stdout);
1406 while (select(fd + 1, &readfds, (fd_set *) 0,
1407 &exceptfds, &tv) != -1) {
1408 unsigned char buf[BUFSIZ];
1409 int nread;
1411 nread = read(fd, buf, sizeof (buf));
1412 if (nread > 0) {
1413 unsigned char *p;
1415 FD_SET(fd, &readfds);
1416 FD_SET(fd, &exceptfds);
1417 for (p = buf; p < buf + nread; p++) {
1418 if (trunc_at_nl && *p == '\n')
1419 goto out;
1420 if (PRINT_CHAR(*p))
1421 (void) putchar((int)*p);
1422 else if (isascii(*p))
1423 (void) fputs(unctrl(*p),
1424 stdout);
1426 } else
1427 break;
1429 out:
1430 (void) close(fd);
1432 } else {
1433 int c;
1434 FILE *fp;
1436 fp = fopen(s, "r");
1437 if (fp) {
1438 while ((c = getc(fp)) != EOF) {
1439 if (trunc_at_nl && c == '\n')
1440 break;
1441 if (PRINT_CHAR(c))
1442 (void) putchar((int)c);
1443 else
1444 if (isascii(c))
1445 (void) fputs(unctrl(c), stdout);
1447 (void) fclose(fp);
1453 void
1454 initscreening(void)
1456 char *options, *value;
1458 if (defopen(defaultfile) == 0) {
1459 char *cp;
1460 int flags;
1463 * ignore case
1465 flags = defcntl(DC_GETFLAGS, 0);
1466 TURNOFF(flags, DC_CASE);
1467 (void) defcntl(DC_SETFLAGS, flags);
1469 if (cp = defread(passvar)) {
1470 options = cp;
1471 while (*options != '\0')
1472 switch (getsubopt(&options, termopts, &value)) {
1473 case TERM_LOW:
1474 termpass |= TS_LOW;
1475 break;
1476 case TERM_HIGH:
1477 termpass |= TS_HIGH;
1478 break;
1481 (void) defopen(NULL); /* close default file */
1486 person_compare(const void *p1, const void *p2)
1488 const struct person *pp1 = *(struct person **)p1;
1489 const struct person *pp2 = *(struct person **)p2;
1490 int r;
1493 * Sort by username.
1495 r = strcmp(pp1->name, pp2->name);
1497 if (r != 0)
1498 return (r);
1501 * If usernames are the same, sort by idle time.
1503 r = pp1->idletime - pp2->idletime;
1505 return (r);
1508 void
1509 sort_by_username()
1511 struct person **sortable, *loop;
1512 size_t i;
1514 sortable = malloc(sizeof (sortable[0]) * nperson);
1516 if (sortable == NULL)
1517 return;
1519 for (i = 0, loop = person1; i < nperson; i++) {
1520 struct person *next = loop->link;
1522 sortable[i] = loop;
1523 loop->link = NULL;
1525 loop = next;
1528 qsort(sortable, nperson, sizeof (sortable[0]), person_compare);
1530 for (i = 1; i < nperson; i++)
1531 sortable[i-1]->link = sortable[i];
1532 person1 = sortable[0];
1534 free(sortable);