77964ca22409b06b0e2457db9c496cf8436c3bb1
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>
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
];
114 char errbuf
[LINE_MAX
];
115 struct kinfo_proc
*kp
;
126 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
,
127 O_RDONLY
, errbuf
)) == NULL
) {
129 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
130 O_RDONLY
, errbuf
)) == NULL
) {
137 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
138 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
142 for (i
= 0; i
< cnt
; i
++) {
145 #if __FreeBSD_version < 500000
146 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
!=
148 pid
= kp
[i
].kp_eproc
.e_ppid
;
150 * pid = kp[i].kp_proc.p_pid;
156 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
159 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
160 safe_strncat(line
, buf
, sizeof(line
));
167 line
[strlen(line
) - 1] = '\0';
171 /* This is for Linux and Solaris. */
172 #elif defined(HAVE_PROCFS)
173 #include <sys/types.h>
174 #include <sys/stat.h>
185 static char *get_pid(uid_t uid
, int multi
)
190 static char line
[LINE_MAX
];
191 pid_t
*pids
= 0, *tpids
;
196 struct pstatus pstat
;
203 if (!procdir
&& firstrun
)
209 if ((procdir
= opendir("/proc")) == NULL
) {
218 while ((ent
= readdir(procdir
)) != NULL
) {
220 char filename
[FILENAME_MAX
];
228 if (!isdigit((unsigned char) *ent
->d_name
))
232 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
234 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
237 if (stat(filename
, &st
) == -1)
241 * The current user owns this file (process id).
243 if (st
.st_uid
== uid
) {
245 if ((fd
= open(filename
, O_RDONLY
)) == -1)
248 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
249 sizeof(struct pstatus
)) {
257 if ((fp
= fopen(filename
, "r")) == NULL
)
260 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
266 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
270 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
281 * Skip duplicate pids.
283 for (i
= 0; i
< pid_index
; i
++) {
288 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
289 safe_strncat(line
, buf
, sizeof(line
));
292 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
298 pids
[pid_index
++] = pid
;
308 line
[strlen(line
) - 1] = '\0';
312 /* Unsupported OS. */
313 static char *get_pid(uid_t uid
, int multi
)
320 /* Break up the last login string into sections and add the sections to the
321 * output string array if needed. */
322 static void last_strings(char *str
)
326 const char *line
, *host
, *when
;
328 line
= host
= when
= (str
) ? "-" : "!";
330 while ((buf
= strsep(&str
, ",")) != NULL
) {
349 for (i
= 0; i
< strlen(last_options
); i
++) {
350 switch (last_options
[i
]) {
352 add_string(&strings
, line
);
355 add_string(&strings
, host
);
358 add_string(&strings
, when
);
361 add_string(&strings
, line
);
362 add_string(&strings
, host
);
363 add_string(&strings
, when
);
370 /* Get the lastlog structure from the lastlog file. */
371 #if __FreeBSD_version >= 900000
372 static char *lastlogin(const struct passwd
*pw
, char *tf
)
375 static char buf
[LINE_MAX
];
377 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
382 last
= getutxuser(pw
->pw_name
);
387 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
388 !last
->ut_line
[0] ? "!" : last
->ut_line
,
389 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ? "-" : last
->ut_host
,
390 stamp(last
->ut_tv
.tv_sec
, tf
));
395 static char *lastlogin(const struct passwd
*pw
, char *tf
)
399 static char buf
[LINE_MAX
];
406 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
407 warn("%s", _PATH_LASTLOG
);
412 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
414 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
415 warn("%s", _PATH_LASTLOG
);
419 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
420 sizeof(struct lastlog
)) {
422 warn("%s", _PATH_LASTLOG
);
429 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
430 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
432 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
433 last
.ll_line
[UT_LINESIZE
-1] = '\0';
436 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
437 last
.ll_line
[UT_LINESIZE
-1] = '\0';
440 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
441 !last
.ll_line
[0] ? "!" : last
.ll_line
,
442 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
443 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
444 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
449 /* This will return an array of utmp structures if a user is logged in, NULL
450 * otherwise. We'll try to keep the utmp file descriptor open if possible to
451 * speed things up a bit. */
452 static UTMP
**get_utmp(const char *user
)
454 UTMP
**logins
= NULL
;
466 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
467 warn("%s", _PATH_UTMP
);
478 while ((u
= getutxent()) != NULL
) {
479 if (!strcmp(u
->ut_user
, user
) && u
->ut_type
!= DEAD_PROCESS
) {
481 lseek(fd
, 0, SEEK_SET
);
483 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
484 if (strcmp(u
.ut_name
, user
) == 0) {
488 if ((tmp
= realloc(logins
,
489 (login_count
+ 2) * sizeof(UTMP
*))) ==
498 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
506 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
507 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
508 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
509 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
510 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
511 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
512 logins
[login_count
]->ut_pid
= u
->ut_pid
;
514 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
515 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
516 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
517 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
518 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
519 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
520 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
521 logins
[login_count
]->ut_pid
= u
->ut_pid
;
524 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
525 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
526 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
527 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
528 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
529 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
530 logins
[login_count
]->ut_time
= u
.ut_time
;
532 logins
[++login_count
] = NULL
;
539 /* The 'mesg' status of the logged in user. */
540 static char *msgstat(UTMP
**u
, int multi
)
542 static char line
[LINE_MAX
];
547 for (i
= 0; i
< login_count
; i
++) {
548 char filename
[FILENAME_MAX
];
550 char m
[2] = { multi
, '\0' };
552 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
554 if (stat(filename
, &st
) == -1)
555 safe_strncat(line
, "!", sizeof(line
));
558 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
561 safe_strncat(line
, m
, sizeof(line
));
567 line
[strlen(line
) - 1] = '\0';
571 /* Returns the users idle time in seconds. */
572 static char *idle(UTMP
**u
, int multi
)
574 static char line
[LINE_MAX
];
581 for (i
= 0; i
< login_count
; i
++) {
582 char buf
[FILENAME_MAX
];
583 char m
[2] = { multi
, '\0' };
585 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
587 if (stat(buf
, &st
) == -1) {
588 safe_strncat(line
, "!", sizeof(line
));
589 safe_strncat(line
, m
, sizeof(line
));
594 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
596 if (u
[i
]->ut_time
> st
.st_atime
) {
598 safe_strncat(line
, "-", sizeof(line
));
599 safe_strncat(line
, m
, sizeof(line
));
606 if (t
< u
[i
]->ut_tv
.tv_sec
)
607 t
= u
[i
]->ut_tv
.tv_sec
;
609 if (t
< u
[i
]->ut_time
)
613 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
614 safe_strncat(line
, buf
, sizeof(line
));
615 safe_strncat(line
, m
, sizeof(line
));
621 line
[strlen(line
) - 1] = '\0';
625 /* This is output if the -h command line option is passed to the main program.
627 void ui_module_help()
629 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
630 printf("\t-y tty\t\t\t\t");
631 printf("-m message status\n");
632 printf("\t-t login time stamp\t\t");
633 printf("-d duration in minutes\n");
634 printf("\t-h hostname\t\t\t");
635 printf("-i seconds idle\n");
636 printf("\t-p login process id\n");
637 printf("\t-l lastlog information"
638 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
642 /* This is the equivalent to main() only without argc and argv available. */
643 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
644 const int verbose
, char *tf
)
651 u
= get_utmp(pw
->pw_name
);
655 char line
[LINE_MAX
] = { '\0' };
657 char m
[2] = { multi
, '\0' };
661 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
664 last_strings(lastlogin(pw
, tf
));
667 for (i
= 0; i
< login_count
; i
++) {
669 && isalnum((unsigned char) u
[i
]->ut_host
[0]))
670 safe_strncat(line
, u
[i
]->ut_host
, sizeof(line
));
672 safe_strncat(line
, "-", sizeof(line
));
674 safe_strncat(line
, m
, sizeof(line
));
678 strncpy(line
, "!", sizeof(line
));
680 line
[strlen(line
) - 1] = '\0';
682 add_string(&strings
, line
);
685 for (i
= 0; i
< login_count
; i
++) {
686 if (u
[i
]->ut_line
[0])
687 safe_strncat(line
, u
[i
]->ut_line
, sizeof(line
));
689 safe_strncat(line
, "!", sizeof(line
));
691 safe_strncat(line
, m
, sizeof(line
));
695 strncpy(line
, "!", sizeof(line
));
697 line
[strlen(line
) - 1] = '\0';
699 add_string(&strings
, line
);
702 add_string(&strings
, msgstat(u
, multi
));
705 for (i
= 0; i
< login_count
; i
++) {
707 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
709 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
711 safe_strncat(line
, m
, sizeof(line
));
715 strncpy(line
, "!", sizeof(line
));
717 line
[strlen(line
) - 1] = '\0';
719 add_string(&strings
, line
);
722 for (i
= 0; i
< login_count
; i
++) {
724 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
725 snprintf(buf
, sizeof(buf
), "%lu",
726 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
728 if ((now
- u
[i
]->ut_time
) > 60) {
729 snprintf(buf
, sizeof(buf
), "%lu",
730 ((now
- u
[i
]->ut_time
) / 60));
732 safe_strncat(line
, buf
, sizeof(line
));
735 safe_strncat(line
, "-", sizeof(line
));
737 safe_strncat(line
, m
, sizeof(line
));
741 strncpy(line
, "!", sizeof(line
));
743 line
[strlen(line
) - 1] = '\0';
745 add_string(&strings
, line
);
749 for (i
= 0; i
< login_count
; i
++) {
751 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
752 safe_strncat(line
, buf
, sizeof(line
));
755 safe_strncat(line
, "!", sizeof(line
));
757 safe_strncat(line
, m
, sizeof(line
));
761 strncpy(line
, "!", sizeof(line
));
763 line
[strlen(line
) - 1] = '\0';
765 add_string(&strings
, line
);
767 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
782 /* See if the last login options (-l) are valid. */
783 static int parse_last_options(const char *args
)
787 for (i
= 0; i
< strlen(args
); i
++) {
802 char *ui_module_options_init(char **defaults
)
805 return LOGIN_OPTION_STRING
;
808 /* Check module option validity. */
809 int ui_module_options(int argc
, char **argv
)
814 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
817 if (parse_last_options(optarg
))
820 last_options
= optarg
;
823 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
835 warnx("login: invalid option -- %c", optopt
);