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