summaryrefslogtreecommitdiff
path: root/src/utmp.c
blob: 53778647aa7e4c8e620ba0c24c767cdeeeed7b06 (plain)
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Copyright (c) 2008, 2009
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 *      Micah Cowan (micah@cowan.name)
 *      Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
 *      Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
 *      Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
 * Copyright (c) 1987 Oliver Laumann
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see the file COPYING); if not, see
 * https://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 ****************************************************************
 */

#include "config.h"

#include "utmp.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdbool.h>

#include "screen.h"

#ifdef HAVE_UTEMPTER
#include <utempter.h>
#endif

#include "misc.h"
#include "tty.h"
#include "winmsg.h"

/*
 *  we have a suid-root helper app that changes the utmp for us
 *  (won't work for login-slots)
 */
#if defined(HAVE_UTEMPTER)
#define UTMP_HELPER
#endif

#ifdef ENABLE_UTMP

static slot_t TtyNameSlot(char *);
static void makeuser(struct utmpx *, char *, char *, pid_t);
static void makedead(struct utmpx *);
static int pututslot(slot_t, struct utmpx *, char *, Window *);
static struct utmpx *getutslot(slot_t);

static int utmpok;
static char UtmpName[] = UTMPXFILE;
#ifndef UTMP_HELPER
static int utmpfd = -1;
#endif

#undef  D_loginhost
#define D_loginhost D_utmp_logintty.ut_host
#if !defined(HAVE_UT_HOST)
#undef  D_loginhost
#define D_loginhost (NULL)
#endif

#endif				/* ENABLE_UTMP */

/*
 * SlotToggle - modify the utmp slot of the fore window.
 *
 * how == true  try to set a utmp slot.
 * how == false try to withdraw a utmp slot.
 *
 * w_slot = -1  window not logged in.
 * w_slot = 0   window not logged in, but should be logged in.
 *              (unable to write utmp, or detached).
 */

#ifndef ENABLE_UTMP
void SlotToggle(bool how)
{
	(void)how; /* unused */
#ifdef UTMPXFILE
	Msg(0, "Unable to modify %s.\n", UTMPXFILE);
#else
	Msg(0, "Unable to modify utmp-database.\n");
#endif
}
#endif

#ifdef ENABLE_UTMP

void SlotToggle(bool how)
{
	if (fore->w_type != W_TYPE_PTY) {
		Msg(0, "Can only work with normal windows.\n");
		return;
	}
	if (how) {
		if ((fore->w_slot == (slot_t) - 1) || (fore->w_slot == (slot_t) 0)) {
			if (SetUtmp(fore) == 0)
				Msg(0, "This window is now logged in.");
			else
				Msg(0, "This window should now be logged in.");
			WindowChanged(fore, WINESC_WFLAGS);
		} else
			Msg(0, "This window is already logged in.");
	} else {
		if (fore->w_slot == (slot_t) - 1)
			Msg(0, "This window is already logged out\n");
		else if (fore->w_slot == (slot_t) 0) {
			Msg(0, "This window is not logged in.");
			fore->w_slot = (slot_t) - 1;
		} else {
			RemoveUtmp(fore);
			if (fore->w_slot != (slot_t) - 1)
				Msg(0, "What? Cannot remove Utmp slot?");
			else
				Msg(0, "This window is no longer logged in.");
#ifdef CAREFULUTMP
			CarefulUtmp();
#endif
			WindowChanged(fore, WINESC_WFLAGS);
		}
	}
}

#ifdef CAREFULUTMP

/* CAREFULUTMP: goodie for paranoid sysadmins: always leave one
 * window logged in
 */
void CarefulUtmp()
{
	Window *p;

	if (!mru_window)		/* hopeless */
		return;
	for (p = mru_window; p; p = p->w_prev_mru)
		if (p->w_ptyfd >= 0 && p->w_slot != (slot_t)-1)
			return;	/* found one, nothing to do */

	for (p = mru_window; p; p = p->w_prev_mru)
		if (p->w_ptyfd >= 0)	/* no zombies please */
			break;
	if (!p)
		return;		/* really hopeless */
	SetUtmp(p);
	Msg(0, "Window %d is now logged in.\n", p->w_number);
}
#endif				/* CAREFULUTMP */

