]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
6e90ca020f475433098bfe304ffd21b15ae52028
[vlc] / modules / control / telnet.c
1 /*****************************************************************************
2  * telnet.c: VLM interface plugin
3  *****************************************************************************
4  * Copyright (C) 2000-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Simon Latapie <garf@videolan.org>
8  *          Laurent Aimar <fenrir@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <vlc/vlc.h>
34 #include <vlc_plugin.h>
35 #include <vlc_interface.h>
36 #include <vlc_input.h>
37
38 #include <stdbool.h>
39 #include <sys/stat.h>
40
41 #include <errno.h>
42 #include <fcntl.h>
43
44 #ifdef HAVE_SYS_TIME_H
45 #    include <sys/time.h>
46 #endif
47
48 #ifdef HAVE_UNISTD_H
49 #   include <unistd.h>
50 #endif
51 #ifdef HAVE_POLL
52 #   include <poll.h>
53 #endif
54
55 #include <vlc_network.h>
56 #include <vlc_url.h>
57 #include <vlc_vlm.h>
58
59 #define READ_MODE_PWD 1
60 #define READ_MODE_CMD 2
61 #define WRITE_MODE_PWD 3 // when we write the word "Password:"
62 #define WRITE_MODE_CMD 4
63
64 /* telnet commands */
65 #define TEL_WILL    251
66 #define TEL_WONT    252
67 #define TEL_DO      253
68 #define TEL_DONT    254
69 #define TEL_IAC     255
70 #define TEL_ECHO    1
71
72 /*****************************************************************************
73  * Module descriptor
74  *****************************************************************************/
75 static int  Open ( vlc_object_t * );
76 static void Close( vlc_object_t * );
77
78 #define TELNETHOST_TEXT N_( "Host" )
79 #define TELNETHOST_LONGTEXT N_( "This is the host on which the " \
80     "interface will listen. It defaults to all network interfaces (0.0.0.0)." \
81     " If you want this interface to be available only on the local " \
82     "machine, enter \"127.0.0.1\"." )
83 #define TELNETPORT_TEXT N_( "Port" )
84 #define TELNETPORT_LONGTEXT N_( "This is the TCP port on which this " \
85     "interface will listen. It defaults to 4212." )
86 #define TELNETPORT_DEFAULT 4212
87 #define TELNETPWD_TEXT N_( "Password" )
88 #define TELNETPWD_LONGTEXT N_( "A single administration password is used " \
89     "to protect this interface. The default value is \"admin\"." )
90 #define TELNETPWD_DEFAULT "admin"
91
92 vlc_module_begin();
93     set_shortname( "Telnet" );
94     set_category( CAT_INTERFACE );
95     set_subcategory( SUBCAT_INTERFACE_CONTROL );
96     add_string( "telnet-host", "", NULL, TELNETHOST_TEXT,
97                  TELNETHOST_LONGTEXT, true );
98     add_integer( "telnet-port", TELNETPORT_DEFAULT, NULL, TELNETPORT_TEXT,
99                  TELNETPORT_LONGTEXT, true );
100     add_password( "telnet-password", TELNETPWD_DEFAULT, NULL, TELNETPWD_TEXT,
101                 TELNETPWD_LONGTEXT, true );
102     set_description( N_("VLM remote control interface") );
103     add_category_hint( "VLM", NULL, false );
104     set_capability( "interface", 0 );
105     set_callbacks( Open , Close );
106 vlc_module_end();
107
108 /*****************************************************************************
109  * Local prototypes.
110  *****************************************************************************/
111 static void Run( intf_thread_t * );
112
113 typedef struct
114 {
115     int        i_mode; /* read or write */
116     int        fd;
117     char       buffer_read[1000]; // 1000 byte per command should be sufficient
118     char      *buffer_write;
119     char      *p_buffer_read;
120     char      *p_buffer_write; // the position in the buffer
121     int        i_buffer_write; // the number of byte we still have to send
122     int        i_tel_cmd; // for specific telnet commands
123
124 } telnet_client_t;
125
126 static char *MessageToString( vlm_message_t *, int );
127 static void Write_message( telnet_client_t *, vlm_message_t *, const char *, int );
128
129 struct intf_sys_t
130 {
131    telnet_client_t **clients;
132    int             i_clients;
133    int             *pi_fd;
134    vlm_t           *mediatheque;
135 };
136
137 /*
138  * getPort: Decide which port to use. There are two possibilities to
139  * specify a port: integrated in the --telnet-host option with :PORT
140  * or using the --telnet-port option. The --telnet-port option has
141  * precedence.
142  * This code relies upon the fact the url.i_port is 0 if the :PORT
143  * option is missing from --telnet-host.
144  */
145 static int getPort(intf_thread_t *p_intf, vlc_url_t url, int i_port)
146 {
147     // Print error if two different ports have been specified
148     if (url.i_port != 0  &&
149         i_port != TELNETPORT_DEFAULT &&
150         url.i_port != i_port )
151     {
152         msg_Err( p_intf, "ignoring port %d and using %d", url.i_port,
153                  i_port);
154     }
155     if (i_port != TELNETPORT_DEFAULT)
156     {
157         return i_port;
158     }
159     if (url.i_port != 0)
160     {
161          return url.i_port;
162     }
163     return i_port;
164 }
165
166 /*****************************************************************************
167  * Open: initialize dummy interface
168  *****************************************************************************/
169 static int Open( vlc_object_t *p_this )
170 {
171     intf_thread_t *p_intf = (intf_thread_t*) p_this;
172     vlm_t *mediatheque;
173     char *psz_address;
174     vlc_url_t url;
175     int i_telnetport;
176
177     if( !(mediatheque = vlm_New( p_intf )) )
178     {
179         msg_Err( p_intf, "cannot start VLM" );
180         return VLC_EGENERIC;
181     }
182
183     msg_Info( p_intf, "using the VLM interface plugin..." );
184
185     i_telnetport = config_GetInt( p_intf, "telnet-port" );
186     psz_address  = config_GetPsz( p_intf, "telnet-host" );
187
188     vlc_UrlParse(&url, psz_address, 0);
189
190     // There might be two ports given, resolve any potentially
191     // conflict
192     url.i_port = getPort(p_intf, url, i_telnetport);
193
194     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
195     if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) )
196                 == NULL )
197     {
198         msg_Err( p_intf, "cannot listen for telnet" );
199         vlc_UrlClean(&url);
200         free( psz_address );
201         free( p_intf->p_sys );
202         return VLC_EGENERIC;
203     }
204     msg_Info( p_intf,
205               "telnet interface started on interface %s %d",
206               url.psz_host, url.i_port );
207
208     p_intf->p_sys->i_clients   = 0;
209     p_intf->p_sys->clients     = NULL;
210     p_intf->p_sys->mediatheque = mediatheque;
211     p_intf->pf_run = Run;
212
213     vlc_UrlClean(&url);
214     free( psz_address );
215     return VLC_SUCCESS;
216 }
217
218 /*****************************************************************************
219  * Close:
220  *****************************************************************************/
221 static void Close( vlc_object_t *p_this )
222 {
223     intf_thread_t *p_intf = (intf_thread_t*)p_this;
224     intf_sys_t    *p_sys  = p_intf->p_sys;
225     int i;
226
227     for( i = 0; i < p_sys->i_clients; i++ )
228     {
229         telnet_client_t *cl = p_sys->clients[i];
230         net_Close( cl->fd );
231         free( cl );
232         p_sys->clients[i] = NULL;
233     }
234     free( p_sys->clients );
235
236     net_ListenClose( p_sys->pi_fd );
237
238     vlm_Delete( p_sys->mediatheque );
239
240     free( p_sys );
241 }
242
243 /*****************************************************************************
244  * Run: main loop
245  *****************************************************************************/
246 static void Run( intf_thread_t *p_intf )
247 {
248     intf_sys_t     *p_sys = p_intf->p_sys;
249     char           *psz_password;
250     unsigned        nlisten = 0;
251
252     for (const int *pfd = p_sys->pi_fd; *pfd != -1; pfd++)
253         nlisten++; /* How many listening sockets do we have? */
254
255     psz_password = config_GetPsz( p_intf, "telnet-password" );
256
257     while( !intf_ShouldDie( p_intf ) )
258     {
259         unsigned ncli = p_sys->i_clients;
260         struct pollfd ufd[ncli + nlisten];
261
262         for (unsigned i = 0; i < ncli; i++)
263         {
264             telnet_client_t *cl = p_sys->clients[i];
265
266             ufd[i].fd = cl->fd;
267             if( (cl->i_mode == WRITE_MODE_PWD) || (cl->i_mode == WRITE_MODE_CMD) )
268                 ufd[i].events = POLLOUT;
269             else
270                 ufd[i].events = POLLIN;
271             ufd[i].revents = 0;
272         }
273
274         for (unsigned i = 0; i < nlisten; i++)
275         {
276             ufd[ncli + i].fd = p_sys->pi_fd[i];
277             ufd[ncli + i].events = POLLIN;
278             ufd[ncli + i].revents = 0;
279         }
280
281         /* FIXME: arbitrary tick */
282         switch (poll (ufd, sizeof (ufd) / sizeof (ufd[0]), 500))
283         {
284             case -1:
285                 if (net_errno != EINTR)
286                 {
287                     msg_Err (p_intf, "network poll error");
288                     msleep (1000);
289                     continue;
290                 }
291             case 0:
292                 continue;
293         }
294
295         /* check if there is something to do with the socket */
296         for (unsigned i = 0; i < ncli; i++)
297         {
298             telnet_client_t *cl = p_sys->clients[i];
299
300             if (ufd[i].revents & (POLLERR|POLLHUP))
301             {
302             drop:
303                 net_Close( cl->fd );
304                 TAB_REMOVE( p_intf->p_sys->i_clients ,
305                             p_intf->p_sys->clients , cl );
306                 free( cl );
307                 continue;
308             }
309
310             if (ufd[i].revents & POLLOUT && (cl->i_buffer_write > 0))
311             {
312                 ssize_t i_len;
313
314                 i_len = send( cl->fd, cl->p_buffer_write ,
315                               cl->i_buffer_write, 0 );
316                 if( i_len > 0 )
317                 {
318                     cl->p_buffer_write += i_len;
319                     cl->i_buffer_write -= i_len;
320                 }
321             }
322             if (ufd[i].revents & POLLIN)
323             {
324                 bool end = false;
325                 ssize_t i_recv;
326
327                 while( ((i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0) &&
328                        ((cl->p_buffer_read - cl->buffer_read) < 999) )
329                 {
330                     switch( cl->i_tel_cmd )
331                     {
332                     case 0:
333                         switch( *(uint8_t *)cl->p_buffer_read )
334                         {
335                         case '\r':
336                             break;
337                         case '\n':
338                             *cl->p_buffer_read = '\n';
339                             end = true;
340                             break;
341                         case TEL_IAC: // telnet specific command
342                             cl->i_tel_cmd = 1;
343                             cl->p_buffer_read++;
344                             break;
345                         default:
346                             cl->p_buffer_read++;
347                             break;
348                         }
349                         break;
350                     case 1:
351                         switch( *(uint8_t *)cl->p_buffer_read )
352                         {
353                         case TEL_WILL: case TEL_WONT:
354                         case TEL_DO: case TEL_DONT:
355                             cl->i_tel_cmd++;
356                             cl->p_buffer_read++;
357                             break;
358                         default:
359                             cl->i_tel_cmd = 0;
360                             cl->p_buffer_read--;
361                             break;
362                         }
363                         break;
364                     case 2:
365                         cl->i_tel_cmd = 0;
366                         cl->p_buffer_read -= 2;
367                         break;
368                     }
369
370                     if( end ) break;
371                 }
372
373                 if( (cl->p_buffer_read - cl->buffer_read) == 999 )
374                 {
375                     Write_message( cl, NULL, "Line too long\r\n",
376                                    cl->i_mode + 2 );
377                 }
378
379                 if (i_recv <= 0 && ( end || errno != EAGAIN ) )
380                     goto drop;
381             }
382         }
383
384         /* and now we should bidouille the data we received / send */
385         for(int i = 0 ; i < p_sys->i_clients ; i++ )
386         {
387             telnet_client_t *cl = p_sys->clients[i];
388
389             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
390             {
391                // we have finished to send
392                cl->i_mode -= 2; // corresponding READ MODE
393             }
394             else if( cl->i_mode == READ_MODE_PWD &&
395                      *cl->p_buffer_read == '\n' )
396             {
397                 *cl->p_buffer_read = '\0';
398                 if( !psz_password || !strcmp( psz_password, cl->buffer_read ) )
399                 {
400                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
401                                    "Master\r\n> ", WRITE_MODE_CMD );
402                 }
403                 else
404                 {
405                     /* wrong password */
406                     Write_message( cl, NULL,
407                                    "\r\nWrong password.\r\nPassword: ",
408                                    WRITE_MODE_PWD );
409                 }
410             }
411             else if( cl->i_mode == READ_MODE_CMD &&
412                      *cl->p_buffer_read == '\n' )
413             {
414                 /* ok, here is a command line */
415                 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
416                     !strncmp( cl->buffer_read, "quit", 4 )  ||
417                     !strncmp( cl->buffer_read, "exit", 4 ) )
418                 {
419                     net_Close( cl->fd );
420                     TAB_REMOVE( p_intf->p_sys->i_clients ,
421                                 p_intf->p_sys->clients , cl );
422                     free( cl );
423                 }
424                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
425                 {
426                     msg_Err( p_intf, "shutdown requested" );
427                     vlc_object_kill( p_intf->p_libvlc );
428                 }
429                 else if( *cl->buffer_read == '@'
430                           && strchr( cl->buffer_read, ' ' ) )
431                 {
432                     /* Module specific commands (use same syntax as in the
433                      * rc interface) */
434                     char *psz_name = cl->buffer_read + 1;
435                     char *psz_cmd, *psz_arg, *psz_msg;
436                     int i_ret;
437
438                     psz_cmd = strchr( cl->buffer_read, ' ' );
439                     *psz_cmd = '\0';  psz_cmd++;
440                     if( ( psz_arg = strchr( psz_cmd, '\n' ) ) ) *psz_arg = '\0';
441                     if( ( psz_arg = strchr( psz_cmd, '\r' ) ) ) *psz_arg = '\0';
442                     if( ( psz_arg = strchr( psz_cmd, ' ' ) )
443                         && *psz_arg )
444                     {
445                         *psz_arg = '\0';
446                         psz_arg++;
447                     }
448
449                     i_ret = var_Command( p_intf, psz_name, psz_cmd, psz_arg,
450                                          &psz_msg );
451
452                     if( psz_msg )
453                     {
454                         vlm_message_t *message;
455                         message = vlm_MessageNew( "Module command", psz_msg );
456                         Write_message( cl, message, NULL, WRITE_MODE_CMD );
457                         vlm_MessageDelete( message );
458                         free( psz_msg );
459                     }
460                 }
461                 else
462                 {
463                     vlm_message_t *message;
464
465                     /* create a standard string */
466                     *cl->p_buffer_read = '\0';
467
468                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
469                                         &message );
470                     if( !strncmp( cl->buffer_read, "help", 4 ) )
471                     {
472                         vlm_message_t *p_my_help =
473                             vlm_MessageNew( "Telnet Specific Commands:", NULL );
474                         vlm_MessageAdd( p_my_help,
475                             vlm_MessageNew( "logout, quit, exit" , NULL ) );
476                         vlm_MessageAdd( p_my_help,
477                             vlm_MessageNew( "shutdown" , NULL ) );
478                         vlm_MessageAdd( p_my_help,
479                             vlm_MessageNew( "@moduleinstance command argument",
480                                              NULL) );
481                         vlm_MessageAdd( message, p_my_help );
482                     }
483                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
484                     vlm_MessageDelete( message );
485
486                 }
487             }
488         }
489
490         /* handle new connections */
491         for (unsigned i = 0; i < nlisten; i++)
492         {
493             int fd;
494
495             if (ufd[ncli + i].revents == 0)
496                 continue;
497
498             fd = net_AcceptSingle (VLC_OBJECT(p_intf), ufd[ncli + i].fd);
499             if (fd == -1)
500                 continue;
501
502             telnet_client_t *cl = malloc( sizeof( telnet_client_t ));
503             if (cl == NULL)
504             {
505                 net_Close (fd);
506                 continue;
507             }
508
509             memset( cl, 0, sizeof(telnet_client_t) );
510             cl->i_tel_cmd = 0;
511             cl->fd = fd;
512             cl->buffer_write = NULL;
513             cl->p_buffer_write = cl->buffer_write;
514             Write_message( cl, NULL,
515                            "Password: \xff\xfb\x01" , WRITE_MODE_PWD );
516             TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
517         }
518     }
519     free( psz_password );
520 }
521
522 static void Write_message( telnet_client_t *client, vlm_message_t *message,
523                            const char *string_message, int i_mode )
524 {
525     char *psz_message;
526
527     client->p_buffer_read = client->buffer_read;
528     (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
529     free( client->buffer_write );
530
531     /* generate the psz_message string */
532     if( message )
533     {
534         /* ok, look for vlm_message_t */
535         psz_message = MessageToString( message, 0 );
536     }
537     else
538     {
539         /* it is a basic string_message */
540         psz_message = strdup( string_message );
541     }
542
543     client->buffer_write = client->p_buffer_write = psz_message;
544     client->i_buffer_write = strlen( psz_message );
545     client->i_mode = i_mode;
546 }
547
548 /* We need the level of the message to put a beautiful indentation.
549  * first level is 0 */
550 static char *MessageToString( vlm_message_t *message, int i_level )
551 {
552 #define STRING_CR "\r\n"
553 #define STRING_TAIL "> "
554
555     char *psz_message;
556     int i, i_message = sizeof( STRING_TAIL );
557
558     if( !message || !message->psz_name )
559     {
560         return strdup( STRING_CR STRING_TAIL );
561     }
562     else if( !i_level && !message->i_child && !message->psz_value  )
563     {
564         /* A command is successful. Don't write anything */
565         return strdup( /*STRING_CR*/ STRING_TAIL );
566     }
567
568     i_message += strlen( message->psz_name ) + i_level * sizeof( "    " ) + 1;
569     psz_message = malloc( i_message );
570     *psz_message = 0;
571     for( i = 0; i < i_level; i++ ) strcat( psz_message, "    " );
572     strcat( psz_message, message->psz_name );
573
574     if( message->psz_value )
575     {
576         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
577             sizeof( STRING_CR );
578         psz_message = realloc( psz_message, i_message );
579         strcat( psz_message, " : " );
580         strcat( psz_message, message->psz_value );
581         strcat( psz_message, STRING_CR );
582     }
583     else
584     {
585         i_message += sizeof( STRING_CR );
586         psz_message = realloc( psz_message, i_message );
587         strcat( psz_message, STRING_CR );
588     }
589
590     for( i = 0; i < message->i_child; i++ )
591     {
592         char *child_message =
593             MessageToString( message->child[i], i_level + 1 );
594
595         i_message += strlen( child_message );
596         psz_message = realloc( psz_message, i_message );
597         strcat( psz_message, child_message );
598         free( child_message );
599     }
600
601     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
602
603     return psz_message;
604 }