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