blob: 305b67cbc8aea2d03a88ec0a33e0f575df7de7fb [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * login.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Fri Mar 24 14:51:08 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * 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 Miller4af51302000-04-16 11:18:38 +100017 *
Damien Miller95def091999-11-25 00:26:21 +110018 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100019
20#include "includes.h"
andre2ff7b5d2000-06-03 14:57:40 +000021RCSID("$Id: login.c,v 1.31 2000/06/03 14:57:40 andre Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100022
andre2ff7b5d2000-06-03 14:57:40 +000023#include "loginrec.h"
Damien Miller2f6a0ad2000-05-31 11:20:11 +100024
Damien Miller5428f641999-11-25 11:54:57 +110025/*
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 Millerd4a8b7e1999-10-27 13:42:43 +100030
Damien Miller4af51302000-04-16 11:18:38 +100031unsigned long
Damien Miller95def091999-11-25 00:26:21 +110032get_last_login_time(uid_t uid, const char *logname,
33 char *buf, unsigned int bufsize)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100034{
andre2ff7b5d2000-06-03 14:57:40 +000035 struct logininfo li;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
andre2ff7b5d2000-06-03 14:57:40 +000037 login_getlastentry_uid(&li, uid);
38 strncpy(buf, li.hostname, bufsize);
39 return li.tv_sec;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040}
41
Damien Miller5428f641999-11-25 11:54:57 +110042/*
andre2ff7b5d2000-06-03 14:57:40 +000043 * Records that the user has logged in. I these parts of operating systems
44 * were more standardized.
Damien Miller5428f641999-11-25 11:54:57 +110045 */
andre2ff7b5d2000-06-03 14:57:40 +000046
Damien Miller4af51302000-04-16 11:18:38 +100047void
Damien Miller166fca82000-04-20 07:42:21 +100048record_login(pid_t pid, const char *ttyname, const char *user, uid_t uid,
Damien Miller34132e52000-01-14 15:45:46 +110049 const char *host, struct sockaddr * addr)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050{
andre2ff7b5d2000-06-03 14:57:40 +000051 struct logininfo *li;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
andre2ff7b5d2000-06-03 14:57:40 +000053 li = login_alloc_entry(pid, user, host, ttyname);
54 login_set_ip4(li, (struct sockaddr_in *)addr);
55 login_login(li);
56 login_free_entry(li);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100057}
Damien Miller95def091999-11-25 00:26:21 +110058
59/* Records that the user has logged out. */
60
Damien Miller4af51302000-04-16 11:18:38 +100061void
Damien Miller166fca82000-04-20 07:42:21 +100062record_logout(pid_t pid, const char *ttyname)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063{
andre2ff7b5d2000-06-03 14:57:40 +000064 struct logininfo *li;
65
66 li = login_alloc_entry(pid, NULL, NULL, ttyname);
67 login_logout(li);
68 login_free_entry(li);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069}