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*38586Sedward static char sccsid[] = "@(#)finger.c 5.17 (Berkeley) 08/11/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 { 96*38586Sedward 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; 117*38586Sedward FILE *fp; 11837659Sbostic struct passwd *pw; 11937664Sedward struct utmp user; 12037664Sedward char name[UT_NAMESIZE + 1]; 1211014Sbill 122*38586Sedward if ((fp = fopen(_PATH_UTMP, "r")) == NULL) { 12337659Sbostic (void)fprintf(stderr, "finger: can't read %s.\n", _PATH_UTMP); 12418606Sedward exit(2); 1251014Sbill } 12637659Sbostic name[UT_NAMESIZE] = NULL; 127*38586Sedward while (fread((char *)&user, sizeof(user), 1, fp) == 1) { 12837659Sbostic if (!user.ut_name[0]) 12918606Sedward continue; 13037664Sedward if ((pn = find_person(user.ut_name)) == NULL) { 13137664Sedward bcopy(user.ut_name, name, UT_NAMESIZE); 13237664Sedward if ((pw = getpwnam(name)) == NULL) 13337664Sedward continue; 13437664Sedward pn = enter_person(pw); 1351014Sbill } 13637664Sedward enter_where(&user, pn); 13718606Sedward } 138*38586Sedward (void)fclose(fp); 13937664Sedward for (pn = phead; lflag && pn != NULL; pn = pn->next) 14037664Sedward enter_lastlog(pn); 14118606Sedward } 1421014Sbill 143*38586Sedward userlist(argc, argv) 144*38586Sedward register argc; 145*38586Sedward register char **argv; 14618606Sedward { 147*38586Sedward register i; 14837664Sedward register PERSON *pn; 14937664Sedward PERSON *nethead; 15037664Sedward struct utmp user; 15137664Sedward struct passwd *pw; 152*38586Sedward int dolocal, *used; 153*38586Sedward FILE *fp; 154*38586Sedward char *index(); 1551014Sbill 156*38586Sedward if (!(used = (int *)calloc((u_int)argc, (u_int)sizeof(int)))) { 157*38586Sedward (void)fprintf(stderr, "finger: out of space.\n"); 158*38586Sedward exit(1); 159*38586Sedward } 160*38586Sedward 16137659Sbostic /* pull out all network requests */ 162*38586Sedward for (i = 0, dolocal = 0, nethead = NULL; i < argc; i++) { 163*38586Sedward if (!index(argv[i], '@')) { 16437659Sbostic dolocal = 1; 16518606Sedward continue; 16616469Ssam } 16737664Sedward pn = palloc(); 16837659Sbostic pn->next = nethead; 16937659Sbostic nethead = pn; 170*38586Sedward pn->name = argv[i]; 171*38586Sedward used[i] = -1; 17218606Sedward } 17337659Sbostic 17437659Sbostic if (!dolocal) 17537659Sbostic goto net; 17637659Sbostic 17718606Sedward /* 17837659Sbostic * traverse the list of possible login names and check the login name 17937664Sedward * and real name against the name specified by the user. 18018606Sedward */ 18137996Sbostic if (mflag) { 182*38586Sedward for (i = 0; i < argc; i++) 183*38586Sedward if (used[i] >= 0 && (pw = getpwnam(argv[i]))) { 18437996Sbostic enter_person(pw); 185*38586Sedward used[i] = 1; 18637996Sbostic } 18737996Sbostic } else while (pw = getpwent()) 188*38586Sedward for (i = 0; i < argc; i++) 189*38586Sedward if (used[i] >= 0 && 190*38586Sedward (!strcasecmp(pw->pw_name, argv[i]) || 191*38586Sedward match(pw, argv[i]))) { 19237996Sbostic enter_person(pw); 193*38586Sedward used[i] = 1; 19437996Sbostic } 19537659Sbostic 19637659Sbostic /* list errors */ 197*38586Sedward for (i = 0; i < argc; i++) 198*38586Sedward if (!used[i]) 19937659Sbostic (void)fprintf(stderr, 200*38586Sedward "finger: %s: no such user.\n", argv[i]); 20137659Sbostic 20237659Sbostic /* handle network requests */ 20337659Sbostic net: for (pn = nethead; pn; pn = pn->next) { 20437659Sbostic netfinger(pn->name); 20537659Sbostic if (pn->next || entries) 20637659Sbostic putchar('\n'); 2071014Sbill } 2081014Sbill 20937664Sedward if (entries == 0) 21037659Sbostic return; 2111014Sbill 21218606Sedward /* 21337659Sbostic * Scan thru the list of users currently logged in, saving 21437659Sbostic * appropriate data whenever a match occurs. 21518606Sedward */ 216*38586Sedward if ((fp = fopen(_PATH_UTMP, "r")) == NULL) { 21737659Sbostic (void)fprintf( stderr, "finger: can't read %s.\n", _PATH_UTMP); 21837659Sbostic exit(1); 21918606Sedward } 220*38586Sedward while (fread((char *)&user, sizeof(user), 1, fp) == 1) { 22137659Sbostic if (!user.ut_name[0]) 22218606Sedward continue; 22337664Sedward if ((pn = find_person(user.ut_name)) == NULL) 22437664Sedward continue; 22537664Sedward enter_where(&user, pn); 22618606Sedward } 227*38586Sedward (void)fclose(fp); 22837664Sedward for (pn = phead; pn != NULL; pn = pn->next) 22937664Sedward enter_lastlog(pn); 2301014Sbill } 231