File: mingetty.c

package info (click to toggle)
mingetty 1.07-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, sarge
  • size: 68 kB
  • ctags: 40
  • sloc: ansic: 352; makefile: 67
file content (445 lines) | stat: -rw-r--r-- 10,846 bytes parent folder | download
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*  mingetty.c
 *
 *  Copyright (C) 1996 Florian La Roche  <laroche@redhat.com>
 *  Copyright (C) 2002, 2003 Red Hat, Inc
 *
 *  This getty can only be used as a small console getty. Look at mgetty
 *  for a real modem getty.
 *
 *  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
 *  2 of the License, or (at your option) any later version.
 */

/* TODO:
 * - autologin only at first login
 * - /etc/mingetty.conf that can be used instead of /etc/inittab for
 *   command line options
 * - Can UTF-8 setup be done within mingetty?
 * - Also add /bin/login-type functionality in here?
 */

#define _GNU_SOURCE 1		       /* Needed to get setsid() */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <signal.h>
#include <fcntl.h>
#include <stdarg.h>
#include <ctype.h>
#include <utmp.h>
#include <getopt.h>
#include <sys/param.h>
#include <syslog.h>
#include <sys/utsname.h>
#include <time.h>

/* name of this program (argv[0]) */
static char *progname;
/* on which tty line are we sitting? (e.g. tty1) */
static char *tty;
/* some information about this host */
static struct utsname uts;
/* the hostname */
static char hn[MAXHOSTNAMELEN + 1];
/* process and session ID of this program */
static pid_t pid, sid;
/* login program invoked */
static char *loginprog = "/bin/login";
/* Do not send a reset string to the terminal. */
static int noclear = 0;
/* Do not print a newline. */
static int nonewline = 0;
/* Do not print /etc/issue. */
static int noissue = 0;
/* Do not call vhangup() on the tty. */
static int nohangup = 0;
/* Do not print any hostname. */
static int nohostname = 0;
/* Print the whole string of gethostname() instead of just until the next "." */
static int longhostname = 0;
/* time to wait, seconds */
static int delay = 0;
/* chroot directory */
static char *ch_root = NULL;
/* working directory to change into */
static char *ch_dir = NULL;
/* 'nice' level of the program */
static int priority = 0;
/* automatic login with this user */
static char *autologin = NULL;

/* error() - output error messages */
static void error (const char *fmt, ...)
{
	va_list va_alist;

	va_start (va_alist, fmt);
	openlog (progname, LOG_PID, LOG_AUTH);
	vsyslog (LOG_ERR, fmt, va_alist);
	/* no need, we exit anyway: closelog (); */
	va_end (va_alist);
	sleep (5);
	exit (EXIT_FAILURE);
}

/* update_utmp() - update our utmp entry */
static void update_utmp (void)
{
	struct utmp ut;
	struct utmp *utp;
	time_t cur_time;

	setutent ();
	while ((utp = getutent ()))
		if (utp->ut_type == INIT_PROCESS && utp->ut_pid == pid)
			break;

	if (utp) {
		memcpy (&ut, utp, sizeof (ut));
	} else {
		/* some inits don't initialize utmp... */
		const char *x = tty;
		memset (&ut, 0, sizeof (ut));
		if (strncmp (x, "tty", 3) == 0)
			x += 3;
		if (strlen (x) > sizeof (ut.ut_id))
			x += strlen (x) - sizeof (ut.ut_id);
		strncpy (ut.ut_id, x, sizeof (ut.ut_id));
	}

	strncpy (ut.ut_user, "LOGIN", sizeof (ut.ut_user));
	strncpy (ut.ut_line, tty, sizeof (ut.ut_line));
	time (&cur_time);
	ut.ut_time = cur_time;
	ut.ut_type = LOGIN_PROCESS;
	ut.ut_pid = pid;
	ut.ut_session = sid;

	pututline (&ut);
	endutent ();

	updwtmp (_PATH_WTMP, &ut);
}

