2 Copyright (C) 2001-2013 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>
35 #ifdef _POSIX2_LINE_MAX
36 #define LINE_MAX _POSIX2_LINE_MAX
44 #include "../strsep.c"
53 #define LOGIN_OPTION_ORDER "pdimyhtl"
54 #define LOGIN_OPTION_STRING "Lpdimyhtl:"
56 static char options
[9]; /* NULL terminated. */
57 static char *last_options
;
58 static char **strings
;
60 static int login_count
;
61 #if (!defined(__FreeBSD_version) || __FreeBSD_version < 900000)
65 void add_string(char ***, const char *);
66 char *stamp(time_t, const char *);
67 char *safe_strncat(char *, const char *, size_t);
69 void ui_module_init(int *chainable
)
87 #if (!defined(__FreeBSD_version) || __FreeBSD_version < 900000)
93 static void free_logins(UTMP
**u
)
98 for (up
= u
; *up
; up
++)
106 /* This is for *BSD (login process id). */
108 static char *get_pid(uid_t uid
, int multi
)
111 static char line
[LINE_MAX
];
113 char errbuf
[LINE_MAX
];
114 struct kinfo_proc
*kp
;
124 #if defined(__NetBSD__) || defined (__OpenBSD__)
125 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
, O_RDONLY
, errbuf
)) == NULL
) {
127 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
128 O_RDONLY
, errbuf
)) == NULL
) {
136 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, sizeof(struct kinfo_proc
),
138 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
142 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
143 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
148 for (i
= 0; i
< cnt
; i
++) {
153 if ((kp
[i
].p_eflag
& EPROC_SLEADER
) && kp
[i
].p_tdev
!= -1)
156 #if __FreeBSD_version < 500000
157 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
159 pid
= kp
[i
].kp_eproc
.e_ppid
;
161 * pid = kp[i].kp_proc.p_pid;
165 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
170 if (!pid
|| pid
== 1)
173 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
174 safe_strncat(line
, buf
, sizeof(line
));
180 line
[strlen(line
) - 1] = '\0';
184 /* This is for Linux and Solaris. */
185 #elif defined(HAVE_PROCFS)
186 #include <sys/types.h>
187 #include <sys/stat.h>
198 static char *get_pid(uid_t uid
, int multi
)
203 static char line
[LINE_MAX
];
204 pid_t
*pids
= 0, *tpids
;
209 struct pstatus pstat
;
216 if (!procdir
&& firstrun
)
222 if ((procdir
= opendir("/proc")) == NULL
) {
231 while ((ent
= readdir(procdir
)) != NULL
) {
233 char filename
[FILENAME_MAX
];
241 if (!isdigit((unsigned char) *ent
->d_name
))
245 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
247 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
250 if (stat(filename
, &st
) == -1)
254 * The current user owns this file (process id).
256 if (st
.st_uid
== uid
) {
258 if ((fd
= open(filename
, O_RDONLY
)) == -1)
261 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
262 sizeof(struct pstatus
)) {
270 if ((fp
= fopen(filename
, "r")) == NULL
)
273 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
279 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
283 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
294 * Skip duplicate pids.
296 for (i
= 0; i
< pid_index
; i
++) {
301 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
302 safe_strncat(line
, buf
, sizeof(line
));
305 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
311 pids
[pid_index
++] = pid
;
321 line
[strlen(line
) - 1] = '\0';
325 /* Unsupported OS. */
326 static char *get_pid(uid_t uid
, int multi
)
333 /* Break up the last login string into sections and add the sections to the
334 * output string array if needed. */
335 static void last_strings(char *str
)
339 const char *line
, *host
, *when
;
341 line
= host
= when
= (str
) ? "-" : "!";
343 while ((buf
= strsep(&str
, ",")) != NULL
) {
362 for (i
= 0; i
< strlen(last_options
); i
++) {
363 switch (last_options
[i
]) {
365 add_string(&strings
, line
);
368 add_string(&strings
, host
);
371 add_string(&strings
, when
);
374 add_string(&strings
, line
);
375 add_string(&strings
, host
);
376 add_string(&strings
, when
);
383 /* Get the lastlog structure from the lastlog file. */
384 #if __FreeBSD_version >= 900000
385 static char *lastlogin(const struct passwd
*pw
, char *tf
)
388 static char buf
[LINE_MAX
];
390 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
395 last
= getutxuser(pw
->pw_name
);
400 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
401 !last
->ut_line
[0] ? "!" : last
->ut_line
,
402 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ? "-" : last
->ut_host
,
403 stamp(last
->ut_tv
.tv_sec
, tf
));
408 static char *lastlogin(const struct passwd
*pw
, char *tf
)
412 static char buf
[LINE_MAX
];
419 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
420 warn("%s", _PATH_LASTLOG
);
425 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
427 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
428 warn("%s", _PATH_LASTLOG
);
432 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
433 sizeof(struct lastlog
)) {
435 warn("%s", _PATH_LASTLOG
);
442 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
443 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
445 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
446 last
.ll_line
[UT_LINESIZE
-1] = '\0';
449 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
450 last
.ll_line
[UT_LINESIZE
-1] = '\0';
453 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
454 !last
.ll_line
[0] ? "!" : last
.ll_line
,
455 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
456 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
457 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
462 /* This will return an array of utmp structures if a user is logged in, NULL
463 * otherwise. We'll try to keep the utmp file descriptor open if possible to
464 * speed things up a bit. */
465 static UTMP
**get_utmp(const char *user
)
467 UTMP
**logins
= NULL
;
468 #if defined HAVE_UTMPX_H && defined (HAVE_SETUTXENT)
479 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
480 warn("%s", _PATH_UTMP
);
488 #if defined HAVE_UTMPX_H && defined (HAVE_SETUTXENT)
491 while ((u
= getutxent()) != NULL
) {
492 if (!strcmp(u
->ut_user
, user
) && u
->ut_type
!= DEAD_PROCESS
) {
494 lseek(fd
, 0, SEEK_SET
);
496 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
497 if (strcmp(u
.ut_name
, user
) == 0) {
501 if ((tmp
= realloc(logins
,
502 (login_count
+ 2) * sizeof(UTMP
*))) ==
511 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
517 #if defined HAVE_UTMPX_H && defined (HAVE_SETUTXENT)
519 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
520 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
521 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
522 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
523 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
524 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
525 logins
[login_count
]->ut_pid
= u
->ut_pid
;
527 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
528 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
529 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
530 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
531 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
532 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
533 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
534 logins
[login_count
]->ut_pid
= u
->ut_pid
;
537 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
538 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
539 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
540 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
541 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
542 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
543 logins
[login_count
]->ut_time
= u
.ut_time
;
545 logins
[++login_count
] = NULL
;
552 /* The 'mesg' status of the logged in user. */
553 static char *msgstat(UTMP
**u
, int multi
)
555 static char line
[LINE_MAX
];
560 for (i
= 0; i
< login_count
; i
++) {
561 char filename
[FILENAME_MAX
];
563 char m
[2] = { multi
, '\0' };
565 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
567 if (stat(filename
, &st
) == -1)
568 safe_strncat(line
, "!", sizeof(line
));
571 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
574 safe_strncat(line
, m
, sizeof(line
));
580 line
[strlen(line
) - 1] = '\0';
584 /* Returns the users idle time in seconds. */
585 static char *idle(UTMP
**u
, int multi
)
587 static char line
[LINE_MAX
];
594 for (i
= 0; i
< login_count
; i
++) {
595 char buf
[FILENAME_MAX
];
596 char m
[2] = { multi
, '\0' };
598 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
600 if (stat(buf
, &st
) == -1) {
601 safe_strncat(line
, "!", sizeof(line
));
602 safe_strncat(line
, m
, sizeof(line
));
607 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
609 if (u
[i
]->ut_time
> st
.st_atime
) {
611 safe_strncat(line
, "-", sizeof(line
));
612 safe_strncat(line
, m
, sizeof(line
));
619 if (t
< u
[i
]->ut_tv
.tv_sec
)
620 t
= u
[i
]->ut_tv
.tv_sec
;
622 if (t
< u
[i
]->ut_time
)
626 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
627 safe_strncat(line
, buf
, sizeof(line
));
628 safe_strncat(line
, m
, sizeof(line
));
634 line
[strlen(line
) - 1] = '\0';
638 /* This is output if the -h command line option is passed to the main program.
640 void ui_module_help()
642 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
643 printf("\t-y tty\t\t\t\t");
644 printf("-m message status\n");
645 printf("\t-t login time stamp\t\t");
646 printf("-d duration in minutes\n");
647 printf("\t-h hostname\t\t\t");
648 printf("-i seconds idle\n");
649 printf("\t-p login process id\n");
650 printf("\t-l lastlog information"
651 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
655 /* This is the equivalent to main() only without argc and argv available. */
656 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
657 const int verbose
, char *tf
)
664 u
= get_utmp(pw
->pw_name
);
668 char line
[LINE_MAX
] = { '\0' };
670 char m
[2] = { multi
, '\0' };
674 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
677 last_strings(lastlogin(pw
, tf
));
680 for (i
= 0; i
< login_count
; i
++) {
682 && isalnum((unsigned char) u
[i
]->ut_host
[0]))
683 safe_strncat(line
, u
[i
]->ut_host
, sizeof(line
));
685 safe_strncat(line
, "-", sizeof(line
));
687 safe_strncat(line
, m
, sizeof(line
));
691 strncpy(line
, "!", sizeof(line
));
693 line
[strlen(line
) - 1] = '\0';
695 add_string(&strings
, line
);
698 for (i
= 0; i
< login_count
; i
++) {
699 if (u
[i
]->ut_line
[0])
700 safe_strncat(line
, u
[i
]->ut_line
, sizeof(line
));
702 safe_strncat(line
, "!", sizeof(line
));
704 safe_strncat(line
, m
, sizeof(line
));
708 strncpy(line
, "!", sizeof(line
));
710 line
[strlen(line
) - 1] = '\0';
712 add_string(&strings
, line
);
715 add_string(&strings
, msgstat(u
, multi
));
718 for (i
= 0; i
< login_count
; i
++) {
720 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
722 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
724 safe_strncat(line
, m
, sizeof(line
));
728 strncpy(line
, "!", sizeof(line
));
730 line
[strlen(line
) - 1] = '\0';
732 add_string(&strings
, line
);
735 for (i
= 0; i
< login_count
; i
++) {
737 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
738 snprintf(buf
, sizeof(buf
), "%lu",
739 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
741 if ((now
- u
[i
]->ut_time
) > 60) {
742 snprintf(buf
, sizeof(buf
), "%lu",
743 ((now
- u
[i
]->ut_time
) / 60));
745 safe_strncat(line
, buf
, sizeof(line
));
748 safe_strncat(line
, "-", sizeof(line
));
750 safe_strncat(line
, m
, sizeof(line
));
754 strncpy(line
, "!", sizeof(line
));
756 line
[strlen(line
) - 1] = '\0';
758 add_string(&strings
, line
);
762 for (i
= 0; i
< login_count
; i
++) {
764 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
765 safe_strncat(line
, buf
, sizeof(line
));
768 safe_strncat(line
, "!", sizeof(line
));
770 safe_strncat(line
, m
, sizeof(line
));
774 strncpy(line
, "!", sizeof(line
));
776 line
[strlen(line
) - 1] = '\0';
778 add_string(&strings
, line
);
780 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
795 /* See if the last login options (-l) are valid. */
796 static int parse_last_options(const char *args
)
800 for (i
= 0; i
< strlen(args
); i
++) {
815 char *ui_module_options_init(char **defaults
)
818 return LOGIN_OPTION_STRING
;
821 /* Check module option validity. */
822 int ui_module_options(int argc
, char **argv
)
827 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
830 if (parse_last_options(optarg
))
833 last_options
= optarg
;
836 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
848 warnx("login: invalid option -- %c", optopt
);