~ubuntu-branches/ubuntu/karmic/asterisk/karmic-security

1.1.1 by Mark Purcell
Import upstream version 1.0
1
/*
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
2
 * Asterisk -- An open source telephony toolkit.
3
 *
4
 * Copyright (C) 1999 - 2005, Digium, Inc.
5
 *
6
 * Mark Spencer <markster@digium.com>
7
 *
8
 * See http://www.asterisk.org for more information about
9
 * the Asterisk project. Please do not directly contact
10
 * any of the maintainers of this project for assistance;
11
 * the project provides a web site, mailing lists and IRC
12
 * channels for your use.
13
 *
14
 * This program is free software, distributed under the terms of
15
 * the GNU General Public License Version 2. See the LICENSE file
16
 * at the top of the source tree.
17
 */
18
19
/*! \file
20
 *
21
 * \brief Block all calls without Caller*ID, require phone # to be entered
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
22
 *
23
 * \author Mark Spencer <markster@digium.com>
1.1.1 by Mark Purcell
Import upstream version 1.0
24
 * 
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
25
 * \ingroup applications
1.1.1 by Mark Purcell
Import upstream version 1.0
26
 */
27
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
28
#include "asterisk.h"
29
1.2.4 by Dave Walker (Daviey)
Import upstream version 1.6.2.0~rc2
30
ASTERISK_FILE_VERSION(__FILE__, "$Revision: 211580 $")
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
31
32
#include "asterisk/lock.h"
33
#include "asterisk/file.h"
34
#include "asterisk/utils.h"
35
#include "asterisk/channel.h"
36
#include "asterisk/pbx.h"
37
#include "asterisk/module.h"
38
#include "asterisk/translate.h"
39
#include "asterisk/image.h"
40
#include "asterisk/callerid.h"
41
#include "asterisk/app.h"
42
#include "asterisk/config.h"
1.1.1 by Mark Purcell
Import upstream version 1.0
43
1.2.2 by Faidon Liambotis
Import upstream version 1.6.2.0~dfsg~beta3
44
/*** DOCUMENTATION
45
	<application name="PrivacyManager" language="en_US">
46
		<synopsis>
47
			Require phone number to be entered, if no CallerID sent
48
		</synopsis>
49
		<syntax>
50
			<parameter name="maxretries">
51
				<para>Total tries caller is allowed to input a callerid. Defaults to <literal>3</literal>.</para>
52
			</parameter>
53
			<parameter name="minlength">
54
				<para>Minimum allowable digits in the input callerid number. Defaults to <literal>10</literal>.</para>
55
			</parameter>
56
			<parameter name="context">
57
				<para>Context to check the given callerid against patterns.</para>
58
			</parameter>
59
		</syntax>
60
		<description>
61
			<para>If no Caller*ID is sent, PrivacyManager answers the channel and asks
62
			the caller to enter their phone number. The caller is given
63
			<replaceable>maxretries</replaceable> attempts to do so. The application does
64
			<emphasis>nothing</emphasis> if Caller*ID was received on the channel.</para>
65
			<para>The application sets the following channel variable upon completion:</para>
66
			<variablelist>
67
				<variable name="PRIVACYMGRSTATUS">
68
					<para>The status of the privacy manager's attempt to collect a phone number from the user.</para>
69
					<value name="SUCCESS"/>
70
					<value name="FAILED"/>
71
				</variable>
72
			</variablelist>
73
		</description>
74
		<see-also>
75
			<ref type="application">Zapateller</ref>
76
		</see-also>
77
	</application>
78
 ***/
