]> andersk Git - openssh.git/blame - login.c
- Merged several minor fixed:
[openssh.git] / login.c
CommitLineData
8efc0c15 1/*
2
3login.c
4
5Author: Tatu Ylonen <ylo@cs.hut.fi>
6
7Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 All rights reserved
9
10Created: Fri Mar 24 14:51:08 1995 ylo
11
12This file performs some of the things login(1) normally does. We cannot
13easily use something like login -p -h host -f user, because there are
14several different logins around, and it is hard to determined what kind of
15login the current system has. Also, we want to be able to execute commands
16on a tty.
17
18*/
19
20#include "includes.h"
21RCSID("$Id$");
22
23#include <utmp.h>
24#include "ssh.h"
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
30/* Returns the time when the user last logged in (or 0 if no previous login
31 is found). The name of the host used last time is returned in buf. */
32
33unsigned long get_last_login_time(uid_t uid, const char *logname,
34 char *buf, unsigned int bufsize)
35{
36 struct lastlog ll;
37 char *lastlog;
38 int fd;
39
40 lastlog = _PATH_LASTLOG;
41
42 buf[0] = '\0';
43
44 fd = open(lastlog, O_RDONLY);
45 if (fd < 0)
46 return 0;
47 lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);
48 if (read(fd, &ll, sizeof(ll)) != sizeof(ll))
49 {
50 close(fd);
51 return 0;
52 }
53 close(fd);
54 if (bufsize > sizeof(ll.ll_host) + 1)
55 bufsize = sizeof(ll.ll_host) + 1;
56 strncpy(buf, ll.ll_host, bufsize - 1);
57 buf[bufsize - 1] = 0;
58 return ll.ll_time;
59}
60
61/* Records that the user has logged in. I these parts of operating systems
62 were more standardized. */
63
64void record_login(int pid, const char *ttyname, const char *user, uid_t uid,
65 const char *host, struct sockaddr_in *addr)
66{
67 int fd;
68 struct lastlog ll;
69 char *lastlog;
70
71 struct utmp u;
72 const char *utmp, *wtmp;
73
74 /* Construct an utmp/wtmp entry. */
75 memset(&u, 0, sizeof(u));
76 strncpy(u.ut_line, ttyname + 5, sizeof(u.ut_line));
77 u.ut_time = time(NULL);
78 strncpy(u.ut_name, user, sizeof(u.ut_name));
79 strncpy(u.ut_host, host, sizeof(u.ut_host));
80
81 /* Figure out the file names. */
82 utmp = _PATH_UTMP;
83 wtmp = _PATH_WTMP;
84
85 login(&u);
86
87 lastlog = _PATH_LASTLOG;
88
89 /* Update lastlog unless actually recording a logout. */
90 if (strcmp(user, "") != 0)
91 {
92 /* It is safer to bzero the lastlog structure first because some
93 systems might have some extra fields in it (e.g. SGI) */
94 memset(&ll, 0, sizeof(ll));
95
96 /* Update lastlog. */
97 ll.ll_time = time(NULL);
98 strncpy(ll.ll_line, ttyname + 5, sizeof(ll.ll_line));
99 strncpy(ll.ll_host, host, sizeof(ll.ll_host));
100 fd = open(lastlog, O_RDWR);
101 if (fd >= 0)
102 {
103 lseek(fd, (off_t)((long)uid * sizeof(ll)), SEEK_SET);
104 if (write(fd, &ll, sizeof(ll)) != sizeof(ll))
105 log("Could not write %.100s: %.100s", lastlog, strerror(errno));
106 close(fd);
107 }
108 }
109}
110
111/* Records that the user has logged out. */
112
113void record_logout(int pid, const char *ttyname)
114{
115 const char *line = ttyname + 5; /* /dev/ttyq8 -> ttyq8 */
116 if (logout(line))
117 logwtmp(line, "", "");
118}
This page took 0.502028 seconds and 5 git commands to generate.