]> git.sesse.net Git - vlc/blob - modules/control/telnet.c
telnet.c: remove the bloat which was needed when strings were localized.
[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 #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         net_Close( cl->fd );
223         free( cl );
224         p_sys->clients[i] = NULL;
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             telnet_client_t *cl = NULL;
257
258             /* to be non blocking */
259 #if defined( WIN32 ) || defined( UNDER_CE )
260             {
261                 unsigned long i_dummy = 1;
262                 ioctlsocket( fd, FIONBIO, &i_dummy );
263             }
264 #else
265             fcntl( fd, F_SETFL, O_NONBLOCK );
266 #endif
267             cl = malloc( sizeof( telnet_client_t ));
268             if( cl )
269             {
270                 memset( cl, 0, sizeof(telnet_client_t) );
271                 cl->i_tel_cmd = 0;
272                 cl->fd = fd;
273                 cl->buffer_write = NULL;
274                 cl->p_buffer_write = cl->buffer_write;
275                 Write_message( cl, NULL,
276                                "Password: \xff\xfb\x01" , WRITE_MODE_PWD );
277
278                 TAB_APPEND( p_sys->i_clients, p_sys->clients, cl );
279             }
280             else
281             {
282                 msg_Err( p_intf, "Out of mem");
283                 continue;
284             }
285         }
286
287         /* to do a proper select */
288         FD_ZERO( &fds_read );
289         FD_ZERO( &fds_write );
290
291         for( i = 0 ; i < p_sys->i_clients ; i++ )
292         {
293             telnet_client_t *cl = p_sys->clients[i];
294
295             if( (cl->i_mode == WRITE_MODE_PWD) || (cl->i_mode == WRITE_MODE_CMD) )
296             {
297                 FD_SET( cl->fd , &fds_write );
298             }
299             else
300             {
301                 FD_SET( cl->fd , &fds_read );
302             }
303             i_handle_max = __MAX( i_handle_max, cl->fd );
304         }
305
306         timeout.tv_sec = 0;
307         timeout.tv_usec = 500*1000;
308
309         i_ret = select( i_handle_max + 1, &fds_read, &fds_write, 0, &timeout );
310         if( (i_ret == -1) && (errno != EINTR) )
311         {
312             msg_Warn( p_intf, "cannot select sockets" );
313             msleep( 1000 );
314             continue;
315         }
316         else if( i_ret <= 0 )
317         {
318             continue;
319         }
320
321         /* check if there is something to do with the socket */
322         for( i = 0 ; i < p_sys->i_clients ; i++ )
323         {
324             telnet_client_t *cl = p_sys->clients[i];
325
326             if( FD_ISSET(cl->fd , &fds_write) && (cl->i_buffer_write > 0) )
327             {
328                 i_len = send( cl->fd, cl->p_buffer_write ,
329                               cl->i_buffer_write, 0 );
330                 if( i_len > 0 )
331                 {
332                     cl->p_buffer_write += i_len;
333                     cl->i_buffer_write -= i_len;
334                 }
335             }
336             else if( FD_ISSET( cl->fd, &fds_read) )
337             {
338                 int i_end = 0;
339                 int i_recv;
340
341                 while( ((i_recv=recv( cl->fd, cl->p_buffer_read, 1, 0 )) > 0) &&
342                        ((cl->p_buffer_read - cl->buffer_read) < 999) )
343                 {
344                     switch( cl->i_tel_cmd )
345                     {
346                     case 0:
347                         switch( *(uint8_t *)cl->p_buffer_read )
348                         {
349                         case '\r':
350                             break;
351                         case '\n':
352                             *cl->p_buffer_read = '\n';
353                             i_end = 1;
354                             break;
355                         case TEL_IAC: // telnet specific command
356                             cl->i_tel_cmd = 1;
357                             cl->p_buffer_read++;
358                             break;
359                         default:
360                             cl->p_buffer_read++;
361                             break;
362                         }
363                         break;
364                     case 1:
365                         switch( *(uint8_t *)cl->p_buffer_read )
366                         {
367                         case TEL_WILL: case TEL_WONT:
368                         case TEL_DO: case TEL_DONT:
369                             cl->i_tel_cmd++;
370                             cl->p_buffer_read++;
371                             break;
372                         default:
373                             cl->i_tel_cmd = 0;
374                             cl->p_buffer_read--;
375                             break;
376                         }
377                         break;
378                     case 2:
379                         cl->i_tel_cmd = 0;
380                         cl->p_buffer_read -= 2;
381                         break;
382                     }
383
384                     if( i_end != 0 ) break;
385                 }
386
387                 if( (cl->p_buffer_read - cl->buffer_read) == 999 )
388                 {
389                     Write_message( cl, NULL, "Line too long\r\n",
390                                    cl->i_mode + 2 );
391                 }
392
393                 if( i_recv == 0 )
394                 {
395                     net_Close( cl->fd );
396                     TAB_REMOVE( p_intf->p_sys->i_clients ,
397                                 p_intf->p_sys->clients , cl );
398                     free( cl );
399                 }
400             }
401         }
402
403         /* and now we should bidouille the data we received / send */
404         for( i = 0 ; i < p_sys->i_clients ; i++ )
405         {
406             telnet_client_t *cl = p_sys->clients[i];
407
408             if( cl->i_mode >= WRITE_MODE_PWD && cl->i_buffer_write == 0 )
409             {
410                // we have finished to send
411                cl->i_mode -= 2; // corresponding READ MODE
412             }
413             else if( cl->i_mode == READ_MODE_PWD &&
414                      *cl->p_buffer_read == '\n' )
415             {
416                 *cl->p_buffer_read = '\0';
417                 if( strcmp( psz_password, cl->buffer_read ) == 0 )
418                 {
419                     Write_message( cl, NULL, "\xff\xfc\x01\r\nWelcome, "
420                                    "Master\r\n> ", WRITE_MODE_CMD );
421                 }
422                 else
423                 {
424                     /* wrong password */
425                     Write_message( cl, NULL,
426                                    "\r\nWrong password.\r\nPassword: ",
427                                    WRITE_MODE_PWD );
428                 }
429             }
430             else if( cl->i_mode == READ_MODE_CMD &&
431                      *cl->p_buffer_read == '\n' )
432             {
433                 /* ok, here is a command line */
434                 if( !strncmp( cl->buffer_read, "logout", 6 ) ||
435                     !strncmp( cl->buffer_read, "quit", 4 )  ||
436                     !strncmp( cl->buffer_read, "exit", 4 ) )
437                 {
438                     net_Close( cl->fd );
439                     TAB_REMOVE( p_intf->p_sys->i_clients ,
440                                 p_intf->p_sys->clients , cl );
441                     free( cl );
442                 }
443                 else if( !strncmp( cl->buffer_read, "shutdown", 8 ) )
444                 {
445                     msg_Err( p_intf, "shutdown requested" );
446                     p_intf->p_libvlc->b_die = VLC_TRUE;
447                 }
448                 else
449                 {
450                     vlm_message_t *message;
451
452                     /* create a standard string */
453                     *cl->p_buffer_read = '\0';
454
455                     vlm_ExecuteCommand( p_sys->mediatheque, cl->buffer_read,
456                                         &message );
457                     if( !strncmp( cl->buffer_read, "help", 4 ) )
458                     {
459                         vlm_message_t *p_my_help =
460                             vlm_MessageNew( "Telnet Specific Commands:", NULL );
461                         vlm_MessageAdd( p_my_help,
462                             vlm_MessageNew( "logout, quit, exit" , NULL ) );
463                         vlm_MessageAdd( p_my_help,
464                             vlm_MessageNew( "shutdown" , NULL ) );
465                         vlm_MessageAdd( message, p_my_help );
466                     }
467                     Write_message( cl, message, NULL, WRITE_MODE_CMD );
468                     vlm_MessageDelete( message );
469
470                 }
471             }
472         }
473     }
474 }
475
476 static void Write_message( telnet_client_t *client, vlm_message_t *message,
477                            char *string_message, int i_mode )
478 {
479     char *psz_message;
480
481     client->p_buffer_read = client->buffer_read;
482     (client->p_buffer_read)[0] = 0; // if (cl->p_buffer_read)[0] = '\n'
483     if( client->buffer_write ) free( client->buffer_write );
484
485     /* generate the psz_message string */
486     if( message )
487     {
488         /* ok, look for vlm_message_t */
489         psz_message = MessageToString( message, 0 );
490     }
491     else
492     {
493         /* it is a basic string_message */
494         psz_message = strdup( string_message );
495     }
496
497     client->buffer_write = client->p_buffer_write = psz_message;
498     client->i_buffer_write = strlen( psz_message );
499     client->i_mode = i_mode;
500 }
501
502 /* We need the level of the message to put a beautiful indentation.
503  * first level is 0 */
504 static char *MessageToString( vlm_message_t *message, int i_level )
505 {
506 #define STRING_CR "\r\n"
507 #define STRING_TAIL "> "
508
509     char *psz_message;
510     int i, i_message = sizeof( STRING_TAIL );
511
512     if( !message || !message->psz_name )
513     {
514         return strdup( STRING_CR STRING_TAIL );
515     }
516     else if( !i_level && !message->i_child && !message->psz_value  )
517     {
518         /* A command is successful. Don't write anything */
519         return strdup( /*STRING_CR*/ STRING_TAIL );
520     }
521
522     i_message += strlen( message->psz_name ) + i_level * sizeof( "    " ) + 1;
523     psz_message = malloc( i_message );
524     *psz_message = 0;
525     for( i = 0; i < i_level; i++ ) strcat( psz_message, "    " );
526     strcat( psz_message, message->psz_name );
527
528     if( message->psz_value )
529     {
530         i_message += sizeof( " : " ) + strlen( message->psz_value ) +
531             sizeof( STRING_CR );
532         psz_message = realloc( psz_message, i_message );
533         strcat( psz_message, " : " );
534         strcat( psz_message, message->psz_value );
535         strcat( psz_message, STRING_CR );
536     }
537     else
538     {
539         i_message += sizeof( STRING_CR );
540         psz_message = realloc( psz_message, i_message );
541         strcat( psz_message, STRING_CR );
542     }
543
544     for( i = 0; i < message->i_child; i++ )
545     {
546         char *child_message =
547             MessageToString( message->child[i], i_level + 1 );
548
549         i_message += strlen( child_message );
550         psz_message = realloc( psz_message, i_message );
551         strcat( psz_message, child_message );
552         free( child_message );
553     }
554
555     if( i_level == 0 ) strcat( psz_message, STRING_TAIL );
556
557     return psz_message;
558 }