void InitUtmp(void)
{
#ifndef UTMP_HELPER
	if ((utmpfd = open(UtmpName, O_RDWR)) == -1) {
		if (errno != EACCES)
			Msg(errno, "%s", UtmpName);
		utmpok = 0;
		return;
	}
	close(utmpfd);		/* it was just a test */
	utmpfd = -1;
#endif				/* UTMP_HELPER */
	utmpok = 1;
}

/*
 * the utmp entry for tty is located and removed.
 * it is stored in D_utmp_logintty.
 */
void RemoveLoginSlot(void)
{
	struct utmpx u, *uu;

	D_loginslot = TtyNameSlot(D_usertty);
	if (D_loginslot == (slot_t) 0 || D_loginslot == (slot_t) - 1)
		return;
#ifdef UTMP_HELPER
	if (eff_uid)		/* helpers can't do login slots. sigh. */
#else
	if (!utmpok)
#endif
	{
		D_loginslot = NULL;
	} else {
		if ((uu = getutslot(D_loginslot)) == NULL) {
			D_loginslot = NULL;
		} else {
			D_utmp_logintty = *uu;
			u = *uu;
			makedead(&u);
			if (pututslot(D_loginslot, &u, NULL, NULL) == 0)
				D_loginslot = NULL;
		}
	}
	if (D_loginslot == (slot_t) 0) {
		/* couldn't remove slot, do a 'mesg n' at least. */
		struct stat stb;
		char *tty;
		D_loginttymode = 0;
		if ((tty = GetPtsPathOrSymlink(D_userfd)) && stat(tty, &stb) == 0 && stb.st_uid == real_uid && !CheckTtyname(tty)
		    && ((int)stb.st_mode & 0777) != 0666) {
			D_loginttymode = (int)stb.st_mode & 0777;
			chmod(D_usertty, stb.st_mode & 0600);
		}
	}
}

/*
 * D_utmp_logintty is reinserted into utmp
 */
void RestoreLoginSlot(void)
{
	char *tty;

	if (utmpok && D_loginslot != (slot_t) 0 && D_loginslot != (slot_t) - 1) {
		if (pututslot(D_loginslot, &D_utmp_logintty, D_loginhost, NULL) == 0)
			Msg(errno, "Could not write %s", UtmpName);
	}
	D_loginslot = (slot_t) 0;
	if (D_loginttymode && (tty = GetPtsPathOrSymlink(D_userfd)) && !CheckTtyname(tty))
		fchmod(D_userfd, D_loginttymode);
}

/*
 * Construct a utmp entry for window wi.
 * the hostname field reflects what we know about the user (display)
 * location. If d_loginhost is not set, then he is local and we write
 * down the name of his terminal line; else he is remote and we keep
 * the hostname here. The letter S and the window id will be appended.
 * A saved utmp entry in wi->w_savut serves as a template, usually.
 */

int SetUtmp(Window *win)
{
	slot_t slot;
	struct utmpx u = { 0 };
	int saved_ut;
#if defined(HAVE_UT_HOST)
	char host[ARRAY_SIZE(u.ut_host)];
#else
	char *host = NULL;
#endif

	win->w_slot = (slot_t) 0;
	if (!utmpok || win->w_type != W_TYPE_PTY)
		return -1;
	if ((slot = TtyNameSlot(win->w_tty)) == (slot_t) 0) {
		return -1;
	}

	if ((saved_ut = memcmp((char *)&win->w_savut, (char *)&u, sizeof(struct utmpx))))
		/* restore original, of which we will adopt all fields but ut_host */
		memmove((char *)&u, (char *)&win->w_savut, sizeof(struct utmpx));

	if (!saved_ut)
		makeuser(&u, stripdev(win->w_tty), LoginName, win->w_pid);

#if defined(HAVE_UT_HOST)
	if (display) {
		snprintf(host, ARRAY_SIZE(host), "%s", D_loginhost);
		if (D_loginslot == (slot_t)0 || D_loginslot == (slot_t)-1 || host[0] == '\0')
			snprintf(host, ARRAY_SIZE(host), ":%s", stripdev(D_usertty));
	} else
		snprintf(host, ARRAY_SIZE(host), "local");

	snprintf(host + strlen(host), ARRAY_SIZE(host) - strlen(host), ":S.%d", win->w_number);

	memcpy(u.ut_host, host, ARRAY_SIZE(u.ut_host));
#endif

	if (pututslot(slot, &u, host, win) == 0) {
		Msg(errno, "Could not write %s", UtmpName);
		return -1;
	}
	win->w_slot = slot;
	memmove((char *)&win->w_savut, (char *)&u, sizeof(struct utmpx));
	return 0;
}

