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"
42 #define LOGIN_OPTION_ORDER "pdimyhtl"
43 #define LOGIN_OPTION_STRING "Lpdimyhtl:"
45 static char options
[9]; /* NULL terminated. */
46 static char *last_options
;
47 static char **strings
;
49 static int login_count
;
50 #if (!defined(__FreeBSD_version) || __FreeBSD_version < 900000)
54 void add_string(char ***, const char *);
55 char *stamp(time_t, const char *);
56 char *safe_strncat(char *, const char *, size_t);
58 void ui_module_init(int *chainable
)
76 #if (!defined(__FreeBSD_version) || __FreeBSD_version < 900000)
82 static void free_logins(UTMP
**u
)
87 for (up
= u
; *up
; up
++)
95 /* This is for *BSD (login process id). */
97 static char *get_pid(uid_t uid
, int multi
)
100 static char line
[LINE_MAX
];
103 char errbuf
[LINE_MAX
];
104 struct kinfo_proc
*kp
;
115 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
,
116 O_RDONLY
, errbuf
)) == NULL
) {
118 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
119 O_RDONLY
, errbuf
)) == NULL
) {
126 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
127 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
131 for (i
= 0; i
< cnt
; i
++) {
134 #if __FreeBSD_version < 500000
135 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
!=
137 pid
= kp
[i
].kp_eproc
.e_ppid
;
139 * pid = kp[i].kp_proc.p_pid;
145 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
148 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
149 safe_strncat(line
, buf
, sizeof(line
));
156 line
[strlen(line
) - 1] = '\0';
160 /* This is for Linux and Solaris. */
161 #elif defined(HAVE_PROCFS)
162 #include <sys/types.h>
163 #include <sys/stat.h>
174 static char *get_pid(uid_t uid
, int multi
)
179 static char line
[LINE_MAX
];
180 pid_t
*pids
= 0, *tpids
;
185 struct pstatus pstat
;
192 if (!procdir
&& firstrun
)
198 if ((procdir
= opendir("/proc")) == NULL
) {
207 while ((ent
= readdir(procdir
)) != NULL
) {
209 char filename
[FILENAME_MAX
];
217 if (!isdigit((unsigned char) *ent
->d_name
))
221 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
223 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
226 if (stat(filename
, &st
) == -1)
230 * The current user owns this file (process id).
232 if (st
.st_uid
== uid
) {
234 if ((fd
= open(filename
, O_RDONLY
)) == -1)
237 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
238 sizeof(struct pstatus
)) {
246 if ((fp
= fopen(filename
, "r")) == NULL
)
249 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
255 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
259 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
270 * Skip duplicate pids.
272 for (i
= 0; i
< pid_index
; i
++) {
277 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
278 safe_strncat(line
, buf
, sizeof(line
));
281 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
287 pids
[pid_index
++] = pid
;
297 line
[strlen(line
) - 1] = '\0';
301 /* Unsupported OS. */
302 static char *get_pid(uid_t uid
, int multi
)
309 /* Break up the last login string into sections and add the sections to the
310 * output string array if needed. */
311 static void last_strings(char *str
)
315 const char *line
, *host
, *when
;
317 line
= host
= when
= (str
) ? "-" : "!";
319 while ((buf
= strsep(&str
, ",")) != NULL
) {
338 for (i
= 0; i
< strlen(last_options
); i
++) {
339 switch (last_options
[i
]) {
341 add_string(&strings
, line
);
344 add_string(&strings
, host
);
347 add_string(&strings
, when
);
350 add_string(&strings
, line
);
351 add_string(&strings
, host
);
352 add_string(&strings
, when
);
359 /* Get the lastlog structure from the lastlog file. */
360 #if __FreeBSD_version >= 900000
361 static char *lastlogin(const struct passwd
*pw
, char *tf
)
364 static char buf
[LINE_MAX
];
366 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
371 last
= getutxuser(pw
->pw_name
);
376 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
377 !last
->ut_line
[0] ? "!" : last
->ut_line
,
378 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ? "-" : last
->ut_host
,
379 stamp(last
->ut_tv
.tv_sec
, tf
));
384 static char *lastlogin(const struct passwd
*pw
, char *tf
)
388 static char buf
[LINE_MAX
];
395 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
396 warn("%s", _PATH_LASTLOG
);
401 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
403 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
404 warn("%s", _PATH_LASTLOG
);
408 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
409 sizeof(struct lastlog
)) {
411 warn("%s", _PATH_LASTLOG
);
418 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
419 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
421 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
422 last
.ll_line
[UT_LINESIZE
-1] = '\0';
425 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
426 last
.ll_line
[UT_LINESIZE
-1] = '\0';
429 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
430 !last
.ll_line
[0] ? "!" : last
.ll_line
,
431 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
432 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
433 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
438 /* This will return an array of utmp structures if a user is logged in, NULL
439 * otherwise. We'll try to keep the utmp file descriptor open if possible to
440 * speed things up a bit. */
441 static UTMP
**get_utmp(const char *user
)
443 UTMP
**logins
= NULL
;
455 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
456 warn("%s", _PATH_UTMP
);
467 while ((u
= getutxent()) != NULL
) {
468 if (!strcmp(u
->ut_user
, user
) && u
->ut_type
!= DEAD_PROCESS
) {
470 lseek(fd
, 0, SEEK_SET
);
472 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
473 if (strcmp(u
.ut_name
, user
) == 0) {
477 if ((tmp
= realloc(logins
,
478 (login_count
+ 2) * sizeof(UTMP
*))) ==
487 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
495 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
496 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
497 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
498 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
499 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
500 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
501 logins
[login_count
]->ut_pid
= u
->ut_pid
;
503 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
504 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
505 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
506 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
507 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
508 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
509 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
510 logins
[login_count
]->ut_pid
= u
->ut_pid
;
513 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
514 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
515 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
516 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
517 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
518 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
519 logins
[login_count
]->ut_time
= u
.ut_time
;
521 logins
[++login_count
] = NULL
;
528 /* The 'mesg' status of the logged in user. */
529 static char *msgstat(UTMP
**u
, int multi
)
531 static char line
[LINE_MAX
];
536 for (i
= 0; i
< login_count
; i
++) {
537 char filename
[FILENAME_MAX
];
539 char m
[2] = { multi
, '\0' };
541 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
543 if (stat(filename
, &st
) == -1)
544 safe_strncat(line
, "!", sizeof(line
));
547 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
550 safe_strncat(line
, m
, sizeof(line
));
556 line
[strlen(line
) - 1] = '\0';
560 /* Returns the users idle time in seconds. */
561 static char *idle(UTMP
**u
, int multi
)
563 static char line
[LINE_MAX
];
570 for (i
= 0; i
< login_count
; i
++) {
571 char buf
[FILENAME_MAX
];
572 char m
[2] = { multi
, '\0' };
574 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
576 if (stat(buf
, &st
) == -1) {
577 safe_strncat(line
, "!", sizeof(line
));
578 safe_strncat(line
, m
, sizeof(line
));
583 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
585 if (u
[i
]->ut_time
> st
.st_atime
) {
587 safe_strncat(line
, "-", sizeof(line
));
588 safe_strncat(line
, m
, sizeof(line
));
595 if (t
< u
[i
]->ut_tv
.tv_sec
)
596 t
= u
[i
]->ut_tv
.tv_sec
;
598 if (t
< u
[i
]->ut_time
)
602 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
603 safe_strncat(line
, buf
, sizeof(line
));
604 safe_strncat(line
, m
, sizeof(line
));
610 line
[strlen(line
) - 1] = '\0';
614 /* This is output if the -h command line option is passed to the main program.
616 void ui_module_help()
618 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
619 printf("\t-y tty\t\t\t\t");
620 printf("-m message status\n");
621 printf("\t-t login time stamp\t\t");
622 printf("-d duration in minutes\n");
623 printf("\t-h hostname\t\t\t");
624 printf("-i seconds idle\n");
625 printf("\t-p login process id\n");
626 printf("\t-l lastlog information"
627 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
631 /* This is the equivalent to main() only without argc and argv available. */
632 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
633 const int verbose
, char *tf
)
640 u
= get_utmp(pw
->pw_name
);
644 char line
[LINE_MAX
] = { '\0' };
646 char m
[2] = { multi
, '\0' };
650 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
653 last_strings(lastlogin(pw
, tf
));
656 for (i
= 0; i
< login_count
; i
++) {
658 && isalnum((unsigned char) u
[i
]->ut_host
[0]))
659 safe_strncat(line
, u
[i
]->ut_host
, sizeof(line
));
661 safe_strncat(line
, "-", sizeof(line
));
663 safe_strncat(line
, m
, sizeof(line
));
667 strncpy(line
, "!", sizeof(line
));
669 line
[strlen(line
) - 1] = '\0';
671 add_string(&strings
, line
);
674 for (i
= 0; i
< login_count
; i
++) {
675 if (u
[i
]->ut_line
[0])
676 safe_strncat(line
, u
[i
]->ut_line
, sizeof(line
));
678 safe_strncat(line
, "!", sizeof(line
));
680 safe_strncat(line
, m
, sizeof(line
));
684 strncpy(line
, "!", sizeof(line
));
686 line
[strlen(line
) - 1] = '\0';
688 add_string(&strings
, line
);
691 add_string(&strings
, msgstat(u
, multi
));
694 for (i
= 0; i
< login_count
; i
++) {
696 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
698 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
700 safe_strncat(line
, m
, sizeof(line
));
704 strncpy(line
, "!", sizeof(line
));
706 line
[strlen(line
) - 1] = '\0';
708 add_string(&strings
, line
);
711 for (i
= 0; i
< login_count
; i
++) {
713 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
714 snprintf(buf
, sizeof(buf
), "%lu",
715 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
717 if ((now
- u
[i
]->ut_time
) > 60) {
718 snprintf(buf
, sizeof(buf
), "%lu",
719 ((now
- u
[i
]->ut_time
) / 60));
721 safe_strncat(line
, buf
, sizeof(line
));
724 safe_strncat(line
, "-", sizeof(line
));
726 safe_strncat(line
, m
, sizeof(line
));
730 strncpy(line
, "!", sizeof(line
));
732 line
[strlen(line
) - 1] = '\0';
734 add_string(&strings
, line
);
738 for (i
= 0; i
< login_count
; i
++) {
740 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
741 safe_strncat(line
, buf
, sizeof(line
));
744 safe_strncat(line
, "!", sizeof(line
));
746 safe_strncat(line
, m
, sizeof(line
));
750 strncpy(line
, "!", sizeof(line
));
752 line
[strlen(line
) - 1] = '\0';
754 add_string(&strings
, line
);
756 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
771 /* See if the last login options (-l) are valid. */
772 static int parse_last_options(const char *args
)
776 for (i
= 0; i
< strlen(args
); i
++) {
791 char *ui_module_options_init(char **defaults
)
794 return LOGIN_OPTION_STRING
;
797 /* Check module option validity. */
798 int ui_module_options(int argc
, char **argv
)
803 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
806 if (parse_last_options(optarg
))
809 last_options
= optarg
;
812 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
824 warnx("login: invalid option -- %c", optopt
);