638c951b06f3c1cd7363956e3372e8061115f571
[asterisk/asterisk.git] / manager.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Channel Management and more
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <pthread.h>
17 #include <string.h>
18 #include <sys/time.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 #include <signal.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <asterisk/channel.h>
26 #include <asterisk/file.h>
27 #include <asterisk/manager.h>
28 #include <asterisk/config.h>
29 #include <asterisk/lock.h>
30 #include <asterisk/logger.h>
31 #include <asterisk/options.h>
32 #include <asterisk/cli.h>
33 #include <asterisk/app.h>
34 #include <asterisk/pbx.h>
35
36 static int enabled = 0;
37 static int portno = DEFAULT_MANAGER_PORT;
38 static int asock = -1;
39 static pthread_t t;
40 static pthread_mutex_t sessionlock = AST_MUTEX_INITIALIZER;
41
42 static struct permalias {
43         int num;
44         char *label;
45 } perms[] = {
46         { EVENT_FLAG_SYSTEM, "system" },
47         { EVENT_FLAG_CALL, "call" },
48         { EVENT_FLAG_LOG, "log" },
49         { EVENT_FLAG_VERBOSE, "verbose" },
50         { EVENT_FLAG_COMMAND, "command" },
51         { EVENT_FLAG_AGENT, "agent" },
52         { -1, "all" },
53 };
54
55 static struct mansession *sessions = NULL;
56 static struct manager_action *first_action = NULL;
57 static pthread_mutex_t actionlock = AST_MUTEX_INITIALIZER;
58
59 static int handle_showmancmds(int fd, int argc, char *argv[])
60 {
61         struct manager_action *cur = first_action;
62
63         ast_pthread_mutex_lock(&actionlock);
64         while(cur) { /* Walk the list of actions */
65                 ast_cli(fd, "\t%s  %s\r\n",cur->action, cur->synopsis);
66                 cur = cur->next;
67         }
68
69         ast_pthread_mutex_unlock(&actionlock);
70         return RESULT_SUCCESS;
71 }
72
73 static int handle_showmanconn(int fd, int argc, char *argv[])
74 {
75         struct mansession *s;
76
77         ast_pthread_mutex_lock(&sessionlock);
78         s = sessions;
79         ast_cli(fd, "  Username\tIP Address\n");
80         while(s) {
81                 ast_cli(fd, "  %s\t\t%s\r\n",s->username, inet_ntoa(s->sin.sin_addr));
82                 s = s->next;
83         }
84
85         ast_pthread_mutex_unlock(&sessionlock);
86         return RESULT_SUCCESS;
87 }
88
89 static char showmancmds_help[] = 
90 "Usage: show manager commands\n"
91 "       Prints a listing of all the available manager commands.\n";
92
93 static char showmanconn_help[] = 
94 "Usage: show manager connected\n"
95 "       Prints a listing of the users that are connected to the\n"
96 "manager interface.\n";
97
98 static struct ast_cli_entry show_mancmds_cli =
99         { { "show", "manager", "commands", NULL },
100         handle_showmancmds, "Show manager commands", showmancmds_help };
101
102 static struct ast_cli_entry show_manconn_cli =
103         { { "show", "manager", "connected", NULL },
104         handle_showmanconn, "Show connected manager users", showmanconn_help };
105
106 static void destroy_session(struct mansession *s)
107 {
108         struct mansession *cur, *prev = NULL;
109         ast_pthread_mutex_lock(&sessionlock);
110         cur = sessions;
111         while(cur) {
112                 if (cur == s)
113                         break;
114                 prev = cur;
115                 cur = cur->next;
116         }
117         if (cur) {
118                 if (prev)
119                         prev->next = cur->next;
120                 else
121                         sessions = cur->next;
122                 if (s->fd > -1)
123                         close(s->fd);
124                 free(s);
125         } else
126                 ast_log(LOG_WARNING, "Trying to delete non-existant session %p?\n", s);
127         ast_pthread_mutex_unlock(&sessionlock);
128         
129 }
130
131 static char *get_header(struct message *m, char *var)
132 {
133         char cmp[80];
134         int x;
135         snprintf(cmp, sizeof(cmp), "%s: ", var);
136         for (x=0;x<m->hdrcount;x++)
137                 if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
138                         return m->headers[x] + strlen(cmp);
139         return "";
140 }
141
142 static void send_error(struct mansession *s, char *error)
143 {
144         ast_pthread_mutex_lock(&s->lock);
145         ast_cli(s->fd, "Response: Error\r\n");
146         ast_cli(s->fd, "Message: %s\r\n\r\n", error);
147         ast_pthread_mutex_unlock(&s->lock);
148 }
149
150 static void send_response(struct mansession *s, char *resp, char *msg)
151 {
152         ast_pthread_mutex_lock(&s->lock);
153         ast_cli(s->fd, "Response: %s\r\n", resp);
154         if (msg)
155                 ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
156         else
157                 ast_cli(s->fd, "\r\n");
158         ast_pthread_mutex_unlock(&s->lock);
159 }
160
161 static void send_ack(struct mansession *s, char *msg)
162 {
163         send_response(s, "Success", msg);
164 }
165
166 static int get_perm(char *instr)
167 {
168         char tmp[256];
169         char *c;
170         int x;
171         int ret = 0;
172         char *stringp=NULL;
173         if (!instr)
174                 return 0;
175         strncpy(tmp, instr, sizeof(tmp) - 1);
176         stringp=tmp;
177         c = strsep(&stringp, ",");
178         while(c) {
179                 for (x=0;x<sizeof(perms) / sizeof(perms[0]);x++) {
180                         if (!strcasecmp(perms[x].label, c)) 
181                                 ret |= perms[x].num;
182                 }
183                 c = strsep(&stringp, ",");
184         }
185         return ret;
186 }
187
188 static int authenticate(struct mansession *s, struct message *m)
189 {
190         struct ast_config *cfg;
191         char *cat;
192         char *user = get_header(m, "Username");
193         char *pass = get_header(m, "Secret");
194         cfg = ast_load("manager.conf");
195         if (!cfg)
196                 return -1;
197         cat = ast_category_browse(cfg, NULL);
198         while(cat) {
199                 if (strcasecmp(cat, "general")) {
200                         /* This is a user */
201                         if (!strcasecmp(cat, user)) {
202                                 char *password = ast_variable_retrieve(cfg, cat, "secret");
203                                 if (password && !strcasecmp(password, pass)) {
204                                         break;
205                                 } else {
206                                         ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", inet_ntoa(s->sin.sin_addr), user);
207                                         ast_destroy(cfg);
208                                         return -1;
209                                 }       
210                         }
211                 }
212                 cat = ast_category_browse(cfg, cat);
213         }
214         if (cat) {
215                 strncpy(s->username, cat, sizeof(s->username) - 1);
216                 s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
217                 s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
218                 ast_destroy(cfg);
219                 return 0;
220         }
221         ast_log(LOG_NOTICE, "%s tried to authenticate with non-existant user '%s'\n", inet_ntoa(s->sin.sin_addr), user);
222         ast_destroy(cfg);
223         return -1;
224 }
225
226 static int action_ping(struct mansession *s, struct message *m)
227 {
228         send_response(s, "Pong", NULL);
229         return 0;
230 }
231
232 static int action_logoff(struct mansession *s, struct message *m)
233 {
234         send_response(s, "Goodbye", "Thanks for all the fish.");
235         return -1;
236 }
237
238 static int action_hangup(struct mansession *s, struct message *m)
239 {
240         struct ast_channel *c = NULL;
241         char *name = get_header(m, "Channel");
242         if (!strlen(name)) {
243                 send_error(s, "No channel specified");
244                 return 0;
245         }
246         c = ast_channel_walk(NULL);
247         while(c) {
248                 if (!strcasecmp(c->name, name)) {
249                         break;
250                 }
251                 c = ast_channel_walk(c);
252         }
253         if (!c) {
254                 send_error(s, "No such channel");
255                 return 0;
256         }
257         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
258         send_ack(s, "Channel Hungup");
259         return 0;
260 }
261
262 static int action_status(struct mansession *s, struct message *m)
263 {
264         struct ast_channel *c;
265         char bridge[256];
266         send_ack(s, "Channel status will follow");
267         c = ast_channel_walk(NULL);
268         while(c) {
269                 if (c->bridge)
270                         snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->bridge->name);
271                 else
272                         strcpy(bridge, "");
273                 if (c->pbx) {
274                         ast_cli(s->fd,
275                         "Event: Status\r\n"
276                         "Channel: %s\r\n"
277                         "CallerID: %s\r\n"
278                         "State: %s\r\n"
279                         "Context: %s\r\n"
280                         "Extension: %s\r\n"
281                         "Priority: %d\r\n"
282                         "%s"
283                         "\r\n",
284                         c->name, c->callerid ? c->callerid : "<unknown>", 
285                         ast_state2str(c->_state), c->context,
286                         c->exten, c->priority, bridge);
287                 } else {
288                         ast_cli(s->fd,
289                         "Event: Status\r\n"
290                         "Channel: %s\r\n"
291                         "CallerID: %s\r\n"
292                         "State: %s\r\n"
293                         "%s"
294                         "\r\n",
295                         c->name, c->callerid ? c->callerid : "<unknown>", 
296                         ast_state2str(c->_state), bridge);
297                 }
298                 c = ast_channel_walk(c);
299         }
300         return 0;
301 }
302
303 static int action_redirect(struct mansession *s, struct message *m)
304 {
305         char *name = get_header(m, "Channel");
306         char *name2 = get_header(m, "ExtraChannel");
307         char *exten = get_header(m, "Exten");
308         char *context = get_header(m, "Context");
309         char *priority = get_header(m, "Priority");
310         int pi = 0;
311         int res;
312         if (!name || !strlen(name)) {
313                 send_error(s, "Channel not specified");
314                 return 0;
315         }
316         if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
317                 send_error(s, "Invalid priority\n");
318                 return 0;
319         }
320         res = ast_async_goto_by_name(name, context, exten, pi);
321         if (!res) {
322                 if (strlen(name2)) {
323                         res = ast_async_goto_by_name(name2, context, exten, pi);
324                         if (!res)
325                                 send_ack(s, "Dual Redirect successful");
326                         else
327                                 send_error(s, "Secondary redirect failed");
328                 } else
329                         send_ack(s, "Redirect successful");
330         } else
331                 send_error(s, "Redirect failed");
332         return 0;
333 }
334
335 static int action_command(struct mansession *s, struct message *m)
336 {
337         char *cmd = get_header(m, "Command");
338         ast_pthread_mutex_lock(&s->lock);
339         s->blocking = 1;
340         ast_pthread_mutex_unlock(&s->lock);
341         ast_cli(s->fd, "Response: Follows\r\n");
342         ast_cli_command(s->fd, cmd);
343         ast_cli(s->fd, "--END COMMAND--\r\n\r\n");
344         ast_pthread_mutex_lock(&s->lock);
345         s->blocking = 0;
346         ast_pthread_mutex_unlock(&s->lock);
347         return 0;
348 }
349
350 static int action_originate(struct mansession *s, struct message *m)
351 {
352         char *name = get_header(m, "Channel");
353         char *exten = get_header(m, "Exten");
354         char *context = get_header(m, "Context");
355         char *priority = get_header(m, "Priority");
356         char *timeout = get_header(m, "Timeout");
357         char *callerid = get_header(m, "CallerID");
358         char *tech, *data;
359         int pi = 0;
360         int res;
361         int to = 30000;
362         int reason = 0;
363         char tmp[256];
364         if (!name) {
365                 send_error(s, "Channel not specified");
366                 return 0;
367         }
368         if (strlen(priority) && (sscanf(priority, "%d", &pi) != 1)) {
369                 send_error(s, "Invalid priority\n");
370                 return 0;
371         }
372         if (strlen(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
373                 send_error(s, "Invalid timeout\n");
374                 return 0;
375         }
376         strncpy(tmp, name, sizeof(tmp) - 1);
377         tech = tmp;
378         data = strchr(tmp, '/');
379         if (!data) {
380                 send_error(s, "Invalid channel\n");
381                 return 0;
382         }
383         *data = '\0';
384         data++;
385         res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 0, strlen(callerid) ? callerid : NULL, NULL );
386         if (!res)
387                 send_ack(s, "Originate successfully queued");
388         else
389                 send_error(s, "Originate failed");
390         return 0;
391 }
392
393 static int action_mailboxstatus(struct mansession *s, struct message *m)
394 {
395         char *mailbox = get_header(m, "Mailbox");
396         if (!mailbox || !strlen(mailbox)) {
397                 send_error(s, "Mailbox not specified");
398                 return 0;
399         }
400         send_ack(s, "Mailbox status will follow");
401         manager_event(EVENT_FLAG_CALL, "MessageWaiting", "Mailbox: %s\r\nWaiting:%d\r\n", mailbox, ast_app_has_voicemail(mailbox));
402         return 0;
403 }
404
405 static int process_message(struct mansession *s, struct message *m)
406 {
407         char action[80];
408         struct manager_action *tmp = first_action;
409
410         strncpy(action, get_header(m, "Action"), sizeof(action));
411
412         if (!strlen(action)) {
413                 send_error(s, "Missing action in request");
414                 return 0;
415         }
416         if (!s->authenticated) {
417                 if (!strcasecmp(action, "Login")) {
418                         if (authenticate(s, m)) {
419                                 sleep(1);
420                                 send_error(s, "Authentication failed");
421                                 return -1;
422                         } else {
423                                 s->authenticated = 1;
424                                 if (option_verbose > 1) 
425                                         ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
426                                 ast_log(LOG_EVENT, "Manager '%s' logged on from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
427                                 send_ack(s, "Authentication accepted");
428                         }
429                 } else 
430                         send_error(s, "Authentication Required");
431         } else {
432                 while( tmp ) {          
433                         if (!strcasecmp(action, tmp->action)) {
434                                 if ((s->writeperm & tmp->authority) == tmp->authority) {
435                                         if (tmp->func(s, m))
436                                                 return -1;
437                                 } else {
438                                         send_error(s, "Permission denied");
439                                 }
440                                 return 0;
441                         }
442                         tmp = tmp->next;
443                 }
444                 send_error(s, "Invalid/unknown command");
445         }
446         return 0;
447 }
448
449 static int get_input(struct mansession *s, char *output)
450 {
451         /* output must have at least sizeof(s->inbuf) space */
452         int res;
453         int x;
454         fd_set fds;
455         for (x=1;x<s->inlen;x++) {
456                 if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
457                         /* Copy output data up to and including \r\n */
458                         memcpy(output, s->inbuf, x + 1);
459                         /* Add trailing \0 */
460                         output[x+1] = '\0';
461                         /* Move remaining data back to the front */
462                         memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
463                         s->inlen -= (x + 1);
464                         return 1;
465                 }
466         } 
467         if (s->inlen >= sizeof(s->inbuf) - 1) {
468                 ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", inet_ntoa(s->sin.sin_addr), s->inbuf);
469                 s->inlen = 0;
470         }
471         FD_ZERO(&fds);
472         FD_SET(s->fd, &fds);
473         res = select(s->fd + 1, &fds, NULL, NULL, NULL);
474         if (res < 0) {
475                 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
476         } else if (res > 0) {
477                 ast_pthread_mutex_lock(&s->lock);
478                 res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
479                 ast_pthread_mutex_unlock(&s->lock);
480                 if (res < 1)
481                         return -1;
482         }
483         s->inlen += res;
484         s->inbuf[s->inlen] = '\0';
485         return 0;
486 }
487
488 static void *session_do(void *data)
489 {
490         struct mansession *s = data;
491         struct message m;
492         int res;
493         
494         ast_pthread_mutex_lock(&s->lock);
495         ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
496         ast_pthread_mutex_unlock(&s->lock);
497         memset(&m, 0, sizeof(&m));
498         for (;;) {
499                 res = get_input(s, m.headers[m.hdrcount]);
500                 if (res > 0) {
501                         /* Strip trailing \r\n */
502                         if (strlen(m.headers[m.hdrcount]) < 2)
503                                 continue;
504                         m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
505                         if (!strlen(m.headers[m.hdrcount])) {
506                                 if (process_message(s, &m))
507                                         break;
508                                 memset(&m, 0, sizeof(&m));
509                         } else if (m.hdrcount < MAX_HEADERS - 1)
510                                 m.hdrcount++;
511                 } else if (res < 0)
512                         break;
513         }
514         if (s->authenticated) {
515                 if (option_verbose > 1) 
516                         ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
517                 ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", s->username, inet_ntoa(s->sin.sin_addr));
518         } else {
519                 if (option_verbose > 1)
520                         ast_verbose(VERBOSE_PREFIX_2 "Connect attempt from '%s' unable to authenticate\n", inet_ntoa(s->sin.sin_addr));
521                 ast_log(LOG_EVENT, "Failed attempt from %s\n", inet_ntoa(s->sin.sin_addr));
522         }
523         destroy_session(s);
524         return NULL;
525 }
526
527 static void *accept_thread(void *ignore)
528 {
529         int as;
530         struct sockaddr_in sin;
531         int sinlen;
532         struct mansession *s;
533         pthread_attr_t attr;
534
535         pthread_attr_init(&attr);
536         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
537         for (;;) {
538                 sinlen = sizeof(sin);
539                 as = accept(asock, &sin, &sinlen);
540                 if (as < 0) {
541                         ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
542                         continue;
543                 }
544                 s = malloc(sizeof(struct mansession));
545                 if (!s) {
546                         ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));
547                         continue;
548                 } 
549                 memset(s, 0, sizeof(struct mansession));
550                 memcpy(&s->sin, &sin, sizeof(sin));
551                 ast_pthread_mutex_init(&s->lock);
552                 s->fd = as;
553                 ast_pthread_mutex_lock(&sessionlock);
554                 s->next = sessions;
555                 sessions = s;
556                 ast_pthread_mutex_unlock(&sessionlock);
557                 if (pthread_create(&t, &attr, session_do, s))
558                         destroy_session(s);
559         }
560         pthread_attr_destroy(&attr);
561         return NULL;
562 }
563
564 int manager_event(int category, char *event, char *fmt, ...)
565 {
566         struct mansession *s;
567         char tmp[4096];
568         va_list ap;
569
570         ast_pthread_mutex_lock(&sessionlock);
571         s = sessions;
572         while(s) {
573                 if ((s->readperm & category) == category) {
574                         ast_pthread_mutex_lock(&s->lock);
575                         if (!s->blocking) {
576                                 ast_cli(s->fd, "Event: %s\r\n", event);
577                                 va_start(ap, fmt);
578                                 vsnprintf(tmp, sizeof(tmp), fmt, ap);
579                                 va_end(ap);
580                                 write(s->fd, tmp, strlen(tmp));
581                                 ast_cli(s->fd, "\r\n");
582                         }
583                         ast_pthread_mutex_unlock(&s->lock);
584                 }
585                 s = s->next;
586         }
587         ast_pthread_mutex_unlock(&sessionlock);
588         return 0;
589 }
590
591 int ast_manager_unregister( char *action ) {
592         struct manager_action *cur = first_action, *prev = first_action;
593
594         ast_pthread_mutex_lock(&actionlock);
595         while( cur ) {          
596                 if (!strcasecmp(action, cur->action)) {
597                         prev->next = cur->next;
598                         free(cur);
599                         if (option_verbose > 1) 
600                                 ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
601                         ast_pthread_mutex_unlock(&actionlock);
602                         return 0;
603                 }
604                 prev = cur;
605                 cur = cur->next;
606         }
607         ast_pthread_mutex_unlock(&actionlock);
608         return 0;
609 }
610
611 int ast_manager_register( char *action, int auth, 
612         int (*func)(struct mansession *s, struct message *m), char *synopsis)
613 {
614         struct manager_action *cur = first_action, *prev = NULL;
615
616         ast_pthread_mutex_lock(&actionlock);
617         while(cur) { /* Walk the list of actions */
618                 prev = cur; 
619                 cur = cur->next;
620         }
621         cur = malloc( sizeof(struct manager_action) );
622         if( !cur ) {
623                 ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
624                 ast_pthread_mutex_unlock(&actionlock);
625                 return -1;
626         }
627         strncpy( cur->action, action, 255 );
628         cur->authority = auth;
629         cur->func = func;
630         cur->synopsis = synopsis;
631         cur->next = NULL;
632
633         if( prev ) prev->next = cur;
634         else first_action = cur;
635
636         if (option_verbose > 1) 
637                 ast_verbose(VERBOSE_PREFIX_2 "Manager registered action %s\n", action);
638         ast_pthread_mutex_unlock(&actionlock);
639         return 0;
640 }
641
642 static int registered = 0;
643
644 int init_manager(void)
645 {
646         struct ast_config *cfg;
647         char *val;
648         int oldportno = portno;
649         static struct sockaddr_in ba;
650         int x = 1;
651         if (!registered) {
652                 /* Register default actions */
653                 ast_manager_register( "Ping", 0, action_ping, "Ping" );
654                 ast_manager_register( "Logoff", 0, action_logoff, "Logoff Manager" );
655                 ast_manager_register( "Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel" );
656                 ast_manager_register( "Status", EVENT_FLAG_CALL, action_status, "Status" );
657                 ast_manager_register( "Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect" );
658                 ast_manager_register( "Originate", EVENT_FLAG_CALL, action_originate, "Originate Call" );
659                 ast_manager_register( "MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox" );
660                 ast_manager_register( "Command", EVENT_FLAG_COMMAND, action_command, "Execute Command" );
661
662                 ast_cli_register(&show_mancmds_cli);
663                 ast_cli_register(&show_manconn_cli);
664                 registered = 1;
665         }
666         portno = DEFAULT_MANAGER_PORT;
667         cfg = ast_load("manager.conf");
668         if (!cfg) {
669                 ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf.  Call management disabled.\n");
670                 return 0;
671         }
672         memset(&ba, 0, sizeof(ba));
673         val = ast_variable_retrieve(cfg, "general", "enabled");
674         if (val)
675                 enabled = ast_true(val);
676
677         if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
678                 if (sscanf(val, "%d", &portno) != 1) {
679                         ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
680                         portno = DEFAULT_MANAGER_PORT;
681                 }
682         }
683         
684         ba.sin_family = AF_INET;
685         ba.sin_port = htons(portno);
686         memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
687         
688         if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
689                 if (!inet_aton(val, &ba.sin_addr)) { 
690                         ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
691                         memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
692                 }
693         }
694
695         if ((asock > -1) && ((portno != oldportno) || !enabled)) {
696 #if 0
697                 /* Can't be done yet */
698                 close(asock);
699                 asock = -1;
700 #else
701                 ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
702 #endif
703         }
704         /* If not enabled, do nothing */
705         if (!enabled)
706                 return 0;
707         if (asock < 0) {
708                 asock = socket(AF_INET, SOCK_STREAM, 0);
709                 if (asock < 0) {
710                         ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
711                         return -1;
712                 }
713                 setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
714                 if (bind(asock, &ba, sizeof(ba))) {
715                         ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
716                         close(asock);
717                         asock = -1;
718                         return -1;
719                 }
720                 if (listen(asock, 2)) {
721                         ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
722                         close(asock);
723                         asock = -1;
724                         return -1;
725                 }
726                 if (option_verbose)
727                         ast_verbose("Asterisk Management interface listening on port %d\n", portno);
728                 pthread_create(&t, NULL, accept_thread, NULL);
729         }
730         ast_destroy(cfg);
731         return 0;
732 }
733
734 int reload_manager(void)
735 {
736         return init_manager();
737 }