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