79
80
1.1.1 by Mark Purcell
Import upstream version 1.0
81
static char *app = "PrivacyManager";
82
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
83
static int privacy_exec (struct ast_channel *chan, void *data)
1.1.1 by Mark Purcell
Import upstream version 1.0
84
{
85
	int res=0;
86
	int retries;
87
	int maxretries = 3;
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
88
	int minlength = 10;
89
	int x = 0;
90
	char phone[30];
91
	char *parse = NULL;
92
	AST_DECLARE_APP_ARGS(args,
93
		AST_APP_ARG(maxretries);
94
		AST_APP_ARG(minlength);
95
		AST_APP_ARG(options);
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
96
		AST_APP_ARG(checkcontext);
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
97
	);
1.1.1 by Mark Purcell
Import upstream version 1.0
98
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
99
	if (!ast_strlen_zero(chan->cid.cid_num)) {
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
100
		ast_verb(3, "CallerID Present: Skipping\n");
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
101
	} else {
1.1.1 by Mark Purcell
Import upstream version 1.0
102
		/*Answer the channel if it is not already*/
103
		if (chan->_state != AST_STATE_UP) {
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
104
			if ((res = ast_answer(chan)))
1.1.1 by Mark Purcell
Import upstream version 1.0
105
				return -1;
106
		}
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
107
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
108
		if (!ast_strlen_zero(data)) {
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
109
			parse = ast_strdupa(data);
110
			
111
			AST_STANDARD_APP_ARGS(args, parse);
112
113
			if (args.maxretries) {
1.2.4 by Dave Walker (Daviey)
Import upstream version 1.6.2.0~rc2
114
				if (sscanf(args.maxretries, "%30d", &x) == 1)
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
115
					maxretries = x;
116
				else
117
					ast_log(LOG_WARNING, "Invalid max retries argument\n");
118
			}
119
			if (args.minlength) {
1.2.4 by Dave Walker (Daviey)
Import upstream version 1.6.2.0~rc2
120
				if (sscanf(args.minlength, "%30d", &x) == 1)
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
121
					minlength = x;
122
				else
123
					ast_log(LOG_WARNING, "Invalid min length argument\n");
124
			}
125
		}		
126
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
127
		/* Play unidentified call */
1.1.1 by Mark Purcell
Import upstream version 1.0
128
		res = ast_safe_sleep(chan, 1000);
129
		if (!res)
130
			res = ast_streamfile(chan, "privacy-unident", chan->language);
131
		if (!res)
132
			res = ast_waitstream(chan, "");
133
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
134
		/* Ask for 10 digit number, give 3 attempts */
1.1.1 by Mark Purcell
Import upstream version 1.0
135
		for (retries = 0; retries < maxretries; retries++) {
136
			if (!res)
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
137
				res = ast_streamfile(chan, "privacy-prompt", chan->language);
138
			if (!res)
139
				res = ast_waitstream(chan, "");
140
141
			if (!res ) 
142
				res = ast_readstring(chan, phone, sizeof(phone) - 1, /* digit timeout ms */ 3200, /* first digit timeout */ 5000, "#");
143
1.1.1 by Mark Purcell
Import upstream version 1.0
144
			if (res < 0)
145
				break;
146
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
147
			/* Make sure we get at least digits */
148
			if (strlen(phone) >= minlength ) {
149
				/* if we have a checkcontext argument, do pattern matching */
150
				if (!ast_strlen_zero(args.checkcontext)) {
151
					if (!ast_exists_extension(NULL, args.checkcontext, phone, 1, NULL)) {
152
						res = ast_streamfile(chan, "privacy-incorrect", chan->language);
153
						if (!res) {
154
							res = ast_waitstream(chan, "");
155
						}
156
					} else {
157
						break;
158
					}
159
				} else {
160
					break;
161
				}
162
			} else {
1.1.1 by Mark Purcell
Import upstream version 1.0
163
				res = ast_streamfile(chan, "privacy-incorrect", chan->language);
164
				if (!res)
165
					res = ast_waitstream(chan, "");
166
			}
167
		}
168
		
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
169
		/* Got a number, play sounds and send them on their way */
1.1.6 by Soren Hansen
Import upstream version 1.2.10.dfsg
170
		if ((retries < maxretries) && res >= 0 ) {
1.1.1 by Mark Purcell
Import upstream version 1.0
171
			res = ast_streamfile(chan, "privacy-thankyou", chan->language);
172
			if (!res)
173
				res = ast_waitstream(chan, "");
1.1.6 by Soren Hansen
Import upstream version 1.2.10.dfsg
174
175
			ast_set_callerid (chan, phone, "Privacy Manager", NULL); 
176
177
			/* Clear the unavailable presence bit so if it came in on PRI
178
			 * the caller id will now be passed out to other channels
179
			 */
180
			chan->cid.cid_pres &= (AST_PRES_UNAVAILABLE ^ 0xFF);
181
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
182
			ast_verb(3, "Changed Caller*ID to %s, callerpres to %d\n",phone,chan->cid.cid_pres);
183
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
184
			pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "SUCCESS");
1.1.1 by Mark Purcell
Import upstream version 1.0
185
		} else {
1.1.4 by Mark Purcell
Import upstream version 1.2.1.dfsg
186
			pbx_builtin_setvar_helper(chan, "PRIVACYMGRSTATUS", "FAILED");
1.1.1 by Mark Purcell
Import upstream version 1.0
187
		}
188
	}
189
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
190
	return 0;
1.1.1 by Mark Purcell
Import upstream version 1.0
191
}
192
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
193
static int unload_module(void)
1.1.1 by Mark Purcell
Import upstream version 1.0
194
{
1.1.23 by Mark Purcell
Import upstream version 1.6.1.0~dfsg~rc3
195
	return ast_unregister_application (app);
1.1.1 by Mark Purcell
Import upstream version 1.0
196
}
197
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
198
static int load_module(void)
199
{
1.2.2 by Faidon Liambotis
Import upstream version 1.6.2.0~dfsg~beta3
200
	return ast_register_application_xml(app, privacy_exec);
1.1.11 by Lionel Porcheron
Import upstream version 1.4.4~dfsg
201
}
202
203
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Require phone number to be entered, if no CallerID sent");