summaryrefslogtreecommitdiff
path: root/usr/src/cmd/ttymon/sttydefs.c
blob: dd19420c6a7e38f0bd979a79883bb057b189ef4e (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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved	*/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <termio.h>
#include <sys/stat.h>
#include <signal.h>
#include <stdarg.h>

#include "tmstruct.h"
#include "tmextern.h"
#include "ttymon.h"

static	int  nflg = 0;		/* -n seen */
static	int  iflg = 0;		/* -i seen */
static	int  fflg = 0;		/* -f seen */
static	int  lflg = 0;		/* -l seen */

static	void	usage(void);
static	void	check_ref(void);
static	void	add_entry(struct Gdef *) __NORETURN;
static	void	remove_entry(char *);
static	int	copy_file(FILE *, FILE *, int, int);
static	int	verify(char *, int);
static	FILE	*open_temp(char *);

/*
 *	sttydefs - add, remove or check entries in /etc/ttydefs
 *
 *	Usage:	   sttydefs -a ttylabel [-n nextlabel] [-i initail-flags]
 *			    [-f final-flags] [-b]
 *		   sttydefs -r ttylabel
 *		   sttydefs -l [ttylabel]
 *
 */

int
main(int argc, char *argv[])
{
	int c;			/* option letter */
	int errflg = 0;		/* error indicator */
	int  aflg = 0;		/* -a seen */
	int  bflg = 0;		/* -b seen */
	int	ret;
	const char *argtmp;
	char	*nextlabel;
	struct Gdef ttydef, *ptr;

	if (argc == 1)
		usage();

	/* Initialize ttydef structure */
	memset(&ttydef, 0, sizeof (ttydef));

	ptr = &ttydef;
	while ((c = getopt(argc, argv, "a:n:i:f:br:l")) != -1) {
		switch (c) {
		case 'a':
			aflg = TRUE;
			ptr->g_id = optarg;
			break;
		case 'n':
			nflg = TRUE;
			ptr->g_nextid = optarg;
			break;
		case 'i':
			iflg = TRUE;
			ptr->g_iflags = optarg;
			break;
		case 'f':
			fflg = TRUE;
			ptr->g_fflags = optarg;
			break;
		case 'b':
			bflg = TRUE;
			ptr->g_autobaud |= A_FLAG;
			break;
		case 'r':
			if ((argc > 3) || (optind < argc))
				usage();
			remove_entry(optarg);
			break;
		case 'l':
			lflg = TRUE;
			if (argc > 3)
				usage();
			if ((ret = check_version(TTYDEFS_VERS, TTYDEFS)) != 0) {
				if (ret != 2) {
					(void) fprintf(stderr,
					    "%s version number is incorrect "
					    "or missing.\n", TTYDEFS);
					exit(1);
				}
				(void) fprintf(stderr,
				    "sttydefs: can't open %s.\n", TTYDEFS);
				exit(1);
			}
			if (argv[optind] == NULL) {
				read_ttydefs(NULL, TRUE);
				printf("\n");
				check_ref();
			} else {
				if (argc == 3) { /* -l ttylabel */
					if (verify(argv[optind], 0) != 0) {
						errflg++;
						break;
					}
					argtmp = argv[optind];
				} else { /* -lttylabel */
					argtmp = argv[optind] + 2;
				}
				read_ttydefs(argtmp, TRUE);
				if (Ndefs == 0) {
					(void) fprintf(stderr,
					    "ttylabel <%s> not found.\n",
					    argtmp);
					exit(1);
				}
				nextlabel = Gdef[--Ndefs].g_nextid;
				Ndefs = 0;
				read_ttydefs(nextlabel, FALSE);
				if (Ndefs == 0) {
					(void) printf("\nWarning -- nextlabel "
					    "<%s> of <%s> does not reference "
					    "any existing ttylabel.\n",
					    nextlabel, argtmp);
				}
			}
			exit(0);
		case '?':
			errflg++;
			break;
		} /* end switch */
		if (errflg)
			usage();
	} /* end while */
	if (optind < argc)
		usage();

	if (aflg) {
		add_entry(ptr);		/* never return */
	}
	if ((iflg) || (fflg) || (bflg) || (nflg))
		usage();
	return (0);
}

/*
 *	verify	- to check if arg is valid
 *		- i.e. arg cannot start with '-' and
 *		  arg must not longer than maxarglen
 *		- return 0 if ok. Otherwise return -1
 */
static	int
verify(char *arg, int maxarglen)
{
	if (*arg == '-') {
		(void) fprintf(stderr, "Invalid argument -- %s.\n", arg);
		return (-1);
	}
	if ((maxarglen) && ((int)strlen(arg) > maxarglen)) {
		arg[maxarglen] = '\0';
		(void) fprintf(stderr, "string too long, truncated to %s.\n",
		    arg);
		return (-1);
	}
	return (0);
}

/*
 * usage - print out a usage message
 */

static	void
usage(void)
{
	(void) fprintf(stderr, "Usage:\tsttydefs -a ttylabel [-n nextlabel] "
	    "[-i initial-flags]\n\t\t [-f final-flags] [-b]\n");
	(void) fprintf(stderr, "\tsttydefs -r ttylabel\n");
	(void) fprintf(stderr, "\tsttydefs -l [ttylabel]\n");
	exit(2);
}

/*
 *	add_entry - add an entry to /etc/ttydefs
 */

static	void
add_entry(struct Gdef *ttydef)
{
	FILE *fp;
	int	errflg = 0;
	char tbuf[BUFSIZ], *tp;
	int  add_version = FALSE;

	if (getuid()) {
		(void) fprintf(stderr, "User not privileged for operation.\n");
		exit(1);
	}
	tp = tbuf;
	*tp = '\0';
	if ((fp = fopen(TTYDEFS, "r")) != NULL) {
		if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
			(void) fprintf(stderr,
			    "%s version number is incorrect or missing.\n",
			    TTYDEFS);
			exit(1);
		}
		if (find_label(fp, ttydef->g_id)) {
			(void) fclose(fp);
			(void) fprintf(stderr,
			    "Invalid request -- ttylabel <%s> already "
			    "exists.\n",
			    ttydef->g_id);
			exit(1);
		}
		(void) fclose(fp);
	} else  {
		add_version = TRUE;
	}
	if ((fp = fopen(TTYDEFS, "a+")) == NULL) {
		(void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
		    strerror(errno));
		exit(1);
	}

	if (add_version) {
		(void) fprintf(fp, "# VERSION=%d\n", TTYDEFS_VERS);
	}


	/* if optional fields are not provided, set to default */
	if (!iflg)
		ttydef->g_iflags = DEFAULT.g_iflags;
	else
		if (check_flags(ttydef->g_iflags) != 0)
			errflg++;
	if (!fflg)
		ttydef->g_fflags = DEFAULT.g_fflags;
	else
		if (check_flags(ttydef->g_fflags) != 0)
			errflg++;
	if (errflg)
		exit(1);

	if (!nflg)
		ttydef->g_nextid = ttydef->g_id;

	if (ttydef->g_autobaud & A_FLAG)  {
		(void) fprintf(fp, "%s:%s:%s:A:%s\n", ttydef->g_id,
		    ttydef->g_iflags, ttydef->g_fflags, ttydef->g_nextid);
	} else {
		(void) fprintf(fp, "%s:%s:%s::%s\n", ttydef->g_id,
		    ttydef->g_iflags, ttydef->g_fflags, ttydef->g_nextid);
	}
	(void) fclose(fp);
	exit(0);
}

