2 Copyright (C) 2001-2011 Ben Kibbey <bjk@luxsci.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
33 #include "../strsep.c"
43 #define LOGIN_OPTION_ORDER "pdimyhtl"
44 #define LOGIN_OPTION_STRING "Lpdimyhtl:"
46 static char options
[9]; /* NULL terminated. */
47 static char *last_options
;
48 static char **strings
;
51 static int login_count
;
53 void add_string(char ***, const char *);
54 char *stamp(time_t, const char *);
56 void ui_module_init(int *chainable
)
78 static void free_logins(UTMP
**u
)
83 for (up
= u
; *up
; up
++)
91 /* This is for *BSD (login process id). */
93 static char *get_pid(uid_t uid
, int multi
)
96 static char line
[LINE_MAX
];
99 char errbuf
[LINE_MAX
];
100 struct kinfo_proc
*kp
;
111 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
,
112 O_RDONLY
, errbuf
)) == NULL
) {
114 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
115 O_RDONLY
, errbuf
)) == NULL
) {
122 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
123 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
127 for (i
= 0; i
< cnt
; i
++) {
130 #if __FreeBSD_version < 500000
131 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
!=
133 pid
= kp
[i
].kp_eproc
.e_ppid
;
135 * pid = kp[i].kp_proc.p_pid;
141 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
144 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
145 safe_strncat(line
, buf
, sizeof(line
));
152 line
[strlen(line
) - 1] = '\0';
156 /* This is for Linux and Solaris. */
157 #elif defined(HAVE_PROCFS)
158 #include <sys/types.h>
159 #include <sys/stat.h>
170 static char *get_pid(uid_t uid
, int multi
)
175 static char line
[LINE_MAX
];
176 pid_t
*pids
= 0, *tpids
;
181 struct pstatus pstat
;
188 if (!procdir
&& firstrun
)
194 if ((procdir
= opendir("/proc")) == NULL
) {
203 while ((ent
= readdir(procdir
)) != NULL
) {
205 char filename
[FILENAME_MAX
];
213 if (!isdigit((unsigned char) *ent
->d_name
))
217 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
219 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
222 if (stat(filename
, &st
) == -1)
226 * The current user owns this file (process id).
228 if (st
.st_uid
== uid
) {
230 if ((fd
= open(filename
, O_RDONLY
)) == -1)
233 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
234 sizeof(struct pstatus
)) {
242 if ((fp
= fopen(filename
, "r")) == NULL
)
245 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
251 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
255 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
266 * Skip duplicate pids.
268 for (i
= 0; i
< pid_index
; i
++) {
273 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
274 safe_strncat(line
, buf
, sizeof(line
));
277 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
283 pids
[pid_index
++] = pid
;
293 line
[strlen(line
) - 1] = '\0';
297 /* Unsupported OS. */
298 static char *get_pid(uid_t uid
, int multi
)
305 /* Break up the last login string into sections and add the sections to the
306 * output string array if needed. */
307 static void last_strings(char *str
)
311 const char *line
, *host
, *when
;
313 line
= host
= when
= (str
) ? "-" : "!";
315 while ((buf
= strsep(&str
, ",")) != NULL
) {
334 for (i
= 0; i
< strlen(last_options
); i
++) {
335 switch (last_options
[i
]) {
337 add_string(&strings
, line
);
340 add_string(&strings
, host
);
343 add_string(&strings
, when
);
346 add_string(&strings
, line
);
347 add_string(&strings
, host
);
348 add_string(&strings
, when
);
355 /* Get the lastlog structure from the lastlog file. */
356 #if __FreeBSD_version >= 900000
357 static char *lastlogin(const struct passwd
*pw
, char *tf
)
360 static char buf
[LINE_MAX
];
362 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
367 last
= getutxuser(pw
->pw_name
);
372 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
373 !last
->ut_line
[0] ? "!" : last
->ut_line
,
374 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ? "-" : last
->ut_host
,
375 !last
->ut_time
[0] ? "!" : stamp(last
->ut_time
, tf
));
380 static char *lastlogin(const struct passwd
*pw
, char *tf
)
384 static char buf
[LINE_MAX
];
391 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
392 warn("%s", _PATH_LASTLOG
);
397 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
399 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
400 warn("%s", _PATH_LASTLOG
);
404 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
405 sizeof(struct lastlog
)) {
407 warn("%s", _PATH_LASTLOG
);
414 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
415 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
417 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
418 last
.ll_line
[UT_LINESIZE
-1] = '\0';
421 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
422 last
.ll_line
[UT_LINESIZE
-1] = '\0';
425 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
426 !last
.ll_line
[0] ? "!" : last
.ll_line
,
427 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
428 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
429 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
434 /* This will return an array of utmp structures if a user is logged in, NULL
435 * otherwise. We'll try to keep the utmp file descriptor open if possible to
436 * speed things up a bit. */
437 static UTMP
**get_utmp(const char *user
)
439 UTMP
**logins
= NULL
;
451 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
452 warn("%s", _PATH_UTMP
);
463 while ((u
= getutxent()) != NULL
) {
464 if (!strcmp(u
->ut_user
, user
) && u
->ut_type
!= DEAD_PROCESS
) {
466 lseek(fd
, 0, SEEK_SET
);
468 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
469 if (strcmp(u
.ut_name
, user
) == 0) {
473 if ((tmp
= realloc(logins
,
474 (login_count
+ 2) * sizeof(UTMP
*))) ==
483 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
491 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
492 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
493 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
494 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
495 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
496 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
497 logins
[login_count
]->ut_pid
= u
->ut_pid
;
499 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
500 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
501 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
502 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
503 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
504 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
505 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
506 logins
[login_count
]->ut_pid
= u
->ut_pid
;
509 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
510 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
511 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
512 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
513 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
514 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
515 logins
[login_count
]->ut_time
= u
.ut_time
;
517 logins
[++login_count
] = NULL
;
524 /* The 'mesg' status of the logged in user. */
525 static char *msgstat(UTMP
**u
, int multi
)
527 static char line
[LINE_MAX
];
532 for (i
= 0; i
< login_count
; i
++) {
533 char filename
[FILENAME_MAX
];
535 char m
[2] = { multi
, '\0' };
537 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
539 if (stat(filename
, &st
) == -1)
540 safe_strncat(line
, "!", sizeof(line
));
543 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
546 safe_strncat(line
, m
, sizeof(line
));
552 line
[strlen(line
) - 1] = '\0';
556 /* Returns the users idle time in seconds. */
557 static char *idle(UTMP
**u
, int multi
)
559 static char line
[LINE_MAX
];
566 for (i
= 0; i
< login_count
; i
++) {
567 char buf
[FILENAME_MAX
];
568 char m
[2] = { multi
, '\0' };
570 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
572 if (stat(buf
, &st
) == -1) {
573 safe_strncat(line
, "!", sizeof(line
));
574 safe_strncat(line
, m
, sizeof(line
));
579 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
581 if (u
[i
]->ut_time
> st
.st_atime
) {
583 safe_strncat(line
, "-", sizeof(line
));
584 safe_strncat(line
, m
, sizeof(line
));
591 if (t
< u
[i
]->ut_tv
.tv_sec
)
592 t
= u
[i
]->ut_tv
.tv_sec
;
594 if (t
< u
[i
]->ut_time
)
598 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
599 safe_strncat(line
, buf
, sizeof(line
));
600 safe_strncat(line
, m
, sizeof(line
));
606 line
[strlen(line
) - 1] = '\0';
610 /* This is output if the -h command line option is passed to the main program.
612 void ui_module_help()
614 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
615 printf("\t-y tty\t\t\t\t");
616 printf("-m message status\n");
617 printf("\t-t login time stamp\t\t");
618 printf("-d duration in minutes\n");
619 printf("\t-h hostname\t\t\t");
620 printf("-i seconds idle\n");
621 printf("\t-p login process id\n");
622 printf("\t-l lastlog information"
623 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
627 /* This is the equivalent to main() only without argc and argv available. */
628 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
629 const int verbose
, char *tf
)
636 u
= get_utmp(pw
->pw_name
);
640 char line
[LINE_MAX
] = { '\0' };
642 char m
[2] = { multi
, '\0' };
646 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
649 last_strings(lastlogin(pw
, tf
));
652 for (i
= 0; i
< login_count
; i
++) {
654 && isalnum((unsigned char) u
[i
]->ut_host
[0]))
655 safe_strncat(line
, u
[i
]->ut_host
, sizeof(line
));
657 safe_strncat(line
, "-", sizeof(line
));
659 safe_strncat(line
, m
, sizeof(line
));
663 strncpy(line
, "!", sizeof(line
));
665 line
[strlen(line
) - 1] = '\0';
667 add_string(&strings
, line
);
670 for (i
= 0; i
< login_count
; i
++) {
671 if (u
[i
]->ut_line
[0])
672 safe_strncat(line
, u
[i
]->ut_line
, sizeof(line
));
674 safe_strncat(line
, "!", sizeof(line
));
676 safe_strncat(line
, m
, sizeof(line
));
680 strncpy(line
, "!", sizeof(line
));
682 line
[strlen(line
) - 1] = '\0';
684 add_string(&strings
, line
);
687 add_string(&strings
, msgstat(u
, multi
));
690 for (i
= 0; i
< login_count
; i
++) {
692 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
694 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
696 safe_strncat(line
, m
, sizeof(line
));
700 strncpy(line
, "!", sizeof(line
));
702 line
[strlen(line
) - 1] = '\0';
704 add_string(&strings
, line
);
707 for (i
= 0; i
< login_count
; i
++) {
709 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
710 snprintf(buf
, sizeof(buf
), "%lu",
711 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
713 if ((now
- u
[i
]->ut_time
) > 60) {
714 snprintf(buf
, sizeof(buf
), "%lu",
715 ((now
- u
[i
]->ut_time
) / 60));
717 safe_strncat(line
, buf
, sizeof(line
));
720 safe_strncat(line
, "-", sizeof(line
));
722 safe_strncat(line
, m
, sizeof(line
));
726 strncpy(line
, "!", sizeof(line
));
728 line
[strlen(line
) - 1] = '\0';
730 add_string(&strings
, line
);
734 for (i
= 0; i
< login_count
; i
++) {
736 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
737 safe_strncat(line
, buf
, sizeof(line
));
740 safe_strncat(line
, "!", sizeof(line
));
742 safe_strncat(line
, m
, sizeof(line
));
746 strncpy(line
, "!", sizeof(line
));
748 line
[strlen(line
) - 1] = '\0';
750 add_string(&strings
, line
);
752 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
767 /* See if the last login options (-l) are valid. */
768 static int parse_last_options(const char *args
)
772 for (i
= 0; i
< strlen(args
); i
++) {
787 char *ui_module_options_init(char **defaults
)
790 return LOGIN_OPTION_STRING
;
793 /* Check module option validity. */
794 int ui_module_options(int argc
, char **argv
)
799 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
802 if (parse_last_options(optarg
))
805 last_options
= optarg
;
808 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
820 warnx("login: invalid option -- %c", optopt
);