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
21 #include <sys/types.h>
36 #include "../strsep.c"
43 void ui_module_init(int *chainable
)
46 fprintf(stderr
, "%s: ui_module_init()\n", __FILE__
);
57 fprintf(stderr
, "%s: ui_module_exit()\n", __FILE__
);
77 /* This is for *BSD (login process id). */
79 char *ui_module_pid(uid_t uid
, int multi
)
82 static char line
[LINE_MAX
];
85 char errbuf
[LINE_MAX
];
86 struct kinfo_proc
*kp
;
97 if ((kd
= kvm_openfiles(NULL
, NULL
, NULL
,
99 if ((kd
= kvm_openfiles(_PATH_DEVNULL
, _PATH_DEVNULL
, _PATH_DEVNULL
,
101 O_RDONLY
, errbuf
)) == NULL
) {
107 if ((kp
= kvm_getprocs(kd
, KERN_PROC_UID
, uid
, &cnt
)) == NULL
) {
108 warnx("kvm_getprocs(): %s", kvm_geterr(kd
));
112 for (i
= 0; i
< cnt
; i
++) {
115 #if __FreeBSD_version < 500000
116 if (kp
[i
].kp_eproc
.e_flag
& EPROC_SLEADER
&& kp
[i
].kp_eproc
.e_tdev
!=
118 pid
= kp
[i
].kp_eproc
.e_ppid
;
120 * pid = kp[i].kp_proc.p_pid;
126 if (kp
[i
].ki_kiflag
& KI_SLEADER
&& kp
[i
].ki_tdev
!= -1) {
129 snprintf(buf
, sizeof(buf
), "%i%c", pid
, multi
);
130 strncat(line
, buf
, sizeof(line
));
137 line
[strlen(line
) - 1] = '\0';
141 /* This is for Linux and Solaris. */
142 #elif defined(HAVE_PROCFS)
143 #include <sys/types.h>
144 #include <sys/stat.h>
155 char *ui_module_pid(uid_t uid
, int multi
)
160 static char line
[LINE_MAX
];
166 struct pstatus pstat
;
173 if (!procdir
&& firstrun
)
179 if ((procdir
= opendir("/proc")) == NULL
) {
188 while ((ent
= readdir(procdir
)) != NULL
) {
190 char filename
[FILENAME_MAX
];
198 if (!isdigit((unsigned char) *ent
->d_name
))
202 snprintf(filename
, sizeof(filename
), "/proc/%s/stat", ent
->d_name
);
204 snprintf(filename
, sizeof(filename
), "/proc/%s/status", ent
->d_name
);
207 if (stat(filename
, &st
) == -1)
211 * The current user owns this file (process id).
213 if (st
.st_uid
== uid
) {
215 if ((fd
= open(filename
, O_RDONLY
)) == -1)
218 if (pread(fd
, &pstat
, sizeof(struct pstatus
), 0) !=
219 sizeof(struct pstatus
)) {
227 if ((fp
= fopen(filename
, "r")) == NULL
)
230 if ((t
= fgets(buf
, sizeof(buf
), fp
)) == NULL
) {
236 if ((i
= sscanf(buf
, "%*i %*s %*c %*i %*i %i", &pid
)) < 1) {
240 if ((i = sscanf(buf, "%*s %*i %li", &ppid)) < 1) {
251 * Skip duplicate pids.
253 for (i
= 0; i
< pid_index
; i
++) {
258 snprintf(buf
, sizeof(buf
), "%li%c", (unsigned long) pid
, multi
);
259 strncat(line
, buf
, sizeof(line
));
262 realloc(pids
, (pid_index
+ 2) * sizeof(pid_t
*))) == NULL
) {
267 pids
[pid_index
++] = pid
;
277 line
[strlen(line
) - 1] = '\0';
281 /* Unsupported OS. */
282 char *ui_module_pid(uid_t uid
, int multi
)
289 /* Break up the last login string into sections and add the sections to the
290 * output string array if needed. */
291 static void last_strings(char *str
)
295 const char *line
, *host
, *when
;
297 line
= host
= when
= (str
) ? "-" : "!";
299 while ((buf
= strsep(&str
, ",")) != NULL
) {
318 for (i
= 0; i
< strlen(last_options
); i
++) {
319 switch (last_options
[i
]) {
321 add_string(&strings
, line
);
324 add_string(&strings
, host
);
327 add_string(&strings
, when
);
330 add_string(&strings
, line
);
331 add_string(&strings
, host
);
332 add_string(&strings
, when
);
341 /* Get the lastlog structure from the lastlog file. */
342 static char *lastlogin(uid_t uid
, char *tf
)
346 static char buf
[LINE_MAX
];
350 char tmp
[64], htmp
[UTX_HOSTSIZE
+ 1];
352 char tmp
[64], htmp
[UT_HOSTSIZE
+ 1];
355 char tmp
[64], htmp
[UT_HOSTSIZE
+ 1];
359 buf
[0] = tmp
[0] = htmp
[0] = '\0';
365 if ((lastlogfd
= open(_PATH_LASTLOG
, O_RDONLY
)) == -1) {
366 warn("%s", _PATH_LASTLOG
);
371 offset
= (long) uid
*sizeof(struct lastlog
);
373 if (lseek(lastlogfd
, offset
, SEEK_SET
) == -1) {
374 warn("%s", _PATH_LASTLOG
);
378 if ((count
= read(lastlogfd
, &last
, sizeof(struct lastlog
))) !=
379 sizeof(struct lastlog
)) {
381 warn("%s", _PATH_LASTLOG
);
386 if (last
.ll_line
[0] == '\0')
387 strncpy(buf
, "!", sizeof(buf
));
389 strncpy(buf
, last
.ll_line
, sizeof(buf
));
391 strncat(buf
, ",", sizeof(buf
));
392 strncpy(htmp
, last
.ll_host
, sizeof(htmp
));
396 htmp
[UTX_HOSTSIZE
] = '\0';
398 htmp
[UT_HOSTSIZE
] = '\0';
401 htmp
[UT_HOSTSIZE
] = '\0';
404 if (htmp
[0] && isalnum((unsigned char) htmp
[0]))
405 strncat(buf
, htmp
, sizeof(buf
));
408 * If a users tty is tty1-n, it must be a console
411 if (last
.ll_line
[0] && isdigit((unsigned char) last
.ll_line
[3]))
412 strncat(buf
, "-", sizeof(buf
));
414 strncat(buf
, "!", sizeof(buf
));
417 strncat(buf
, ",", sizeof(buf
));
420 strncat(buf
, stamp(last
.ll_time
, tf
), sizeof(buf
));
422 strncat(buf
, "!", sizeof(buf
));
427 /* This will return an array of utmp structures if a user is logged in, NULL
428 * otherwise. We'll try to keep the utmp file descriptor open if possible to
429 * speed things up a bit. */
430 static UTMP
**get_utmp(const char *user
)
432 UTMP
**logins
= NULL
;
445 if ((fd
= open(_PATH_UTMP
, O_RDONLY
)) == -1) {
446 warn("%s", _PATH_UTMP
);
457 while ((u
= getutxent()) != NULL
) {
458 if (strcmp(u
->ut_name
, user
) == 0) {
460 lseek(fd
, 0, SEEK_SET
);
462 while ((count
= read(fd
, &u
, sizeof(UTMP
))) == sizeof(UTMP
)) {
463 if (strcmp(u
.ut_name
, user
) == 0) {
465 if ((logins
= realloc(logins
,
466 (login_count
+ 2) * sizeof(UTMP
*))) ==
472 if ((logins
[login_count
] = malloc(sizeof(UTMP
))) == NULL
) {
479 strncpy(logins
[login_count
]->ut_name
, u
->ut_name
, UTX_NAMESIZE
);
480 strncpy(logins
[login_count
]->ut_line
, u
->ut_line
, UTX_LINESIZE
);
481 strncpy(logins
[login_count
]->ut_host
, u
->ut_host
, UTX_HOSTSIZE
);
482 logins
[login_count
]->ut_pid
= u
->ut_pid
;
484 strncpy(logins
[login_count
]->ut_name
, u
->ut_name
, UT_NAMESIZE
);
485 strncpy(logins
[login_count
]->ut_line
, u
->ut_line
, UT_LINESIZE
);
486 strncpy(logins
[login_count
]->ut_host
, u
->ut_host
, UT_HOSTSIZE
);
487 logins
[login_count
]->ut_tv
.tv_sec
= u
->ut_tv
.tv_sec
;
488 logins
[login_count
]->ut_pid
= u
->ut_pid
;
491 strncpy(logins
[login_count
]->ut_name
, u
.ut_name
, UT_NAMESIZE
);
492 strncpy(logins
[login_count
]->ut_line
, u
.ut_line
, UT_LINESIZE
);
493 strncpy(logins
[login_count
]->ut_host
, u
.ut_host
, UT_HOSTSIZE
);
494 logins
[login_count
]->ut_host
[UT_HOSTSIZE
] = '\0';
495 logins
[login_count
]->ut_time
= u
.ut_time
;
497 logins
[++login_count
] = NULL
;
504 /* The 'mesg' status of the logged in user. */
505 static char *msgstat(UTMP
** u
, int multi
)
507 static char line
[LINE_MAX
];
512 for (i
= 0; i
< login_count
; i
++) {
513 char filename
[FILENAME_MAX
];
515 char m
[2] = { multi
, '\0' };
517 snprintf(filename
, sizeof(filename
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
519 if (stat(filename
, &st
) == -1)
520 strncat(line
, "!", sizeof(line
));
523 (st
.st_mode
& S_IWGRP
|| st
.st_mode
& S_IWOTH
) ? "1" : "0",
526 strncat(line
, m
, sizeof(line
));
532 line
[strlen(line
) - 1] = '\0';
536 /* Returns the users idle time in seconds. */
537 static char *idle(UTMP
** u
, int multi
)
539 static char line
[LINE_MAX
];
546 for (i
= 0; i
< login_count
; i
++) {
547 char buf
[FILENAME_MAX
];
548 char m
[2] = { multi
, '\0' };
550 snprintf(buf
, sizeof(buf
), "%s%s", _PATH_DEV
, u
[i
]->ut_line
);
552 if (stat(buf
, &st
) == -1) {
553 strncat(line
, "!", sizeof(line
));
554 strncat(line
, m
, sizeof(line
));
559 if (u
[i
]->ut_tv
.tv_sec
> st
.st_atime
) {
561 if (u
[i
]->ut_time
> st
.st_atime
) {
563 strncat(line
, "-", sizeof(line
));
564 strncat(line
, m
, sizeof(line
));
571 if (t
< u
[i
]->ut_tv
.tv_sec
)
572 t
= u
[i
]->ut_tv
.tv_sec
;
574 if (t
< u
[i
]->ut_time
)
578 snprintf(buf
, sizeof(buf
), "%lu", (now
- t
<= 0) ? 0 : now
- t
);
579 strncat(line
, buf
, sizeof(line
));
580 strncat(line
, m
, sizeof(line
));
586 line
[strlen(line
) - 1] = '\0';
590 /* This is output if the -h command line option is passed to the main program.
592 void ui_module_help()
595 fprintf(stderr
, "%s: ui_module_help()\n", __FILE__
);
598 printf(" Login information [-L (-%s)]:\n", LOGIN_OPTION_ORDER
);
599 printf("\t-y tty\t\t\t\t");
600 printf("-m message status\n");
601 printf("\t-t login time stamp\t\t");
602 printf("-d duration in minutes\n");
603 printf("\t-h hostname\t\t\t");
604 printf("-i seconds idle\n");
605 printf("\t-p login process id\n");
606 printf("\t-l lastlog information"
607 " (any of tt[y],[h]ostname,[t]ime, or [a]ll)\n\n");
611 /* This is the equivalent to main() only without argc and argv available. */
612 int ui_module_exec(char ***s
, const struct passwd
*pw
, const int multi
,
613 const int verbose
, char *tf
)
616 UTMP
**u
= NULL
, **up
;
620 u
= get_utmp(pw
->pw_name
);
624 char line
[LINE_MAX
] = { '\0' };
626 char m
[2] = { multi
, '\0' };
629 char htmp
[UTX_HOSTSIZE
+ 1];
631 char htmp
[UT_HOSTSIZE
+ 1];
634 char htmp
[UT_HOSTSIZE
+ 1];
639 add_string(&strings
, (u
) ? idle(u
, multi
) : "!");
642 last_strings(lastlogin(pw
->pw_uid
, tf
));
645 for (i
= 0; i
< login_count
; i
++) {
647 && isalnum((unsigned char) u
[i
]->ut_host
[0])) {
648 strcpy(htmp
, u
[i
]->ut_host
);
649 htmp
[sizeof(htmp
) - 1] = '\0';
650 strncat(line
, htmp
, sizeof(line
));
654 * If a users tty is tty1-n, it must be a console
658 && isdigit((unsigned char) u
[i
]->ut_line
[3]))
659 strncat(line
, "-", sizeof(line
));
661 strncat(line
, "!", sizeof(line
));
664 strncat(line
, m
, sizeof(line
));
668 strncpy(line
, "!", sizeof(line
));
670 line
[strlen(line
) - 1] = '\0';
672 add_string(&strings
, line
);
675 for (i
= 0; i
< login_count
; i
++) {
676 if (u
[i
]->ut_line
[0])
677 strncat(line
, u
[i
]->ut_line
, sizeof(line
));
679 strncat(line
, "!", sizeof(line
));
681 strncat(line
, m
, sizeof(line
));
685 strncpy(line
, "!", sizeof(line
));
687 line
[strlen(line
) - 1] = '\0';
689 add_string(&strings
, line
);
692 add_string(&strings
, msgstat(u
, multi
));
695 for (i
= 0; i
< login_count
; i
++) {
697 strncat(line
, stamp(u
[i
]->ut_tv
.tv_sec
, tf
), sizeof(line
));
699 strncat(line
, stamp(u
[i
]->ut_time
, tf
), sizeof(line
));
701 strncat(line
, m
, sizeof(line
));
705 strncpy(line
, "!", sizeof(line
));
707 line
[strlen(line
) - 1] = '\0';
709 add_string(&strings
, line
);
712 for (i
= 0; i
< login_count
; i
++) {
714 if ((now
- u
[i
]->ut_tv
.tv_sec
) > 60) {
715 snprintf(buf
, sizeof(buf
), "%lu",
716 ((now
- u
[i
]->ut_tv
.tv_sec
) / 60));
718 if ((now
- u
[i
]->ut_time
) > 60) {
719 snprintf(buf
, sizeof(buf
), "%lu",
720 ((now
- u
[i
]->ut_time
) / 60));
722 strncat(line
, buf
, sizeof(line
));
725 strncat(line
, "-", sizeof(line
));
727 strncat(line
, m
, sizeof(line
));
731 strncpy(line
, "!", sizeof(line
));
733 line
[strlen(line
) - 1] = '\0';
735 add_string(&strings
, line
);
739 for (i
= 0; i
< login_count
; i
++) {
741 snprintf(buf
, sizeof(buf
), "%li", (long) u
[i
]->ut_pid
);
742 strncat(line
, buf
, sizeof(line
));
745 strncat(line
, "!", sizeof(line
));
747 strncat(line
, m
, sizeof(line
));
751 strncpy(line
, "!", sizeof(line
));
753 line
[strlen(line
) - 1] = '\0';
755 add_string(&strings
, line
);
757 add_string(&strings
, (u
) ? ui_module_pid(pw
->pw_uid
, multi
) : "!");
768 for (up
= u
; *up
; up
++)
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
)
811 fprintf(stderr
, "%s: ui_module_options()\n", __FILE__
);
814 while ((opt
= getopt(argc
, argv
, LOGIN_OPTION_STRING
)) != -1) {
817 last_options
= optarg
;
829 warnx("login: invalid option -- %c", optopt
);
835 if (parse_last_options(last_options
))
840 strncpy(options
, LOGIN_OPTION_ORDER
, sizeof(options
));