/usr.bin/finger/finger.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 405 lines · 280 code · 38 blank · 87 comment · 112 complexity · 8cac2286bae80268469763404a141a14 MD5 · raw file

  1. /*
  2. * Copyright (c) 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Tony Nardo of the Johns Hopkins University/Applied Physics Lab.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. /*
  33. * Luke Mewburn <lm@rmit.edu.au> added the following on 940622:
  34. * - mail status ("No Mail", "Mail read:...", or "New Mail ...,
  35. * Unread since ...".)
  36. * - 4 digit phone extensions (3210 is printed as x3210.)
  37. * - host/office toggling in short format with -h & -o.
  38. * - short day names (`Tue' printed instead of `Jun 21' if the
  39. * login time is < 6 days.
  40. */
  41. #ifndef lint
  42. static const char copyright[] =
  43. "@(#) Copyright (c) 1989, 1993\n\
  44. The Regents of the University of California. All rights reserved.\n";
  45. #endif /* not lint */
  46. #if 0
  47. #ifndef lint
  48. static char sccsid[] = "@(#)finger.c 8.5 (Berkeley) 5/4/95";
  49. #endif
  50. #endif
  51. #include <sys/cdefs.h>
  52. __FBSDID("$FreeBSD$");
  53. /*
  54. * Finger prints out information about users. It is not portable since
  55. * certain fields (e.g. the full user name, office, and phone numbers) are
  56. * extracted from the gecos field of the passwd file which other UNIXes
  57. * may not have or may use for other things.
  58. *
  59. * There are currently two output formats; the short format is one line
  60. * per user and displays login name, tty, login time, real name, idle time,
  61. * and either remote host information (default) or office location/phone
  62. * number, depending on if -h or -o is used respectively.
  63. * The long format gives the same information (in a more legible format) as
  64. * well as home directory, shell, mail info, and .plan/.project files.
  65. */
  66. #include <sys/types.h>
  67. #include <sys/socket.h>
  68. #include <db.h>
  69. #include <err.h>
  70. #include <pwd.h>
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <time.h>
  75. #include <unistd.h>
  76. #include <utmpx.h>
  77. #include <locale.h>
  78. #include "finger.h"
  79. #include "pathnames.h"
  80. DB *db;
  81. time_t now;
  82. int entries, gflag, kflag, lflag, mflag, pplan, sflag, oflag;
  83. sa_family_t family = PF_UNSPEC;
  84. int d_first = -1;
  85. char tbuf[1024];
  86. int invoker_root = 0;
  87. static void loginlist(void);
  88. static int option(int, char **);
  89. static void usage(void);
  90. static void userlist(int, char **);
  91. static int
  92. option(int argc, char **argv)
  93. {
  94. int ch;
  95. optind = 1; /* reset getopt */
  96. while ((ch = getopt(argc, argv, "46gklmpsho")) != -1)
  97. switch(ch) {
  98. case '4':
  99. family = AF_INET;
  100. break;
  101. case '6':
  102. family = AF_INET6;
  103. break;
  104. case 'g':
  105. gflag = 1;
  106. break;
  107. case 'k':
  108. kflag = 1; /* keep going without utmp */
  109. break;
  110. case 'l':
  111. lflag = 1; /* long format */
  112. break;
  113. case 'm':
  114. mflag = 1; /* force exact match of names */
  115. break;
  116. case 'p':
  117. pplan = 1; /* don't show .plan/.project */
  118. break;
  119. case 's':
  120. sflag = 1; /* short format */
  121. break;
  122. case 'h':
  123. oflag = 0; /* remote host info */
  124. break;
  125. case 'o':
  126. oflag = 1; /* office info */
  127. break;
  128. case '?':
  129. default:
  130. usage();
  131. }
  132. return optind;
  133. }
  134. static void
  135. usage(void)
  136. {
  137. (void)fprintf(stderr,
  138. "usage: finger [-46gklmpsho] [user ...] [user@host ...]\n");
  139. exit(1);
  140. }
  141. int
  142. main(int argc, char **argv)
  143. {
  144. int envargc, argcnt;
  145. char *envargv[3];
  146. struct passwd *pw;
  147. static char myname[] = "finger";
  148. if (getuid() == 0 || geteuid() == 0) {
  149. invoker_root = 1;
  150. if ((pw = getpwnam(UNPRIV_NAME)) && pw->pw_uid > 0) {
  151. if (setgid(pw->pw_gid) != 0)
  152. err(1, "setgid()");
  153. if (setuid(pw->pw_uid) != 0)
  154. err(1, "setuid()");
  155. } else {
  156. if (setgid(UNPRIV_UGID) != 0)
  157. err(1, "setgid()");
  158. if (setuid(UNPRIV_UGID) != 0)
  159. err(1, "setuid()");
  160. }
  161. }
  162. (void) setlocale(LC_ALL, "");
  163. /* remove this line to get remote host */
  164. oflag = 1; /* default to old "office" behavior */
  165. /*
  166. * Process environment variables followed by command line arguments.
  167. */
  168. if ((envargv[1] = getenv("FINGER"))) {
  169. envargc = 2;
  170. envargv[0] = myname;
  171. envargv[2] = NULL;
  172. (void) option(envargc, envargv);
  173. }
  174. argcnt = option(argc, argv);
  175. argc -= argcnt;
  176. argv += argcnt;
  177. (void)time(&now);
  178. setpassent(1);
  179. if (!*argv) {
  180. /*
  181. * Assign explicit "small" format if no names given and -l
  182. * not selected. Force the -s BEFORE we get names so proper
  183. * screening will be done.
  184. */
  185. if (!lflag)
  186. sflag = 1; /* if -l not explicit, force -s */
  187. loginlist();
  188. if (entries == 0)
  189. (void)printf("No one logged on.\n");
  190. } else {
  191. userlist(argc, argv);
  192. /*
  193. * Assign explicit "large" format if names given and -s not
  194. * explicitly stated. Force the -l AFTER we get names so any
  195. * remote finger attempts specified won't be mishandled.
  196. */
  197. if (!sflag)
  198. lflag = 1; /* if -s not explicit, force -l */
  199. }
  200. if (entries) {
  201. if (lflag)
  202. lflag_print();
  203. else
  204. sflag_print();
  205. }
  206. return (0);
  207. }
  208. static void
  209. loginlist(void)
  210. {
  211. PERSON *pn;
  212. DBT data, key;
  213. struct passwd *pw;
  214. struct utmpx *user;
  215. int r, sflag1;
  216. if (kflag)
  217. errx(1, "can't list logins without reading utmp");
  218. setutxent();
  219. while ((user = getutxent()) != NULL) {
  220. if (user->ut_type != USER_PROCESS)
  221. continue;
  222. if ((pn = find_person(user->ut_user)) == NULL) {
  223. if ((pw = getpwnam(user->ut_user)) == NULL)
  224. continue;
  225. if (hide(pw))
  226. continue;
  227. pn = enter_person(pw);
  228. }
  229. enter_where(user, pn);
  230. }
  231. endutxent();
  232. if (db && lflag)
  233. for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
  234. PERSON *tmp;
  235. r = (*db->seq)(db, &key, &data, sflag1);
  236. if (r == -1)
  237. err(1, "db seq");
  238. if (r == 1)
  239. break;
  240. memmove(&tmp, data.data, sizeof tmp);
  241. enter_lastlog(tmp);
  242. }
  243. }
  244. static void
  245. userlist(int argc, char **argv)
  246. {
  247. PERSON *pn;
  248. DBT data, key;
  249. struct utmpx *user;
  250. struct passwd *pw;
  251. int r, sflag1, *used, *ip;
  252. char **ap, **nargv, **np, **p;
  253. FILE *conf_fp;
  254. char conf_alias[LINE_MAX];
  255. char *conf_realname;
  256. int conf_length;
  257. if ((nargv = malloc((argc+1) * sizeof(char *))) == NULL ||
  258. (used = calloc(argc, sizeof(int))) == NULL)
  259. err(1, NULL);
  260. /* Pull out all network requests. */
  261. for (ap = p = argv, np = nargv; *p; ++p)
  262. if (strchr(*p, '@'))
  263. *np++ = *p;
  264. else
  265. *ap++ = *p;
  266. *np++ = NULL;
  267. *ap++ = NULL;
  268. if (!*argv)
  269. goto net;
  270. /*
  271. * Mark any arguments beginning with '/' as invalid so that we
  272. * don't accidentally confuse them with expansions from finger.conf
  273. */
  274. for (p = argv, ip = used; *p; ++p, ++ip)
  275. if (**p == '/') {
  276. *ip = 1;
  277. warnx("%s: no such user", *p);
  278. }
  279. /*
  280. * Traverse the finger alias configuration file of the form
  281. * alias:(user|alias), ignoring comment lines beginning '#'.
  282. */
  283. if ((conf_fp = fopen(_PATH_FINGERCONF, "r")) != NULL) {
  284. while(fgets(conf_alias, sizeof(conf_alias), conf_fp) != NULL) {
  285. conf_length = strlen(conf_alias);
  286. if (*conf_alias == '#' || conf_alias[--conf_length] != '\n')
  287. continue;
  288. conf_alias[conf_length] = '\0'; /* Remove trailing LF */
  289. if ((conf_realname = strchr(conf_alias, ':')) == NULL)
  290. continue;
  291. *conf_realname = '\0'; /* Replace : with NUL */
  292. for (p = argv; *p; ++p) {
  293. if (strcmp(*p, conf_alias) == 0) {
  294. if ((*p = strdup(conf_realname+1)) == NULL) {
  295. err(1, NULL);
  296. }
  297. }
  298. }
  299. }
  300. (void)fclose(conf_fp);
  301. }
  302. /*
  303. * Traverse the list of possible login names and check the login name
  304. * and real name against the name specified by the user. If the name
  305. * begins with a '/', try to read the file of that name instead of
  306. * gathering the traditional finger information.
  307. */
  308. if (mflag)
  309. for (p = argv, ip = used; *p; ++p, ++ip) {
  310. if (**p != '/' || *ip == 1 || !show_text("", *p, "")) {
  311. if (((pw = getpwnam(*p)) != NULL) && !hide(pw))
  312. enter_person(pw);
  313. else if (!*ip)
  314. warnx("%s: no such user", *p);
  315. }
  316. }
  317. else {
  318. while ((pw = getpwent()) != NULL) {
  319. for (p = argv, ip = used; *p; ++p, ++ip)
  320. if (**p == '/' && *ip != 1
  321. && show_text("", *p, ""))
  322. *ip = 1;
  323. else if (match(pw, *p) && !hide(pw)) {
  324. enter_person(pw);
  325. *ip = 1;
  326. }
  327. }
  328. for (p = argv, ip = used; *p; ++p, ++ip)
  329. if (!*ip)
  330. warnx("%s: no such user", *p);
  331. }
  332. /* Handle network requests. */
  333. net: for (p = nargv; *p;) {
  334. netfinger(*p++);
  335. if (*p || entries)
  336. printf("\n");
  337. }
  338. free(used);
  339. if (entries == 0)
  340. return;
  341. if (kflag)
  342. return;
  343. /*
  344. * Scan thru the list of users currently logged in, saving
  345. * appropriate data whenever a match occurs.
  346. */
  347. setutxent();
  348. while ((user = getutxent()) != NULL) {
  349. if (user->ut_type != USER_PROCESS)
  350. continue;
  351. if ((pn = find_person(user->ut_user)) == NULL)
  352. continue;
  353. enter_where(user, pn);
  354. }
  355. endutxent();
  356. if (db)
  357. for (sflag1 = R_FIRST;; sflag1 = R_NEXT) {
  358. PERSON *tmp;
  359. r = (*db->seq)(db, &key, &data, sflag1);
  360. if (r == -1)
  361. err(1, "db seq");
  362. if (r == 1)
  363. break;
  364. memmove(&tmp, data.data, sizeof tmp);
  365. enter_lastlog(tmp);
  366. }
  367. }