static	void
remove_entry(char *ttylabel)
{
	FILE *tfp;		/* file pointer for temp file */
	int line;		/* line number entry is on */
	FILE *fp;		/* scratch file pointer */
	char *tname = "/etc/.ttydefs";

	if (getuid()) {
		(void) fprintf(stderr, "User not privileged for operation.\n");
		exit(1);
	}
	fp = fopen(TTYDEFS, "r");
	if (fp == NULL) {
		(void) fprintf(stderr, "Could not open \"%s\": %s", TTYDEFS,
		    strerror(errno));
		exit(1);
	}
	if (check_version(TTYDEFS_VERS, TTYDEFS) != 0) {
		(void) fprintf(stderr,
		    "%s version number is incorrect or missing.\n", TTYDEFS);
		exit(1);
	}
	if ((line = find_label(fp, ttylabel)) == 0) {
		(void) fprintf(stderr,
		    "Invalid request, ttylabel <%s> does not exist.\n",
		    ttylabel);
		exit(1);
	}
	tfp = open_temp(tname);
	if (line != 1)
		if (copy_file(fp, tfp, 1, line - 1)) {
			(void) fprintf(stderr, "Error accessing temp file.\n");
			exit(1);
		}
	if (copy_file(fp, tfp, line + 1, -1)) {
		(void) fprintf(stderr, "Error accessing temp file.\n");
		exit(1);
	}
	(void) fclose(fp);
	if (fclose(tfp) == EOF) {
		(void) unlink(tname);
		(void) fprintf(stderr, "Error closing temp file.\n");
		exit(1);
	}
	(void) unlink(TTYDEFS);
	if (rename(tname, TTYDEFS) != 0) {
		perror("Rename failed");
		(void) unlink(tname);
		exit(1);
	}
	exit(0);
}