/*
 * if slot could be removed or was 0,  win->w_slot = -1;
 * else not changed.
 */

int RemoveUtmp(Window *win)
{
	struct utmpx u = { 0 };
	struct utmpx *uu;
	slot_t slot;

	slot = win->w_slot;
	if (!utmpok)
		return -1;
	if (slot == (slot_t)0 || slot == (slot_t)-1) {
		win->w_slot = (slot_t)-1;
		return 0;
	}
	if ((uu = getutslot(slot)) == NULL) {
		Msg(0, "Utmp slot not found -> not removed");
		return -1;
	}
	memmove((char *)&win->w_savut, (char *)uu, sizeof(struct utmpx));
	u = *uu;
	makedead(&u);
	if (pututslot(slot, &u, NULL, win) == 0) {
		Msg(errno, "Could not write %s", UtmpName);
		return -1;
	}
	win->w_slot = (slot_t)-1;
	return 0;
}

/*********************************************************************
 *
 *  routines using the getut* api
 */

#define SLOT_USED(u) (u->ut_type == USER_PROCESS)

static struct utmpx *getutslot(slot_t slot)
{
	struct utmpx u = { 0 };
	memcpy(u.ut_line, (char *)slot, ARRAY_SIZE(u.ut_line));
	setutxent();
	return getutxline(&u);
}

static int pututslot(slot_t slot, struct utmpx *u, char *host, Window *win)
{
	(void)slot; /* unused */
#ifdef HAVE_UTEMPTER
	if (eff_uid && win && win->w_ptyfd != -1) {
		/* sigh, linux hackers made the helper functions void */
		if (SLOT_USED(u))
			utempter_add_record(win->w_ptyfd, host);
		else
			utempter_remove_record(win->w_ptyfd);
		/*
		 * As documented in libutempter: "During execution of
		 * the privileged process spawned by these functions,
		 * SIGCHLD signal handler will be temporarily set to
		 * the default action." Thus in case a SIGCHLD has
		 * been lost, we send a SIGCHLD to oneself in order to
		 * avoid zombies: https://savannah.gnu.org/bugs/?25089
		 */
		kill(getpid(), SIGCHLD);

		return 1;	/* pray for success */
	}
#else
	(void)host; /* unused */
	(void)win; /* unused */
#endif

	setutxent();
	return pututxline(u) != NULL;
}

static void makedead(struct utmpx *u)
{
	u->ut_type = DEAD_PROCESS;
#if defined(HAVE_UT_EXIT)
	u->ut_exit.e_termination = 0;
	u->ut_exit.e_exit = 0;
#endif
	u->ut_user[0] = 0;	/* for Digital UNIX, kilbi@rad.rwth-aachen.de */
}

static void makeuser(struct utmpx *u, char *line, char *user, pid_t pid)
{
	time_t now;
	u->ut_type = USER_PROCESS;
	memcpy(u->ut_user, user, ARRAY_SIZE(u->ut_user));
	/* Now the tricky part... guess ut_id */
	memcpy(u->ut_id, line + 3, ARRAY_SIZE(u->ut_id));
	memcpy(u->ut_line, line, ARRAY_SIZE(u->ut_line));
	u->ut_pid = pid;
	/* must use temp variable because of NetBSD/sparc64, where
	 * ut_xtime is long(64) but time_t is int(32) */
	(void)time(&now);
	u->ut_tv.tv_sec = now;
}

static slot_t TtyNameSlot(char *name)
{
	return stripdev(name);
}

#endif				/* ENABLE_UTMP */