root/src/noit_console.c

Revision 4c963d7e24928576cb0e4a600c35e964b4ba5cca, 11.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

write the eventer... checks are working, but the telnet console is not, refs #32

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #ifdef HAVE_ALLOCA_H
11 #include <alloca.h>
12 #endif
13 #include <errno.h>
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #ifdef HAVE_STROPTS_H
19 #include <stropts.h>
20 #endif
21 #ifdef HAVE_SYS_STREAM_H
22 #include <sys/stream.h>
23 #endif
24 #ifdef HAVE_TERMIOS_H
25 #include <termios.h>
26 #endif
27 #ifdef HAVE_UTIL_H
28 #include <util.h>
29 #endif
30 #include <arpa/telnet.h>
31 #include <signal.h>
32
33 #include "eventer/eventer.h"
34 #include "utils/noit_log.h"
35 #include "noit_listener.h"
36 #include "noit_console.h"
37 #include "noit_tokenizer.h"
38
39 static void
40 nc_telnet_cooker(noit_console_closure_t ncct) {
41   char *tmpbuf, *p, *n;
42   int r;
43
44   tmpbuf = ncct->outbuf;
45   if(ncct->outbuf_len == 0) return;
46
47   p = ncct->outbuf + ncct->outbuf_completed;
48   r = ncct->outbuf_len - ncct->outbuf_completed;
49   n = memchr(p, '\n', r);
50   /* No '\n'? Nothin' to do */
51   if(!n) {
52     ncct->outbuf_cooked = ncct->outbuf_len;
53     return;
54   }
55
56   /* Forget the outbuf -- it is now tmpbuf */
57   ncct->outbuf = NULL;
58   ncct->outbuf_allocd = 0;
59   ncct->outbuf_len = 0;
60   ncct->outbuf_completed = 0;
61   ncct->outbuf_cooked = 0;
62   do {
63     if(n == tmpbuf || *(n-1) != '\r') {
64       nc_write(ncct, p, n-p);   r -= n-p;
65       nc_write(ncct, "\r", 1);
66       p = n;
67     }
68     n = memchr(p+1, '\n', r-1);
69   } while(n);
70   nc_write(ncct, p, r);
71   ncct->outbuf_cooked = ncct->outbuf_len;
72   free(tmpbuf);
73 }
74 int
75 nc_printf(noit_console_closure_t ncct, const char *fmt, ...) {
76   int len;
77   va_list arg;
78   va_start(arg, fmt);
79   len = nc_vprintf(ncct, fmt, arg);
80   va_end(arg);
81   return len;
82 }
83 int
84 nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg) {
85 #ifdef va_copy
86   va_list copy;
87 #endif
88   int lenwanted;
89  
90   if(!ncct->outbuf_allocd) {
91     ncct->outbuf = malloc(4096);
92     if(!ncct->outbuf) return 0;
93     ncct->outbuf_allocd = 4096;
94   }
95   while(1) {
96     char *newbuf;
97 #ifdef va_copy
98     va_copy(copy, arg);
99     lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len,
100                           ncct->outbuf_allocd - ncct->outbuf_len,
101                           fmt, copy);
102     va_end(copy);
103 #else
104     lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len,
105                           ncct->outbuf_allocd - ncct->outbuf_len,
106                           fmt, arg);
107 #endif
108     if(ncct->outbuf_len + lenwanted < ncct->outbuf_allocd) {
109       /* All went well, things are as we want them. */
110       ncct->outbuf_len += lenwanted;
111       return lenwanted;
112     }
113
114     /* We need to enlarge the buffer */
115     lenwanted += ncct->outbuf_len;
116     lenwanted /= 4096;
117     lenwanted += 1;
118     lenwanted *= 4096;
119     newbuf = realloc(ncct->outbuf, lenwanted);
120     if(!newbuf) {
121       return 0;
122     }
123     ncct->outbuf = newbuf;
124     ncct->outbuf_allocd = lenwanted;
125   }
126   return -1;
127 }
128 int
129 nc_write(noit_console_closure_t ncct, const void *buf, int len) {
130   if(!ncct->outbuf_allocd) {
131     ncct->outbuf = malloc(len);
132     if(!ncct->outbuf) return 0;
133     ncct->outbuf_allocd = len;
134   }
135   else if(ncct->outbuf_allocd < ncct->outbuf_len + len) {
136     char *newbuf;
137     newbuf = realloc(ncct->outbuf, ncct->outbuf_len + len);
138     if(!newbuf) return 0;
139     ncct->outbuf = newbuf;
140   }
141   memcpy(ncct->outbuf + ncct->outbuf_len, buf, len);
142   ncct->outbuf_len += len;
143   return len;
144 }
145
146 static void
147 noit_console_userdata_free(void *data) {
148   noit_console_userdata_t *userdata = data;
149   if(userdata) {
150     if(userdata->name) free(userdata->name);
151     if(userdata->freefunc)
152       userdata->freefunc(userdata->data);
153     free(userdata);
154   }
155 }
156 void
157 noit_console_closure_free(noit_console_closure_t ncct) {
158   if(ncct->el) el_end(ncct->el);
159   if(ncct->hist) history_end(ncct->hist);
160   if(ncct->pty_master >= 0) close(ncct->pty_master);
161   if(ncct->pty_slave >= 0) close(ncct->pty_slave);
162   if(ncct->outbuf) free(ncct->outbuf);
163   if(ncct->telnet) noit_console_telnet_free(ncct->telnet);
164   noit_hash_destroy(&ncct->userdata, NULL, noit_console_userdata_free);
165   while(ncct->state_stack) {
166     noit_console_state_stack_t *tmp;
167     tmp = ncct->state_stack;
168     ncct->state_stack = tmp->last;
169     free(tmp);
170   }
171   free(ncct);
172 }
173
174 noit_console_closure_t
175 noit_console_closure_alloc() {
176   noit_console_closure_t new_ncct;
177   new_ncct = calloc(1, sizeof(*new_ncct));
178   noit_hash_init(&new_ncct->userdata);
179   noit_console_state_push_state(new_ncct, noit_console_state_initial());
180   new_ncct->pty_master = -1;
181   new_ncct->pty_slave = -1;
182   return new_ncct;
183 }
184
185 void
186 noit_console_userdata_set(struct __noit_console_closure *ncct,
187                           const char *name, void *data,
188                           state_userdata_free_func_t freefunc) {
189   noit_console_userdata_t *item;
190   item = calloc(1, sizeof(*item));
191   item->name = strdup(name);
192   item->data = data;
193   item->freefunc = freefunc;
194   noit_hash_replace(&ncct->userdata, item->name, strlen(item->name),
195                     item, NULL, noit_console_userdata_free);
196 }
197  
198 void *
199 noit_console_userdata_get(struct __noit_console_closure *ncct,
200                           const char *name) {
201   noit_console_userdata_t *item;
202   if(noit_hash_retrieve(&ncct->userdata, name, strlen(name),
203                         (void **)&item))
204     return item->data;
205   return NULL;
206 }
207
208
209 int
210 noit_console_continue_sending(noit_console_closure_t ncct,
211                               int *mask) {
212   int len;
213   eventer_t e = ncct->e;
214   if(!ncct->outbuf_len) return 0;
215   if(ncct->output_cooker) ncct->output_cooker(ncct);
216   while(ncct->outbuf_len > ncct->outbuf_completed) {
217     len = e->opset->write(e->fd, ncct->outbuf + ncct->outbuf_completed,
218                           ncct->outbuf_len - ncct->outbuf_completed,
219                           mask, e);
220     if(len < 0) {
221       if(errno == EAGAIN) return -1;
222       /* Do something else here? */
223       return -1;
224     }
225     ncct->outbuf_completed += len;
226   }
227   len = ncct->outbuf_len;
228   free(ncct->outbuf);
229   ncct->outbuf = NULL;
230   ncct->outbuf_allocd = ncct->outbuf_len =
231     ncct->outbuf_completed = ncct->outbuf_cooked = 0;
232   return len;
233 }
234
235 void
236 noit_console_init() {
237   el_multi_init();
238   signal(SIGTTOU, SIG_IGN);
239   eventer_name_callback("noit_console", noit_console_handler);
240 }
241
242 void
243 noit_console_dispatch(eventer_t e, const char *buffer,
244                       noit_console_closure_t ncct) {
245   char **cmds;
246   HistEvent ev;
247   int i, cnt = 32;
248
249   cmds = alloca(32 * sizeof(*cmds));
250   i = noit_tokenize(buffer, cmds, &cnt);
251
252   /* < 0 is an error, that's fine.  We want it in the history to "fix" */
253   /* > 0 means we had arguments, so let's put it in the history */
254   /* 0 means nothing -- and that isn't worthy of history inclusion */
255   if(i) history(ncct->hist, &ev, H_ENTER, buffer);
256
257   if(i>cnt) nc_printf(ncct, "Command length too long.\n");
258   else if(i<0) nc_printf(ncct, "Error at offset: %d\n", 0-i);
259   else noit_console_state_do(ncct, cnt, cmds);
260 }
261
262 void
263 noit_console_motd(eventer_t e, acceptor_closure_t *ac,
264                   noit_console_closure_t ncct) {
265   int ssl;
266   ssl = eventer_get_eventer_ssl_ctx(e) ? 1 : 0;
267   nc_printf(ncct, "noitd%s: %s\n",
268             ssl ? "(secure)" : "",
269             ac->remote_cn ? ac->remote_cn : "(no auth)");
270 }
271
272 int
273 allocate_pty(int *master, int *slave) {
274 #ifdef HAVE_OPENPTY
275   if(openpty(master, slave, NULL, NULL, NULL)) return -1;
276   if(ioctl(ncct->pty_master, FIONBIO, &on)) return -1;
277 #else
278   /* STREAMS... sigh */
279   char   *slavename;
280   extern char *ptsname();
281
282   *master = open("/dev/ptmx", O_RDWR);  /* open master */
283   if(*master < 0) return -1;
284   grantpt(*master);                     /* change permission of   slave */
285   unlockpt(*master);                    /* unlock slave */
286   slavename = ptsname(*master);         /* get name of slave */
287   *slave = open(slavename, O_RDWR);    /* open slave */
288   if(*slave < 0) {
289     close(*master);
290     *master = -1;
291     return -1;
292   }
293   ioctl(*slave, I_PUSH, "ptem");       /* push ptem */
294   ioctl(*slave, I_PUSH, "ldterm");     /* push ldterm*/
295   return 0;
296 #endif
297 }
298
299 int
300 noit_console_handler(eventer_t e, int mask, void *closure,
301                      struct timeval *now) {
302   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
303   int keep_going;
304   acceptor_closure_t *ac = closure;
305   noit_console_closure_t ncct = ac->service_ctx;
306
307   if(mask & EVENTER_EXCEPTION || (ncct && ncct->wants_shutdown)) {
308 socket_error:
309     /* Exceptions cause us to simply snip the connection */
310     eventer_remove_fd(e->fd);
311     e->opset->close(e->fd, &newmask, e);
312     if(ncct) noit_console_closure_free(ncct);
313     if(ac) acceptor_closure_free(ac);
314     return 0;
315   }
316
317   if(!ac->service_ctx) {
318     ncct = ac->service_ctx = noit_console_closure_alloc();
319   }
320   if(!ncct->initialized) {
321     int on = 1;
322     ncct->e = e;
323     if(allocate_pty(&ncct->pty_master, &ncct->pty_slave)) {
324       nc_printf(ncct, "Failed to open pty: %s\n", strerror(errno));
325       ncct->wants_shutdown = 1;
326     }
327     else {
328       const char *line_protocol;
329       HistEvent ev;
330       ncct->hist = history_init();
331       history(ncct->hist, &ev, H_SETSIZE, 500);
332       ncct->el = el_init("noitd", ncct->pty_master, NULL,
333                          e->fd, e, e->fd, e);
334       el_set(ncct->el, EL_USERDATA, ncct);
335       el_set(ncct->el, EL_EDITOR, "emacs");
336       el_set(ncct->el, EL_HIST, history, ncct->hist);
337       if(!noit_hash_retrieve(ac->config,
338                              "line_protocol", strlen("line_protocol"),
339                              (void **)&line_protocol)) {
340         line_protocol = NULL;
341       }
342       if(line_protocol && !strcasecmp(line_protocol, "telnet")) {
343         ncct->telnet = noit_console_telnet_alloc(ncct);
344         ncct->output_cooker = nc_telnet_cooker;
345       }
346       noit_console_state_init(ncct);
347     }
348     noit_console_motd(e, ac, ncct);
349     ncct->initialized = 1;
350   }
351
352   /* If we still have data to send back to the client, this will take
353    * care of that
354    */
355   if(noit_console_continue_sending(ncct, &newmask) < 0) {
356     if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error;
357     return newmask | EVENTER_EXCEPTION;
358   }
359
360   for(keep_going=1 ; keep_going ; ) {
361     int len, plen;
362     char sbuf[4096];
363     const char *buffer;
364
365     keep_going = 0;
366
367     buffer = el_gets(ncct->el, &plen);
368     if(!el_eagain(ncct->el)) {
369       if(!buffer) {
370         buffer = "exit";
371         plen = 4;
372         nc_write(ncct, "\n", 1);
373       }
374       keep_going++;
375     }
376
377     len = e->opset->read(e->fd, sbuf, sizeof(sbuf)-1, &newmask, e);
378     if(len == 0 || (len < 0 && errno != EAGAIN)) {
379       eventer_remove_fd(e->fd);
380       close(e->fd);
381       return 0;
382     }
383     if(len > 0) {
384       keep_going++;
385       sbuf[len] = '\0';
386       if(ncct->telnet) {
387         noit_console_telnet_telrcv(ncct, sbuf, len);
388         ptyflush(ncct);
389       }
390       else {
391         write(ncct->pty_slave, sbuf, len);
392       }
393     }
394     if(buffer) {
395       char *cmd_buffer;
396       cmd_buffer = malloc(plen+1);
397       memcpy(cmd_buffer, buffer, plen);
398       /* chomp */
399       cmd_buffer[plen] = '\0';
400       if(cmd_buffer[plen-1] == '\n') cmd_buffer[plen-1] = '\0';
401       noitL(noit_debug, "IN[%d]: '%s'\n", plen, cmd_buffer);
402       noit_console_dispatch(e, cmd_buffer, ncct);
403       free(cmd_buffer);
404     }
405     if(noit_console_continue_sending(ncct, &newmask) == -1) {
406       if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error;
407       return newmask | EVENTER_EXCEPTION;
408     }
409     if(ncct->wants_shutdown) goto socket_error;
410   }
411   return newmask | EVENTER_EXCEPTION;
412 }
413
Note: See TracBrowser for help on using the browser.