/*
 * open_temp - open up a temp file
 *
 *	args:	tname - temp file name
 */

static	FILE *
open_temp(char *tname)
{
	FILE *fp;			/* fp associated with tname */
	struct sigaction sigact;	/* for signal handling */

	sigact.sa_flags = 0;
	sigact.sa_handler = SIG_IGN;
	(void) sigemptyset(&sigact.sa_mask);
	(void) sigaddset(&sigact.sa_mask, SIGHUP);
	(void) sigaddset(&sigact.sa_mask, SIGINT);
	(void) sigaddset(&sigact.sa_mask, SIGQUIT);
	(void) sigaction(SIGHUP, &sigact, NULL);
	(void) sigaction(SIGINT, &sigact, NULL);
	(void) sigaction(SIGQUIT, &sigact, NULL);
	(void) umask(0333);
	if (access(tname, 0) != -1) {
		(void) fprintf(stderr, "tempfile busy; try again later.\n");
		exit(1);
	}
	fp = fopen(tname, "w");
	if (fp == NULL) {
		perror("Cannot create tempfile");
		exit(1);
	}
	return (fp);
}

/*
 * copy_file - copy information from one file to another, return 0 on
 *	success, -1 on failure
 *
 *	args:	fp - source file's file pointer
 *		tfp - destination file's file pointer
 *		start - starting line number
 *		finish - ending line number (-1 indicates entire file)
 */


static	int
copy_file(FILE *fp, FILE *tfp, int start, int finish)
{
	int i;		/* loop variable */
	char dummy[BUFSIZ];	/* scratch buffer */

/*
 * always start from the beginning because line numbers are absolute
 */

	rewind(fp);

/*
 * get to the starting point of interest
 */

	if (start != 1) {
		for (i = 1; i < start; i++)
			if (!fgets(dummy, BUFSIZ, fp))
				return (-1);
	}

/*
 * copy as much as was requested
 */

	if (finish != -1) {
		for (i = start; i <= finish; i++) {
			if (!fgets(dummy, BUFSIZ, fp))
				return (-1);
			if (fputs(dummy, tfp) == EOF)
				return (-1);
		}
	} else {
		for (;;) {
			if (fgets(dummy, BUFSIZ, fp) == NULL) {
				if (feof(fp))
					break;
				else
					return (-1);
			}
			if (fputs(dummy, tfp) == EOF)
				return (-1);
		}
	}
	return (0);
}

/*
 *	check_ref	- to check if nextlabel are referencing
 *			  existing ttylabel
 */
static	void
check_ref(void)
{
	int	i;
	struct	Gdef	*np;

	np = &Gdef[0];
	for (i = 0; i < Ndefs; i++, np++) {
		if (find_def(np->g_nextid) == NULL) {
			(void) printf("Warning -- nextlabel <%s> of <%s> "
			    "does not reference any existing ttylabel.\n",
			    np->g_nextid, np->g_id);
		}
	}
}

/*
 *	log	- print a message to stdout
 */

void
log(const char *msg, ...)
{
	va_list ap;
	if (lflg) {
		va_start(ap, msg);
		(void) vprintf(msg, ap);
		va_end(ap);
		(void) printf("\n");
	} else {
		va_start(ap, msg);
		(void) vfprintf(stderr, msg, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}
}