Index: include/asterisk/manager.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/manager.h,v retrieving revision 1.17 diff -u -r1.17 manager.h --- include/asterisk/manager.h 30 Aug 2005 18:32:09 -0000 1.17 +++ include/asterisk/manager.h 18 Sep 2005 06:53:50 -0000 @@ -66,6 +66,18 @@ #define MAX_HEADERS 80 #define MAX_LEN 256 +struct ast_manager_user { + char username[80]; + char *secret; + char *deny; + char *permit; + char *read; + char *write; + unsigned int displayconnects:1; + struct ast_manager_user *next; +}; + + struct mansession { /*! Execution thread */ pthread_t t; @@ -140,6 +152,12 @@ */ int ast_manager_unregister( char *action ); +/*! Add a manager_user to current list of manager */ +int *ast_manager_add(struct ast_manager_user *amu); + +/*! Get an manager by his name */ +struct ast_manager_user *ast_get_manager_by_name_locked(const char *name); + /*! External routines may send asterisk manager events this way */ /*! \param category Event category, matches manager authorization \param event Event name Index: manager.c =================================================================== RCS file: /usr/cvsroot/asterisk/manager.c,v retrieving revision 1.111 diff -u -r1.111 manager.c --- manager.c 14 Sep 2005 20:46:49 -0000 1.111 +++ manager.c 18 Sep 2005 06:52:44 -0000 @@ -79,6 +79,9 @@ static pthread_t t; AST_MUTEX_DEFINE_STATIC(sessionlock); + +static struct ast_manager_user *amus =NULL; +AST_MUTEX_DEFINE_STATIC(amulock); static int block_sockets = 0; static struct permalias { @@ -230,30 +233,122 @@ return RESULT_SUCCESS; } -static char showmancmd_help[] = -"Usage: show manager command \n" -" Shows the detailed description for a specific Asterisk manager interface command.\n"; - -static char showmancmds_help[] = -"Usage: show manager commands\n" -" Prints a listing of all the available Asterisk manager interface commands.\n"; - -static char showmanconn_help[] = -"Usage: show manager connected\n" -" Prints a listing of the users that are currently connected to the\n" -"Asterisk manager interface.\n"; - -static struct ast_cli_entry show_mancmd_cli = - { { "show", "manager", "command", NULL }, - handle_showmancmd, "Show a manager interface command", showmancmd_help, complete_show_mancmd }; - -static struct ast_cli_entry show_mancmds_cli = - { { "show", "manager", "commands", NULL }, - handle_showmancmds, "List manager interface commands", showmancmds_help }; - -static struct ast_cli_entry show_manconn_cli = - { { "show", "manager", "connected", NULL }, - handle_showmanconn, "Show connected manager interface users", showmanconn_help }; + +static int handle_showmanager(int fd, int argc, char *argv[]) +{ + struct ast_manager_user *tmp; + int command; + + if (argc < 3 || argc > 4 ) + return RESULT_SHOWUSAGE; + + if ( !strcasecmp(argv[2],"connected") ) + return handle_showmanconn(fd,argc,argv); + else if ( !strcasecmp(argv[2],"commands") ) + return handle_showmancmds(fd,argc,argv); + + command = (argc > 2 && (!strcasecmp(argv[2],"command"))); + if (command) { + return handle_showmancmd(fd,argc,argv); + } + + + /* try to lock manager_user list ... */ + if (ast_mutex_lock(&amulock)) { + ast_log(LOG_ERROR, "Unable to lock manager_user list\n"); + return -1; + } + + tmp = ast_get_manager_by_name_locked(argv[2]); + + if (!tmp) { + ast_cli(fd, "There are no manager called %s\n",argv[2]); + ast_mutex_unlock(&amulock); + return -1; + } + ast_cli(fd,"\n"); + ast_cli(fd, + " username: %s\n" + " secret: %s\n" + " deny: %s\n" + " permit: %s\n" + " read: %s\n" + " write: %s\n" + "displayconnects: %s\n", + (tmp->username ? tmp->username : "(N/A)"), + (tmp->secret ? tmp->secret : "(N/A)"), + (tmp->deny ? tmp->deny : "(N/A)"), + (tmp->permit ? tmp->permit : "(N/A)"), + (tmp->read ? tmp->read : "(N/A)"), + (tmp->write ? tmp->write : "(N/A)"), + (tmp->displayconnects ? "yes" : "no")); + ast_mutex_unlock(&amulock); + + return RESULT_SUCCESS; +} + + +static int handle_showmanagers(int fd, int argc, char *argv[]) { +#define FORMAT "%-35.35s %-35.35s\n" +#define CONCISE_FORMAT "%s:%s\n" + struct ast_manager_user *tmp; + int count_amu = 0, concise = 0; + + if ( argc > 3 ) + return RESULT_SHOWUSAGE; + + concise = (argc == 3 && (!strcasecmp(argv[2],"concise"))); + + /* try to lock manager_user list ... */ + if (ast_mutex_lock(&amulock)) { + ast_log(LOG_ERROR, "Unable to lock manager_user list\n"); + return -1; + } + + tmp=amus; + if (!tmp) { + ast_cli(fd, "There are no manager user.\n"); + ast_mutex_unlock(&amulock); + return -1; + } + if (concise) { + ast_cli(fd,"\n"); + while (tmp) { + ast_cli(fd, CONCISE_FORMAT,tmp->username, (tmp->secret?tmp->secret: "(N/A)")); + tmp = tmp->next; + } + return RESULT_SUCCESS; + } + ast_cli(fd, FORMAT, "username", "secret"); + ast_cli(fd, FORMAT, "--------", "------"); + while (tmp) { + ast_cli(fd, FORMAT,tmp->username, (tmp->secret?tmp->secret: "(N/A)")); + tmp = tmp->next; + count_amu++; + } + ast_mutex_unlock(&amulock); + ast_cli(fd,"-------------------\n"); + ast_cli(fd,"%d manager users configured.\n", count_amu); + return RESULT_SUCCESS; +} + +static char showmanagers_help[] = +"Usage: show managers\n" +" Prints a listing of all managers that are currently configured on that\n" +" system.\n"; + +static char showmanager_help[] = +" Usage: show manager foobar\n" +" Display all the infos related to the manager foobar.\n"; + + +static struct ast_cli_entry show_managers_cli = + { { "show", "managers", NULL }, + handle_showmanagers, "Show all managers users (connected or not)", showmanagers_help }; + +static struct ast_cli_entry show_manager_cli = +{ { "show", "manager", NULL }, handle_showmanager, "Display information on a specific manager", showmanager_help}; + static void destroy_session(struct mansession *s) { @@ -1544,12 +1639,25 @@ return 0; } +int *ast_manager_add(struct ast_manager_user *amu) { + if ( !amu) { + ast_log(LOG_DEBUG, "You cant pass NULL to that function"); + return NULL; + } + ast_mutex_lock(&amulock); + amu->next = amus; + amus = amu; + ast_mutex_unlock(&amulock); + return NULL; +} + + static int registered = 0; int init_manager(void) { struct ast_config *cfg; - char *val; + char *val,*cat; int oldportno = portno; static struct sockaddr_in ba; int x = 1; @@ -1571,9 +1679,8 @@ ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount ); ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands); - ast_cli_register(&show_mancmd_cli); - ast_cli_register(&show_mancmds_cli); - ast_cli_register(&show_manconn_cli); + ast_cli_register(&show_managers_cli); + ast_cli_register(&show_manager_cli); ast_extension_state_add(NULL, NULL, manager_state_cb, NULL); registered = 1; } @@ -1631,6 +1738,59 @@ ast_log(LOG_WARNING, "Unable to change management port / enabled\n"); #endif } + + cat = ast_category_browse(cfg, NULL); + amus=NULL; /* Resetting the boss */ + while(cat) { + if (!strcasecmp(cat, "general")) { + ast_log(LOG_NOTICE, "ignoring the cat general\n"); + cat = ast_category_browse(cfg, cat); + continue; + } + + struct ast_manager_user *amu = malloc(sizeof(struct ast_manager_user)); + memset(amu, 0, sizeof(struct ast_manager_user)); + struct ast_variable *var; + var = ast_variable_browse(cfg, cat); + + while (var) { + if (!strcasecmp(var->name, "secret")) { + if (amu->secret) + free(amu->secret); + amu->secret=strdup(var->value); + } else if (!strcasecmp(var->name, "deny") ) { + if (amu->deny) + free(amu->deny); + amu->deny=strdup(var->value); + } else if (!strcasecmp(var->name, "permit") ) { + if (amu->permit) + free(amu->permit); + amu->permit=strdup(var->value); + } else if (!strcasecmp(var->name, "read") ) { + if (amu->read) + free(amu->read); + amu->read=strdup(var->value); + } else if (!strcasecmp(var->name, "write") ) { + if (amu->write) + free(amu->write); + amu->write=strdup(var->value); + } else if (!strcasecmp(var->name, "displayconnects") ) { + amu->displayconnects=ast_true(var->value); + } else { + ast_log(LOG_DEBUG, "%s is unknown.\n",var->name); + } + var = var->next; + } + + ast_copy_string(amu->username,cat,sizeof(amu->username)); + + ast_log(LOG_DEBUG, "Adding %s\n",amu->username); + ast_manager_add(amu); + amu=NULL; + cat = ast_category_browse(cfg, cat); + } + + ast_config_destroy(cfg); /* If not enabled, do nothing */ @@ -1668,3 +1828,19 @@ manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n"); return init_manager(); } + + +struct ast_manager_user *ast_get_manager_by_name_locked(const char *name) +{ + struct ast_manager_user *tmp = NULL; + tmp=amus; + while (tmp) { + if (!strcasecmp(tmp->username, name)) + break; + tmp = tmp->next; + } + if ( tmp) + return tmp; + return NULL; +} +