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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
include $(SRC)/lib/Makefile.lib
SUBDIRS = $(MACH64)
all: TARGET= all
clean: TARGET= clean
clobber: TARGET= clobber
install: TARGET= install
HDRDIR= common
HDRS= utempter.h
.KEEP_STATE:
all clean clobber install: $(SUBDIRS)
check: $(CHECKHDRS)
install_h: $(ROOTHDRS)
$(SUBDIRS): FRC
@cd $@; pwd; $(MAKE) $(TARGET)
FRC:
include $(SRC)/lib/Makefile.targ
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
LIBRARY= libutempter.a
VERS= .1
OBJECTS= utempter.o
include $(SRC)/lib/Makefile.lib
SRCDIR= ../common
LDLIBS += -lc
LIBS= $(DYNLIB)
CSTD= $(CSTD_GNU99)
.KEEP_STATE:
all: $(LIBS)
include $(SRC)/lib/Makefile.targ
Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
UTEMPTER LIBRARY
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
include ../Makefile.com
include ../../Makefile.lib.64
install: all $(ROOTLIBS64) $(ROOTLINKS64)
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
#
# MAPFILE HEADER START
#
# WARNING: STOP NOW. DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
# usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#
$mapfile_version 2
SYMBOL_VERSION ILLUMOS_0.1 {
global:
addToUtmp;
removeFromUtmp;
removeLineFromUtmp;
utempter_add_record;
utempter_remove_added_record;
utempter_remove_record;
local:
*;
};
/*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <utmpx.h>
static int last_fd = -1;
static void
utempter_update_utmpx(int type, int fd, const char *host)
{
struct utmpx ut;
struct passwd *pw;
uid_t uid;
(void) memset(&ut, 0, sizeof (ut));
ut.ut_type = type;
ut.ut_pid = getpid();
ut.ut_session = getsid(0);
(void) gettimeofday(&ut.ut_tv, NULL);
if (snprintf(ut.ut_id, sizeof (ut.ut_id), "f%d", fd) >=
sizeof (ut.ut_id))
return;
uid = getuid();
if ((pw = getpwuid(uid)) == NULL)
return;
(void) strlcpy(ut.ut_user, pw->pw_name, sizeof (ut.ut_user));
if (type == DEAD_PROCESS) {
struct utmpx *ut1;
struct utmpx ut2;
(void) memset(&ut2, 0, sizeof (ut2));
ut2.ut_type = USER_PROCESS;
if (snprintf(ut2.ut_id, sizeof (ut2.ut_id), "f%d", fd) >=
sizeof (ut2.ut_id))
return;
if ((ut1 = getutxid(&ut2)) == NULL)
return;
(void) strlcpy(ut.ut_line, ut1->ut_line, sizeof (ut.ut_line));
} else {
char *line = ptsname(fd);
if (line == NULL)
return;
(void) strlcpy(ut.ut_line, line + strlen("/dev/"),
sizeof (ut.ut_line));
}
if (host != NULL) {
(void) strlcpy(ut.ut_host, host, sizeof (ut.ut_host));
ut.ut_syslen = strlen(ut.ut_host) + 1;
}
setutxent();
(void) pututxline(&ut);
endutxent();
}
int
utempter_add_record(int fd, const char *host)
{
utempter_update_utmpx(USER_PROCESS, fd, host);
last_fd = fd;
return (0);
}
int
utempter_remove_record(int fd)
{
utempter_update_utmpx(DEAD_PROCESS, fd, NULL);
if (last_fd == fd)
last_fd = -1;
return (0);
}
int
utempter_remove_added_record(void)
{
if (last_fd < 0)
return (0);
utempter_update_utmpx(DEAD_PROCESS, last_fd, NULL);
last_fd = -1;
return (0);
}
void
addToUtmp(const char *pty __unused, const char *host, int fd)
{
(void) utempter_add_record(fd, host);
}
void
removeFromUtmp(void)
{
(void) utempter_remove_added_record();
}
void
removeLineFromUtmp(const char *pty __unused, int fd)
{
(void) utempter_remove_record(fd);
}
/*
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef _UTEMPTER_H
#define _UTEMPTER_H
#ifdef __cplusplus
extern "C" {
#endif
int utempter_add_record(int, const char *);
int utempter_remove_added_record(void);
int utempter_remove_record(int);
void addToUtmp(const char *, const char *, int);
void removeFromUtmp(void);
void removeLineFromUtmp(const char *, int);
#ifdef __cplusplus
}
#endif
#endif /* !_UTEMPTER_H */
|