Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * login.c |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 10 | * Created: Fri Mar 24 14:51:08 1995 ylo |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | * This file performs some of the things login(1) normally does. We cannot |
| 13 | * easily use something like login -p -h host -f user, because there are |
| 14 | * several different logins around, and it is hard to determined what kind of |
| 15 | * login the current system has. Also, we want to be able to execute commands |
| 16 | * on a tty. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 17 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 18 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 19 | |
| 20 | #include "includes.h" |
andre | 61e6725 | 2000-06-04 17:07:49 +0000 | [diff] [blame] | 21 | RCSID("$Id: login.c,v 1.32 2000/06/04 17:07:49 andre Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 22 | |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 23 | #include "loginrec.h" |
Damien Miller | 2f6a0ad | 2000-05-31 11:20:11 +1000 | [diff] [blame] | 24 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 25 | /* |
| 26 | * Returns the time when the user last logged in. Returns 0 if the |
| 27 | * information is not available. This must be called before record_login. |
| 28 | * The host the user logged in from will be returned in buf. |
| 29 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 30 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 31 | unsigned long |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 32 | get_last_login_time(uid_t uid, const char *logname, |
| 33 | char *buf, unsigned int bufsize) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 34 | { |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 35 | struct logininfo li; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
andre | 61e6725 | 2000-06-04 17:07:49 +0000 | [diff] [blame] | 37 | login_get_lastlog(&li, uid); |
| 38 | strlcpy(buf, li.hostname, bufsize); |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 39 | return li.tv_sec; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | } |
| 41 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 42 | /* |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 43 | * Records that the user has logged in. I these parts of operating systems |
| 44 | * were more standardized. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 45 | */ |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 46 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 47 | void |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 48 | record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid, |
Damien Miller | 34132e5 | 2000-01-14 15:45:46 +1100 | [diff] [blame] | 49 | const char *host, struct sockaddr * addr) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 50 | { |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 51 | struct logininfo *li; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 52 | |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 53 | li = login_alloc_entry(pid, user, host, ttyname); |
andre | 61e6725 | 2000-06-04 17:07:49 +0000 | [diff] [blame] | 54 | login_set_addr(li, addr, sizeof(struct sockaddr)); |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 55 | login_login(li); |
| 56 | login_free_entry(li); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 58 | |
| 59 | /* Records that the user has logged out. */ |
| 60 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 61 | void |
Damien Miller | 166fca8 | 2000-04-20 07:42:21 +1000 | [diff] [blame] | 62 | record_logout(pid_t pid, const char *ttyname) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | { |
andre | 2ff7b5d | 2000-06-03 14:57:40 +0000 | [diff] [blame] | 64 | struct logininfo *li; |
| 65 | |
| 66 | li = login_alloc_entry(pid, NULL, NULL, ttyname); |
| 67 | login_logout(li); |
| 68 | login_free_entry(li); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | } |