1 /*
2 * Copyright © 2021 Manuel Stoeckl
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
usage()32 static int usage()
33 {
34 fprintf(stderr, "usage: fake_ssh [-R A:B] [-t] destination command...\n");
35 return EXIT_FAILURE;
36 }
37
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40 if (argc < 2) {
41 return usage();
42 }
43 argv++;
44 argc--;
45
46 bool pseudoterminal = false;
47 char *link = NULL;
48 char *destination = NULL;
49 while (argc > 0) {
50 if (strcmp(argv[0], "-t") == 0) {
51 pseudoterminal = true;
52 argv++;
53 argc--;
54 } else if (strcmp(argv[0], "-R") == 0) {
55 link = argv[1];
56 argv += 2;
57 argc -= 2;
58 } else {
59 destination = argv[0];
60 argv++;
61 argc--;
62 break;
63 }
64 }
65
66 if (link) {
67 char *p1 = link, *p2 = NULL;
68 for (char *c = link; *c; c++) {
69 if (*c == ':') {
70 *c = '\0';
71 p2 = c + 1;
72 break;
73 }
74 }
75 if (!p2) {
76 fprintf(stderr, "Failed to split forwarding descriptor '%s'\n",
77 p1);
78 return EXIT_FAILURE;
79 }
80 unlink(p1);
81 if (symlink(p2, p1) == -1) {
82 fprintf(stderr, "Symlinking '%s' to '%s' failed\n", p2,
83 p1);
84 return EXIT_FAILURE;
85 }
86 }
87 (void)destination;
88 (void)pseudoterminal;
89
90 if (execvp(argv[0], argv) == -1) {
91 fprintf(stderr, "Failed to run program '%s'\n", argv[0]);
92 return EXIT_FAILURE;
93 }
94
95 return EXIT_SUCCESS;
96 }
97