| 1 |
/* |
|---|
| 2 |
* Copyright (c) 2007, OmniTI Computer Consulting, Inc. |
|---|
| 3 |
* All rights reserved. |
|---|
| 4 |
* |
|---|
| 5 |
* Redistribution and use in source and binary forms, with or without |
|---|
| 6 |
* modification, are permitted provided that the following conditions are |
|---|
| 7 |
* met: |
|---|
| 8 |
* |
|---|
| 9 |
* * Redistributions of source code must retain the above copyright |
|---|
| 10 |
* notice, this list of conditions and the following disclaimer. |
|---|
| 11 |
* * Redistributions in binary form must reproduce the above |
|---|
| 12 |
* copyright notice, this list of conditions and the following |
|---|
| 13 |
* disclaimer in the documentation and/or other materials provided |
|---|
| 14 |
* with the distribution. |
|---|
| 15 |
* * Neither the name OmniTI Computer Consulting, Inc. nor the names |
|---|
| 16 |
* of its contributors may be used to endorse or promote products |
|---|
| 17 |
* derived from this software without specific prior written |
|---|
| 18 |
* permission. |
|---|
| 19 |
* |
|---|
| 20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
#include "noit_defines.h" |
|---|
| 34 |
|
|---|
| 35 |
#include <assert.h> |
|---|
| 36 |
#include <stdio.h> |
|---|
| 37 |
#include <unistd.h> |
|---|
| 38 |
#ifdef HAVE_ALLOCA_H |
|---|
| 39 |
#include <alloca.h> |
|---|
| 40 |
#endif |
|---|
| 41 |
#include <errno.h> |
|---|
| 42 |
#ifdef HAVE_SYS_IOCTL_H |
|---|
| 43 |
#include <sys/ioctl.h> |
|---|
| 44 |
#endif |
|---|
| 45 |
#include <sys/types.h> |
|---|
| 46 |
#include <sys/stat.h> |
|---|
| 47 |
#include <fcntl.h> |
|---|
| 48 |
#ifdef HAVE_STROPTS_H |
|---|
| 49 |
#include <stropts.h> |
|---|
| 50 |
#endif |
|---|
| 51 |
#ifdef HAVE_SYS_STREAM_H |
|---|
| 52 |
#include <sys/stream.h> |
|---|
| 53 |
#endif |
|---|
| 54 |
#ifdef HAVE_TERMIOS_H |
|---|
| 55 |
#include <termios.h> |
|---|
| 56 |
#endif |
|---|
| 57 |
#ifdef HAVE_PTY_H |
|---|
| 58 |
#include <pty.h> |
|---|
| 59 |
#endif |
|---|
| 60 |
#ifdef HAVE_UTIL_H |
|---|
| 61 |
#include <util.h> |
|---|
| 62 |
#endif |
|---|
| 63 |
#ifdef HAVE_LIBUTIL_H |
|---|
| 64 |
#include <libutil.h> |
|---|
| 65 |
#endif |
|---|
| 66 |
#include <arpa/telnet.h> |
|---|
| 67 |
#include <signal.h> |
|---|
| 68 |
|
|---|
| 69 |
#include "eventer/eventer.h" |
|---|
| 70 |
#include "utils/noit_log.h" |
|---|
| 71 |
#include "noit_listener.h" |
|---|
| 72 |
#include "noit_console.h" |
|---|
| 73 |
#include "noit_tokenizer.h" |
|---|
| 74 |
|
|---|
| 75 |
#include "noitedit/sys.h" |
|---|
| 76 |
#include "noitedit/el.h" |
|---|
| 77 |
#include "noitedit/fcns.h" |
|---|
| 78 |
#include "noitedit/map.h" |
|---|
| 79 |
|
|---|
| 80 |
static void |
|---|
| 81 |
nc_telnet_cooker(noit_console_closure_t ncct) { |
|---|
| 82 |
char *tmpbuf, *p, *n; |
|---|
| 83 |
int r; |
|---|
| 84 |
|
|---|
| 85 |
tmpbuf = ncct->outbuf; |
|---|
| 86 |
if(ncct->outbuf_len == 0) return; |
|---|
| 87 |
|
|---|
| 88 |
p = ncct->outbuf + ncct->outbuf_completed; |
|---|
| 89 |
r = ncct->outbuf_len - ncct->outbuf_completed; |
|---|
| 90 |
n = memchr(p, '\n', r); |
|---|
| 91 |
/* No '\n'? Nothin' to do */ |
|---|
| 92 |
if(!n) { |
|---|
| 93 |
ncct->outbuf_cooked = ncct->outbuf_len; |
|---|
| 94 |
return; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
/* Forget the outbuf -- it is now tmpbuf */ |
|---|
| 98 |
ncct->outbuf = NULL; |
|---|
| 99 |
ncct->outbuf_allocd = 0; |
|---|
| 100 |
ncct->outbuf_len = 0; |
|---|
| 101 |
ncct->outbuf_completed = 0; |
|---|
| 102 |
ncct->outbuf_cooked = 0; |
|---|
| 103 |
do { |
|---|
| 104 |
nc_write(ncct, p, n-p); r -= n-p; |
|---|
| 105 |
if(n == tmpbuf || *(n-1) != '\r') |
|---|
| 106 |
nc_write(ncct, "\r", 1); |
|---|
| 107 |
p = n; |
|---|
| 108 |
n = memchr(p+1, '\n', r-1); |
|---|
| 109 |
} while(n); |
|---|
| 110 |
nc_write(ncct, p, r); |
|---|
| 111 |
ncct->outbuf_cooked = ncct->outbuf_len; |
|---|
| 112 |
free(tmpbuf); |
|---|
| 113 |
} |
|---|
| 114 |
int |
|---|
| 115 |
nc_printf(noit_console_closure_t ncct, const char *fmt, ...) { |
|---|
| 116 |
int len; |
|---|
| 117 |
va_list arg; |
|---|
| 118 |
va_start(arg, fmt); |
|---|
| 119 |
len = nc_vprintf(ncct, fmt, arg); |
|---|
| 120 |
va_end(arg); |
|---|
| 121 |
return len; |
|---|
| 122 |
} |
|---|
| 123 |
int |
|---|
| 124 |
nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg) { |
|---|
| 125 |
#ifdef va_copy |
|---|
| 126 |
va_list copy; |
|---|
| 127 |
#endif |
|---|
| 128 |
int lenwanted; |
|---|
| 129 |
|
|---|
| 130 |
if(!ncct->outbuf_allocd) { |
|---|
| 131 |
ncct->outbuf = malloc(4096); |
|---|
| 132 |
if(!ncct->outbuf) return 0; |
|---|
| 133 |
ncct->outbuf_allocd = 4096; |
|---|
| 134 |
} |
|---|
| 135 |
while(1) { |
|---|
| 136 |
char *newbuf; |
|---|
| 137 |
#ifdef va_copy |
|---|
| 138 |
va_copy(copy, arg); |
|---|
| 139 |
lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len, |
|---|
| 140 |
ncct->outbuf_allocd - ncct->outbuf_len, |
|---|
| 141 |
fmt, copy); |
|---|
| 142 |
va_end(copy); |
|---|
| 143 |
#else |
|---|
| 144 |
lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len, |
|---|
| 145 |
ncct->outbuf_allocd - ncct->outbuf_len, |
|---|
| 146 |
fmt, arg); |
|---|
| 147 |
#endif |
|---|
| 148 |
if(ncct->outbuf_len + lenwanted < ncct->outbuf_allocd) { |
|---|
| 149 |
/* All went well, things are as we want them. */ |
|---|
| 150 |
ncct->outbuf_len += lenwanted; |
|---|
| 151 |
return lenwanted; |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
/* We need to enlarge the buffer */ |
|---|
| 155 |
lenwanted += ncct->outbuf_len; |
|---|
| 156 |
lenwanted /= 4096; |
|---|
| 157 |
lenwanted += 1; |
|---|
| 158 |
lenwanted *= 4096; |
|---|
| 159 |
newbuf = realloc(ncct->outbuf, lenwanted); |
|---|
| 160 |
if(!newbuf) { |
|---|
| 161 |
return 0; |
|---|
| 162 |
} |
|---|
| 163 |
ncct->outbuf = newbuf; |
|---|
| 164 |
ncct->outbuf_allocd = lenwanted; |
|---|
| 165 |
} |
|---|
| 166 |
/* NOTREACHED */ |
|---|
| 167 |
} |
|---|
| 168 |
int |
|---|
| 169 |
nc_write(noit_console_closure_t ncct, const void *buf, int len) { |
|---|
| 170 |
if(!ncct->outbuf_allocd) { |
|---|
| 171 |
ncct->outbuf = malloc(len); |
|---|
| 172 |
if(!ncct->outbuf) return 0; |
|---|
| 173 |
ncct->outbuf_allocd = len; |
|---|
| 174 |
} |
|---|
| 175 |
else if(ncct->outbuf_allocd < ncct->outbuf_len + len) { |
|---|
| 176 |
char *newbuf; |
|---|
| 177 |
newbuf = realloc(ncct->outbuf, ncct->outbuf_len + len); |
|---|
| 178 |
if(!newbuf) return 0; |
|---|
| 179 |
ncct->outbuf = newbuf; |
|---|
| 180 |
} |
|---|
| 181 |
memcpy(ncct->outbuf + ncct->outbuf_len, buf, len); |
|---|
| 182 |
ncct->outbuf_len += len; |
|---|
| 183 |
return len; |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
static void |
|---|
| 187 |
noit_console_userdata_free(void *data) { |
|---|
| 188 |
noit_console_userdata_t *userdata = data; |
|---|
| 189 |
if(userdata) { |
|---|
| 190 |
if(userdata->name) free(userdata->name); |
|---|
| 191 |
if(userdata->freefunc) |
|---|
| 192 |
userdata->freefunc(userdata->data); |
|---|
| 193 |
free(userdata); |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|
| 196 |
void |
|---|
| 197 |
noit_console_closure_free(noit_console_closure_t ncct) { |
|---|
| 198 |
noit_log_stream_t lf; |
|---|
| 199 |
if(ncct->el) el_end(ncct->el); |
|---|
| 200 |
if(ncct->hist) history_end(ncct->hist); |
|---|
| 201 |
if(ncct->pty_master >= 0) close(ncct->pty_master); |
|---|
| 202 |
if(ncct->pty_slave >= 0) close(ncct->pty_slave); |
|---|
| 203 |
if(ncct->outbuf) free(ncct->outbuf); |
|---|
| 204 |
if(ncct->telnet) noit_console_telnet_free(ncct->telnet); |
|---|
| 205 |
noit_hash_destroy(&ncct->userdata, NULL, noit_console_userdata_free); |
|---|
| 206 |
while(ncct->state_stack) { |
|---|
| 207 |
noit_console_state_stack_t *tmp; |
|---|
| 208 |
tmp = ncct->state_stack; |
|---|
| 209 |
ncct->state_stack = tmp->last; |
|---|
| 210 |
free(tmp); |
|---|
| 211 |
} |
|---|
| 212 |
lf = noit_log_stream_find(ncct->feed_path); |
|---|
| 213 |
noit_log_stream_remove(ncct->feed_path); |
|---|
| 214 |
if(lf) { |
|---|
| 215 |
noit_log_stream_free(lf); |
|---|
| 216 |
} |
|---|
| 217 |
free(ncct); |
|---|
| 218 |
} |
|---|
| 219 |
|
|---|
| 220 |
noit_console_closure_t |
|---|
| 221 |
noit_console_closure_alloc() { |
|---|
| 222 |
noit_console_closure_t new_ncct; |
|---|
| 223 |
new_ncct = calloc(1, sizeof(*new_ncct)); |
|---|
| 224 |
noit_hash_init(&new_ncct->userdata); |
|---|
| 225 |
noit_console_state_push_state(new_ncct, noit_console_state_initial()); |
|---|
| 226 |
new_ncct->pty_master = -1; |
|---|
| 227 |
new_ncct->pty_slave = -1; |
|---|
| 228 |
return new_ncct; |
|---|
| 229 |
} |
|---|
| 230 |
|
|---|
| 231 |
void |
|---|
| 232 |
noit_console_userdata_set(struct __noit_console_closure *ncct, |
|---|
| 233 |
const char *name, void *data, |
|---|
| 234 |
state_userdata_free_func_t freefunc) { |
|---|
| 235 |
noit_console_userdata_t *item; |
|---|
| 236 |
item = calloc(1, sizeof(*item)); |
|---|
| 237 |
item->name = strdup(name); |
|---|
| 238 |
item->data = data; |
|---|
| 239 |
item->freefunc = freefunc; |
|---|
| 240 |
noit_hash_replace(&ncct->userdata, item->name, strlen(item->name), |
|---|
| 241 |
item, NULL, noit_console_userdata_free); |
|---|
| 242 |
} |
|---|
| 243 |
|
|---|
| 244 |
void * |
|---|
| 245 |
noit_console_userdata_get(struct __noit_console_closure *ncct, |
|---|
| 246 |
const char *name) { |
|---|
| 247 |
void *vitem; |
|---|
| 248 |
if(noit_hash_retrieve(&ncct->userdata, name, strlen(name), |
|---|
| 249 |
&vitem)) |
|---|
| 250 |
return ((noit_console_userdata_t *)vitem)->data; |
|---|
| 251 |
return NULL; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
int |
|---|
| 256 |
noit_console_continue_sending(noit_console_closure_t ncct, |
|---|
| 257 |
int *mask) { |
|---|
| 258 |
int len; |
|---|
| 259 |
eventer_t e = ncct->e; |
|---|
| 260 |
if(!ncct->outbuf_len) return 0; |
|---|
| 261 |
if(ncct->output_cooker) ncct->output_cooker(ncct); |
|---|
| 262 |
while(ncct->outbuf_len > ncct->outbuf_completed) { |
|---|
| 263 |
len = e->opset->write(e->fd, ncct->outbuf + ncct->outbuf_completed, |
|---|
| 264 |
ncct->outbuf_len - ncct->outbuf_completed, |
|---|
| 265 |
mask, e); |
|---|
| 266 |
if(len < 0) { |
|---|
| 267 |
if(errno == EAGAIN) return -1; |
|---|
| 268 |
/* Do something else here? */ |
|---|
| 269 |
return -1; |
|---|
| 270 |
} |
|---|
| 271 |
ncct->outbuf_completed += len; |
|---|
| 272 |
} |
|---|
| 273 |
len = ncct->outbuf_len; |
|---|
| 274 |
free(ncct->outbuf); |
|---|
| 275 |
ncct->outbuf = NULL; |
|---|
| 276 |
ncct->outbuf_allocd = ncct->outbuf_len = |
|---|
| 277 |
ncct->outbuf_completed = ncct->outbuf_cooked = 0; |
|---|
| 278 |
return len; |
|---|
| 279 |
} |
|---|
| 280 |
|
|---|
| 281 |
void |
|---|
| 282 |
noit_console_dispatch(eventer_t e, const char *buffer, |
|---|
| 283 |
noit_console_closure_t ncct) { |
|---|
| 284 |
char **cmds; |
|---|
| 285 |
HistEvent ev; |
|---|
| 286 |
int i, cnt = 32; |
|---|
| 287 |
|
|---|
| 288 |
cmds = alloca(32 * sizeof(*cmds)); |
|---|
| 289 |
i = noit_tokenize(buffer, cmds, &cnt); |
|---|
| 290 |
|
|---|
| 291 |
/* < 0 is an error, that's fine. We want it in the history to "fix" */ |
|---|
| 292 |
/* > 0 means we had arguments, so let's put it in the history */ |
|---|
| 293 |
/* 0 means nothing -- and that isn't worthy of history inclusion */ |
|---|
| 294 |
if(i) history(ncct->hist, &ev, H_ENTER, buffer); |
|---|
| 295 |
|
|---|
| 296 |
if(i>cnt) nc_printf(ncct, "Command length too long.\n"); |
|---|
| 297 |
else if(i<0) nc_printf(ncct, "Error at offset: %d\n", 0-i); |
|---|
| 298 |
else noit_console_state_do(ncct, cnt, cmds); |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
void |
|---|
| 302 |
noit_console_motd(eventer_t e, acceptor_closure_t *ac, |
|---|
| 303 |
noit_console_closure_t ncct) { |
|---|
| 304 |
int ssl; |
|---|
| 305 |
ssl = eventer_get_eventer_ssl_ctx(e) ? 1 : 0; |
|---|
| 306 |
nc_printf(ncct, "noitd%s: %s\n", |
|---|
| 307 |
ssl ? "(secure)" : "", |
|---|
| 308 |
ac->remote_cn ? ac->remote_cn : "(no auth)"); |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
int |
|---|
| 312 |
allocate_pty(int *master, int *slave) { |
|---|
| 313 |
#if defined(HAVE_OPENPTY) || (defined(HAVE_DECL_OPENPTY) && HAVE_DECL_OPENPTY != 0) |
|---|
| 314 |
if(openpty(master, slave, NULL, NULL, NULL)) return -1; |
|---|
| 315 |
#else |
|---|
| 316 |
/* STREAMS... sigh */ |
|---|
| 317 |
char *slavename; |
|---|
| 318 |
extern char *ptsname(); |
|---|
| 319 |
|
|---|
| 320 |
*master = open("/dev/ptmx", O_RDWR); /* open master */ |
|---|
| 321 |
if(*master < 0) return -1; |
|---|
| 322 |
grantpt(*master); /* change permission of slave */ |
|---|
| 323 |
unlockpt(*master); /* unlock slave */ |
|---|
| 324 |
slavename = ptsname(*master); /* get name of slave */ |
|---|
| 325 |
*slave = open(slavename, O_RDWR); /* open slave */ |
|---|
| 326 |
if(*slave < 0) { |
|---|
| 327 |
close(*master); |
|---|
| 328 |
*master = -1; |
|---|
| 329 |
return -1; |
|---|
| 330 |
} |
|---|
| 331 |
/* This is a bit backwards as we using the PTY backwards. |
|---|
| 332 |
* We want to make the master a tty instead of the slave... odd, I know. |
|---|
| 333 |
*/ |
|---|
| 334 |
ioctl(*master, I_PUSH, "ptem"); /* push ptem */ |
|---|
| 335 |
ioctl(*master, I_PUSH, "ldterm"); /* push ldterm*/ |
|---|
| 336 |
#endif |
|---|
| 337 |
if(eventer_set_fd_nonblocking(*master)) return -1; |
|---|
| 338 |
noitL(noit_debug, "allocate_pty -> %d,%d\n", *master, *slave); |
|---|
| 339 |
return 0; |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
int |
|---|
| 343 |
noit_console_handler(eventer_t e, int mask, void *closure, |
|---|
| 344 |
struct timeval *now) { |
|---|
| 345 |
int newmask = EVENTER_READ | EVENTER_EXCEPTION; |
|---|
| 346 |
int keep_going; |
|---|
| 347 |
acceptor_closure_t *ac = closure; |
|---|
| 348 |
noit_console_closure_t ncct = ac->service_ctx; |
|---|
| 349 |
|
|---|
| 350 |
if(mask & EVENTER_EXCEPTION || (ncct && ncct->wants_shutdown)) { |
|---|
| 351 |
socket_error: |
|---|
| 352 |
/* Exceptions cause us to simply snip the connection */ |
|---|
| 353 |
|
|---|
| 354 |
/* This removes the log feed which is important to do before calling close */ |
|---|
| 355 |
eventer_remove_fd(e->fd); |
|---|
| 356 |
if(ncct) noit_console_closure_free(ncct); |
|---|
| 357 |
if(ac) acceptor_closure_free(ac); |
|---|
| 358 |
e->opset->close(e->fd, &newmask, e); |
|---|
| 359 |
return 0; |
|---|
| 360 |
} |
|---|
| 361 |
|
|---|
| 362 |
if(!ac->service_ctx) { |
|---|
| 363 |
ncct = ac->service_ctx = noit_console_closure_alloc(); |
|---|
| 364 |
} |
|---|
| 365 |
if(!ncct->initialized) { |
|---|
| 366 |
ncct->e = e; |
|---|
| 367 |
if(allocate_pty(&ncct->pty_master, &ncct->pty_slave)) { |
|---|
| 368 |
nc_printf(ncct, "Failed to open pty: %s\n", strerror(errno)); |
|---|
| 369 |
ncct->wants_shutdown = 1; |
|---|
| 370 |
goto socket_error; |
|---|
| 371 |
} |
|---|
| 372 |
else { |
|---|
| 373 |
int i; |
|---|
| 374 |
const char *line_protocol; |
|---|
| 375 |
HistEvent ev; |
|---|
| 376 |
|
|---|
| 377 |
ncct->hist = history_init(); |
|---|
| 378 |
history(ncct->hist, &ev, H_SETSIZE, 500); |
|---|
| 379 |
ncct->el = el_init("noitd", ncct->pty_master, NULL, |
|---|
| 380 |
e->fd, e, e->fd, e); |
|---|
| 381 |
if(!ncct->el) goto socket_error; |
|---|
| 382 |
if(el_set(ncct->el, EL_USERDATA, ncct)) { |
|---|
| 383 |
noitL(noit_error, "Cannot set userdata on noitedit session\n"); |
|---|
| 384 |
goto socket_error; |
|---|
| 385 |
} |
|---|
| 386 |
if(el_set(ncct->el, EL_EDITOR, "emacs")) |
|---|
| 387 |
noitL(noit_error, "Cannot set emacs mode on console\n"); |
|---|
| 388 |
if(el_set(ncct->el, EL_HIST, history, ncct->hist)) |
|---|
| 389 |
noitL(noit_error, "Cannot set history on console\n"); |
|---|
| 390 |
el_set(ncct->el, EL_ADDFN, "noit_complete", |
|---|
| 391 |
"auto completion functions for noit", noit_edit_complete); |
|---|
| 392 |
el_set(ncct->el, EL_BIND, "^I", "noit_complete", NULL); |
|---|
| 393 |
for(i=EL_NUM_FCNS; i < ncct->el->el_map.nfunc; i++) { |
|---|
| 394 |
if(ncct->el->el_map.func[i] == noit_edit_complete) { |
|---|
| 395 |
ncct->noit_edit_complete_cmdnum = i; |
|---|
| 396 |
break; |
|---|
| 397 |
} |
|---|
| 398 |
} |
|---|
| 399 |
|
|---|
| 400 |
if(!noit_hash_retr_str(ac->config, |
|---|
| 401 |
"line_protocol", strlen("line_protocol"), |
|---|
| 402 |
&line_protocol)) { |
|---|
| 403 |
line_protocol = NULL; |
|---|
| 404 |
} |
|---|
| 405 |
if(line_protocol && !strcasecmp(line_protocol, "telnet")) { |
|---|
| 406 |
ncct->telnet = noit_console_telnet_alloc(ncct); |
|---|
| 407 |
ncct->output_cooker = nc_telnet_cooker; |
|---|
| 408 |
} |
|---|
| 409 |
noit_console_state_init(ncct); |
|---|
| 410 |
} |
|---|
| 411 |
snprintf(ncct->feed_path, sizeof(ncct->feed_path), "console/%d", e->fd); |
|---|
| 412 |
noit_log_stream_new(ncct->feed_path, "noit_console", ncct->feed_path, |
|---|
| 413 |
ncct, NULL); |
|---|
| 414 |
noit_console_motd(e, ac, ncct); |
|---|
| 415 |
ncct->initialized = 1; |
|---|
| 416 |
} |
|---|
| 417 |
|
|---|
| 418 |
/* If we still have data to send back to the client, this will take |
|---|
| 419 |
* care of that |
|---|
| 420 |
*/ |
|---|
| 421 |
if(noit_console_continue_sending(ncct, &newmask) < 0) { |
|---|
| 422 |
if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error; |
|---|
| 423 |
return newmask | EVENTER_EXCEPTION; |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
for(keep_going=1 ; keep_going ; ) { |
|---|
| 427 |
int len, plen; |
|---|
| 428 |
char sbuf[4096]; |
|---|
| 429 |
const char *buffer; |
|---|
| 430 |
|
|---|
| 431 |
keep_going = 0; |
|---|
| 432 |
|
|---|
| 433 |
buffer = el_gets(ncct->el, &plen); |
|---|
| 434 |
if(!el_eagain(ncct->el)) { |
|---|
| 435 |
if(!buffer) { |
|---|
| 436 |
buffer = "exit"; |
|---|
| 437 |
plen = 4; |
|---|
| 438 |
nc_write(ncct, "\n", 1); |
|---|
| 439 |
} |
|---|
| 440 |
keep_going++; |
|---|
| 441 |
} |
|---|
| 442 |
|
|---|
| 443 |
len = e->opset->read(e->fd, sbuf, sizeof(sbuf)-1, &newmask, e); |
|---|
| 444 |
if(len == 0 || (len < 0 && errno != EAGAIN)) { |
|---|
| 445 |
eventer_remove_fd(e->fd); |
|---|
| 446 |
if(ncct) noit_console_closure_free(ncct); |
|---|
| 447 |
if(ac) acceptor_closure_free(ac); |
|---|
| 448 |
e->opset->close(e->fd, &newmask, e); |
|---|
| 449 |
return 0; |
|---|
| 450 |
} |
|---|
| 451 |
if(len > 0) { |
|---|
| 452 |
keep_going++; |
|---|
| 453 |
sbuf[len] = '\0'; |
|---|
| 454 |
if(ncct->telnet) { |
|---|
| 455 |
noit_console_telnet_telrcv(ncct, sbuf, len); |
|---|
| 456 |
ptyflush(ncct); |
|---|
| 457 |
} |
|---|
| 458 |
else { |
|---|
| 459 |
int written; |
|---|
| 460 |
written = write(ncct->pty_slave, sbuf, len); |
|---|
| 461 |
if(written <= 0) goto socket_error; |
|---|
| 462 |
assert(written == len); |
|---|
| 463 |
} |
|---|
| 464 |
} |
|---|
| 465 |
if(buffer) { |
|---|
| 466 |
char *cmd_buffer; |
|---|
| 467 |
cmd_buffer = malloc(plen+1); |
|---|
| 468 |
memcpy(cmd_buffer, buffer, plen); |
|---|
| 469 |
/* chomp */ |
|---|
| 470 |
cmd_buffer[plen] = '\0'; |
|---|
| 471 |
if(cmd_buffer[plen-1] == '\n') cmd_buffer[plen-1] = '\0'; |
|---|
| 472 |
noitL(noit_debug, "IN[%d]: '%s'\n", plen, cmd_buffer); |
|---|
| 473 |
noit_console_dispatch(e, cmd_buffer, ncct); |
|---|
| 474 |
free(cmd_buffer); |
|---|
| 475 |
} |
|---|
| 476 |
if(noit_console_continue_sending(ncct, &newmask) == -1) { |
|---|
| 477 |
if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error; |
|---|
| 478 |
return newmask | EVENTER_EXCEPTION; |
|---|
| 479 |
} |
|---|
| 480 |
if(ncct->wants_shutdown) goto socket_error; |
|---|
| 481 |
} |
|---|
| 482 |
return newmask | EVENTER_EXCEPTION; |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
static int |
|---|
| 486 |
noit_console_logio_open(noit_log_stream_t ls) { |
|---|
| 487 |
return 0; |
|---|
| 488 |
} |
|---|
| 489 |
static int |
|---|
| 490 |
noit_console_logio_reopen(noit_log_stream_t ls) { |
|---|
| 491 |
/* no op */ |
|---|
| 492 |
return 0; |
|---|
| 493 |
} |
|---|
| 494 |
static int |
|---|
| 495 |
noit_console_logio_write(noit_log_stream_t ls, const void *buf, size_t len) { |
|---|
| 496 |
noit_console_closure_t ncct; |
|---|
| 497 |
ncct = noit_log_stream_get_ctx(ls); |
|---|
| 498 |
int rv, rlen, mask; |
|---|
| 499 |
if(!ncct) return 0; |
|---|
| 500 |
rlen = nc_write(ncct, buf, len); |
|---|
| 501 |
while((rv = noit_console_continue_sending(ncct, &mask)) == -1 && errno == EINTR); |
|---|
| 502 |
if(rv == -1 && errno == EAGAIN) { |
|---|
| 503 |
eventer_update(ncct->e, mask | EVENTER_EXCEPTION); |
|---|
| 504 |
} |
|---|
| 505 |
return rlen; |
|---|
| 506 |
} |
|---|
| 507 |
static int |
|---|
| 508 |
noit_console_logio_close(noit_log_stream_t ls) { |
|---|
| 509 |
noit_console_closure_t ncct; |
|---|
| 510 |
ncct = noit_log_stream_get_ctx(ls); |
|---|
| 511 |
if(!ncct) return 0; |
|---|
| 512 |
ncct->e = NULL; |
|---|
| 513 |
noit_log_stream_set_ctx(ls, NULL); |
|---|
| 514 |
return 0; |
|---|
| 515 |
} |
|---|
| 516 |
static logops_t noit_console_logio_ops = { |
|---|
| 517 |
noit_console_logio_open, |
|---|
| 518 |
noit_console_logio_reopen, |
|---|
| 519 |
noit_console_logio_write, |
|---|
| 520 |
NULL, |
|---|
| 521 |
noit_console_logio_close, |
|---|
| 522 |
NULL, |
|---|
| 523 |
NULL |
|---|
| 524 |
}; |
|---|
| 525 |
|
|---|
| 526 |
int |
|---|
| 527 |
noit_console_write_xml(void *vncct, const char *buffer, int len) { |
|---|
| 528 |
noit_console_closure_t ncct = vncct; |
|---|
| 529 |
return nc_write(ncct, buffer, len); |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
int |
|---|
| 533 |
noit_console_close_xml(void *vncct) { |
|---|
| 534 |
return 0; |
|---|
| 535 |
} |
|---|
| 536 |
|
|---|
| 537 |
void |
|---|
| 538 |
noit_console_init(const char *progname) { |
|---|
| 539 |
if(progname) { |
|---|
| 540 |
char buff[32]; |
|---|
| 541 |
snprintf(buff, sizeof(buff), "%s# ", progname); |
|---|
| 542 |
noit_console_set_default_prompt(buff); |
|---|
| 543 |
} |
|---|
| 544 |
el_multi_init(); |
|---|
| 545 |
signal(SIGTTOU, SIG_IGN); |
|---|
| 546 |
noit_register_logops("noit_console", &noit_console_logio_ops); |
|---|
| 547 |
eventer_name_callback("noit_console", noit_console_handler); |
|---|
| 548 |
} |
|---|
| 549 |
|
|---|