121554Sdist /* 237659Sbostic * Copyright (c) 1989 The Regents of the University of California. 335627Sbostic * All rights reserved. 435627Sbostic * 535627Sbostic * Redistribution and use in source and binary forms are permitted 635627Sbostic * provided that the above copyright notice and this paragraph are 735627Sbostic * duplicated in all such forms and that any documentation, 835627Sbostic * advertising materials, and other materials related to such 935627Sbostic * distribution and use acknowledge that the software was developed 1035627Sbostic * by the University of California, Berkeley. The name of the 1135627Sbostic * University may not be used to endorse or promote products derived 1235627Sbostic * from this software without specific prior written permission. 1335627Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435627Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1537659Sbostic * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1621554Sdist */ 1721554Sdist 1813619Ssam #ifndef lint 1921554Sdist char copyright[] = 2037659Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ 2121554Sdist All rights reserved.\n"; 2235627Sbostic #endif /* not lint */ 231014Sbill 2421554Sdist #ifndef lint 25*38901Sbostic static char sccsid[] = "@(#)finger.c 5.18 (Berkeley) 09/01/89"; 2635627Sbostic #endif /* not lint */ 2721554Sdist 2818606Sedward /* 2937659Sbostic * Finger prints out information about users. It is not portable since 3037659Sbostic * certain fields (e.g. the full user name, office, and phone numbers) are 3137659Sbostic * extracted from the gecos field of the passwd file which other UNIXes 3237659Sbostic * may not have or may use for other things. 331014Sbill * 3437659Sbostic * There are currently two output formats; the short format is one line 3537659Sbostic * per user and displays login name, tty, login time, real name, idle time, 3637659Sbostic * and office location/phone number. The long format gives the same 3737659Sbostic * information (in a more legible format) as well as home directory, shell, 3837659Sbostic * mail info, and .plan/.project files. 391014Sbill */ 401014Sbill 4137659Sbostic #include <sys/param.h> 4237659Sbostic #include <sys/file.h> 4337629Sbostic #include <stdio.h> 4437659Sbostic #include "finger.h" 4537629Sbostic #include "pathnames.h" 461014Sbill 4737659Sbostic time_t now; 4837664Sedward int lflag, sflag, mflag, pplan; 4937659Sbostic char tbuf[1024]; 501014Sbill 5118606Sedward main(argc, argv) 5218606Sedward int argc; 5337659Sbostic char **argv; 5418606Sedward { 5537659Sbostic extern int optind; 5637659Sbostic int ch; 5737659Sbostic time_t time(); 581014Sbill 5937659Sbostic while ((ch = getopt(argc, argv, "lmps")) != EOF) 6037659Sbostic switch(ch) { 6137659Sbostic case 'l': 6237659Sbostic lflag = 1; /* long format */ 6337659Sbostic break; 6437659Sbostic case 'm': 6537659Sbostic mflag = 1; /* force exact match of names */ 6637659Sbostic break; 6737659Sbostic case 'p': 6837659Sbostic pplan = 1; /* don't show .plan/.project */ 6937659Sbostic break; 7037659Sbostic case 's': 7137659Sbostic sflag = 1; /* short format */ 7237659Sbostic break; 7337659Sbostic case '?': 7437659Sbostic default: 7537659Sbostic (void)fprintf(stderr, 7637659Sbostic "usage: finger [-lmps] [login ...]\n"); 7737659Sbostic exit(1); 7837659Sbostic } 7937659Sbostic argc -= optind; 8037659Sbostic argv += optind; 8137659Sbostic 8237659Sbostic (void)time(&now); 8337659Sbostic setpassent(1); 8437659Sbostic if (!*argv) { 8537659Sbostic /* 8637659Sbostic * Assign explicit "small" format if no names given and -l 8737659Sbostic * not selected. Force the -s BEFORE we get names so proper 8837659Sbostic * screening will be done. 8937659Sbostic */ 9037659Sbostic if (!lflag) 9137659Sbostic sflag = 1; /* if -l not explicit, force -s */ 9237659Sbostic loginlist(); 9337664Sedward if (entries == 0) 9437659Sbostic (void)printf("No one logged on.\n"); 9537659Sbostic } else { 9638586Sedward userlist(argc, argv); 9737659Sbostic /* 9837659Sbostic * Assign explicit "large" format if names given and -s not 9937659Sbostic * explicitly stated. Force the -l AFTER we get names so any 10037659Sbostic * remote finger attempts specified won't be mishandled. 10137659Sbostic */ 10237659Sbostic if (!sflag) 10337659Sbostic lflag = 1; /* if -s not explicit, force -l */ 10437659Sbostic } 10537664Sedward if (entries != 0) { 10637659Sbostic if (lflag) 10737659Sbostic lflag_print(); 10837659Sbostic else 10937659Sbostic sflag_print(); 11037659Sbostic } 11118606Sedward exit(0); 11218606Sedward } 1131014Sbill 11437659Sbostic loginlist() 1151014Sbill { 11637659Sbostic register PERSON *pn; 11737659Sbostic struct passwd *pw; 11837664Sedward struct utmp user; 11937664Sedward char name[UT_NAMESIZE + 1]; 1201014Sbill 121*38901Sbostic if (!freopen(_PATH_UTMP, "r", stdin)) { 12237659Sbostic (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP); 12318606Sedward exit(2); 1241014Sbill } 12537659Sbostic name[UT_NAMESIZE] = NULL; 126*38901Sbostic while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 12737659Sbostic if (!user.ut_name[0]) 12818606Sedward continue; 12937664Sedward if ((pn = find_person(user.ut_name)) == NULL) { 13037664Sedward bcopy(user.ut_name, name, UT_NAMESIZE); 13137664Sedward if ((pw = getpwnam(name)) == NULL) 13237664Sedward continue; 13337664Sedward pn = enter_person(pw); 1341014Sbill } 13537664Sedward enter_where(&user, pn); 13618606Sedward } 13737664Sedward for (pn = phead; lflag && pn != NULL; pn = pn->next) 13837664Sedward enter_lastlog(pn); 13918606Sedward } 1401014Sbill 14138586Sedward userlist(argc, argv) 14238586Sedward register argc; 14338586Sedward register char **argv; 14418606Sedward { 14538586Sedward register i; 14637664Sedward register PERSON *pn; 14737664Sedward PERSON *nethead; 14837664Sedward struct utmp user; 14937664Sedward struct passwd *pw; 15038586Sedward int dolocal, *used; 15138586Sedward char *index(); 1521014Sbill 15338586Sedward if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) { 15438586Sedward (void)fprintf(stderr, "finger: out of space.\n"); 15538586Sedward exit(1); 15638586Sedward } 15738586Sedward 15837659Sbostic /* pull out all network requests */ 15938586Sedward for (i = 0, dolocal = 0, nethead = NULL; i < argc; i++) { 16038586Sedward if (!index(argv[i], '@')) { 16137659Sbostic dolocal = 1; 16218606Sedward continue; 16316469Ssam } 16437664Sedward pn = palloc(); 16537659Sbostic pn->next = nethead; 16637659Sbostic nethead = pn; 16738586Sedward pn->name = argv[i]; 16838586Sedward used[i] = -1; 16918606Sedward } 17037659Sbostic 17137659Sbostic if (!dolocal) 17237659Sbostic goto net; 17337659Sbostic 17418606Sedward /* 17537659Sbostic * traverse the list of possible login names and check the login name 17637664Sedward * and real name against the name specified by the user. 17718606Sedward */ 17837996Sbostic if (mflag) { 17938586Sedward for (i = 0; i < argc; i++) 18038586Sedward if (used[i] >= 0 && (pw = getpwnam(argv[i]))) { 18137996Sbostic enter_person(pw); 18238586Sedward used[i] = 1; 18337996Sbostic } 18437996Sbostic } else while (pw = getpwent()) 18538586Sedward for (i = 0; i < argc; i++) 18638586Sedward if (used[i] >= 0 && 18738586Sedward (!strcasecmp(pw->pw_name, argv[i]) || 18838586Sedward match(pw, argv[i]))) { 18937996Sbostic enter_person(pw); 19038586Sedward used[i] = 1; 19137996Sbostic } 19237659Sbostic 19337659Sbostic /* list errors */ 19438586Sedward for (i = 0; i < argc; i++) 19538586Sedward if (!used[i]) 19637659Sbostic (void)fprintf(stderr, 19738586Sedward "finger: %s: no such user.\n", argv[i]); 19837659Sbostic 19937659Sbostic /* handle network requests */ 20037659Sbostic net: for (pn = nethead; pn; pn = pn->next) { 20137659Sbostic netfinger(pn->name); 20237659Sbostic if (pn->next || entries) 20337659Sbostic putchar('\n'); 2041014Sbill } 2051014Sbill 20637664Sedward if (entries == 0) 20737659Sbostic return; 2081014Sbill 20918606Sedward /* 21037659Sbostic * Scan thru the list of users currently logged in, saving 21137659Sbostic * appropriate data whenever a match occurs. 21218606Sedward */ 213*38901Sbostic if (!freopen(_PATH_UTMP, "r", stdin)) { 21437659Sbostic (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP); 21537659Sbostic exit(1); 21618606Sedward } 217*38901Sbostic while (fread((char *)&user, sizeof(user), 1, stdin) == 1) { 21837659Sbostic if (!user.ut_name[0]) 21918606Sedward continue; 22037664Sedward if ((pn = find_person(user.ut_name)) == NULL) 22137664Sedward continue; 22237664Sedward enter_where(&user, pn); 22318606Sedward } 22437664Sedward for (pn = phead; pn != NULL; pn = pn->next) 22537664Sedward enter_lastlog(pn); 2261014Sbill } 227