2 Copyright (C) 2001-2006 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 02111-1307 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
)
79 /* This is for *BSD (login process id). */
81 static char *get_pid(uid_t uid
, int multi
)
84 static char line
[LINE_MAX
];
87 char errbuf
[LINE_MAX
];
88 struct kinfo_proc
*kp
;
99 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
,
101 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
103 O_RDONLY
, errbuf
)) == NULL
) {
109 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
110 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
114 for (i
= 0; i
< cnt
; i
++) {
117 #if __FreeBSD_version < 500000
118 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
!=
120 pid
= kp
[i
].kp_eproc
.e_ppid
;
122 * pid = kp[i].kp_proc.p_pid;
128 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
131 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
132 safe_strncat(line
, buf
, sizeof(line
));
139 line
[strlen(line
) - 1] = '\0';
143 /* This is for Linux and Solaris. */
144 #elif defined(HAVE_PROCFS)
145 #include <sys/types.h>
146 #include <sys/stat.h>
157 static char *get_pid(uid_t uid
, int multi
)
162 static char line
[LINE_MAX
];
168 struct pstatus pstat
;
175 if (!procdir
&& firstrun
)
181 if ((procdir
= opendir("/proc")) == NULL
) {
190 while ((ent
= readdir(procdir
)) != NULL
) {
192 char filename
[FILENAME_MAX
];
200 if (!isdigit((unsigned char) *ent
->d_name
))
204 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
206 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
209 if (stat(filename
, &st
) == -1)
213 * The current user owns this file (process id).
215 if (st
.st_uid
== uid
) {
217 if ((fd
= open(filename
, O_RDONLY
)) == -1)
220 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
221 sizeof(struct pstatus
)) {
229 if ((fp
= fopen(filename
, "r")) == NULL
)
232 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
238 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
242 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
253 * Skip duplicate pids.
255 for (i
= 0; i
< pid_index
; i
++) {
260 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
261 safe_strncat(line
, buf
, sizeof(line
));
264 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
269 pids
[pid_index
++] = pid
;
279 line
[strlen(line
) - 1] = '\0';
283 /* Unsupported OS. */
284 static char *get_pid(uid_t uid
, int multi
)
291 /* Break up the last login string into sections and add the sections to the
292 * output string array if needed. */
293 static void last_strings(char *str
)
297 const char *line
, *host
, *when
;
299 line
= host
= when
= (str
) ? "-" : "!";
301 while ((buf
= strsep(&str
, ",")) != NULL
) {
320 for (i
= 0; i
< strlen(last_options
); i
++) {
321 switch (last_options
[i
]) {
323 add_string(&strings
, line
);
326 add_string(&strings
, host
);
329 add_string(&strings
, when
);
332 add_string(&strings
, line
);
333 add_string(&strings
, host
);
334 add_string(&strings
, when
);
341 /* Get the lastlog structure from the lastlog file. */
342 #if (defined(__FreeBSD_version) && __FreeBSD_version >= 900000)
343 static char *lastlogin(const struct passwd
*pw
, char *tf
)
346 static char buf
[LINE_MAX
];
348 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
353 last
= getutxuser(pw
->pw_name
);
358 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
359 !last
->ut_line
[0] ? "!" : last
->ut_line
,
360 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ?
361 !isdigit(last
->ut_line
[3]) ? "!" : "-" : last
->ut_host
,
362 !last
->ut_time
[0] ? "!" : stamp(last
->ut_time
, tf
));
367 static char *lastlogin(const struct passwd
*pw
, char *tf
)
371 static char buf
[LINE_MAX
];
378 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
379 warn("%s", _PATH_LASTLOG
);
384 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
386 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
387 warn("%s", _PATH_LASTLOG
);
391 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
392 sizeof(struct lastlog
)) {
394 warn("%s", _PATH_LASTLOG
);
401 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
402 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
404 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
405 last
.ll_line
[UT_LINESIZE
-1] = '\0';
408 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
409 last
.ll_line
[UT_LINESIZE
-1] = '\0';
412 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
413 !last
.ll_line
[0] ? "!" : last
.ll_line
,
414 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
415 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
416 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
421 /* This will return an array of utmp structures if a user is logged in, NULL
422 * otherwise. We'll try to keep the utmp file descriptor open if possible to
423 * speed things up a bit. */
424 static UTMP
**get_utmp(const char *user
)
426 UTMP
**logins
= NULL
;
438 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
439 warn("%s", _PATH_UTMP
);
450 while ((u
= getutxent()) != NULL
) {
451 if (!strcmp(u
->ut_user
, user
)) {
453 lseek(fd
, 0, SEEK_SET
);
455 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
456 if (strcmp(u
.ut_name
, user
) == 0) {
458 if ((logins
= realloc(logins
,
459 (login_count
+ 2) * sizeof(UTMP
*))) ==
465 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
472 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
473 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
474 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
475 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
476 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
477 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
478 logins
[login_count
]->ut_pid
= u
->ut_pid
;
480 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
481 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
482 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
483 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
484 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
485 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
486 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
487 logins
[login_count
]->ut_pid
= u
->ut_pid
;
490 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
491 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
492 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
493 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
494 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
495 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
496 logins
[login_count
]->ut_time
= u
.ut_time
;
498 logins
[++login_count
] = NULL
;
505 /* The 'mesg' status of the logged in user. */
506 static char *msgstat(UTMP
**u
, int multi
)
508 static char line
[LINE_MAX
];
513 for (i
= 0; i
< login_count
; i
++) {
514 char filename
[FILENAME_MAX
];
516 char m
[2] = { multi
, '\0' };
518 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
520 if (stat(filename
, &st
) == -1)
521 safe_strncat(line
, "!", sizeof(line
));
524 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
527 safe_strncat(line
, m
, sizeof(line
));
533 line
[strlen(line
) - 1] = '\0';
537 /* Returns the users idle time in seconds. */
538 static char *idle(UTMP
**u
, int multi
)
540 static char line
[LINE_MAX
];
547 for (i
= 0; i
< login_count
; i
++) {
548 char buf
[FILENAME_MAX
];
549 char m
[2] = { multi
, '\0' };
551 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
553 if (stat(buf
, &st
) == -1) {
554 safe_strncat(line
, "!", sizeof(line
));
555 safe_strncat(line
, m
, sizeof(line
));
560 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
562 if (u
[i
]->ut_time
> st
.st_atime
) {
564 safe_strncat(line
, "-", sizeof(line
));
565 safe_strncat(line
, m
, sizeof(line
));
572 if (t
< u
[i
]->ut_tv
.tv_sec
)
573 t
= u
[i
]->ut_tv
.tv_sec
;
575 if (t
< u
[i
]->ut_time
)
579 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
580 safe_strncat(line
, buf
, sizeof(line
));
581 safe_strncat(line
, m
, sizeof(line
));
587 line
[strlen(line
) - 1] = '\0';
591 /* This is output if the -h command line option is passed to the main program.
593 void ui_module_help()
595 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
596 printf("\t-y tty\t\t\t\t");
597 printf("-m message status\n");
598 printf("\t-t login time stamp\t\t");
599 printf("-d duration in minutes\n");
600 printf("\t-h hostname\t\t\t");
601 printf("-i seconds idle\n");
602 printf("\t-p login process id\n");
603 printf("\t-l lastlog information"
604 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
608 /* This is the equivalent to main() only without argc and argv available. */
609 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
610 const int verbose
, char *tf
)
613 UTMP
**u
= NULL
, **up
;
617 u
= get_utmp(pw
->pw_name
);
621 char line
[LINE_MAX
] = { '\0' };
623 char m
[2] = { multi
, '\0' };
627 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
630 last_strings(lastlogin(pw
, tf
));
633 for (i
= 0; i
< login_count
; i
++) {
635 && isalnum((unsigned char) u
[i
]->ut_host
[0])) {
636 safe_strncat(line
, u
[i
]->ut_host
, sizeof(line
));
640 * If a users tty is tty1-n, it must be a console
644 && isdigit((unsigned char) u
[i
]->ut_line
[3]))
645 safe_strncat(line
, "-", sizeof(line
));
647 safe_strncat(line
, "!", sizeof(line
));
650 safe_strncat(line
, m
, sizeof(line
));
654 strncpy(line
, "!", sizeof(line
));
656 line
[strlen(line
) - 1] = '\0';
658 add_string(&strings
, line
);
661 for (i
= 0; i
< login_count
; i
++) {
662 if (u
[i
]->ut_line
[0])
663 safe_strncat(line
, u
[i
]->ut_line
, sizeof(line
));
665 safe_strncat(line
, "!", sizeof(line
));
667 safe_strncat(line
, m
, sizeof(line
));
671 strncpy(line
, "!", sizeof(line
));
673 line
[strlen(line
) - 1] = '\0';
675 add_string(&strings
, line
);
678 add_string(&strings
, msgstat(u
, multi
));
681 for (i
= 0; i
< login_count
; i
++) {
683 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
685 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), 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
++) {
700 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
701 snprintf(buf
, sizeof(buf
), "%lu",
702 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
704 if ((now
- u
[i
]->ut_time
) > 60) {
705 snprintf(buf
, sizeof(buf
), "%lu",
706 ((now
- u
[i
]->ut_time
) / 60));
708 safe_strncat(line
, buf
, sizeof(line
));
711 safe_strncat(line
, "-", sizeof(line
));
713 safe_strncat(line
, m
, sizeof(line
));
717 strncpy(line
, "!", sizeof(line
));
719 line
[strlen(line
) - 1] = '\0';
721 add_string(&strings
, line
);
725 for (i
= 0; i
< login_count
; i
++) {
727 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
728 safe_strncat(line
, buf
, sizeof(line
));
731 safe_strncat(line
, "!", sizeof(line
));
733 safe_strncat(line
, m
, sizeof(line
));
737 strncpy(line
, "!", sizeof(line
));
739 line
[strlen(line
) - 1] = '\0';
741 add_string(&strings
, line
);
743 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
754 for (up
= u
; *up
; up
++)
764 /* See if the last login options (-l) are valid. */
765 static int parse_last_options(const char *args
)
769 for (i
= 0; i
< strlen(args
); i
++) {
784 char *ui_module_options_init(char **defaults
)
787 return LOGIN_OPTION_STRING
;
790 /* Check module option validity. */
791 int ui_module_options(int argc
, char **argv
)
796 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
799 if (parse_last_options(optarg
))
802 last_options
= optarg
;
805 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
817 warnx("login: invalid option -- %c", optopt
);