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 #ifdef HAVE_GETLASTLOGX
385 static char *lastlogin(const struct passwd
*pw
, char *tf
)
387 struct lastlogx
*last
= getlastlogx(_PATH_LASTLOGX
, pw
->pw_uid
, NULL
);
388 static char buf
[LINE_MAX
];
393 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
394 !last
->ll_line
[0] ? "!" : last
->ll_line
,
395 (!last
->ll_host
[0] || !isalnum(last
->ll_host
[0])) ? "-" : last
->ll_host
,
396 stamp(last
->ll_tv
.tv_sec
, tf
));
400 #if __FreeBSD_version >= 900000
401 static char *lastlogin(const struct passwd
*pw
, char *tf
)
404 static char buf
[LINE_MAX
];
406 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
411 last
= getutxuser(pw
->pw_name
);
416 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
417 !last
->ut_line
[0] ? "!" : last
->ut_line
,
418 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ? "-" : last
->ut_host
,
419 stamp(last
->ut_tv
.tv_sec
, tf
));
424 static char *lastlogin(const struct passwd
*pw
, char *tf
)
428 static char buf
[LINE_MAX
];
435 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
436 warn("%s", _PATH_LASTLOG
);
441 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
443 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
444 warn("%s", _PATH_LASTLOG
);
448 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
449 sizeof(struct lastlog
)) {
451 warn("%s", _PATH_LASTLOG
);
458 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
459 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
461 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
462 last
.ll_line
[UT_LINESIZE
-1] = '\0';
465 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
466 last
.ll_line
[UT_LINESIZE
-1] = '\0';
469 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
470 !last
.ll_line
[0] ? "!" : last
.ll_line
,
471 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
472 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
473 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
479 /* This will return an array of utmp structures if a user is logged in, NULL
480 * otherwise. We'll try to keep the utmp file descriptor open if possible to
481 * speed things up a bit. */
482 static UTMP
**get_utmp(const char *user
)
484 UTMP
**logins
= NULL
;
485 #if defined HAVE_UTMPX_H && defined (HAVE_SETUTXENT)
496 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
497 warn("%s", _PATH_UTMP
);
505 #if defined HAVE_UTMPX_H && defined (HAVE_SETUTXENT)
508 while ((u
= getutxent()) != NULL
) {
509 if (!strcmp(u
->ut_user
, user
) && u
->ut_type
!= DEAD_PROCESS
) {
511 lseek(fd
, 0, SEEK_SET
);
513 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
514 if (strcmp(u
.ut_name
, user
) == 0) {
518 if ((tmp
= realloc(logins
,
519 (login_count
+ 2) * sizeof(UTMP
*))) ==
528 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
534 #if defined HAVE_UTMPX_H && defined (HAVE_SETUTXENT)
536 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
537 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
538 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
539 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
540 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
541 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
542 logins
[login_count
]->ut_pid
= u
->ut_pid
;
544 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
545 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
546 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
547 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
548 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
549 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
550 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
551 logins
[login_count
]->ut_pid
= u
->ut_pid
;
554 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
555 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
556 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
557 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
558 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
559 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
560 logins
[login_count
]->ut_time
= u
.ut_time
;
562 logins
[++login_count
] = NULL
;
569 /* The 'mesg' status of the logged in user. */
570 static char *msgstat(UTMP
**u
, int multi
)
572 static char line
[LINE_MAX
];
577 for (i
= 0; i
< login_count
; i
++) {
578 char filename
[FILENAME_MAX
];
580 char m
[2] = { multi
, '\0' };
582 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
584 if (stat(filename
, &st
) == -1)
585 safe_strncat(line
, "!", sizeof(line
));
588 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
591 safe_strncat(line
, m
, sizeof(line
));
597 line
[strlen(line
) - 1] = '\0';
601 /* Returns the users idle time in seconds. */
602 static char *idle(UTMP
**u
, int multi
)
604 static char line
[LINE_MAX
];
611 for (i
= 0; i
< login_count
; i
++) {
612 char buf
[FILENAME_MAX
];
613 char m
[2] = { multi
, '\0' };
615 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
617 if (stat(buf
, &st
) == -1) {
618 safe_strncat(line
, "!", sizeof(line
));
619 safe_strncat(line
, m
, sizeof(line
));
624 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
626 if (u
[i
]->ut_time
> st
.st_atime
) {
628 safe_strncat(line
, "-", sizeof(line
));
629 safe_strncat(line
, m
, sizeof(line
));
636 if (t
< u
[i
]->ut_tv
.tv_sec
)
637 t
= u
[i
]->ut_tv
.tv_sec
;
639 if (t
< u
[i
]->ut_time
)
643 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
644 safe_strncat(line
, buf
, sizeof(line
));
645 safe_strncat(line
, m
, sizeof(line
));
651 line
[strlen(line
) - 1] = '\0';
655 /* This is output if the -h command line option is passed to the main program.
657 void ui_module_help()
659 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
660 printf("\t-y tty\t\t\t\t");
661 printf("-m message status\n");
662 printf("\t-t login time stamp\t\t");
663 printf("-d duration in minutes\n");
664 printf("\t-h hostname\t\t\t");
665 printf("-i seconds idle\n");
666 printf("\t-p login process id\n");
667 printf("\t-l lastlog information"
668 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
672 /* This is the equivalent to main() only without argc and argv available. */
673 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
674 const int verbose
, char *tf
)
681 u
= get_utmp(pw
->pw_name
);
685 char line
[LINE_MAX
] = { '\0' };
687 char m
[2] = { multi
, '\0' };
691 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
694 last_strings(lastlogin(pw
, tf
));
697 for (i
= 0; i
< login_count
; i
++) {
699 && isalnum((unsigned char) u
[i
]->ut_host
[0]))
700 safe_strncat(line
, u
[i
]->ut_host
, 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 for (i
= 0; i
< login_count
; i
++) {
716 if (u
[i
]->ut_line
[0])
717 safe_strncat(line
, u
[i
]->ut_line
, sizeof(line
));
719 safe_strncat(line
, "!", sizeof(line
));
721 safe_strncat(line
, m
, sizeof(line
));
725 strncpy(line
, "!", sizeof(line
));
727 line
[strlen(line
) - 1] = '\0';
729 add_string(&strings
, line
);
732 add_string(&strings
, msgstat(u
, multi
));
735 for (i
= 0; i
< login_count
; i
++) {
737 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
739 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
741 safe_strncat(line
, m
, sizeof(line
));
745 strncpy(line
, "!", sizeof(line
));
747 line
[strlen(line
) - 1] = '\0';
749 add_string(&strings
, line
);
752 for (i
= 0; i
< login_count
; i
++) {
754 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
755 snprintf(buf
, sizeof(buf
), "%lu",
756 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
758 if ((now
- u
[i
]->ut_time
) > 60) {
759 snprintf(buf
, sizeof(buf
), "%lu",
760 ((now
- u
[i
]->ut_time
) / 60));
762 safe_strncat(line
, buf
, sizeof(line
));
765 safe_strncat(line
, "-", sizeof(line
));
767 safe_strncat(line
, m
, sizeof(line
));
771 strncpy(line
, "!", sizeof(line
));
773 line
[strlen(line
) - 1] = '\0';
775 add_string(&strings
, line
);
779 for (i
= 0; i
< login_count
; i
++) {
781 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
782 safe_strncat(line
, buf
, sizeof(line
));
785 safe_strncat(line
, "!", sizeof(line
));
787 safe_strncat(line
, m
, sizeof(line
));
791 strncpy(line
, "!", sizeof(line
));
793 line
[strlen(line
) - 1] = '\0';
795 add_string(&strings
, line
);
797 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
812 /* See if the last login options (-l) are valid. */
813 static int parse_last_options(const char *args
)
817 for (i
= 0; i
< strlen(args
); i
++) {
832 char *ui_module_options_init(char **defaults
)
835 return LOGIN_OPTION_STRING
;
838 /* Check module option validity. */
839 int ui_module_options(int argc
, char **argv
)
844 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
847 if (parse_last_options(optarg
))
850 last_options
= optarg
;
853 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
865 warnx("login: invalid option -- %c", optopt
);