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 478
|
/*
20140124
20241209 - reformated using clang-format
Jan Mojzis
Public domain.
The 'channel' library is used to handle data from/to SSH channel (rfc4254).
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <paths.h>
extern char *ptsname(int);
#include "byte.h"
#include "bug.h"
#include "newenv.h"
#include "e.h"
#include "purge.h"
#include "connectioninfo.h"
#include "iptostr.h"
#include "porttostr.h"
#include "buf.h"
#include "str.h"
#include "logsys.h"
#include "loginshell.h"
#include "trymlock.h"
#include "limit.h"
#include "channel.h"
/*
1. channel not open: maxpacket == 0, pid == 0
2. channel open: maxpacket != 0, pid == 0
3. child executed: maxpacket != 0, pid > 0, fd[01] != -1
4. child eof: maxpacket != 0, pid > 0, fd[12] == -1
5. child died: maxpacket != 0, pid == -1
*/
struct channel channel = {0};
/*
The 'channel_open' function opens the channel.
It sets 'localwindow' and maxpacket, values obtained from
from SSH_MSG_CHANNEL_OPEN message.
Function also obtaines connection information and sets
environment variables PATH, SSH_CONNECTION and MAIL.
*/
int channel_open(const char *user, crypto_uint32 id, crypto_uint32 remotewindow,
crypto_uint32 maxpacket, crypto_uint32 *localwindow) {
struct buf b = {channel.buf0, 0, CHANNEL_BUFSIZE};
if (!localwindow) bug_inval();
if (!maxpacket) bug_inval();
if (!remotewindow) bug_inval();
if (channel.maxpacket != 0) return 0;
if (channel.pid != 0) return 0;
/* copy user-name */
if (!str_copyn(channel.user, sizeof channel.user, user)) bug_nomem();
/* set id, maxpacket, remotewindow, localwindow */
channel.id = id;
channel.maxpacket = maxpacket;
channel.remotewindow = remotewindow;
channel.localwindow = *localwindow = CHANNEL_BUFSIZE;
/* copy PATH */
if (!newenv_copyenv("PATH")) {
if (!newenv_env("PATH", "/bin:/usr/bin")) return 0;
}
/* create env. SSH_CONNECTION */
connectioninfo(channel.localip, channel.localport, channel.remoteip,
channel.remoteport);
buf_purge(&b);
buf_puts(&b, channel.remoteip);
buf_puts(&b, " ");
buf_puts(&b, channel.remoteport);
buf_puts(&b, " ");
buf_puts(&b, channel.localip);
buf_puts(&b, " ");
buf_puts(&b, channel.localport);
buf_putnum8(&b, 0);
if (!newenv_env("SSH_CONNECTION", (char *) b.buf)) return 0;
#ifdef _PATH_MAILDIR
/* create env. MAIL */
buf_purge(&b);
buf_puts(&b, _PATH_MAILDIR);
buf_puts(&b, "/");
buf_puts(&b, user);
buf_putnum8(&b, 0);
if (!newenv_env("MAIL", (char *) b.buf)) return 0;
#endif
purge(channel.buf0, sizeof channel.buf0);
return 1;
}
/*
The 'channel_openterminal' function opens terminal,
sets environment variable TERM and initial terminal windowsize.
*/
int channel_openterminal(const char *name, crypto_uint32 a, crypto_uint32 b,
crypto_uint32 x, crypto_uint32 y) {
if (channel.maxpacket == 0) bug_proto();
if (channel.pid != 0) bug_proto();
if (channel.flagterminal == 1) bug_proto();
if (!channel_openpty(&channel.master, &channel.slave)) return 0;
if (!newenv_env("TERM", name)) return 0;
channel.flagterminal = 1;
channel.a = a;
channel.b = b;
channel.x = x;
channel.y = y;
return 1;
}
/*
The 'channel_ptyresize' function sets new
terminal windowsize.
*/
void channel_ptyresize(crypto_uint32 a, crypto_uint32 b, crypto_uint32 x,
crypto_uint32 y) {
struct winsize w;
if (channel.maxpacket == 0) bug_proto();
if (channel.pid <= 0) bug_proto();
if (!a && !b && !x && !y) return;
w.ws_col = a;
w.ws_row = b;
w.ws_xpixel = x;
w.ws_ypixel = y;
ioctl(channel.fd0, TIOCSWINSZ, &w);
}
/*
The 'channel_env' adds new environment variable
sent from client.
*/
int channel_env(const char *x, const char *y) {
if (channel.maxpacket == 0) bug_proto();
if (channel.pid != 0) bug_proto();
if (!x || !y) bug_inval();
return newenv_lowenv(x, y);
}
/*
The 'channel_exec' function executes new process.
If terminal is requsted, than users shell is executed,
if exec is requested, than command 'cmd' is executed.
Process is executed under appropriate users UID.
*/
int channel_exec(const char *cmd) {
char *run[4];
char *shell;
char *name;
int fd[3];
char ln[NAME_MAX + 2];
if (channel.maxpacket == 0) bug_proto();
if (channel.pid != 0) bug_proto();
if (channel.flagterminal) {
channel.pid = channel_forkpty(fd, channel.master, channel.slave);
if (channel.pid > 0) {
name = ptsname(fd[0]);
if (!name) bug();
if (!str_copyn(channel.termname, sizeof channel.termname, name))
bug_nomem();
}
}
else { channel.pid = channel_fork(fd); }
if (channel.pid == -1) return 0;
if (channel.pid == 0) {
logsys_login(channel.user, channel.remoteip, 0, 0);
if (!channel_droppriv(channel.user, &shell)) _exit(111);
if (cmd) {
run[0] = shell;
run[1] = (char *) "-c";
run[2] = (char *) cmd;
run[3] = 0;
}
else {
if (!loginshell(ln, sizeof ln, shell)) bug();
run[0] = ln;
run[1] = 0;
}
signal(SIGPIPE, SIG_DFL);
newenv_exec(shell, run);
_exit(111);
}
channel.fd0 = fd[0];
channel.fd1 = fd[1];
channel.fd2 = fd[2];
channel.len0 = 0;
newenv_purge();
if (channel.flagterminal && channel.pid > 0) {
channel_ptyresize(channel.a, channel.b, channel.x, channel.y);
}
return 1;
}
/*
The 'channel_put' function adds data from
client to childs buffer.
*/
void channel_put(unsigned char *buf, long long len) {
if (channel.maxpacket == 0) bug_proto();
if (channel.pid <= 0) bug_proto();
if (channel.fd0 == -1) bug_proto();
if (!buf || len < 0) bug_inval();
if (channel.len0 + len > CHANNEL_BUFSIZE) bug_nomem();
byte_copy(channel.buf0 + channel.len0, len, buf);
channel.len0 += len;
channel.localwindow -= len;
}
/*
The 'channel_puteof' function adds information
that remote side closed standard output.
*/
void channel_puteof(void) {
if (channel.maxpacket == 0) bug_proto();
if (channel.pid == 0) bug_proto();
if (channel.fd0 == -1) bug_proto();
channel.remoteeof = 1;
if (channel.len0 == 0 && !channel.flagterminal) {
close(channel.fd0);
channel.fd0 = -1;
}
}
/*
The 'channel_putisready' function returns
if child is ready accept data.
*/
int channel_putisready(void) {
if (channel.maxpacket == 0) return 0;
if (channel.pid <= 0) return 0;
if (channel.fd0 == -1) return 0;
return (CHANNEL_BUFSIZE > channel.len0);
}
/*
The 'channel_read' function reads
data from childs standard output.
*/
long long channel_read(unsigned char *buf, long long len) {
long long r;
if (channel.maxpacket == 0) bug_proto();
if (channel.pid <= 0) bug_proto();
if (channel.fd1 == -1) bug_proto();
if (!buf || len < 0) bug_inval();
if (channel.remotewindow <= 0) return 0;
r = len;
if (r > 1048576) r = 1048576;
if (r > channel.remotewindow) r = channel.remotewindow;
r = read(channel.fd1, buf, r);
if (r == -1) {
if (errno == EINTR) return 0;
if (errno == EAGAIN) return 0;
if (errno == EWOULDBLOCK) return 0;
}
if (r <= 0) {
channel.fd1 = -1;
return 0;
}
channel.remotewindow -= r;
return r;
}
/*
The 'channel_extendedread' function reads
data from childs error output.
*/
long long channel_extendedread(unsigned char *buf, long long len) {
long long r;
if (channel.maxpacket == 0) bug_proto();
if (channel.pid <= 0) bug_proto();
if (channel.fd2 == -1) bug_proto();
if (!buf || len < 0) bug_inval();
if (channel.remotewindow <= 0) return 0;
r = len;
if (r > 1048576) r = 1048576;
if (r > channel.remotewindow) r = channel.remotewindow;
r = read(channel.fd2, buf, r);
if (r == -1) {
if (errno == EINTR) return 0;
if (errno == EAGAIN) return 0;
if (errno == EWOULDBLOCK) return 0;
}
if (r <= 0) {
channel.fd2 = -1;
return 0;
}
channel.remotewindow -= r;
return r;
}
/*
The 'channel_readisready' function returns
if we can read data from childs standard output.
*/
int channel_readisready(void) {
if (channel.maxpacket == 0 || channel.pid == 0) return 0;
if (channel.fd1 == -1) return 0;
return (channel.remotewindow > 0);
}
/*
The 'channel_extendedreadisready' function returns
if we can read data from childs error output.
*/
int channel_extendedreadisready(void) {
if (channel.maxpacket == 0 || channel.pid == 0) return 0;
if (channel.fd2 == -1) return 0;
return (channel.remotewindow > 0);
}
/*
The 'channel_write' function writes
data to childs standard input.
*/
int channel_write(void) {
long long w;
if (channel.maxpacket == 0) bug_proto();
if (channel.pid <= 0) bug_proto();
if (channel.fd0 == -1) bug_proto();
if (channel.len0 <= 0) return 1;
w = write(channel.fd0, channel.buf0, channel.len0);
if (w == -1) {
if (errno == EINTR) return 1;
if (errno == EAGAIN) return 1;
if (errno == EWOULDBLOCK) return 1;
}
if (w <= 0) {
channel.fd0 = -1;
return 0;
}
byte_copy(channel.buf0, channel.len0 - w, channel.buf0 + w);
purge(channel.buf0 + channel.len0 - w, w);
channel.len0 -= w;
if (channel.remoteeof && channel.len0 == 0 && !channel.flagterminal) {
close(channel.fd0);
channel.fd0 = -1;
}
return 1;
}
/*
The 'channel_writeisready' function returns
if we can write data to childs standard input.
*/
int channel_writeisready(void) {
if (channel.maxpacket == 0) return 0;
if (channel.pid <= 0) return 0;
if (channel.fd0 == -1) return 0;
return (channel.len0 > 0);
}
/*
The 'channel_iseof' function returns
if child closed standard and error output.
*/
int channel_iseof(void) {
if (channel.maxpacket == 0) return 0;
if (channel.pid == 0) return 0;
if (channel.pid == -1) return 1;
return (channel.fd1 == -1 && channel.fd2 == -1);
}
/*
The 'channel_waitnohang' function returns
if child exited or was killed.
*/
int channel_waitnohang(int *s, int *e) {
int r, status;
if (!s || !e) bug_inval();
if (channel.maxpacket == 0) bug_proto();
if (channel.pid <= 0) return 0;
do {
r = waitpid(channel.pid, &status, WNOHANG);
} while (r == -1 && errno == EINTR);
if (r <= 0) return 0;
if (channel.flagterminal) {
logsys_logout(channel.user, channel.remoteip, channel.termname,
channel.pid);
}
if (WIFEXITED(status)) {
*e = WEXITSTATUS(status);
*s = 0;
}
else if (WIFSIGNALED(status)) {
*e = 0;
*s = WTERMSIG(status);
}
else { *e = *s = -1; }
channel.pid = -1;
return 1;
}
/*
Remove sentitive data from allocated memory.
*/
void channel_purge(void) {
purge(&channel, sizeof channel);
trymunlock(&channel, sizeof channel);
}
/*
Initialize channel structure.
*/
void channel_init(void) {
trymlock(&channel, sizeof channel);
purge(&channel, sizeof channel);
channel.maxpacket = 0;
channel.remoteeof = 0;
channel.len0 = 0;
channel.pid = 0;
channel.flagterminal = 0;
channel.master = -1;
channel.slave = -1;
}
int channel_getfd0(void) { return channel.fd0; }
int channel_getfd1(void) { return channel.fd1; }
int channel_getfd2(void) { return channel.fd2; }
long long channel_getlen0(void) { return channel.len0; }
crypto_uint32 channel_getid(void) { return channel.id; }
crypto_uint32 channel_getlocalwindow(void) { return channel.localwindow; }
void channel_incrementremotewindow(crypto_uint32 x) {
channel.remotewindow += x;
}
void channel_incrementlocalwindow(crypto_uint32 x) { channel.localwindow += x; }
|