--- asterisk-cvs-040609/asterisk/channels/chan_sip.c	2004-06-09 03:45:08.000000000 +0200
+++ asterisk-cvs-040609-nogren/asterisk/channels/chan_sip.c	2004-06-14 16:56:07.000000000 +0200
@@ -154,6 +154,8 @@ static int globalrtptimeout = 0;
 
 static int globalrtpholdtimeout = 0;
 
+static int globaltrustrpid = 0;
+
 static int usecnt =0;
 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
 
@@ -326,6 +328,8 @@ static struct sip_pvt {
     	int stateid;
 	int dialogver;
 	
+        int trustrpid;
+	
         int dtmfmode;
         struct ast_dsp *vad;
 	
@@ -379,6 +383,7 @@ struct sip_user {
 	int outUse;
 	int outgoinglimit;
 	int restrictcid;
+        int trustrpid;
 	struct ast_ha *ha;
 	struct sip_user *next;
 };
@@ -411,6 +416,7 @@ struct sip_peer {
 	unsigned int callgroup;
 	unsigned int pickupgroup;
         int dtmfmode;
+        int trustrpid;
 	struct sockaddr_in addr;
 	struct in_addr mask;
 
@@ -2071,6 +2077,7 @@ static struct sip_pvt *sip_alloc(char *c
 	/* Assign default music on hold class */
         strncpy(p->musicclass, globalmusicclass, sizeof(p->musicclass));
 	p->dtmfmode = globaldtmfmode;
+	p->trustrpid = globaltrustrpid;
 	p->rtptimeout = globalrtptimeout;
 	p->rtpholdtimeout = globalrtpholdtimeout;
 	p->capability = capability;
@@ -4726,12 +4733,39 @@ static char *get_calleridname(char *inpu
 	return output;
 }
 
+/*--- get_rpid_num: Get caller id number from Remote-Party-ID header field 
+ *                  Returns true if valid and no privacy setting found 
+ */
+static int get_rpid_num(char *input,char *output)
+{
+  char *start;
+  char *end;
+
+  if(!input)
+    return 0;
+
+  if(strstr(input,"privacy=full") || strstr(input,"privacy=uri"))
+    return 0;
+
+  start = strchr(input,':');
+  end = strchr(input,'@');
+  start++;
+  end--;
+  if (!start || !end || (end <= input))
+    return 0;
+  /* we found "number" */
+  strncpy(output,start,(int)(end-start+1));
+  return 1;
+}
+
+
 /*--- check_user: Check if matching user or peer is defined ---*/
 static int check_user(struct sip_pvt *p, struct sip_request *req, char *cmd, char *uri, int reliable, struct sockaddr_in *sin, int ignore)
 {
 	struct sip_user *user;
 	struct sip_peer *peer;
 	char *of, from[256] = "", *c;
+	char *rpid,rpid_num[50];
 	int res = 0;
 	char *t;
 	char calleridname[50];
@@ -4744,6 +4778,12 @@ static int check_user(struct sip_pvt *p,
 	strncpy(from, of, sizeof(from) - 1);
 	memset(calleridname,0,sizeof(calleridname));
 	get_calleridname(from,calleridname);
+
+	rpid = get_header(req, "Remote-Party-ID");
+	if(!ast_strlen_zero(rpid))
+	  strncpy(p->remote_party_id,rpid,sizeof(p->remote_party_id)-1);
+	memset(rpid_num,0,sizeof(rpid_num));
+
 	of = ditch_braces(from);
 	if (ast_strlen_zero(p->exten)) {
 		t = uri;
@@ -4765,10 +4805,6 @@ static int check_user(struct sip_pvt *p,
 		*c = '\0';
 	if ((c = strchr(of, ':')))
 		*c = '\0';
-	if (*calleridname)
-		sprintf(p->callerid,"\"%s\" <%s>",calleridname,of);
-	else
-		strncpy(p->callerid, of, sizeof(p->callerid) - 1);
 	if (ast_strlen_zero(of))
 			return 0;
 	ast_mutex_lock(&userl.lock);
@@ -4795,6 +4831,7 @@ static int check_user(struct sip_pvt *p,
 			strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)  -1);
 			strncpy(p->language, user->language, sizeof(p->language)  -1);
 			strncpy(p->musicclass, user->musicclass, sizeof(p->musicclass)  -1);
+			p->trustrpid = user->trustrpid;
 			p->canreinvite = user->canreinvite;
 			p->amaflags = user->amaflags;
 			p->callgroup = user->callgroup;
@@ -4869,6 +4908,8 @@ static int check_user(struct sip_pvt *p,
 					else
 						p->noncodeccapability &= ~AST_RTP_DTMF;
 				}
+				p->trustrpid = peer->trustrpid;
+
 			}
 			if (peer->temponly) {
 				if (peer->ha) {
@@ -4882,6 +4923,32 @@ static int check_user(struct sip_pvt *p,
 		ast_mutex_unlock(&peerl.lock);
 
 	}
+
+	// Set callerid "name <number>"
+	// first, trust the configured callerid
+	if (user && !ast_strlen_zero(user->callerid) && !ast_strlen_zero(p->callerid)) {
+	  strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
+
+	  // then see if config or defaultconfig says to trust Remote-Party-ID header
+	} else if(p->trustrpid && !ast_strlen_zero(p->remote_party_id) && get_rpid_num(p->remote_party_id,rpid_num)) {
+
+	    // privacy not set, and valid number. Use that instead of from-header number
+
+	  if (*calleridname)
+	    sprintf(p->callerid,"\"%s\" <%s>",calleridname,rpid_num);
+	  else
+	    strncpy(p->callerid, rpid_num, sizeof(p->callerid) - 1);
+
+	} else {
+	  // Couldn't trust, set normal name/number
+	  
+	  if (*calleridname)
+	    sprintf(p->callerid,"\"%s\" <%s>",calleridname,of);
+	  else
+	    strncpy(p->callerid, of, sizeof(p->callerid) - 1);
+	}
+
+
 	return res;
 }
 
@@ -7209,6 +7276,7 @@ static struct sip_user *build_user(char 
 		user->capability = capability;
 
 		user->canreinvite = globalcanreinvite;
+		user->trustrpid = globaltrustrpid;
 		/* set default context */
 		strncpy(user->context, context, sizeof(user->context)-1);
 		strncpy(user->language, language, sizeof(user->language)-1);
@@ -7285,6 +7353,8 @@ static struct sip_user *build_user(char 
 				user->insecure = ast_true(v->value);
 			} else if (!strcasecmp(v->name, "restrictcid")) {
 				user->restrictcid = ast_true(v->value);
+			} else if (!strcasecmp(v->name, "trustrpid")) {
+                                user->trustrpid = ast_true(v->value);
 			}
 			/*else if (strcasecmp(v->name,"type"))
 			 *	ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
@@ -7321,6 +7391,7 @@ static struct sip_peer *temp_peer(char *
 	peer->rtpholdtimeout = globalrtpholdtimeout;
 	peer->selfdestruct = 1;
 	peer->dynamic = 1;
+	peer->trustrpid = globaltrustrpid;
 	reg_source_db(peer);
 	return peer;
 }
@@ -7380,6 +7451,7 @@ static struct sip_peer *build_peer(char 
 		peer->rtptimeout = globalrtptimeout;
 		peer->rtpholdtimeout = globalrtpholdtimeout;
 		peer->dtmfmode = 0;
+		peer->trustrpid = globaltrustrpid;
 		while(v) {
 			if (!strcasecmp(v->name, "secret")) 
 				strncpy(peer->secret, v->value, sizeof(peer->secret)-1);
@@ -7503,6 +7575,8 @@ static struct sip_peer *build_peer(char 
 					ast_log(LOG_WARNING, "Qualification of peer '%s' should be 'yes', 'no', or a number of milliseconds at line %d of sip.conf\n", peer->name, v->lineno);
 					peer->maxms = 0;
 				}
+			} else if (!strcasecmp(v->name, "trustrpid")) {
+                                peer->trustrpid = ast_true(v->value);
 			}
 			/* else if (strcasecmp(v->name,"type"))
 			 *	ast_log(LOG_WARNING, "Ignoring %s\n", v->name);
@@ -7616,6 +7690,8 @@ static int reload_config(void)
 			autocreatepeer = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "srvlookup")) {
 			srvlookup = ast_true(v->value);
+		} else if (!strcasecmp(v->name, "trustrpid")) {
+		        globaltrustrpid = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "pedantic")) {
 			pedanticsipchecking = ast_true(v->value);
 		} else if (!strcasecmp(v->name, "canreinvite")) {