/* open_tty - set up tty as standard { input, output, error } */
static void open_tty (void)
{
	struct sigaction sa, sa_old;
	char buf[40];
	int fd;

	/* Reset permissions on the console device */
	if ((strncmp(tty, "tty", 3) == 0) && (isdigit(tty[3]))) {
		strcpy (buf, "/dev/vcs");
		strcat (buf, &tty[3]);
		if (chown (buf, 0, 3) || chmod (buf, 0600))
			if (errno != ENOENT)
				error ("%s: %s", buf, strerror(errno));

		strcpy (buf, "/dev/vcsa");
		strcat (buf, &tty[3]);
		if (chown (buf, 0, 3) || chmod (buf, 0600))
			if (errno != ENOENT)
				error ("%s: %s", buf, strerror(errno));
	}

	/* Set up new standard input. */
	if (tty[0] == '/') {
		strncpy (buf, tty, sizeof(buf)-1);
		buf[sizeof(buf)-1] = '\0';
	} else {
		strcpy (buf, "/dev/");
		strncat (buf, tty, sizeof(buf)-strlen(buf)-1);
	}
	/* There is always a race between this reset and the call to
	   vhangup() that s.o. can use to get access to your tty. */
	if (chown (buf, 0, 0) || chmod (buf, 0600))
		if (errno != EROFS)
			error ("%s: %s", tty, strerror (errno));

	sa.sa_handler = SIG_IGN;
	sa.sa_flags = 0;
	sigemptyset (&sa.sa_mask);
	sigaction (SIGHUP, &sa, &sa_old);

	/* vhangup() will replace all open file descriptors in the kernel
	   that point to our controlling tty by a dummy that will deny
	   further reading/writing to our device. It will also reset the
	   tty to sane defaults, so we don't have to modify the tty device
	   for sane settings. We also get a SIGHUP/SIGCONT.
	 */
	if ((fd = open (buf, O_RDWR, 0)) < 0)
		error ("%s: cannot open tty: %s", tty, strerror (errno));
	if (ioctl (fd, TIOCSCTTY, (void *) 1) == -1)
		error ("%s: no controlling tty: %s", tty, strerror (errno));
	if (!isatty (fd))
		error ("%s: not a tty", tty);

	if (nohangup == 0) {
		if (vhangup ())
			error ("%s: vhangup() failed", tty);
		/* Get rid of the present stdout/stderr. */
		close (2);
		close (1);
		close (0);
		close (fd);
		if ((fd = open (buf, O_RDWR, 0)) != 0)
			error ("%s: cannot open tty: %s", tty,
				strerror (errno));
		if (ioctl (fd, TIOCSCTTY, (void *) 1) == -1)
			error ("%s: no controlling tty: %s", tty,
				strerror (errno));
	}
	/* Set up stdin/stdout/stderr. */
	if (dup2 (fd, 0) != 0 || dup2 (fd, 1) != 1 || dup2 (fd, 2) != 2)
		error ("%s: dup2(): %s", tty, strerror (errno));
	if (fd > 2)
		close (fd);

	/* Write a reset string to the terminal. This is very linux-specific
	   and should be checked for other systems. */
	if (noclear == 0)
		write (0, "\033c", 2);

	sigaction (SIGHUP, &sa_old, NULL);
}

static void output_special_char (unsigned char c)
{
	switch (c) {
	case 's':
		printf ("%s", uts.sysname);
		break;
	case 'n':
		printf ("%s", uts.nodename);
		break;
	case 'r':
		printf ("%s", uts.release);
		break;
	case 'v':
		printf ("%s", uts.version);
		break;
	case 'm':
		printf ("%s", uts.machine);
		break;
	case 'o':
		printf ("%s", uts.domainname);
		break;
	case 'd':
	case 't':
		{
			time_t cur_time;
			struct tm *tm;
#if	0
			char buff[20];

			time (&cur_time);
			tm = localtime (&cur_time);
			strftime (buff, sizeof (buff),
				c == 'd'? "%a %b %d %Y" : "%X", tm);
			fputs (buff, stdout);
			break;
#else
			time (&cur_time);
			tm = localtime (&cur_time);
			if (c == 'd') /* ISO 8601 */
				printf ("%d-%02d-%02d", 1900 + tm->tm_year,
					tm->tm_mon + 1, tm->tm_mday);
			else
				printf ("%02d:%02d:%02d", tm->tm_hour,
					tm->tm_min, tm->tm_sec);
			break;
#endif
		}

	case 'l':
		printf ("%s", tty);
		break;
	case 'u':
	case 'U':
		{
			int users = 0;
			struct utmp *ut;
			setutent ();
			while ((ut = getutent ()))
				if (ut->ut_type == USER_PROCESS)
					users++;
			endutent ();
			printf ("%d", users);
			if (c == 'U')
				printf (" user%s", users == 1 ? "" : "s");
			break;
		}
	default:
		putchar (c);
	}
}

