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"
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
)
78 static void free_logins(UTMP
**u
)
83 for (up
= u
; *up
; up
++)
91 /* This is for *BSD (login process id). */
93 static char *get_pid(uid_t uid
, int multi
)
96 static char line
[LINE_MAX
];
99 char errbuf
[LINE_MAX
];
100 struct kinfo_proc
*kp
;
111 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
,
112 O_RDONLY
, errbuf
)) == NULL
) {
114 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
115 O_RDONLY
, errbuf
)) == NULL
) {
122 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
123 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
127 for (i
= 0; i
< cnt
; i
++) {
130 #if __FreeBSD_version < 500000
131 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
!=
133 pid
= kp
[i
].kp_eproc
.e_ppid
;
135 * pid = kp[i].kp_proc.p_pid;
141 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
144 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
145 safe_strncat(line
, buf
, sizeof(line
));
152 line
[strlen(line
) - 1] = '\0';
156 /* This is for Linux and Solaris. */
157 #elif defined(HAVE_PROCFS)
158 #include <sys/types.h>
159 #include <sys/stat.h>
170 static char *get_pid(uid_t uid
, int multi
)
175 static char line
[LINE_MAX
];
176 pid_t
*pids
= 0, *tpids
;
181 struct pstatus pstat
;
188 if (!procdir
&& firstrun
)
194 if ((procdir
= opendir("/proc")) == NULL
) {
203 while ((ent
= readdir(procdir
)) != NULL
) {
205 char filename
[FILENAME_MAX
];
213 if (!isdigit((unsigned char) *ent
->d_name
))
217 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
219 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
222 if (stat(filename
, &st
) == -1)
226 * The current user owns this file (process id).
228 if (st
.st_uid
== uid
) {
230 if ((fd
= open(filename
, O_RDONLY
)) == -1)
233 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
234 sizeof(struct pstatus
)) {
242 if ((fp
= fopen(filename
, "r")) == NULL
)
245 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
251 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
255 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
266 * Skip duplicate pids.
268 for (i
= 0; i
< pid_index
; i
++) {
273 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
274 safe_strncat(line
, buf
, sizeof(line
));
277 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
283 pids
[pid_index
++] = pid
;
293 line
[strlen(line
) - 1] = '\0';
297 /* Unsupported OS. */
298 static char *get_pid(uid_t uid
, int multi
)
305 /* Break up the last login string into sections and add the sections to the
306 * output string array if needed. */
307 static void last_strings(char *str
)
311 const char *line
, *host
, *when
;
313 line
= host
= when
= (str
) ? "-" : "!";
315 while ((buf
= strsep(&str
, ",")) != NULL
) {
334 for (i
= 0; i
< strlen(last_options
); i
++) {
335 switch (last_options
[i
]) {
337 add_string(&strings
, line
);
340 add_string(&strings
, host
);
343 add_string(&strings
, when
);
346 add_string(&strings
, line
);
347 add_string(&strings
, host
);
348 add_string(&strings
, when
);
355 /* Get the lastlog structure from the lastlog file. */
356 #if __FreeBSD_version >= 900000
357 static char *lastlogin(const struct passwd
*pw
, char *tf
)
360 static char buf
[LINE_MAX
];
362 if (setutxdb(UTXDB_LASTLOGIN
, NULL
) == -1) {
367 last
= getutxuser(pw
->pw_name
);
372 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
373 !last
->ut_line
[0] ? "!" : last
->ut_line
,
374 (!last
->ut_host
[0] || !isalnum(last
->ut_host
[0])) ?
375 !isdigit(last
->ut_line
[3]) ? "!" : "-" : last
->ut_host
,
376 !last
->ut_time
[0] ? "!" : stamp(last
->ut_time
, tf
));
381 static char *lastlogin(const struct passwd
*pw
, char *tf
)
385 static char buf
[LINE_MAX
];
392 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
393 warn("%s", _PATH_LASTLOG
);
398 offset
= (long) pw
->pw_uid
* sizeof(struct lastlog
);
400 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
401 warn("%s", _PATH_LASTLOG
);
405 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
406 sizeof(struct lastlog
)) {
408 warn("%s", _PATH_LASTLOG
);
415 last
.ll_host
[UTX_HOSTSIZE
-1] = '\0';
416 last
.ll_line
[UTX_LINESIZE
-1] = '\0';
418 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
419 last
.ll_line
[UT_LINESIZE
-1] = '\0';
422 last
.ll_host
[UT_HOSTSIZE
-1] = '\0';
423 last
.ll_line
[UT_LINESIZE
-1] = '\0';
426 snprintf(buf
, sizeof(buf
), "%s,%s,%s",
427 !last
.ll_line
[0] ? "!" : last
.ll_line
,
428 (!last
.ll_host
[0] || !isalnum(last
.ll_host
[0])) ?
429 !isdigit(last
.ll_line
[3]) ? "!" : "-" : last
.ll_host
,
430 !last
.ll_time
? "!" : stamp(last
.ll_time
, tf
));
435 /* This will return an array of utmp structures if a user is logged in, NULL
436 * otherwise. We'll try to keep the utmp file descriptor open if possible to
437 * speed things up a bit. */
438 static UTMP
**get_utmp(const char *user
)
440 UTMP
**logins
= NULL
;
452 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
453 warn("%s", _PATH_UTMP
);
464 while ((u
= getutxent()) != NULL
) {
465 if (!strcmp(u
->ut_user
, user
) && u
->ut_type
!= DEAD_PROCESS
) {
467 lseek(fd
, 0, SEEK_SET
);
469 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
470 if (strcmp(u
.ut_name
, user
) == 0) {
474 if ((tmp
= realloc(logins
,
475 (login_count
+ 2) * sizeof(UTMP
*))) ==
484 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
492 memcpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
493 logins
[login_count
]->ut_name
[UTX_NAMESIZE
-1] = 0;
494 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
495 logins
[login_count
]->ut_line
[UTX_LINESIZE
-1] = 0;
496 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
497 logins
[login_count
]->ut_host
[UTX_HOSTSIZE
-1] = 0;
498 logins
[login_count
]->ut_pid
= u
->ut_pid
;
500 memcpy(logins
[login_count
]->ut_user
, u
->ut_user
, UT_NAMESIZE
);
501 logins
[login_count
]->ut_user
[UT_NAMESIZE
-1] = 0;
502 memcpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
503 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
504 memcpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
505 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
506 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
507 logins
[login_count
]->ut_pid
= u
->ut_pid
;
510 memcpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
511 logins
[login_count
]->ut_name
[UT_NAMESIZE
-1] = 0;
512 memcpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
513 logins
[login_count
]->ut_line
[UT_LINESIZE
-1] = 0;
514 memcpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
515 logins
[login_count
]->ut_host
[UT_HOSTSIZE
-1] = 0;
516 logins
[login_count
]->ut_time
= u
.ut_time
;
518 logins
[++login_count
] = NULL
;
525 /* The 'mesg' status of the logged in user. */
526 static char *msgstat(UTMP
**u
, int multi
)
528 static char line
[LINE_MAX
];
533 for (i
= 0; i
< login_count
; i
++) {
534 char filename
[FILENAME_MAX
];
536 char m
[2] = { multi
, '\0' };
538 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
540 if (stat(filename
, &st
) == -1)
541 safe_strncat(line
, "!", sizeof(line
));
544 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
547 safe_strncat(line
, m
, sizeof(line
));
553 line
[strlen(line
) - 1] = '\0';
557 /* Returns the users idle time in seconds. */
558 static char *idle(UTMP
**u
, int multi
)
560 static char line
[LINE_MAX
];
567 for (i
= 0; i
< login_count
; i
++) {
568 char buf
[FILENAME_MAX
];
569 char m
[2] = { multi
, '\0' };
571 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
573 if (stat(buf
, &st
) == -1) {
574 safe_strncat(line
, "!", sizeof(line
));
575 safe_strncat(line
, m
, sizeof(line
));
580 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
582 if (u
[i
]->ut_time
> st
.st_atime
) {
584 safe_strncat(line
, "-", sizeof(line
));
585 safe_strncat(line
, m
, sizeof(line
));
592 if (t
< u
[i
]->ut_tv
.tv_sec
)
593 t
= u
[i
]->ut_tv
.tv_sec
;
595 if (t
< u
[i
]->ut_time
)
599 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
600 safe_strncat(line
, buf
, sizeof(line
));
601 safe_strncat(line
, m
, sizeof(line
));
607 line
[strlen(line
) - 1] = '\0';
611 /* This is output if the -h command line option is passed to the main program.
613 void ui_module_help()
615 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
616 printf("\t-y tty\t\t\t\t");
617 printf("-m message status\n");
618 printf("\t-t login time stamp\t\t");
619 printf("-d duration in minutes\n");
620 printf("\t-h hostname\t\t\t");
621 printf("-i seconds idle\n");
622 printf("\t-p login process id\n");
623 printf("\t-l lastlog information"
624 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
628 /* This is the equivalent to main() only without argc and argv available. */
629 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
630 const int verbose
, char *tf
)
637 u
= get_utmp(pw
->pw_name
);
641 char line
[LINE_MAX
] = { '\0' };
643 char m
[2] = { multi
, '\0' };
647 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
650 last_strings(lastlogin(pw
, tf
));
653 for (i
= 0; i
< login_count
; i
++) {
655 && isalnum((unsigned char) u
[i
]->ut_host
[0])) {
656 safe_strncat(line
, u
[i
]->ut_host
, sizeof(line
));
660 * If a users tty is tty1-n, it must be a console
664 && isdigit((unsigned char) u
[i
]->ut_line
[3]))
665 safe_strncat(line
, "-", sizeof(line
));
667 safe_strncat(line
, "!", sizeof(line
));
670 safe_strncat(line
, m
, sizeof(line
));
674 strncpy(line
, "!", sizeof(line
));
676 line
[strlen(line
) - 1] = '\0';
678 add_string(&strings
, line
);
681 for (i
= 0; i
< login_count
; i
++) {
682 if (u
[i
]->ut_line
[0])
683 safe_strncat(line
, u
[i
]->ut_line
, 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 add_string(&strings
, msgstat(u
, multi
));
701 for (i
= 0; i
< login_count
; i
++) {
703 safe_strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
705 safe_strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
707 safe_strncat(line
, m
, sizeof(line
));
711 strncpy(line
, "!", sizeof(line
));
713 line
[strlen(line
) - 1] = '\0';
715 add_string(&strings
, line
);
718 for (i
= 0; i
< login_count
; i
++) {
720 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
721 snprintf(buf
, sizeof(buf
), "%lu",
722 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
724 if ((now
- u
[i
]->ut_time
) > 60) {
725 snprintf(buf
, sizeof(buf
), "%lu",
726 ((now
- u
[i
]->ut_time
) / 60));
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
);
745 for (i
= 0; i
< login_count
; i
++) {
747 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
748 safe_strncat(line
, buf
, sizeof(line
));
751 safe_strncat(line
, "!", sizeof(line
));
753 safe_strncat(line
, m
, sizeof(line
));
757 strncpy(line
, "!", sizeof(line
));
759 line
[strlen(line
) - 1] = '\0';
761 add_string(&strings
, line
);
763 add_string(&strings
, (u
) ? get_pid(pw
->pw_uid
, multi
) : "!");
778 /* See if the last login options (-l) are valid. */
779 static int parse_last_options(const char *args
)
783 for (i
= 0; i
< strlen(args
); i
++) {
798 char *ui_module_options_init(char **defaults
)
801 return LOGIN_OPTION_STRING
;
804 /* Check module option validity. */
805 int ui_module_options(int argc
, char **argv
)
810 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
813 if (parse_last_options(optarg
))
816 last_options
= optarg
;
819 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));
831 warnx("login: invalid option -- %c", optopt
);