Changeset 256:70ad9c490986 in mediastreamer2


Ignore:
Timestamp:
Feb 13, 2009 11:19:13 PM (4 years ago)
Author:
smorlat <smorlat@…>
Branch:
default
Message:

linphonecsh and linphonec use a unix socket by default, for security reasons

git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@259 3f6dc0c8-ddfe-455d-9043-3cd528dc4637

Location:
linphone
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • linphone/NEWS

    r236 r256  
    77        * linphonec can compile and work without libreadline 
    88        * enable translations on windows 
     9        * enable lookups of SRV records 
    910        * bugfixing as usual 
    1011 
  • linphone/console/commands.c

    r248 r256  
    12441244                if (cfg){ 
    12451245                        if (linphone_proxy_config_is_registered(cfg)){ 
    1246                                 linphonec_out("identity=%s duration=%i\n", 
     1246                                linphonec_out("registered, identity=%s duration=%i\n", 
    12471247                                        linphone_proxy_config_get_identity(cfg), 
    12481248                                        linphone_proxy_config_get_expires(cfg)); 
     
    12691269                        break; 
    12701270                        case GSTATE_CALL_OUT_CONNECTED: 
    1271                                 linphonec_out("hook=%s duration=%i\n", linphonec_get_callee(), 
     1271                                linphonec_out("Call out, hook=%s duration=%i\n", linphonec_get_callee(), 
    12721272                                        linphone_core_get_current_call_duration(lc)); 
    12731273                        break; 
  • linphone/console/linphonec.c

    r245 r256  
    4848#include <sys/socket.h> 
    4949#include <netdb.h> 
     50#include <sys/un.h> 
     51#include <sys/stat.h> 
    5052#endif 
    5153 
     
    131133static bool_t preview_enabled=FALSE; 
    132134static bool_t show_general_state=FALSE; 
     135static bool_t unix_socket=FALSE; 
    133136static int tcp_port=0; /* see --tcp: tcp port to listen for commands */ 
    134137LPC_AUTH_STACK auth_stack; 
     
    139142static ortp_socket_t client_sock=-1; 
    140143char prompt[PROMPT_MAX_LEN]; 
     144char sock_unix_path[128]={0}; 
     145static ortp_thread_t net_reader_th; 
     146static bool_t net_reader_run=FALSE; 
     147static ortp_socket_t server_sock; 
    141148 
    142149LinphoneCoreVTable linphonec_vtable = { 
     
    367374 
    368375static ortp_socket_t create_server_socket(int port){ 
    369         ortp_socket_t server_sock; 
    370         char service[12]; 
    371         int tmp,err; 
    372         /*setup the server socket */ 
    373         struct addrinfo *ai=NULL; 
    374         struct addrinfo hints; 
    375         memset(&hints,0,sizeof(hints)); 
    376         hints.ai_family=AF_INET; 
    377         snprintf(service,sizeof(service),"%i",port); 
    378  
    379         getaddrinfo("127.0.0.1",service,&hints,&ai); 
    380         if (ai==NULL){ 
    381                 fprintf(stderr,"getaddrinfo failed on port %s",service); 
    382                 exit(-1); 
    383         } 
    384         server_sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
    385         tmp=1; 
    386         err=setsockopt(server_sock,SOL_SOCKET,SO_REUSEADDR,(void*)&tmp,sizeof(tmp)); 
    387         if (err<0) fprintf(stderr,"Error in setsockopt(): %s\n",getSocketError()); 
    388         if (bind(server_sock,ai->ai_addr,ai->ai_addrlen)!=0){ 
    389                 fprintf(stderr,"Failed to bind command socket."); 
    390                 exit(-1); 
    391         } 
    392         listen(server_sock,1); 
    393         return server_sock; 
    394 } 
    395  
    396 static void *tcp_thread(void*p){ 
     376        ortp_socket_t sock; 
     377        if (!unix_socket){ 
     378                char service[12]; 
     379                /*setup the server socket */ 
     380                struct addrinfo *ai=NULL; 
     381                struct addrinfo hints; 
     382                memset(&hints,0,sizeof(hints)); 
     383                hints.ai_family=AF_INET; 
     384                snprintf(service,sizeof(service),"%i",port); 
     385         
     386                getaddrinfo("127.0.0.1",service,&hints,&ai); 
     387                if (ai==NULL){ 
     388                        fprintf(stderr,"getaddrinfo failed on port %s",service); 
     389                        exit(-1); 
     390                } 
     391                sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
     392                if (bind(sock,ai->ai_addr,ai->ai_addrlen)!=0){ 
     393                        fprintf(stderr,"Failed to bind command socket.\n"); 
     394                        exit(-1); 
     395                } 
     396                listen(sock,1); 
     397        }else{ 
     398#ifndef WIN32 
     399                struct sockaddr_un sa; 
     400                sock=socket(AF_UNIX,SOCK_STREAM,0); 
     401                sa.sun_family=AF_UNIX; 
     402                snprintf(sock_unix_path,sizeof(sock_unix_path)-1,"/tmp/linphonec-%i",getuid()); 
     403                strncpy(sa.sun_path,sock_unix_path,sizeof(sa.sun_path)-1); 
     404                unlink(sock_unix_path);/*in case we didn't finished properly previous time */ 
     405                fchmod(sock,S_IRUSR|S_IWUSR); 
     406                if (bind(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){ 
     407                        fprintf(stderr,"Failed to bind command unix socket.\n"); 
     408                        exit(-1); 
     409                } 
     410                listen(sock,1); 
     411                printf("Listening from unix socket %s\n",sock_unix_path); 
     412#else 
     413                fprintf(stderr,"Window pipe implementation not written yet.\n"); 
     414#endif 
     415        } 
     416        return sock; 
     417} 
     418 
     419static void *net_thread(void*p){ 
    397420        char tmp[250]; 
    398         ortp_socket_t server_sock; 
    399421        struct sockaddr_storage ss; 
     422#ifndef WIN32 
     423        struct sockaddr_un su; 
     424#endif 
    400425        socklen_t ssize; 
    401         bool_t run=TRUE; 
    402426         
    403427        server_sock=create_server_socket(tcp_port); 
    404428        if (server_sock==-1) return NULL; 
    405         while(run){ 
     429        while(net_reader_run){ 
    406430                while(client_sock!=-1){ /*sleep until the last command is finished*/ 
    407431#ifndef WIN32 
     
    411435#endif 
    412436                } 
    413                 ssize=sizeof(ss); 
    414                 if ((client_sock=accept(server_sock,(struct sockaddr*)&ss,&ssize))!=-1){ 
     437                if (!unix_socket){ 
     438                        ssize=sizeof(ss); 
     439                        client_sock=accept(server_sock,(struct sockaddr*)&ss,&ssize); 
     440                }else{ 
     441                        ssize=sizeof(su); 
     442                        client_sock=accept(server_sock,(struct sockaddr*)&su,&ssize); 
     443                } 
     444                if (client_sock!=-1){ 
    415445                        int len; 
    416446                        /*now read from the client */ 
     
    419449                                tmp[len]='\0'; 
    420450                                strcpy(received_prompt,tmp); 
    421                                 if (strcmp(received_prompt,"quit")==0) run=FALSE; 
    422451                                printf("Receiving command '%s'\n",received_prompt);fflush(stdout); 
    423452                                have_prompt=TRUE; 
     
    429458                        } 
    430459                         
    431                 } 
    432         } 
     460                }else{ 
     461                        if (net_reader_run) fprintf(stderr,"accept() failed: %s\n",getSocketError()); 
     462                } 
     463        } 
     464         
    433465        return NULL; 
    434466} 
    435467 
    436 static void start_tcp_reader(void){ 
    437         ortp_thread_t th; 
     468static void start_net_reader(void){ 
    438469        ms_mutex_init(&prompt_mutex,NULL); 
    439         ortp_thread_create(&th,NULL,tcp_thread,NULL); 
     470        net_reader_run=TRUE; 
     471        ortp_thread_create(&net_reader_th,NULL,net_thread,NULL); 
     472} 
     473 
     474static void stop_net_reader(void){ 
     475        net_reader_run=FALSE; 
     476        close(server_sock); 
     477        if (sock_unix_path[0]!=0){ 
     478                unlink(sock_unix_path); 
     479        } 
     480        /*ortp_thread_join(net_reader_th,NULL);*/ 
    440481} 
    441482 
     
    447488#else 
    448489        static bool_t prompt_reader_started=FALSE; 
    449         static bool_t tcp_reader_started=FALSE; 
     490        static bool_t net_reader_started=FALSE; 
    450491        if (!prompt_reader_started){ 
    451492                start_prompt_reader(); 
    452493                prompt_reader_started=TRUE; 
    453494        } 
    454         if (tcp_port>0 && !tcp_reader_started){ 
    455                 start_tcp_reader(); 
    456                 tcp_reader_started=TRUE; 
     495        if ((tcp_port>0 || unix_socket) && !net_reader_started){ 
     496                start_net_reader(); 
     497                net_reader_started=TRUE; 
    457498        } 
    458499        fprintf(stdout,"%s",prompt); 
     
    633674        linphonec_finish_readline(); 
    634675#endif 
     676 
     677        if (net_reader_run) 
     678                stop_net_reader(); 
    635679        linphone_core_uninit (&linphonec); 
    636680 
     
    10071051                        if (tcp_port==0) tcp_port=DEFAULT_TCP_PORT; 
    10081052                } 
     1053                else if (strncmp ("--pipe", argv[arg_num], 6) == 0) 
     1054                { 
     1055                        unix_socket=1; 
     1056                } 
    10091057                else if (old_arg_num == arg_num) 
    10101058                { 
  • linphone/console/shell.c

    r248 r256  
    3030#include <sys/socket.h> 
    3131#include <netdb.h> 
     32#include <sys/un.h> 
     33 
    3234#endif 
    3335 
     
    3739#define DEFAULT_REPLY_SIZE 4096 
    3840 
     41#define STATUS_REGISTERED (1<<0) 
     42#define STATUS_REGISTERING (1<<1) 
     43#define STATUS_DIALING (1<<2) 
     44#define STATUS_AUTOANSWER (1<<3) 
     45#define STATUS_IN_CONNECTED (1<<4) /* incoming call accepted */ 
     46#define STATUS_OUT_CONNECTED (1<<5) /*outgoing call accepted */ 
     47 
     48static int tcp=0; 
     49 
     50static int make_status_value(const char *status_string){ 
     51        int ret=0; 
     52        if (strstr(status_string,"registered, identity=")){ 
     53                ret|=STATUS_REGISTERED; 
     54        } 
     55        if (strstr(status_string,"registered=-1")){ 
     56                ret|=STATUS_REGISTERING; 
     57        } 
     58        if (strstr(status_string,"autoanswer=1")){ 
     59                ret|=STATUS_AUTOANSWER; 
     60        } 
     61        if (strstr(status_string,"dialing")){ 
     62                ret|=STATUS_DIALING; 
     63        } 
     64        if (strstr(status_string,"Call out")){ 
     65                ret|=STATUS_OUT_CONNECTED; 
     66        } 
     67        if (strstr(status_string,"hook=answered")){ 
     68                ret|=STATUS_IN_CONNECTED; 
     69        } 
     70        return ret; 
     71} 
     72 
    3973static int send_command(const char *command, const char * port, char *reply, int reply_len, int print_errors){ 
    4074        ortp_socket_t sock; 
    41         struct addrinfo *ai=NULL; 
    42         struct addrinfo hints; 
     75        int i; 
    4376        int err; 
    44         int i; 
    45         memset(&hints,0,sizeof(hints)); 
    46         hints.ai_family=AF_INET; 
    47         hints.ai_socktype=SOCK_STREAM; 
    48         err=getaddrinfo("127.0.0.1",port,&hints,&ai); 
    49         if (err!=0){ 
    50                 if (print_errors) fprintf(stderr,"ERROR: getaddrinfo failed: error %i\n", err); 
     77        if (tcp){ 
     78                struct addrinfo *ai=NULL; 
     79                struct addrinfo hints; 
     80                memset(&hints,0,sizeof(hints)); 
     81                hints.ai_family=AF_INET; 
     82                hints.ai_socktype=SOCK_STREAM; 
     83                err=getaddrinfo("127.0.0.1",port,&hints,&ai); 
     84                if (err!=0){ 
     85                        if (print_errors) fprintf(stderr,"ERROR: getaddrinfo failed: error %i\n", err); 
     86                        return -1; 
     87                } 
     88                sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
     89                if (connect(sock,ai->ai_addr,ai->ai_addrlen)!=0){ 
     90                        if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket.\n"); 
     91                        freeaddrinfo(ai); 
     92                        return -1; 
     93                } 
     94                freeaddrinfo(ai); 
     95        }else{ 
     96#ifndef WIN32 
     97                struct sockaddr_un sa; 
     98                char path[128]; 
     99                sock=socket(AF_UNIX,SOCK_STREAM,0); 
     100                sa.sun_family=AF_UNIX; 
     101                snprintf(path,sizeof(path)-1,"/tmp/linphonec-%i",getuid()); 
     102                strncpy(sa.sun_path,path,sizeof(sa.sun_path)-1); 
     103                if (connect(sock,(struct sockaddr*)&sa,sizeof(sa))!=0){ 
     104                        if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket: %s\n",getSocketError()); 
     105                        return -1; 
     106                } 
     107#else 
     108                fprintf(stderr,"ERROR: windows pipes communication not yet implemented.\n"); 
    51109                return -1; 
    52         } 
    53         sock=socket(PF_INET,SOCK_STREAM,IPPROTO_TCP); 
    54         if (connect(sock,ai->ai_addr,ai->ai_addrlen)!=0){ 
    55                 if (print_errors) fprintf(stderr,"ERROR: Failed to connect socket.\n"); 
    56                 freeaddrinfo(ai); 
    57                 return -1; 
    58         } 
    59         freeaddrinfo(ai); 
     110#endif 
     111        } 
    60112        if (send(sock,command,strlen(command),0)<0){ 
    61113                if (print_errors) fprintf(stderr,"ERROR: Fail to send command to remote linphonec\n"); 
     
    97149        j=0; 
    98150        args[j++]="linphonec"; 
    99         args[j++]="--tcp"; 
    100         args[j++]=DEFAULT_TCP_PORT; 
     151        if (tcp){ 
     152                args[j++]="--tcp"; 
     153                args[j++]=DEFAULT_TCP_PORT; 
     154        }else args[j++]="--pipe"; 
    101155        args[j++]="-c"; 
    102156        args[j++]="/dev/null"; 
     
    196250static int status_execute(int argc, char *argv[]){ 
    197251        char cmd[512]; 
     252        char reply[DEFAULT_REPLY_SIZE]; 
     253        int err; 
     254         
    198255        if (argc==1){ 
    199256                snprintf(cmd,sizeof(cmd),"status %s",argv[0]); 
    200                 return send_generic_command(cmd,TRUE); 
     257                err=send_command(cmd,DEFAULT_TCP_PORT,reply,sizeof(reply),TRUE); 
     258                if (err==0) { 
     259                        printf("%s",reply); 
     260                        err=make_status_value(reply); 
     261                } 
     262                return err; 
    201263        }else{ 
    202264                print_usage(); 
     
    213275        ortp_init(); 
    214276        for(argi=1;argi<argc;++argi){ 
    215                 if (strcmp(argv[argi],"init")==0){ 
     277                if (strcmp(argv[argi],"--tcp")==0){ 
     278                        tcp=1; 
     279                }else if (strcmp(argv[argi],"init")==0){ 
    216280                        /*check if there is running instance*/ 
    217281                        if (send_generic_command("help",0)==0){ 
     
    219283                        } 
    220284                        spawn_linphonec(argc-argi-1,&argv[argi+1]); 
     285                        if (tcp) fprintf(stderr,"WARNING: using --tcp is unsafe: unprivilegied users can make calls.\n"); 
    221286                        return 0; 
    222287                }else if (strcmp(argv[argi],"generic")==0){ 
Note: See TracChangeset for help on using the changeset viewer.