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
|
/******************************************************************************
(c) 2000-2007 Christine Caulfield christine.caulfield@googlemail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
******************************************************************************/
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <termios.h>
#include <string.h>
#include <list>
#include <queue>
#include <string>
#include <map>
#include <sstream>
#include <cstdlib>
#include "lat.h"
#include "utils.h"
#include "session.h"
#include "localport.h"
#include "connection.h"
#include "circuit.h"
#include "latcpcircuit.h"
#include "server.h"
#include "serversession.h"
ServerSession::ServerSession(class LATConnection &p, LAT_SessionStartCmd *cmd,
std::string shellcmd,
uid_t uid, gid_t gid,
unsigned char remid,
unsigned char localid, bool clean):
LATSession(p, remid, localid, clean),
command(shellcmd),
cmd_uid(uid),
cmd_gid(gid)
{
max_read_size = cmd->dataslotsize;
debuglog(("new server session: localid %d, remote id %d, data slot size: %d\n",
localid, remid, max_read_size));
}
int ServerSession::new_session(unsigned char *_remote_node,
char *service, char *port,
unsigned char c)
{
credit = c;
strcpy(remote_service, service);
strcpy(remote_port, port);
strcpy(remote_node, (char *)_remote_node);
int status = create_session(_remote_node);
if (status == 0)
{
status = send_login_response();
if (credit) send_issue();
}
return status;
}
// Send the login response packet.
int ServerSession::send_login_response()
{
unsigned char buf[1600];
unsigned char slotbuf[256];
LAT_SessionReply *reply = (LAT_SessionReply *)buf;
int ptr = 0;
int slotptr = 0;
// ACK
reply->header.cmd = LAT_CCMD_SREPLY;
reply->header.num_slots = 0;
reply->slot.length = 0;
reply->slot.cmd = 0x0;
reply->slot.local_session = 0;
reply->slot.remote_session = 0;
parent.send_message(buf,sizeof(LAT_Header), LATConnection::REPLY);
slotbuf[slotptr++] = 0x01; // Service Class
slotbuf[slotptr++] = 0x01; // Min Attention slot size
slotbuf[slotptr++] = max_read_size;
slotbuf[slotptr++] = 0x00; // Dest service length/name
slotbuf[slotptr++] = 0x00; // Source service length/name
slotbuf[slotptr++] = 0x01; // Param type 1
slotbuf[slotptr++] = 0x02; // Param Length 2
slotbuf[slotptr++] = 0x00; // Value 0 (woz 1024: 04 00)
slotbuf[slotptr++] = 0x00; //
slotbuf[slotptr++] = 0x04; // Param type 4 (PTY name)
add_string(slotbuf, &slotptr, (unsigned char *)ptyname);
slotbuf[slotptr++] = 0x00; // NUL terminated (??)
add_slot(buf, ptr, 0x9f, slotbuf, slotptr);
slotptr = 0;
// Send a data_b slot
if (credit)
{
slotbuf[slotptr++] = 0x26; // Flags
slotbuf[slotptr++] = 0x13; // Stop output char XOFF
slotbuf[slotptr++] = 0x11; // Start output char XON
slotbuf[slotptr++] = 0x13; // Stop input char XOFF
slotbuf[slotptr++] = 0x11; // Start input char XON
// data_b slots count against credit
// Hmm, this credit-starves queued connections
// add_slot(buf, ptr, 0xaf, slotbuf, slotptr);
// credit--;
}
slotptr = 0;
// Stuff the client full of credits
add_slot(buf, ptr, 0x0f, slotbuf, 0);
add_slot(buf, ptr, 0x0f, slotbuf, 0);
parent.queue_message(buf, ptr);
return 0;
}
int ServerSession::create_session(unsigned char *remote_node)
{
int slave_fd;
debuglog(("create session: local: %d, remote: %d\n",
local_session, remote_session));
if (openpty(&master_fd,
&slave_fd, NULL, NULL, NULL) != 0)
return -1; /* REJECT */
strcpy(ptyname, ttyname(slave_fd));
state = STARTING;
switch (fork())
{
case 0: // Child
{
int fd = slave_fd;
setsid();
// Set terminal characteristics
struct termios tio;
tcgetattr(slave_fd, &tio);
tio.c_oflag |= ONLCR;
tcsetattr(slave_fd, TCSANOW, &tio);
ioctl(fd, TIOCSCTTY, (char *)NULL);
close(master_fd);
if (fd != 0) dup2(fd, 0);
if (fd != 1) dup2(fd, 1);
if (fd != 2) dup2(fd, 2);
if (fd > 2) close (fd);
close_all_fds();
// Become the requested user.
struct passwd *user_pwd = getpwuid(cmd_uid);
if (user_pwd)
initgroups(user_pwd->pw_name, cmd_gid);
setgid(cmd_gid);
setuid(cmd_uid);
// Get the command to run
// and do it.
execute_command(command.c_str());
// Argh! It returned.
syslog(LOG_ERR, "Error in starting %s: %m", command.c_str());
// Exit now so that the parent will get EOF on the channel
exit(-1);
}
case -1: // Failed
syslog(LOG_ERR, "Error forking %s: %m", command.c_str());
perror("fork");
close(master_fd);
close(slave_fd);
return -1;
default: // Parent
close(slave_fd);
fcntl(master_fd, F_SETFL, fcntl(master_fd, F_GETFL, 0) | O_NONBLOCK);
connected = true;
sleep(0); // Give login a chance to start
break;
}
return 0;
}
/* Hopefully this will work for BSDs
as well as Linux */
#ifdef RLIMIT_NOFILE
# define LAT_RLIMIT_FILES RLIMIT_NOFILE
#else
# ifdef RLIMIT_OFILE
# define LAT_RLIMIT_FILES RLIMIT_OFILE
# endif
#endif
void ServerSession::close_all_fds()
{
struct rlimit rl;
if (getrlimit(LAT_RLIMIT_FILES, &rl) == -1)
rl.rlim_cur = 256;
if (rl.rlim_cur == RLIM_INFINITY)
rl.rlim_cur = 256;
for (unsigned int i=3; i<rl.rlim_cur; i++)
close(i);
}
void ServerSession::execute_command(const char *command)
{
char cmd[strlen(command)+1];
strcpy(cmd, command);
// Count the args
int num_args = 0;
strtok(cmd, " ");
while (strtok(NULL, " ")) num_args++;
// Make sure we have a clean copy of the command
strcpy(cmd, command);
char *argv[num_args+1];
int argc = 0;
const char *cmdname;
const char *thisarg;
// Gather the args
thisarg = strtok(cmd, " ");
// Keep the path for the first arg to execvp
char fullcmd[strlen(thisarg)+1];
strcpy(fullcmd, thisarg);
// For the first arg, remove the path if there is one.
cmdname = strrchr(thisarg, '/')+1;
if (cmdname == (char *)1) cmdname = thisarg;
// We can man malloc all we like here because exec() will
// "free" it for us !
do
{
if (argc == 0) thisarg = cmdname;
argv[argc] = (char *)malloc(strlen(thisarg)+1);
strcpy(argv[argc], thisarg);
thisarg = strtok(NULL, " ");
argc++;
} while (thisarg);
// Need a NULL at the end of the list
argv[argc++] = NULL;
// Set some environment variables.
// /bin/login will clear these it's true but other
// services may find them useful.
setenv("LAT_LOCAL_SERVICE", parent.get_servicename(), 1);
setenv("LAT_REMOTE_NODE", remote_node, 1);
setenv("LAT_REMOTE_PORT", remote_port, 1);
// Run it.
execvp(fullcmd, argv);
}
|