/* do_prompt - show login prompt, optionally preceded by /etc/issue contents */
static void do_prompt (int showlogin)
{
	FILE *fd;
	int c;

	if (nonewline == 0)
		putchar ('\n');
	if (noissue == 0 && (fd = fopen ("/etc/issue", "r"))) {
		while ((c = getc (fd)) != EOF) {
			if (c == '\\')
				output_special_char (getc (fd));
			else
				putchar (c);
		}
		fclose (fd);
	}
	if (nohostname == 0)
		printf ("%s ", hn);
	if (showlogin)
		printf ("login: ");
	fflush (stdout);
}

static char *get_logname (void)
{
	static char logname[40];
	char *bp;
	unsigned char c;

	tcflush (0, TCIFLUSH);		/* flush pending input */
	for (*logname = 0; *logname == 0;) {
		do_prompt (1);
		for (bp = logname;;) {
			if (read (0, &c, 1) < 1) {
				if (errno == EINTR || errno == EIO
					|| errno == ENOENT)
					exit (EXIT_SUCCESS);
				error ("%s: read: %s", tty, strerror (errno));
			}
			if (c == '\n' || c == '\r') {
				*bp = 0;
				break;
			} else if (!isprint (c))
				error ("%s: invalid character 0x%x in login"
					" name", tty, c);
			else if ((size_t)(bp - logname) >= sizeof (logname) - 1)
				error ("%s: too long login name", tty);
			else
				*bp++ = c;
		}
	}
	return logname;
}

static void usage (void)
{
	error ("usage: '%s [--noclear] [--nonewline] [--noissue] "
		"[--nohangup] [--nohostname] [--long-hostname] "
		"[--loginprog=/bin/login] [--nice=10] [--delay=10] "
		"[--chdir=/home] [--chroot=/chroot] [--autologin=user] "
		"tty' with e.g. tty=tty1", progname);
}

static struct option const long_options[] = {
	{ "autologin", required_argument, NULL, 'a' },
	{ "chdir", required_argument, NULL, 'w' },
	{ "chroot", required_argument, NULL, 'r' },
	{ "delay", required_argument, NULL, 'd' },
	{ "noclear", no_argument, &noclear, 1 },
	{ "nonewline", no_argument, &nonewline, 1 },
	{ "noissue", no_argument, &noissue, 1 },
	{ "nohangup", no_argument, &nohangup, 1 },
	{ "no-hostname", no_argument, &nohostname, 1 }, /* compat option */
	{ "nohostname", no_argument, &nohostname, 1 },
	{ "loginprog", required_argument, NULL, 'l' },
	{ "long-hostname", no_argument, &longhostname, 1 },
	{ "nice", required_argument, NULL, 'n' },
	{ 0, 0, 0, 0 }
};

int main (int argc, char **argv)
{
	char *logname, *s;
	int c;

	progname = argv[0];
	if (!progname)
		progname = "mingetty";
	uname (&uts);
	gethostname (hn, MAXHOSTNAMELEN);
	hn[MAXHOSTNAMELEN] = '\0';
	pid = getpid ();
	sid = getsid (0);
#if	defined(s390) || defined(__s390__)
	putenv ("TERM=dumb");
#else
	putenv ("TERM=linux");
#endif

	while ((c = getopt_long (argc, argv, "a:d:l:n:w:r:", long_options,
		(int *) 0)) != EOF) {
		switch (c) {
		case 0:
			break;
		case 'a':
			autologin = optarg;
			break;
		case 'd':
			delay = atoi (optarg);
			break;
		case 'l':
			loginprog = optarg;
			break;
		case 'n':
			priority = atoi (optarg);
			break;
		case 'r':
			ch_root = optarg;
			break;
		case 'w':
			ch_dir = optarg;
			break;
		default:
			usage ();
		}
	}
	if (longhostname == 0 && (s = strchr (hn, '.')))
		*s = '\0';
	tty = argv[optind];
	if (!tty)
		usage ();

	if (strncmp (tty, "/dev/", 5) == 0) /* ignore leading "/dev/" */
		tty += 5;

	update_utmp ();
	if (delay)
		sleep (delay);
	open_tty ();
	if (autologin) {
		do_prompt (0);
		printf ("login: %s (automatic login)\n", autologin);
		logname = autologin;
	} else
		while ((logname = get_logname ()) == 0)
			/* do nothing */ ;

	if (ch_root)
		chroot (ch_root);
	if (ch_dir)
		chdir (ch_dir);
	if (priority)
		nice (priority);

	execl (loginprog, loginprog, autologin? "-f" : "--", logname, NULL);
	error ("%s: can't exec %s: %s", tty, loginprog, strerror (errno));
	sleep (5);
	exit (EXIT_FAILURE);
}