[go: up one dir, main page]

Menu

[3dc168]: / uacct.c  Maximize  Restore  History

Download this file

196 lines (172 with data), 4.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// This file is part of the loginx project
//
// Copyright (c) 2013 by Mike Sharov <msharov@users.sourceforge.net>
// This file is free software, distributed under the ISC license.
#include "defs.h"
#include <pwd.h>
#include <grp.h>
#include <utmp.h>
#include <time.h>
#include <fcntl.h>
static struct account** _accts = NULL;
static unsigned _naccts = 0;
static unsigned _selected_account = 0;
static gid_t _ttygroup = 0;
static void CleanupAccounts (void)
{
if (!_accts)
return;
for (unsigned i = 0; i < _naccts; ++i) {
xfree (_accts[i]->name);
xfree (_accts[i]->dir);
xfree (_accts[i]->shell);
xfree (_accts[i]);
}
xfreenull (_accts);
}
static bool CanLogin (const struct passwd* pw)
{
if (!pw->pw_shell || strstr (pw->pw_shell, "nologin"))
return false;
setusershell();
const char* ushell = getusershell();
if (!ushell)
return true; // /etc/shells missing - allow all shells
do {
if (!strcmp (pw->pw_shell, ushell))
return true;
} while ((ushell = getusershell()));
return false;
}
acclist_t ReadAccounts (void)
{
if (_accts)
return (acclist_t) _accts;
_ttygroup = getgid();
struct group* ttygr = getgrnam("tty");
if (ttygr) // If no tty group, use user's primary group
_ttygroup = ttygr->gr_gid;
endgrent();
unsigned nac = 0;
setpwent();
for (struct passwd* pw; (pw = getpwent());)
nac += CanLogin (pw);
_accts = xmalloc ((nac+1)*sizeof(struct account*));
atexit (CleanupAccounts);
unsigned iac = 0;
setpwent();
for (struct passwd* pw; iac < nac && (pw = getpwent());) {
if (!CanLogin (pw))
continue;
_accts[iac] = (struct account*) xmalloc (sizeof(struct account));
_accts[iac]->uid = pw->pw_uid;
_accts[iac]->gid = pw->pw_gid;
_accts[iac]->name = strdup (pw->pw_name);
_accts[iac]->dir = strdup (pw->pw_dir);
_accts[iac]->shell = strdup (pw->pw_shell);
++iac;
}
_naccts = iac;
endpwent();
endusershell();
// For each valid user, get the last login time from lastlog
int fd = open (_PATH_LASTLOG, O_RDONLY);
if (fd >= 0) {
struct stat st;
if (fstat (fd, &st) == 0) {
const unsigned maxuid = st.st_size / sizeof(struct lastlog);
for (unsigned i = 0; i < _naccts; ++i)
if (_accts[i]->uid < maxuid)
pread (fd, &_accts[i]->ltime, sizeof(_accts[i]->ltime), _accts[i]->uid * sizeof(struct lastlog));
}
close (fd);
}
// Reset the selected user index to the one logged in last
time_t maxtime = 0;
unsigned lli = 0;
for (unsigned i = 0; i < _naccts; ++i) {
if (_accts[i]->ltime >= maxtime) {
maxtime = _accts[i]->ltime;
lli = i;
}
}
_selected_account = lli;
return (acclist_t) _accts;
}
unsigned NAccounts (void)
{
return _naccts;
}
unsigned SelectedAccount (void)
{
return _selected_account;
}
void SetSelectedAccount (unsigned i)
{
if (i < _naccts)
_selected_account = i;
}
const struct account* SetSelectedAccountByName (const char* name)
{
if (!name)
return NULL;
acclist_t al = ReadAccounts();
for (unsigned i = 0; i < _naccts; ++i) {
if (0 != strcmp (name, al[i]->name))
continue;
_selected_account = i;
return al[i];
}
return NULL;
}
const struct account* SelectedAccountInfo (void)
{
acclist_t al = ReadAccounts();
return _selected_account < _naccts ? al[_selected_account] : NULL;
}
gid_t GetTTYGroup (void)
{
return (!_ttygroup && _selected_account < _naccts)
? _accts[_selected_account]->gid : _ttygroup;
}
void WriteLastlog (void)
{
if (_selected_account >= _naccts)
return;
uid_t luid = _accts[_selected_account]->uid;
int fd = open (_PATH_LASTLOG, O_WRONLY| O_CREAT, 644);
if (fd < 0)
return;
struct lastlog ll;
memset (&ll, 0, sizeof(ll));
const char* ttyname = strrchr (_ttypath, '/');
if (!ttyname++)
ttyname = _ttypath;
strncpy (ll.ll_line, ttyname, sizeof(ll.ll_line)-1);
gethostname (ll.ll_host, sizeof(ll.ll_host)-1);
ll.ll_time = time (NULL);
pwrite (fd, &ll, sizeof(ll), luid*sizeof(ll));
close (fd);
}
void WriteUtmp (const struct account* acct, pid_t pid, short uttype, int exitcode)
{
struct utmp ut;
memset (&ut, 0, sizeof(ut));
ut.ut_type = uttype;
ut.ut_pid = pid;
ut.ut_exit.e_exit = exitcode;
const char* ttydev = strrchr(_ttypath, '/');
if (!ttydev++)
ttydev = _ttypath;
strncpy (ut.ut_line, ttydev, sizeof(ut.ut_line)-1);
strncpy (ut.ut_id, ttydev, sizeof(ut.ut_id)); // ut_id is not a 0-terminated string
strncpy (ut.ut_user, acct ? acct->name : "LOGIN", sizeof(ut.ut_user)-1);
struct timeval tv;
gettimeofday (&tv, NULL);
ut.ut_tv.tv_sec = tv.tv_sec;
ut.ut_tv.tv_usec = tv.tv_usec;
setutent();
pututline (&ut);
endutent();
updwtmp (_PATH_WTMP, &ut);
}