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