xref: /openbsd/regress/usr.bin/ssh/modpipe.c (revision 73471bf0)
1 /*
2  * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 /* $OpenBSD: modpipe.c,v 1.6 2013/11/21 03:16:47 djm Exp $ */
18 
19 #include <sys/types.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <err.h>
25 #include <errno.h>
26 
27 static void
28 usage(void)
29 {
30 	fprintf(stderr, "Usage: modpipe -w [-m modspec ...] < in > out\n");
31 	fprintf(stderr, "modspec is one of:\n");
32 	fprintf(stderr, "    xor:offset:value       - XOR \"value\" at \"offset\"\n");
33 	fprintf(stderr, "    andor:offset:val1:val2 - AND \"val1\" then OR \"val2\" at \"offset\"\n");
34 	exit(1);
35 }
36 
37 #define MAX_MODIFICATIONS 256
38 struct modification {
39 	enum { MOD_XOR, MOD_AND_OR } what;
40 	unsigned long long offset;
41 	u_int8_t m1, m2;
42 };
43 
44 static void
45 parse_modification(const char *s, struct modification *m)
46 {
47 	char what[16+1];
48 	int n, m1, m2;
49 
50 	bzero(m, sizeof(*m));
51 	if ((n = sscanf(s, "%16[^:]%*[:]%llu%*[:]%i%*[:]%i",
52 	    what, &m->offset, &m1, &m2)) < 3)
53 		errx(1, "Invalid modification spec \"%s\"", s);
54 	if (strcasecmp(what, "xor") == 0) {
55 		if (n > 3)
56 			errx(1, "Invalid modification spec \"%s\"", s);
57 		if (m1 < 0 || m1 > 0xff)
58 			errx(1, "Invalid XOR modification value");
59 		m->what = MOD_XOR;
60 		m->m1 = m1;
61 	} else if (strcasecmp(what, "andor") == 0) {
62 		if (n != 4)
63 			errx(1, "Invalid modification spec \"%s\"", s);
64 		if (m1 < 0 || m1 > 0xff)
65 			errx(1, "Invalid AND modification value");
66 		if (m2 < 0 || m2 > 0xff)
67 			errx(1, "Invalid OR modification value");
68 		m->what = MOD_AND_OR;
69 		m->m1 = m1;
70 		m->m2 = m2;
71 	} else
72 		errx(1, "Invalid modification type \"%s\"", what);
73 }
74 
75 int
76 main(int argc, char **argv)
77 {
78 	int ch;
79 	u_char buf[8192];
80 	size_t total;
81 	ssize_t r, s, o;
82 	struct modification mods[MAX_MODIFICATIONS];
83 	u_int i, wflag = 0, num_mods = 0;
84 
85 	while ((ch = getopt(argc, argv, "wm:")) != -1) {
86 		switch (ch) {
87 		case 'm':
88 			if (num_mods >= MAX_MODIFICATIONS)
89 				errx(1, "Too many modifications");
90 			parse_modification(optarg, &(mods[num_mods++]));
91 			break;
92 		case 'w':
93 			wflag = 1;
94 			break;
95 		default:
96 			usage();
97 			/* NOTREACHED */
98 		}
99 	}
100 	for (total = 0;;) {
101 		r = s = read(STDIN_FILENO, buf, sizeof(buf));
102 		if (r == 0)
103 			break;
104 		if (r < 0) {
105 			if (errno == EAGAIN || errno == EINTR)
106 				continue;
107 			err(1, "read");
108 		}
109 		for (i = 0; i < num_mods; i++) {
110 			if (mods[i].offset < total ||
111 			    mods[i].offset >= total + s)
112 				continue;
113 			switch (mods[i].what) {
114 			case MOD_XOR:
115 				buf[mods[i].offset - total] ^= mods[i].m1;
116 				break;
117 			case MOD_AND_OR:
118 				buf[mods[i].offset - total] &= mods[i].m1;
119 				buf[mods[i].offset - total] |= mods[i].m2;
120 				break;
121 			}
122 		}
123 		for (o = 0; o < s; o += r) {
124 			r = write(STDOUT_FILENO, buf, s - o);
125 			if (r == 0)
126 				break;
127 			if (r < 0) {
128 				if (errno == EAGAIN || errno == EINTR)
129 					continue;
130 				err(1, "write");
131 			}
132 		}
133 		total += s;
134 	}
135 	/* Warn if modifications not reached in input stream */
136 	r = 0;
137 	for (i = 0; wflag && i < num_mods; i++) {
138 		if (mods[i].offset < total)
139 			continue;
140 		r = 1;
141 		fprintf(stderr, "modpipe: warning - mod %u not reached\n", i);
142 	}
143 	return r;
144 }
145