add missing break (issue #5154)
[asterisk/asterisk.git] / manager.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * The Asterisk Management Interface - AMI
5  *
6  * Channel Management and more
7  * 
8  * Copyright (C) 1999 - 2005, Digium, Inc.
9  *
10  * Mark Spencer <markster@digium.com>
11  *
12  * This program is free software, distributed under the terms of
13  * the GNU General Public License
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 #include <netdb.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/tcp.h>
25 #include <arpa/inet.h>
26 #include <signal.h>
27 #include <errno.h>
28 #include <unistd.h>
29
30 #include "asterisk.h"
31
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
33
34 #include "asterisk/channel.h"
35 #include "asterisk/file.h"
36 #include "asterisk/manager.h"
37 #include "asterisk/config.h"
38 #include "asterisk/callerid.h"
39 #include "asterisk/lock.h"
40 #include "asterisk/logger.h"
41 #include "asterisk/options.h"
42 #include "asterisk/cli.h"
43 #include "asterisk/app.h"
44 #include "asterisk/pbx.h"
45 #include "asterisk/md5.h"
46 #include "asterisk/acl.h"
47 #include "asterisk/utils.h"
48
49 struct fast_originate_helper {
50         char tech[256];
51         char data[256];
52         int timeout;
53         char app[256];
54         char appdata[256];
55         char cid_name[256];
56         char cid_num[256];
57         char context[256];
58         char exten[256];
59         char idtext[256];
60         int priority;
61         struct ast_variable *vars;
62 };
63
64 static int enabled = 0;
65 static int portno = DEFAULT_MANAGER_PORT;
66 static int asock = -1;
67 static int displayconnects = 1;
68
69 static pthread_t t;
70 AST_MUTEX_DEFINE_STATIC(sessionlock);
71 static int block_sockets = 0;
72
73 static struct permalias {
74         int num;
75         char *label;
76 } perms[] = {
77         { EVENT_FLAG_SYSTEM, "system" },
78         { EVENT_FLAG_CALL, "call" },
79         { EVENT_FLAG_LOG, "log" },
80         { EVENT_FLAG_VERBOSE, "verbose" },
81         { EVENT_FLAG_COMMAND, "command" },
82         { EVENT_FLAG_AGENT, "agent" },
83         { EVENT_FLAG_USER, "user" },
84         { -1, "all" },
85         { 0, "none" },
86 };
87
88 static struct mansession *sessions = NULL;
89 static struct manager_action *first_action = NULL;
90 AST_MUTEX_DEFINE_STATIC(actionlock);
91
92 /* If you are calling carefulwrite, it is assumed that you are calling
93    it on a file descriptor that _DOES_ have NONBLOCK set.  This way,
94    there is only one system call made to do a write, unless we actually
95    have a need to wait.  This way, we get better performance. */
96 int ast_carefulwrite(int fd, char *s, int len, int timeoutms) 
97 {
98         /* Try to write string, but wait no more than ms milliseconds
99            before timing out */
100         int res=0;
101         struct pollfd fds[1];
102         while(len) {
103                 res = write(fd, s, len);
104                 if ((res < 0) && (errno != EAGAIN)) {
105                         return -1;
106                 }
107                 if (res < 0) res = 0;
108                 len -= res;
109                 s += res;
110                 fds[0].fd = fd;
111                 fds[0].events = POLLOUT;
112                 /* Wait until writable again */
113                 res = poll(fds, 1, timeoutms);
114                 if (res < 1)
115                         return -1;
116         }
117         return res;
118 }
119
120 /*--- authority_to_str: Convert authority code to string with serveral options */
121 static char *authority_to_str(int authority, char *res, int reslen)
122 {
123         int running_total = 0, i;
124         memset(res, 0, reslen);
125         for (i=0; i<sizeof(perms) / sizeof(perms[0]) - 1; i++) {
126                 if (authority & perms[i].num) {
127                         if (*res) {
128                                 strncat(res, ",", (reslen > running_total) ? reslen - running_total : 0);
129                                 running_total++;
130                         }
131                         strncat(res, perms[i].label, (reslen > running_total) ? reslen - running_total : 0);
132                         running_total += strlen(perms[i].label);
133                 }
134         }
135         if (ast_strlen_zero(res)) {
136                 ast_copy_string(res, "<none>", reslen);
137         }
138         return res;
139 }
140
141 static char *complete_show_mancmd(char *line, char *word, int pos, int state)
142 {
143         struct manager_action *cur = first_action;
144         int which = 0;
145
146         ast_mutex_lock(&actionlock);
147         while (cur) { /* Walk the list of actions */
148                 if (!strncasecmp(word, cur->action, strlen(word))) {
149                         if (++which > state) {
150                                 char *ret = strdup(cur->action);
151                                 ast_mutex_unlock(&actionlock);
152                                 return ret;
153                         }
154                 }
155                 cur = cur->next;
156         }
157         ast_mutex_unlock(&actionlock);
158         return NULL;
159 }
160
161 static int handle_showmancmd(int fd, int argc, char *argv[])
162 {
163         struct manager_action *cur = first_action;
164         char authority[80];
165         int num;
166
167         if (argc != 4)
168                 return RESULT_SHOWUSAGE;
169         ast_mutex_lock(&actionlock);
170         while (cur) { /* Walk the list of actions */
171                 for (num = 3; num < argc; num++) {
172                         if (!strcasecmp(cur->action, argv[num])) {
173                                 ast_cli(fd, "Action: %s\nSynopsis: %s\nPrivilege: %s\n%s\n", cur->action, cur->synopsis, authority_to_str(cur->authority, authority, sizeof(authority) -1), cur->description ? cur->description : "");
174                         }
175                 }
176                 cur = cur->next;
177         }
178
179         ast_mutex_unlock(&actionlock);
180         return RESULT_SUCCESS;
181 }
182
183 /*--- handle_showmancmds: CLI command */
184 /* Should change to "manager show commands" */
185 static int handle_showmancmds(int fd, int argc, char *argv[])
186 {
187         struct manager_action *cur = first_action;
188         char authority[80];
189         char *format = "  %-15.15s  %-15.15s  %-55.55s\n";
190
191         ast_mutex_lock(&actionlock);
192         ast_cli(fd, format, "Action", "Privilege", "Synopsis");
193         ast_cli(fd, format, "------", "---------", "--------");
194         while (cur) { /* Walk the list of actions */
195                 ast_cli(fd, format, cur->action, authority_to_str(cur->authority, authority, sizeof(authority) -1), cur->synopsis);
196                 cur = cur->next;
197         }
198
199         ast_mutex_unlock(&actionlock);
200         return RESULT_SUCCESS;
201 }
202
203 /*--- handle_showmanconn: CLI command show manager connected */
204 /* Should change to "manager show connected" */
205 static int handle_showmanconn(int fd, int argc, char *argv[])
206 {
207         struct mansession *s;
208         char iabuf[INET_ADDRSTRLEN];
209         char *format = "  %-15.15s  %-15.15s\n";
210         ast_mutex_lock(&sessionlock);
211         s = sessions;
212         ast_cli(fd, format, "Username", "IP Address");
213         while (s) {
214                 ast_cli(fd, format,s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
215                 s = s->next;
216         }
217
218         ast_mutex_unlock(&sessionlock);
219         return RESULT_SUCCESS;
220 }
221
222 static char showmancmd_help[] = 
223 "Usage: show manager command <actionname>\n"
224 "       Shows the detailed description for a specific Asterisk manager interface command.\n";
225
226 static char showmancmds_help[] = 
227 "Usage: show manager commands\n"
228 "       Prints a listing of all the available Asterisk manager interface commands.\n";
229
230 static char showmanconn_help[] = 
231 "Usage: show manager connected\n"
232 "       Prints a listing of the users that are currently connected to the\n"
233 "Asterisk manager interface.\n";
234
235 static struct ast_cli_entry show_mancmd_cli =
236         { { "show", "manager", "command", NULL },
237         handle_showmancmd, "Show a manager interface command", showmancmd_help, complete_show_mancmd };
238
239 static struct ast_cli_entry show_mancmds_cli =
240         { { "show", "manager", "commands", NULL },
241         handle_showmancmds, "List manager interface commands", showmancmds_help };
242
243 static struct ast_cli_entry show_manconn_cli =
244         { { "show", "manager", "connected", NULL },
245         handle_showmanconn, "Show connected manager interface users", showmanconn_help };
246
247 static void destroy_session(struct mansession *s)
248 {
249         struct mansession *cur, *prev = NULL;
250         ast_mutex_lock(&sessionlock);
251         cur = sessions;
252         while(cur) {
253                 if (cur == s)
254                         break;
255                 prev = cur;
256                 cur = cur->next;
257         }
258         if (cur) {
259                 if (prev)
260                         prev->next = cur->next;
261                 else
262                         sessions = cur->next;
263                 if (s->fd > -1)
264                         close(s->fd);
265                 ast_mutex_destroy(&s->lock);
266                 free(s);
267         } else
268                 ast_log(LOG_WARNING, "Trying to delete nonexistent session %p?\n", s);
269         ast_mutex_unlock(&sessionlock);
270         
271 }
272
273 char *astman_get_header(struct message *m, char *var)
274 {
275         char cmp[80];
276         int x;
277         snprintf(cmp, sizeof(cmp), "%s: ", var);
278         for (x=0;x<m->hdrcount;x++)
279                 if (!strncasecmp(cmp, m->headers[x], strlen(cmp)))
280                         return m->headers[x] + strlen(cmp);
281         return "";
282 }
283
284 struct ast_variable *astman_get_variables(struct message *m)
285 {
286         int varlen, x;
287         struct ast_variable *head = NULL, *cur;
288         char *var, *val;
289         
290         varlen = strlen("Variable: ");  
291
292         for (x = 0; x < m->hdrcount; x++) {
293                 if (!strncasecmp("Variable: ", m->headers[x], varlen)) {
294                         var = val = ast_strdupa(m->headers[x] + varlen);
295                         if (!var)
296                                 return head;                            
297                         strsep(&val, "=");
298                         if (!val || ast_strlen_zero(var))
299                                 continue;
300                         cur = ast_variable_new(var, val);
301                         if (head) {
302                                 cur->next = head;
303                                 head = cur;
304                         } else
305                                 head = cur;
306                 }
307         }
308
309         return head;
310 }
311
312 void astman_send_error(struct mansession *s, struct message *m, char *error)
313 {
314         char *id = astman_get_header(m,"ActionID");
315         ast_mutex_lock(&s->lock);
316         ast_cli(s->fd, "Response: Error\r\n");
317         if (id && !ast_strlen_zero(id))
318                 ast_cli(s->fd, "ActionID: %s\r\n",id);
319         ast_cli(s->fd, "Message: %s\r\n\r\n", error);
320         ast_mutex_unlock(&s->lock);
321 }
322
323 void astman_send_response(struct mansession *s, struct message *m, char *resp, char *msg)
324 {
325         char *id = astman_get_header(m,"ActionID");
326         ast_mutex_lock(&s->lock);
327         ast_cli(s->fd, "Response: %s\r\n", resp);
328         if (id && !ast_strlen_zero(id))
329                 ast_cli(s->fd, "ActionID: %s\r\n",id);
330         if (msg)
331                 ast_cli(s->fd, "Message: %s\r\n\r\n", msg);
332         else
333                 ast_cli(s->fd, "\r\n");
334         ast_mutex_unlock(&s->lock);
335 }
336
337 void astman_send_ack(struct mansession *s, struct message *m, char *msg)
338 {
339         astman_send_response(s, m, "Success", msg);
340 }
341
342 /* Tells you if smallstr exists inside bigstr
343    which is delim by delim and uses no buf or stringsep
344    ast_instring("this|that|more","this",',') == 1;
345
346    feel free to move this to app.c -anthm */
347 static int ast_instring(char *bigstr, char *smallstr, char delim) 
348 {
349         char *val = bigstr, *next;
350
351         do {
352                 if ((next = strchr(val, delim))) {
353                         if (!strncmp(val, smallstr, (next - val)))
354                                 return 1;
355                         else
356                                 continue;
357                 } else
358                         return !strcmp(smallstr, val);
359
360         } while (*(val = (next + 1)));
361
362         return 0;
363 }
364
365 static int get_perm(char *instr)
366 {
367         int x = 0, ret = 0;
368
369         if (!instr)
370                 return 0;
371
372         for (x=0; x<sizeof(perms) / sizeof(perms[0]); x++)
373                 if (ast_instring(instr, perms[x].label, ','))
374                         ret |= perms[x].num;
375         
376         return ret;
377 }
378
379 static int ast_is_number(char *string) 
380 {
381         int ret = 1, x = 0;
382
383         if (!string)
384                 return 0;
385
386         for (x=0; x < strlen(string); x++) {
387                 if (!(string[x] >= 48 && string[x] <= 57)) {
388                         ret = 0;
389                         break;
390                 }
391         }
392         
393         return ret ? atoi(string) : 0;
394 }
395
396 static int ast_strings_to_mask(char *string) 
397 {
398         int x, ret = -1;
399         
400         x = ast_is_number(string);
401
402         if (x) {
403                 ret = x;
404         } else if (!string || ast_strlen_zero(string)) {
405                 ret = -1;
406         } else if (ast_false(string)) {
407                 ret = 0;
408         } else if (ast_true(string)) {
409                 ret = 0;
410                 for (x=0; x<sizeof(perms) / sizeof(perms[0]); x++)
411                         ret |= perms[x].num;            
412         } else {
413                 ret = 0;
414                 for (x=0; x<sizeof(perms) / sizeof(perms[0]); x++) {
415                         if (ast_instring(string, perms[x].label, ',')) 
416                                 ret |= perms[x].num;            
417                 }
418         }
419
420         return ret;
421 }
422
423 /* 
424    Rather than braindead on,off this now can also accept a specific int mask value 
425    or a ',' delim list of mask strings (the same as manager.conf) -anthm
426 */
427
428 static int set_eventmask(struct mansession *s, char *eventmask)
429 {
430         int maskint = ast_strings_to_mask(eventmask);
431
432         ast_mutex_lock(&s->lock);
433         if (maskint >= 0)       
434                 s->send_events = maskint;
435         ast_mutex_unlock(&s->lock);
436         
437         return maskint;
438 }
439
440 static int authenticate(struct mansession *s, struct message *m)
441 {
442         struct ast_config *cfg;
443         char iabuf[INET_ADDRSTRLEN];
444         char *cat;
445         char *user = astman_get_header(m, "Username");
446         char *pass = astman_get_header(m, "Secret");
447         char *authtype = astman_get_header(m, "AuthType");
448         char *key = astman_get_header(m, "Key");
449         char *events = astman_get_header(m, "Events");
450         
451         cfg = ast_config_load("manager.conf");
452         if (!cfg)
453                 return -1;
454         cat = ast_category_browse(cfg, NULL);
455         while(cat) {
456                 if (strcasecmp(cat, "general")) {
457                         /* This is a user */
458                         if (!strcasecmp(cat, user)) {
459                                 struct ast_variable *v;
460                                 struct ast_ha *ha = NULL;
461                                 char *password = NULL;
462                                 v = ast_variable_browse(cfg, cat);
463                                 while (v) {
464                                         if (!strcasecmp(v->name, "secret")) {
465                                                 password = v->value;
466                                         } else if (!strcasecmp(v->name, "permit") ||
467                                                    !strcasecmp(v->name, "deny")) {
468                                                 ha = ast_append_ha(v->name, v->value, ha);
469                                         }       
470                                                 
471                                         v = v->next;
472                                 }
473                                 if (ha && !ast_apply_ha(ha, &(s->sin))) {
474                                         ast_log(LOG_NOTICE, "%s failed to pass IP ACL as '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
475                                         ast_free_ha(ha);
476                                         ast_config_destroy(cfg);
477                                         return -1;
478                                 } else if (ha)
479                                         ast_free_ha(ha);
480                                 if (!strcasecmp(authtype, "MD5")) {
481                                         if (key && !ast_strlen_zero(key) && s->challenge) {
482                                                 int x;
483                                                 int len=0;
484                                                 char md5key[256] = "";
485                                                 struct MD5Context md5;
486                                                 unsigned char digest[16];
487                                                 MD5Init(&md5);
488                                                 MD5Update(&md5, (unsigned char *) s->challenge, strlen(s->challenge));
489                                                 MD5Update(&md5, (unsigned char *) password, strlen(password));
490                                                 MD5Final(digest, &md5);
491                                                 for (x=0;x<16;x++)
492                                                         len += sprintf(md5key + len, "%2.2x", digest[x]);
493                                                 if (!strcmp(md5key, key))
494                                                         break;
495                                                 else {
496                                                         ast_config_destroy(cfg);
497                                                         return -1;
498                                                 }
499                                         }
500                                 } else if (password && !strcasecmp(password, pass)) {
501                                         break;
502                                 } else {
503                                         ast_log(LOG_NOTICE, "%s failed to authenticate as '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
504                                         ast_config_destroy(cfg);
505                                         return -1;
506                                 }       
507                         }
508                 }
509                 cat = ast_category_browse(cfg, cat);
510         }
511         if (cat) {
512                 ast_copy_string(s->username, cat, sizeof(s->username));
513                 s->readperm = get_perm(ast_variable_retrieve(cfg, cat, "read"));
514                 s->writeperm = get_perm(ast_variable_retrieve(cfg, cat, "write"));
515                 ast_config_destroy(cfg);
516                 if (events)
517                         set_eventmask(s, events);
518                 return 0;
519         }
520         ast_log(LOG_NOTICE, "%s tried to authenticate with nonexistent user '%s'\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), user);
521         ast_config_destroy(cfg);
522         return -1;
523 }
524
525 static char mandescr_ping[] = 
526 "Description: A 'Ping' action will ellicit a 'Pong' response.  Used to keep the "
527 "  manager connection open.\n"
528 "Variables: NONE\n";
529
530 static int action_ping(struct mansession *s, struct message *m)
531 {
532         astman_send_response(s, m, "Pong", NULL);
533         return 0;
534 }
535
536 static char mandescr_listcommands[] = 
537 "Description: Returns the action name and synopsis for every\n"
538 "  action that is available to the user\n"
539 "Variables: NONE\n";
540
541 static int action_listcommands(struct mansession *s, struct message *m)
542 {
543         struct manager_action *cur = first_action;
544         char idText[256] = "";
545         char temp[BUFSIZ];
546         char *id = astman_get_header(m,"ActionID");
547
548         if (id && !ast_strlen_zero(id))
549                 snprintf(idText,256,"ActionID: %s\r\n",id);
550         ast_cli(s->fd, "Response: Success\r\n%s", idText);
551         ast_mutex_lock(&s->lock);
552         ast_mutex_lock(&actionlock);
553         while (cur) { /* Walk the list of actions */
554                 if ((s->writeperm & cur->authority) == cur->authority)
555                         ast_cli(s->fd, "%s: %s (Priv: %s)\r\n", cur->action, cur->synopsis, authority_to_str(cur->authority, temp, sizeof(temp)) );
556                 cur = cur->next;
557         }
558         ast_mutex_unlock(&actionlock);
559         ast_cli(s->fd, "\r\n");
560         ast_mutex_unlock(&s->lock);
561
562         return 0;
563 }
564
565 static char mandescr_events[] = 
566 "Description: Enable/Disable sending of events to this manager\n"
567 "  client.\n"
568 "Variables:\n"
569 "       EventMask: 'on' if all events should be sent,\n"
570 "               'off' if no events should be sent,\n"
571 "               'system,call,log' to select which flags events should have to be sent.\n";
572
573 static int action_events(struct mansession *s, struct message *m)
574 {
575         char *mask = astman_get_header(m, "EventMask");
576         int res;
577
578         res = set_eventmask(s, mask);
579         if (res > 0)
580                 astman_send_response(s, m, "Events On", NULL);
581         else if (res == 0)
582                 astman_send_response(s, m, "Events Off", NULL);
583
584         return 0;
585 }
586
587 static char mandescr_logoff[] = 
588 "Description: Logoff this manager session\n"
589 "Variables: NONE\n";
590
591 static int action_logoff(struct mansession *s, struct message *m)
592 {
593         astman_send_response(s, m, "Goodbye", "Thanks for all the fish.");
594         return -1;
595 }
596
597 static char mandescr_hangup[] = 
598 "Description: Hangup a channel\n"
599 "Variables: \n"
600 "       Channel: The channel name to be hungup\n";
601
602 static int action_hangup(struct mansession *s, struct message *m)
603 {
604         struct ast_channel *c = NULL;
605         char *name = astman_get_header(m, "Channel");
606         if (ast_strlen_zero(name)) {
607                 astman_send_error(s, m, "No channel specified");
608                 return 0;
609         }
610         c = ast_get_channel_by_name_locked(name);
611         if (!c) {
612                 astman_send_error(s, m, "No such channel");
613                 return 0;
614         }
615         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
616         ast_mutex_unlock(&c->lock);
617         astman_send_ack(s, m, "Channel Hungup");
618         return 0;
619 }
620
621 static char mandescr_setvar[] = 
622 "Description: Set a local channel variable.\n"
623 "Variables: (Names marked with * are required)\n"
624 "       *Channel: Channel to set variable for\n"
625 "       *Variable: Variable name\n"
626 "       *Value: Value\n";
627
628 static int action_setvar(struct mansession *s, struct message *m)
629 {
630         struct ast_channel *c = NULL;
631         char *name = astman_get_header(m, "Channel");
632         char *varname = astman_get_header(m, "Variable");
633         char *varval = astman_get_header(m, "Value");
634         
635         if (!strlen(name)) {
636                 astman_send_error(s, m, "No channel specified");
637                 return 0;
638         }
639         if (!strlen(varname)) {
640                 astman_send_error(s, m, "No variable specified");
641                 return 0;
642         }
643
644         c = ast_get_channel_by_name_locked(name);
645         if (!c) {
646                 astman_send_error(s, m, "No such channel");
647                 return 0;
648         }
649         
650         pbx_builtin_setvar_helper(c,varname,varval);
651           
652         ast_mutex_unlock(&c->lock);
653         astman_send_ack(s, m, "Variable Set");
654         return 0;
655 }
656
657 static char mandescr_getvar[] = 
658 "Description: Get the value of a local channel variable.\n"
659 "Variables: (Names marked with * are required)\n"
660 "       *Channel: Channel to read variable from\n"
661 "       *Variable: Variable name\n"
662 "       ActionID: Optional Action id for message matching.\n";
663
664 static int action_getvar(struct mansession *s, struct message *m)
665 {
666         struct ast_channel *c = NULL;
667         char *name = astman_get_header(m, "Channel");
668         char *varname = astman_get_header(m, "Variable");
669         char *id = astman_get_header(m,"ActionID");
670         char *varval;
671         char *varval2=NULL;
672
673         if (!strlen(name)) {
674                 astman_send_error(s, m, "No channel specified");
675                 return 0;
676         }
677         if (!strlen(varname)) {
678                 astman_send_error(s, m, "No variable specified");
679                 return 0;
680         }
681
682         c = ast_get_channel_by_name_locked(name);
683         if (!c) {
684                 astman_send_error(s, m, "No such channel");
685                 return 0;
686         }
687         
688         varval=pbx_builtin_getvar_helper(c,varname);
689         if (varval)
690                 varval2 = ast_strdupa(varval);
691         if (!varval2)
692                 varval2 = "";
693         ast_mutex_unlock(&c->lock);
694         ast_mutex_lock(&s->lock);
695         ast_cli(s->fd, "Response: Success\r\n"
696                 "%s: %s\r\n" ,varname,varval2);
697         if (id && !ast_strlen_zero(id))
698                 ast_cli(s->fd, "ActionID: %s\r\n",id);
699         ast_cli(s->fd, "\r\n");
700         ast_mutex_unlock(&s->lock);
701
702         return 0;
703 }
704
705
706 /*--- action_status: Manager "status" command to show channels */
707 /* Needs documentation... */
708 static int action_status(struct mansession *s, struct message *m)
709 {
710         char *id = astman_get_header(m,"ActionID");
711         char *name = astman_get_header(m,"Channel");
712         char idText[256] = "";
713         struct ast_channel *c;
714         char bridge[256];
715         struct timeval now = ast_tvnow();
716         long elapsed_seconds=0;
717         int all = !name || ast_strlen_zero(name); /* set if we want all channels */
718
719         astman_send_ack(s, m, "Channel status will follow");
720         if (id && !ast_strlen_zero(id))
721                 snprintf(idText,256,"ActionID: %s\r\n",id);
722         if (all)
723                 c = ast_channel_walk_locked(NULL);
724         else {
725                 c = ast_get_channel_by_name_locked(name);
726                 if (!c) {
727                         astman_send_error(s, m, "No such channel");
728                         return 0;
729                 }
730         }
731         /* if we look by name, we break after the first iteration */
732         while(c) {
733                 if (c->_bridge)
734                         snprintf(bridge, sizeof(bridge), "Link: %s\r\n", c->_bridge->name);
735                 else
736                         bridge[0] = '\0';
737                 ast_mutex_lock(&s->lock);
738                 if (c->pbx) {
739                         if (c->cdr) {
740                                 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
741                         }
742                         ast_cli(s->fd,
743                         "Event: Status\r\n"
744                         "Privilege: Call\r\n"
745                         "Channel: %s\r\n"
746                         "CallerID: %s\r\n"
747                         "CallerIDName: %s\r\n"
748                         "Account: %s\r\n"
749                         "State: %s\r\n"
750                         "Context: %s\r\n"
751                         "Extension: %s\r\n"
752                         "Priority: %d\r\n"
753                         "Seconds: %ld\r\n"
754                         "%s"
755                         "Uniqueid: %s\r\n"
756                         "%s"
757                         "\r\n",
758                         c->name, 
759                         c->cid.cid_num ? c->cid.cid_num : "<unknown>", 
760                         c->cid.cid_name ? c->cid.cid_name : "<unknown>", 
761                         c->accountcode,
762                         ast_state2str(c->_state), c->context,
763                         c->exten, c->priority, (long)elapsed_seconds, bridge, c->uniqueid, idText);
764                 } else {
765                         ast_cli(s->fd,
766                         "Event: Status\r\n"
767                         "Privilege: Call\r\n"
768                         "Channel: %s\r\n"
769                         "CallerID: %s\r\n"
770                         "CallerIDName: %s\r\n"
771                         "Account: %s\r\n"
772                         "State: %s\r\n"
773                         "%s"
774                         "Uniqueid: %s\r\n"
775                         "%s"
776                         "\r\n",
777                         c->name, 
778                         c->cid.cid_num ? c->cid.cid_num : "<unknown>", 
779                         c->cid.cid_name ? c->cid.cid_name : "<unknown>", 
780                         c->accountcode,
781                         ast_state2str(c->_state), bridge, c->uniqueid, idText);
782                 }
783                 ast_mutex_unlock(&s->lock);
784                 ast_mutex_unlock(&c->lock);
785                 if (!all)
786                         break;
787                 c = ast_channel_walk_locked(c);
788         }
789         ast_mutex_lock(&s->lock);
790         ast_cli(s->fd,
791         "Event: StatusComplete\r\n"
792         "%s"
793         "\r\n",idText);
794         ast_mutex_unlock(&s->lock);
795         return 0;
796 }
797
798 static char mandescr_redirect[] = 
799 "Description: Redirect (transfer) a call.\n"
800 "Variables: (Names marked with * are required)\n"
801 "       *Channel: Channel to redirect\n"
802 "       ExtraChannel: Second call leg to transfer (optional)\n"
803 "       *Exten: Extension to transfer to\n"
804 "       *Context: Context to transfer to\n"
805 "       *Priority: Priority to transfer to\n"
806 "       ActionID: Optional Action id for message matching.\n";
807
808 /*--- action_redirect: The redirect manager command */
809 static int action_redirect(struct mansession *s, struct message *m)
810 {
811         char *name = astman_get_header(m, "Channel");
812         char *name2 = astman_get_header(m, "ExtraChannel");
813         char *exten = astman_get_header(m, "Exten");
814         char *context = astman_get_header(m, "Context");
815         char *priority = astman_get_header(m, "Priority");
816         struct ast_channel *chan, *chan2 = NULL;
817         int pi = 0;
818         int res;
819
820         if (!name || ast_strlen_zero(name)) {
821                 astman_send_error(s, m, "Channel not specified");
822                 return 0;
823         }
824         if (!ast_strlen_zero(priority) && (sscanf(priority, "%d", &pi) != 1)) {
825                 astman_send_error(s, m, "Invalid priority\n");
826                 return 0;
827         }
828         chan = ast_get_channel_by_name_locked(name);
829         if (!chan) {
830                 char buf[BUFSIZ];
831                 snprintf(buf, sizeof(buf), "Channel does not exist: %s", name);
832                 astman_send_error(s, m, buf);
833                 return 0;
834         }
835         if (!ast_strlen_zero(name2))
836                 chan2 = ast_get_channel_by_name_locked(name2);
837         res = ast_async_goto(chan, context, exten, pi);
838         if (!res) {
839                 if (!ast_strlen_zero(name2)) {
840                         if (chan2)
841                                 res = ast_async_goto(chan2, context, exten, pi);
842                         else
843                                 res = -1;
844                         if (!res)
845                                 astman_send_ack(s, m, "Dual Redirect successful");
846                         else
847                                 astman_send_error(s, m, "Secondary redirect failed");
848                 } else
849                         astman_send_ack(s, m, "Redirect successful");
850         } else
851                 astman_send_error(s, m, "Redirect failed");
852         if (chan)
853                 ast_mutex_unlock(&chan->lock);
854         if (chan2)
855                 ast_mutex_unlock(&chan2->lock);
856         return 0;
857 }
858
859 static char mandescr_command[] = 
860 "Description: Run a CLI command.\n"
861 "Variables: (Names marked with * are required)\n"
862 "       *Command: Asterisk CLI command to run\n"
863 "       ActionID: Optional Action id for message matching.\n";
864
865 /*--- action_command: Manager command "command" - execute CLI command */
866 static int action_command(struct mansession *s, struct message *m)
867 {
868         char *cmd = astman_get_header(m, "Command");
869         char *id = astman_get_header(m, "ActionID");
870         ast_mutex_lock(&s->lock);
871         s->blocking = 1;
872         ast_mutex_unlock(&s->lock);
873         ast_cli(s->fd, "Response: Follows\r\nPrivilege: Command\r\n");
874         if (id && !ast_strlen_zero(id))
875                 ast_cli(s->fd, "ActionID: %s\r\n", id);
876         /* FIXME: Wedge a ActionID response in here, waiting for later changes */
877         ast_cli_command(s->fd, cmd);
878         ast_cli(s->fd, "--END COMMAND--\r\n\r\n");
879         ast_mutex_lock(&s->lock);
880         s->blocking = 0;
881         ast_mutex_unlock(&s->lock);
882         return 0;
883 }
884
885 static void *fast_originate(void *data)
886 {
887         struct fast_originate_helper *in = data;
888         int res;
889         int reason = 0;
890         struct ast_channel *chan = NULL;
891
892         if (!ast_strlen_zero(in->app)) {
893                 res = ast_pbx_outgoing_app(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->app, in->appdata, &reason, 1, 
894                         !ast_strlen_zero(in->cid_num) ? in->cid_num : NULL, 
895                         !ast_strlen_zero(in->cid_name) ? in->cid_name : NULL,
896                         in->vars, &chan);
897         } else {
898                 res = ast_pbx_outgoing_exten(in->tech, AST_FORMAT_SLINEAR, in->data, in->timeout, in->context, in->exten, in->priority, &reason, 1, 
899                         !ast_strlen_zero(in->cid_num) ? in->cid_num : NULL, 
900                         !ast_strlen_zero(in->cid_name) ? in->cid_name : NULL,
901                         in->vars, &chan);
902         }   
903         if (!res)
904                 manager_event(EVENT_FLAG_CALL,
905                         "OriginateSuccess",
906                         "%s"
907                         "Channel: %s/%s\r\n"
908                         "Context: %s\r\n"
909                         "Exten: %s\r\n"
910                         "Reason: %d\r\n"
911                         "Uniqueid: %s\r\n",
912                         in->idtext, in->tech, in->data, in->context, in->exten, reason, chan ? chan->uniqueid : "<null>");
913         else
914                 manager_event(EVENT_FLAG_CALL,
915                         "OriginateFailure",
916                         "%s"
917                         "Channel: %s/%s\r\n"
918                         "Context: %s\r\n"
919                         "Exten: %s\r\n"
920                         "Reason: %d\r\n"
921                         "Uniqueid: %s\r\n",
922                         in->idtext, in->tech, in->data, in->context, in->exten, reason, chan ? chan->uniqueid : "<null>");
923
924         /* Locked by ast_pbx_outgoing_exten or ast_pbx_outgoing_app */
925         if (chan)
926                 ast_mutex_unlock(&chan->lock);
927         free(in);
928         return NULL;
929 }
930
931 static char mandescr_originate[] = 
932 "Description: Generates an outgoing call to a Extension/Context/Priority or\n"
933 "  Application/Data\n"
934 "Variables: (Names marked with * are required)\n"
935 "       *Channel: Channel name to call\n"
936 "       Exten: Extension to use (requires 'Context' and 'Priority')\n"
937 "       Context: Context to use (requires 'Exten' and 'Priority')\n"
938 "       Priority: Priority to use (requires 'Exten' and 'Context')\n"
939 "       Application: Application to use\n"
940 "       Data: Data to use (requires 'Application')\n"
941 "       Timeout: How long to wait for call to be answered (in ms)\n"
942 "       CallerID: Caller ID to be set on the outgoing channel\n"
943 "       Variable: Channel variable to set (VAR1=value1|VAR2=value2)\n"
944 "       Account: Account code\n"
945 "       Async: Set to 'true' for fast origination\n";
946
947 static int action_originate(struct mansession *s, struct message *m)
948 {
949         char *name = astman_get_header(m, "Channel");
950         char *exten = astman_get_header(m, "Exten");
951         char *context = astman_get_header(m, "Context");
952         char *priority = astman_get_header(m, "Priority");
953         char *timeout = astman_get_header(m, "Timeout");
954         char *callerid = astman_get_header(m, "CallerID");
955         char *account = astman_get_header(m, "Account");
956         char *app = astman_get_header(m, "Application");
957         char *appdata = astman_get_header(m, "Data");
958         char *async = astman_get_header(m, "Async");
959         char *id = astman_get_header(m, "ActionID");
960         struct ast_variable *vars = astman_get_variables(m);
961         char *tech, *data;
962         char *l=NULL, *n=NULL;
963         int pi = 0;
964         int res;
965         int to = 30000;
966         int reason = 0;
967         char tmp[256];
968         char tmp2[256];
969         
970         pthread_t th;
971         pthread_attr_t attr;
972         if (!name) {
973                 astman_send_error(s, m, "Channel not specified");
974                 return 0;
975         }
976         if (!ast_strlen_zero(priority) && (sscanf(priority, "%d", &pi) != 1)) {
977                 astman_send_error(s, m, "Invalid priority\n");
978                 return 0;
979         }
980         if (!ast_strlen_zero(timeout) && (sscanf(timeout, "%d", &to) != 1)) {
981                 astman_send_error(s, m, "Invalid timeout\n");
982                 return 0;
983         }
984         ast_copy_string(tmp, name, sizeof(tmp));
985         tech = tmp;
986         data = strchr(tmp, '/');
987         if (!data) {
988                 astman_send_error(s, m, "Invalid channel\n");
989                 return 0;
990         }
991         *data = '\0';
992         data++;
993         ast_copy_string(tmp2, callerid, sizeof(tmp2));
994         ast_callerid_parse(tmp2, &n, &l);
995         if (n) {
996                 if (ast_strlen_zero(n))
997                         n = NULL;
998         }
999         if (l) {
1000                 ast_shrink_phone_number(l);
1001                 if (ast_strlen_zero(l))
1002                         l = NULL;
1003         }
1004         if (account) {
1005                 struct ast_variable *newvar;
1006                 newvar = ast_variable_new("CDR(accountcode|r)", account);
1007                 newvar->next = vars;
1008                 vars = newvar;
1009         }
1010         if (ast_true(async)) {
1011                 struct fast_originate_helper *fast = malloc(sizeof(struct fast_originate_helper));
1012                 if (!fast) {
1013                         res = -1;
1014                 } else {
1015                         memset(fast, 0, sizeof(struct fast_originate_helper));
1016                         if (id && !ast_strlen_zero(id))
1017                                 snprintf(fast->idtext, sizeof(fast->idtext), "ActionID: %s\r\n", id);
1018                         ast_copy_string(fast->tech, tech, sizeof(fast->tech));
1019                         ast_copy_string(fast->data, data, sizeof(fast->data));
1020                         ast_copy_string(fast->app, app, sizeof(fast->app));
1021                         ast_copy_string(fast->appdata, appdata, sizeof(fast->appdata));
1022                         if (l)
1023                                 ast_copy_string(fast->cid_num, l, sizeof(fast->cid_num));
1024                         if (n)
1025                                 ast_copy_string(fast->cid_name, n, sizeof(fast->cid_name));
1026                         fast->vars = vars;      
1027                         ast_copy_string(fast->context, context, sizeof(fast->context));
1028                         ast_copy_string(fast->exten, exten, sizeof(fast->exten));
1029                         fast->timeout = to;
1030                         fast->priority = pi;
1031                         pthread_attr_init(&attr);
1032                         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1033                         if (ast_pthread_create(&th, &attr, fast_originate, fast)) {
1034                                 res = -1;
1035                         } else {
1036                                 res = 0;
1037                         }
1038                 }
1039         } else if (!ast_strlen_zero(app)) {
1040                 res = ast_pbx_outgoing_app(tech, AST_FORMAT_SLINEAR, data, to, app, appdata, &reason, 1, l, n, vars, NULL);
1041         } else {
1042                 if (exten && context && pi)
1043                         res = ast_pbx_outgoing_exten(tech, AST_FORMAT_SLINEAR, data, to, context, exten, pi, &reason, 1, l, n, vars, NULL);
1044                 else {
1045                         astman_send_error(s, m, "Originate with 'Exten' requires 'Context' and 'Priority'");
1046                         return 0;
1047                 }
1048         }   
1049         if (!res)
1050                 astman_send_ack(s, m, "Originate successfully queued");
1051         else
1052                 astman_send_error(s, m, "Originate failed");
1053         return 0;
1054 }
1055
1056 static char mandescr_mailboxstatus[] = 
1057 "Description: Checks a voicemail account for status.\n"
1058 "Variables: (Names marked with * are required)\n"
1059 "       *Mailbox: Full mailbox ID <mailbox>@<vm-context>\n"
1060 "       ActionID: Optional ActionID for message matching.\n"
1061 "Returns number of messages.\n"
1062 "       Message: Mailbox Status\n"
1063 "       Mailbox: <mailboxid>\n"
1064 "       Waiting: <count>\n"
1065 "\n";
1066 static int action_mailboxstatus(struct mansession *s, struct message *m)
1067 {
1068         char *mailbox = astman_get_header(m, "Mailbox");
1069         char *id = astman_get_header(m,"ActionID");
1070         char idText[256] = "";
1071         int ret;
1072         if (!mailbox || ast_strlen_zero(mailbox)) {
1073                 astman_send_error(s, m, "Mailbox not specified");
1074                 return 0;
1075         }
1076         if (id && !ast_strlen_zero(id))
1077                 snprintf(idText,256,"ActionID: %s\r\n",id);
1078         ret = ast_app_has_voicemail(mailbox, NULL);
1079         ast_mutex_lock(&s->lock);
1080         ast_cli(s->fd, "Response: Success\r\n"
1081                                    "%s"
1082                                    "Message: Mailbox Status\r\n"
1083                                    "Mailbox: %s\r\n"
1084                                    "Waiting: %d\r\n\r\n", idText, mailbox, ret);
1085         ast_mutex_unlock(&s->lock);
1086         return 0;
1087 }
1088
1089 static char mandescr_mailboxcount[] = 
1090 "Description: Checks a voicemail account for new messages.\n"
1091 "Variables: (Names marked with * are required)\n"
1092 "       *Mailbox: Full mailbox ID <mailbox>@<vm-context>\n"
1093 "       ActionID: Optional ActionID for message matching.\n"
1094 "Returns number of new and old messages.\n"
1095 "       Message: Mailbox Message Count\n"
1096 "       Mailbox: <mailboxid>\n"
1097 "       NewMessages: <count>\n"
1098 "       OldMessages: <count>\n"
1099 "\n";
1100 static int action_mailboxcount(struct mansession *s, struct message *m)
1101 {
1102         char *mailbox = astman_get_header(m, "Mailbox");
1103         char *id = astman_get_header(m,"ActionID");
1104         char idText[256] = "";
1105         int newmsgs = 0, oldmsgs = 0;
1106         if (!mailbox || ast_strlen_zero(mailbox)) {
1107                 astman_send_error(s, m, "Mailbox not specified");
1108                 return 0;
1109         }
1110         ast_app_messagecount(mailbox, &newmsgs, &oldmsgs);
1111         if (id && !ast_strlen_zero(id)) {
1112                 snprintf(idText,256,"ActionID: %s\r\n",id);
1113         }
1114         ast_mutex_lock(&s->lock);
1115         ast_cli(s->fd, "Response: Success\r\n"
1116                                    "%s"
1117                                    "Message: Mailbox Message Count\r\n"
1118                                    "Mailbox: %s\r\n"
1119                                    "NewMessages: %d\r\n"
1120                                    "OldMessages: %d\r\n" 
1121                                    "\r\n",
1122                                     idText,mailbox, newmsgs, oldmsgs);
1123         ast_mutex_unlock(&s->lock);
1124         return 0;
1125 }
1126
1127 static char mandescr_extensionstate[] = 
1128 "Description: Report the extension state for given extension.\n"
1129 "  If the extension has a hint, will use devicestate to check\n"
1130 "  the status of the device connected to the extension.\n"
1131 "Variables: (Names marked with * are required)\n"
1132 "       *Exten: Extension to check state on\n"
1133 "       *Context: Context for extension\n"
1134 "       ActionId: Optional ID for this transaction\n"
1135 "Will return an \"Extension Status\" message.\n"
1136 "The response will include the hint for the extension and the status.\n";
1137
1138 static int action_extensionstate(struct mansession *s, struct message *m)
1139 {
1140         char *exten = astman_get_header(m, "Exten");
1141         char *context = astman_get_header(m, "Context");
1142         char *id = astman_get_header(m,"ActionID");
1143         char idText[256] = "";
1144         char hint[256] = "";
1145         int status;
1146         if (!exten || ast_strlen_zero(exten)) {
1147                 astman_send_error(s, m, "Extension not specified");
1148                 return 0;
1149         }
1150         if (!context || ast_strlen_zero(context))
1151                 context = "default";
1152         status = ast_extension_state(NULL, context, exten);
1153         ast_get_hint(hint, sizeof(hint) - 1, NULL, 0, NULL, context, exten);
1154         if (id && !ast_strlen_zero(id)) {
1155                 snprintf(idText,256,"ActionID: %s\r\n",id);
1156         }
1157         ast_mutex_lock(&s->lock);
1158         ast_cli(s->fd, "Response: Success\r\n"
1159                                    "%s"
1160                                    "Message: Extension Status\r\n"
1161                                    "Exten: %s\r\n"
1162                                    "Context: %s\r\n"
1163                                    "Hint: %s\r\n"
1164                                    "Status: %d\r\n\r\n",
1165                                    idText,exten, context, hint, status);
1166         ast_mutex_unlock(&s->lock);
1167         return 0;
1168 }
1169
1170 static char mandescr_timeout[] = 
1171 "Description: Hangup a channel after a certain time.\n"
1172 "Variables: (Names marked with * are required)\n"
1173 "       *Channel: Channel name to hangup\n"
1174 "       *Timeout: Maximum duration of the call (sec)\n"
1175 "Acknowledges set time with 'Timeout Set' message\n";
1176
1177 static int action_timeout(struct mansession *s, struct message *m)
1178 {
1179         struct ast_channel *c = NULL;
1180         char *name = astman_get_header(m, "Channel");
1181         int timeout = atoi(astman_get_header(m, "Timeout"));
1182         if (ast_strlen_zero(name)) {
1183                 astman_send_error(s, m, "No channel specified");
1184                 return 0;
1185         }
1186         if (!timeout) {
1187                 astman_send_error(s, m, "No timeout specified");
1188                 return 0;
1189         }
1190         c = ast_get_channel_by_name_locked(name);
1191         if (!c) {
1192                 astman_send_error(s, m, "No such channel");
1193                 return 0;
1194         }
1195         ast_channel_setwhentohangup(c, timeout);
1196         ast_mutex_unlock(&c->lock);
1197         astman_send_ack(s, m, "Timeout Set");
1198         return 0;
1199 }
1200
1201 static int process_message(struct mansession *s, struct message *m)
1202 {
1203         char action[80] = "";
1204         struct manager_action *tmp = first_action;
1205         char *id = astman_get_header(m,"ActionID");
1206         char idText[256] = "";
1207         char iabuf[INET_ADDRSTRLEN];
1208
1209         ast_copy_string(action, astman_get_header(m, "Action"), sizeof(action));
1210         ast_log( LOG_DEBUG, "Manager received command '%s'\n", action );
1211
1212         if (ast_strlen_zero(action)) {
1213                 astman_send_error(s, m, "Missing action in request");
1214                 return 0;
1215         }
1216         if (id && !ast_strlen_zero(id)) {
1217                 snprintf(idText,256,"ActionID: %s\r\n",id);
1218         }
1219         if (!s->authenticated) {
1220                 if (!strcasecmp(action, "Challenge")) {
1221                         char *authtype;
1222                         authtype = astman_get_header(m, "AuthType");
1223                         if (!strcasecmp(authtype, "MD5")) {
1224                                 if (!s->challenge || ast_strlen_zero(s->challenge)) {
1225                                         ast_mutex_lock(&s->lock);
1226                                         snprintf(s->challenge, sizeof(s->challenge), "%d", rand());
1227                                         ast_mutex_unlock(&s->lock);
1228                                 }
1229                                 ast_mutex_lock(&s->lock);
1230                                 ast_cli(s->fd, "Response: Success\r\n"
1231                                                 "%s"
1232                                                 "Challenge: %s\r\n\r\n",
1233                                                 idText,s->challenge);
1234                                 ast_mutex_unlock(&s->lock);
1235                                 return 0;
1236                         } else {
1237                                 astman_send_error(s, m, "Must specify AuthType");
1238                                 return 0;
1239                         }
1240                 } else if (!strcasecmp(action, "Login")) {
1241                         if (authenticate(s, m)) {
1242                                 sleep(1);
1243                                 astman_send_error(s, m, "Authentication failed");
1244                                 return -1;
1245                         } else {
1246                                 s->authenticated = 1;
1247                                 if (option_verbose > 1) {
1248                                         if ( displayconnects ) {
1249                                                 ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged on from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
1250                                         }
1251                                 }
1252                                 ast_log(LOG_EVENT, "Manager '%s' logged on from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
1253                                 astman_send_ack(s, m, "Authentication accepted");
1254                         }
1255                 } else if (!strcasecmp(action, "Logoff")) {
1256                         astman_send_ack(s, m, "See ya");
1257                         return -1;
1258                 } else
1259                         astman_send_error(s, m, "Authentication Required");
1260         } else {
1261                 while( tmp ) {          
1262                         if (!strcasecmp(action, tmp->action)) {
1263                                 if ((s->writeperm & tmp->authority) == tmp->authority) {
1264                                         if (tmp->func(s, m))
1265                                                 return -1;
1266                                 } else {
1267                                         astman_send_error(s, m, "Permission denied");
1268                                 }
1269                                 return 0;
1270                         }
1271                         tmp = tmp->next;
1272                 }
1273                 astman_send_error(s, m, "Invalid/unknown command");
1274         }
1275         return 0;
1276 }
1277
1278 static int get_input(struct mansession *s, char *output)
1279 {
1280         /* output must have at least sizeof(s->inbuf) space */
1281         int res;
1282         int x;
1283         struct pollfd fds[1];
1284         char iabuf[INET_ADDRSTRLEN];
1285         for (x=1;x<s->inlen;x++) {
1286                 if ((s->inbuf[x] == '\n') && (s->inbuf[x-1] == '\r')) {
1287                         /* Copy output data up to and including \r\n */
1288                         memcpy(output, s->inbuf, x + 1);
1289                         /* Add trailing \0 */
1290                         output[x+1] = '\0';
1291                         /* Move remaining data back to the front */
1292                         memmove(s->inbuf, s->inbuf + x + 1, s->inlen - x);
1293                         s->inlen -= (x + 1);
1294                         return 1;
1295                 }
1296         } 
1297         if (s->inlen >= sizeof(s->inbuf) - 1) {
1298                 ast_log(LOG_WARNING, "Dumping long line with no return from %s: %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr), s->inbuf);
1299                 s->inlen = 0;
1300         }
1301         fds[0].fd = s->fd;
1302         fds[0].events = POLLIN;
1303         res = poll(fds, 1, -1);
1304         if (res < 0) {
1305                 ast_log(LOG_WARNING, "Select returned error: %s\n", strerror(errno));
1306                 return -1;
1307         } else if (res > 0) {
1308                 ast_mutex_lock(&s->lock);
1309                 res = read(s->fd, s->inbuf + s->inlen, sizeof(s->inbuf) - 1 - s->inlen);
1310                 ast_mutex_unlock(&s->lock);
1311                 if (res < 1)
1312                         return -1;
1313         }
1314         s->inlen += res;
1315         s->inbuf[s->inlen] = '\0';
1316         return 0;
1317 }
1318
1319 static void *session_do(void *data)
1320 {
1321         struct mansession *s = data;
1322         struct message m;
1323         char iabuf[INET_ADDRSTRLEN];
1324         int res;
1325         
1326         ast_mutex_lock(&s->lock);
1327         ast_cli(s->fd, "Asterisk Call Manager/1.0\r\n");
1328         ast_mutex_unlock(&s->lock);
1329         memset(&m, 0, sizeof(&m));
1330         for (;;) {
1331                 res = get_input(s, m.headers[m.hdrcount]);
1332                 if (res > 0) {
1333                         /* Strip trailing \r\n */
1334                         if (strlen(m.headers[m.hdrcount]) < 2)
1335                                 continue;
1336                         m.headers[m.hdrcount][strlen(m.headers[m.hdrcount]) - 2] = '\0';
1337                         if (ast_strlen_zero(m.headers[m.hdrcount])) {
1338                                 if (process_message(s, &m))
1339                                         break;
1340                                 memset(&m, 0, sizeof(&m));
1341                         } else if (m.hdrcount < MAX_HEADERS - 1)
1342                                 m.hdrcount++;
1343                 } else if (res < 0)
1344                         break;
1345         }
1346         if (s->authenticated) {
1347                 if (option_verbose > 1) {
1348                         if (displayconnects) 
1349                                 ast_verbose(VERBOSE_PREFIX_2 "Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));    
1350                 }
1351                 ast_log(LOG_EVENT, "Manager '%s' logged off from %s\n", s->username, ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
1352         } else {
1353                 if (option_verbose > 1) {
1354                         if ( displayconnects )
1355                                 ast_verbose(VERBOSE_PREFIX_2 "Connect attempt from '%s' unable to authenticate\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
1356                 }
1357                 ast_log(LOG_EVENT, "Failed attempt from %s\n", ast_inet_ntoa(iabuf, sizeof(iabuf), s->sin.sin_addr));
1358         }
1359         destroy_session(s);
1360         return NULL;
1361 }
1362
1363 static void *accept_thread(void *ignore)
1364 {
1365         int as;
1366         struct sockaddr_in sin;
1367         socklen_t sinlen;
1368         struct mansession *s;
1369         struct protoent *p;
1370         int arg = 1;
1371         int flags;
1372         pthread_attr_t attr;
1373
1374         pthread_attr_init(&attr);
1375         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1376
1377         for (;;) {
1378                 sinlen = sizeof(sin);
1379                 as = accept(asock, (struct sockaddr *)&sin, &sinlen);
1380                 if (as < 0) {
1381                         ast_log(LOG_NOTICE, "Accept returned -1: %s\n", strerror(errno));
1382                         continue;
1383                 }
1384                 p = getprotobyname("tcp");
1385                 if (p) {
1386                         if( setsockopt(as, p->p_proto, TCP_NODELAY, (char *)&arg, sizeof(arg) ) < 0 ) {
1387                                 ast_log(LOG_WARNING, "Failed to set manager tcp connection to TCP_NODELAY mode: %s\n", strerror(errno));
1388                         }
1389                 }
1390                 s = malloc(sizeof(struct mansession));
1391                 if (!s) {
1392                         ast_log(LOG_WARNING, "Failed to allocate management session: %s\n", strerror(errno));
1393                         continue;
1394                 } 
1395                 memset(s, 0, sizeof(struct mansession));
1396                 memcpy(&s->sin, &sin, sizeof(sin));
1397
1398                 if(! block_sockets) {
1399                         /* For safety, make sure socket is non-blocking */
1400                         flags = fcntl(as, F_GETFL);
1401                         fcntl(as, F_SETFL, flags | O_NONBLOCK);
1402                 }
1403                 ast_mutex_init(&s->lock);
1404                 s->fd = as;
1405                 s->send_events = -1;
1406                 ast_mutex_lock(&sessionlock);
1407                 s->next = sessions;
1408                 sessions = s;
1409                 ast_mutex_unlock(&sessionlock);
1410                 if (ast_pthread_create(&t, &attr, session_do, s))
1411                         destroy_session(s);
1412         }
1413         pthread_attr_destroy(&attr);
1414         return NULL;
1415 }
1416
1417 /*--- manager_event: Send AMI event to client */
1418 int manager_event(int category, char *event, char *fmt, ...)
1419 {
1420         struct mansession *s;
1421         char tmp[4096];
1422         va_list ap;
1423
1424         ast_mutex_lock(&sessionlock);
1425         s = sessions;
1426         while(s) {
1427                 if (((s->readperm & category) == category) && ((s->send_events & category) == category) ) {
1428                         ast_mutex_lock(&s->lock);
1429                         if (!s->blocking) {
1430                                 ast_cli(s->fd, "Event: %s\r\n", event);
1431                                 ast_cli(s->fd, "Privilege: %s\r\n", authority_to_str(category, tmp, sizeof(tmp)));
1432                                 va_start(ap, fmt);
1433                                 vsnprintf(tmp, sizeof(tmp), fmt, ap);
1434                                 va_end(ap);
1435                                 ast_carefulwrite(s->fd,tmp,strlen(tmp),100);
1436                                 ast_cli(s->fd, "\r\n");
1437                         }
1438                         ast_mutex_unlock(&s->lock);
1439                 }
1440                 s = s->next;
1441         }
1442         ast_mutex_unlock(&sessionlock);
1443         return 0;
1444 }
1445
1446 int ast_manager_unregister( char *action ) {
1447         struct manager_action *cur = first_action, *prev = first_action;
1448
1449         ast_mutex_lock(&actionlock);
1450         while( cur ) {          
1451                 if (!strcasecmp(action, cur->action)) {
1452                         prev->next = cur->next;
1453                         free(cur);
1454                         if (option_verbose > 1) 
1455                                 ast_verbose(VERBOSE_PREFIX_2 "Manager unregistered action %s\n", action);
1456                         ast_mutex_unlock(&actionlock);
1457                         return 0;
1458                 }
1459                 prev = cur;
1460                 cur = cur->next;
1461         }
1462         ast_mutex_unlock(&actionlock);
1463         return 0;
1464 }
1465
1466 static int manager_state_cb(char *context, char *exten, int state, void *data)
1467 {
1468         /* Notify managers of change */
1469         manager_event(EVENT_FLAG_CALL, "ExtensionStatus", "Exten: %s\r\nContext: %s\r\nStatus: %d\r\n", exten, context, state);
1470         return 0;
1471 }
1472
1473 static int ast_manager_register_struct(struct manager_action *act)
1474 {
1475         struct manager_action *cur = first_action, *prev = NULL;
1476         int ret;
1477
1478         ast_mutex_lock(&actionlock);
1479         while(cur) { /* Walk the list of actions */
1480                 ret = strcasecmp(cur->action, act->action);
1481                 if (ret == 0) {
1482                         ast_log(LOG_WARNING, "Manager: Action '%s' already registered\n", act->action);
1483                         ast_mutex_unlock(&actionlock);
1484                         return -1;
1485                 } else if (ret > 0) {
1486                         /* Insert these alphabetically */
1487                         if (prev) {
1488                                 act->next = prev->next;
1489                                 prev->next = act;
1490                         } else {
1491                                 act->next = first_action;
1492                                 first_action = act;
1493                         }
1494                         break;
1495                 }
1496                 prev = cur; 
1497                 cur = cur->next;
1498         }
1499         
1500         if (!cur) {
1501                 if (prev)
1502                         prev->next = act;
1503                 else
1504                         first_action = act;
1505                 act->next = NULL;
1506         }
1507
1508         if (option_verbose > 1) 
1509                 ast_verbose(VERBOSE_PREFIX_2 "Manager registered action %s\n", act->action);
1510         ast_mutex_unlock(&actionlock);
1511         return 0;
1512 }
1513
1514 int ast_manager_register2(const char *action, int auth, int (*func)(struct mansession *s, struct message *m), const char *synopsis, const char *description)
1515 {
1516         struct manager_action *cur;
1517
1518         cur = malloc(sizeof(struct manager_action));
1519         if (!cur) {
1520                 ast_log(LOG_WARNING, "Manager: out of memory trying to register action\n");
1521                 ast_mutex_unlock(&actionlock);
1522                 return -1;
1523         }
1524         cur->action = action;
1525         cur->authority = auth;
1526         cur->func = func;
1527         cur->synopsis = synopsis;
1528         cur->description = description;
1529         cur->next = NULL;
1530
1531         ast_manager_register_struct(cur);
1532
1533         return 0;
1534 }
1535
1536 static int registered = 0;
1537
1538 int init_manager(void)
1539 {
1540         struct ast_config *cfg;
1541         char *val;
1542         int oldportno = portno;
1543         static struct sockaddr_in ba;
1544         int x = 1;
1545         if (!registered) {
1546                 /* Register default actions */
1547                 ast_manager_register2("Ping", 0, action_ping, "Keepalive command", mandescr_ping);
1548                 ast_manager_register2("Events", 0, action_events, "Control Event Flow", mandescr_events);
1549                 ast_manager_register2("Logoff", 0, action_logoff, "Logoff Manager", mandescr_logoff);
1550                 ast_manager_register2("Hangup", EVENT_FLAG_CALL, action_hangup, "Hangup Channel", mandescr_hangup);
1551                 ast_manager_register("Status", EVENT_FLAG_CALL, action_status, "Lists channel status" );
1552                 ast_manager_register2("Setvar", EVENT_FLAG_CALL, action_setvar, "Set Channel Variable", mandescr_setvar );
1553                 ast_manager_register2("Getvar", EVENT_FLAG_CALL, action_getvar, "Gets a Channel Variable", mandescr_getvar );
1554                 ast_manager_register2("Redirect", EVENT_FLAG_CALL, action_redirect, "Redirect (transfer) a call", mandescr_redirect );
1555                 ast_manager_register2("Originate", EVENT_FLAG_CALL, action_originate, "Originate Call", mandescr_originate);
1556                 ast_manager_register2("Command", EVENT_FLAG_COMMAND, action_command, "Execute Asterisk CLI Command", mandescr_command );
1557                 ast_manager_register2("ExtensionState", EVENT_FLAG_CALL, action_extensionstate, "Check Extension Status", mandescr_extensionstate );
1558                 ast_manager_register2("AbsoluteTimeout", EVENT_FLAG_CALL, action_timeout, "Set Absolute Timeout", mandescr_timeout );
1559                 ast_manager_register2("MailboxStatus", EVENT_FLAG_CALL, action_mailboxstatus, "Check Mailbox", mandescr_mailboxstatus );
1560                 ast_manager_register2("MailboxCount", EVENT_FLAG_CALL, action_mailboxcount, "Check Mailbox Message Count", mandescr_mailboxcount );
1561                 ast_manager_register2("ListCommands", 0, action_listcommands, "List available manager commands", mandescr_listcommands);
1562
1563                 ast_cli_register(&show_mancmd_cli);
1564                 ast_cli_register(&show_mancmds_cli);
1565                 ast_cli_register(&show_manconn_cli);
1566                 ast_extension_state_add(NULL, NULL, manager_state_cb, NULL);
1567                 registered = 1;
1568         }
1569         portno = DEFAULT_MANAGER_PORT;
1570         displayconnects = 1;
1571         cfg = ast_config_load("manager.conf");
1572         if (!cfg) {
1573                 ast_log(LOG_NOTICE, "Unable to open management configuration manager.conf.  Call management disabled.\n");
1574                 return 0;
1575         }
1576         memset(&ba, 0, sizeof(ba));
1577         val = ast_variable_retrieve(cfg, "general", "enabled");
1578         if (val)
1579                 enabled = ast_true(val);
1580
1581         val = ast_variable_retrieve(cfg, "general", "block-sockets");
1582         if(val)
1583                 block_sockets = ast_true(val);
1584
1585         if ((val = ast_variable_retrieve(cfg, "general", "port"))) {
1586                 if (sscanf(val, "%d", &portno) != 1) {
1587                         ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
1588                         portno = DEFAULT_MANAGER_PORT;
1589                 }
1590         } else if ((val = ast_variable_retrieve(cfg, "general", "portno"))) {
1591                 if (sscanf(val, "%d", &portno) != 1) {
1592                         ast_log(LOG_WARNING, "Invalid port number '%s'\n", val);
1593                         portno = DEFAULT_MANAGER_PORT;
1594                 }
1595                 ast_log(LOG_NOTICE, "Use of portno in manager.conf deprecated.  Please use 'port=%s' instead.\n", val);
1596         }
1597         /* Parsing the displayconnects */
1598         if ((val = ast_variable_retrieve(cfg, "general", "displayconnects"))) {
1599                         displayconnects = ast_true(val);;
1600         }
1601                                 
1602         
1603         ba.sin_family = AF_INET;
1604         ba.sin_port = htons(portno);
1605         memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
1606         
1607         if ((val = ast_variable_retrieve(cfg, "general", "bindaddr"))) {
1608                 if (!inet_aton(val, &ba.sin_addr)) { 
1609                         ast_log(LOG_WARNING, "Invalid address '%s' specified, using 0.0.0.0\n", val);
1610                         memset(&ba.sin_addr, 0, sizeof(ba.sin_addr));
1611                 }
1612         }
1613         
1614         if ((asock > -1) && ((portno != oldportno) || !enabled)) {
1615 #if 0
1616                 /* Can't be done yet */
1617                 close(asock);
1618                 asock = -1;
1619 #else
1620                 ast_log(LOG_WARNING, "Unable to change management port / enabled\n");
1621 #endif
1622         }
1623         ast_config_destroy(cfg);
1624         
1625         /* If not enabled, do nothing */
1626         if (!enabled) {
1627                 return 0;
1628         }
1629         if (asock < 0) {
1630                 asock = socket(AF_INET, SOCK_STREAM, 0);
1631                 if (asock < 0) {
1632                         ast_log(LOG_WARNING, "Unable to create socket: %s\n", strerror(errno));
1633                         return -1;
1634                 }
1635                 setsockopt(asock, SOL_SOCKET, SO_REUSEADDR, &x, sizeof(x));
1636                 if (bind(asock, (struct sockaddr *)&ba, sizeof(ba))) {
1637                         ast_log(LOG_WARNING, "Unable to bind socket: %s\n", strerror(errno));
1638                         close(asock);
1639                         asock = -1;
1640                         return -1;
1641                 }
1642                 if (listen(asock, 2)) {
1643                         ast_log(LOG_WARNING, "Unable to listen on socket: %s\n", strerror(errno));
1644                         close(asock);
1645                         asock = -1;
1646                         return -1;
1647                 }
1648                 if (option_verbose)
1649                         ast_verbose("Asterisk Management interface listening on port %d\n", portno);
1650                 ast_pthread_create(&t, NULL, accept_thread, NULL);
1651         }
1652         return 0;
1653 }
1654
1655 int reload_manager(void)
1656 {
1657         manager_event(EVENT_FLAG_SYSTEM, "Reload", "Message: Reload Requested\r\n");
1658         return init_manager();
1659 }