rtsecuritymanager/rtsecuritymanagerserver/src/rtsecmgrpolicyparser.cpp
changeset 0 99ef825efeca
equal deleted inserted replaced
-1:000000000000 0:99ef825efeca
       
     1 /*
       
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:       Utility XML parser to parse trust and access policy files
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <w32std.h>
       
    25 #include <f32file.h>
       
    26 
       
    27 #include "rtsecmgrpolicyparser.h"
       
    28 #include "rtsecmgrdata.h"
       
    29 #include "rtsecmgrserverdef.h"
       
    30 
       
    31 #include <gmxmldocument.h>
       
    32 #include <gmxmlelement.h>
       
    33 #include <gmxmlcharacterdata.h>
       
    34 #include <gmxmltext.h>
       
    35 #include <gmxmlnode.h>
       
    36 
       
    37 _LIT (KPolicy, "policy");
       
    38 _LIT (KAllow, "allow");
       
    39 _LIT (KUser, "user");
       
    40 _LIT (KDomain, "domain");
       
    41 _LIT (KNameAttr, "name");
       
    42 _LIT (KCondition, "condition");
       
    43 _LIT (KCapabilities, "capabilities");
       
    44 _LIT (KDefCondition, "defaultCondition");
       
    45 _LIT (KText, "#TEXT");
       
    46 _LIT (KComment, "comment");
       
    47 _LIT (KAlias, "alias");
       
    48 
       
    49 CPolicyParser::CPolicyParser()
       
    50 	{
       
    51 	}
       
    52 
       
    53 CPolicyParser* CPolicyParser::NewL()
       
    54 	{
       
    55 	CPolicyParser* self = CPolicyParser::NewLC ();
       
    56 	//nothing to construct as of now
       
    57 	CleanupStack::Pop (self);
       
    58 	return self;
       
    59 	}
       
    60 
       
    61 CPolicyParser* CPolicyParser::NewLC()
       
    62 	{
       
    63 	CPolicyParser* self = new (ELeave) CPolicyParser();
       
    64 	CleanupStack::PushL (self);
       
    65 	//nothing to construct as of now
       
    66 	return self;
       
    67 	}
       
    68 
       
    69 CPolicyParser::~CPolicyParser()
       
    70 	{
       
    71 	if ( iDomParser)
       
    72 		{
       
    73 		delete iDomParser;
       
    74 		iDomParser = NULL;
       
    75 		}
       
    76 	}
       
    77 
       
    78 TInt CPolicyParser::GetPolicyInfo(RFile& aSecPolicy,
       
    79 		RProtectionDomains& aPolicyInfo, RAliasGroup& aAliasGroup)
       
    80 	{
       
    81 	isTrustInfo = EFalse;
       
    82 
       
    83 	if ( iDomParser)
       
    84 		{
       
    85 		delete iDomParser;
       
    86 		iDomParser = NULL;
       
    87 		}
       
    88 
       
    89 	iDomParser = CMDXMLParser::NewL (this);
       
    90 	iLastError = KErrNone;
       
    91 	
       
    92 	RFile parseHandle;
       
    93 	User::LeaveIfError ( parseHandle.Duplicate ( aSecPolicy));
       
    94 
       
    95 	iPolicyInfo.Reset (); //cleans-up the array, but doesn't delete the entries..
       
    96 	iAliasGroup.Reset ();
       
    97 
       
    98 	iLastError = iDomParser->ParseFile (parseHandle); //xml file read operation
       
    99 	
       
   100 	if ( KErrNone == iLastError)
       
   101 		iWaitScheduler.Start (); //actual parsing starts here asynchronously
       
   102 
       
   103 	if ( KErrNone==iLastError)   //parsing is successful
       
   104 	{
       
   105 		aPolicyInfo = iPolicyInfo;
       
   106 		aAliasGroup = iAliasGroup;
       
   107 	}
       
   108 
       
   109 	return iLastError;
       
   110 	}
       
   111 
       
   112 // Call back function called when a parsing operation completes.
       
   113 void CPolicyParser::ParseFileCompleteL()
       
   114 	{
       
   115 	iLastError = iDomParser->Error();
       
   116 	
       
   117 	if(iLastError == KErrXMLMissingDocTypeTag)
       
   118 	    iLastError = KErrNone;
       
   119 	
       
   120 	iWaitScheduler.AsyncStop ();
       
   121 	    	
       
   122 	if(iLastError!=KErrNone)
       
   123 		{
       
   124 		    return;
       
   125 		}
       
   126 	else
       
   127 	    {
       
   128           	if ( iDomParser && !isTrustInfo) //pre-requisite that the DOM instance is intact
       
   129         		{
       
   130         		CMDXMLDocument* documentNode = iDomParser->DetachXMLDoc ();
       
   131         
       
   132         		if ( documentNode)
       
   133         			{
       
   134         			CleanupStack::PushL(documentNode);
       
   135         			//Gets the root element of the DOM tree.
       
   136         			CMDXMLElement* rootElemNode = documentNode->DocumentElement ();
       
   137         
       
   138         			CMDXMLNode* rootNode = rootElemNode->FirstChild ();
       
   139         
       
   140         			if ( !rootNode)
       
   141         				{
       
   142         				iLastError = EErrInvalidRootNode;
       
   143         				CleanupStack::PopAndDestroy(documentNode);
       
   144         				return;
       
   145         				}
       
   146         
       
   147         			TBool isValid = EFalse;
       
   148         
       
   149         			for (; rootNode;rootNode=rootNode->NextSibling ())
       
   150         				{
       
   151         				if ( rootNode->NodeType ()==CMDXMLNode::EElementNode)
       
   152         					{
       
   153         					if ( 0==rootNode->NodeName().CompareF (KPolicy ()))
       
   154         						{
       
   155         						isValid = ETrue;
       
   156         						break;
       
   157         						}
       
   158         					else
       
   159         						continue;
       
   160         					}
       
   161         				else
       
   162         					{
       
   163         					 if(KErrNone == rootNode->NodeName().CompareF(KText))
       
   164         						{
       
   165         						iLastError = EErrJunkContent;
       
   166         						iPolicyInfo.ResetAndDestroy ();
       
   167         						iAliasGroup.ResetAndDestroy ();
       
   168         						CleanupStack::PopAndDestroy(documentNode);
       
   169         						return;										
       
   170         						}
       
   171         					}
       
   172         				}
       
   173         
       
   174         			if ( !isValid)
       
   175         				{
       
   176         				iLastError = EErrInvalidRootNode;
       
   177         				iPolicyInfo.ResetAndDestroy ();
       
   178         				iAliasGroup.ResetAndDestroy ();
       
   179         				CleanupStack::PopAndDestroy(documentNode);
       
   180         				return;
       
   181         				}
       
   182         
       
   183         			TPtrC attrVal;
       
   184         
       
   185         			if ( rootNode->HasChildNodes ())
       
   186         				{
       
   187         				CMDXMLNode* childNode = rootNode->FirstChild ();
       
   188         
       
   189         				//Now get the list of <alias> nodes
       
   190         				while ( childNode)
       
   191         					{
       
   192         					TPtrC nodeName = childNode->NodeName ();
       
   193         
       
   194         					//ALIAS PARSING
       
   195         					if ( nodeName.CompareF (KAlias)== 0)
       
   196         						{
       
   197         						CMDXMLElement
       
   198         								* aliasNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   199         					
       
   200         						//store alias information in object
       
   201         						CPermission* aliasObj = NULL;
       
   202         																					
       
   203         							{//Get the attribute "name" to get the alias name		    								    		
       
   204         							aliasNode->GetAttribute(KNameAttr, attrVal);
       
   205         
       
   206         							if ( attrVal.Length ()>0)
       
   207         								{
       
   208         									if ( isAliasPresent (attrVal))
       
   209         									{
       
   210         									//invalid xml file
       
   211         									if(aliasObj)
       
   212         										delete aliasObj;
       
   213         									iPolicyInfo.ResetAndDestroy ();
       
   214         									iAliasGroup.ResetAndDestroy ();
       
   215         									iLastError = EErrRepeatedAliasTag;
       
   216         									CleanupStack::PopAndDestroy(documentNode);
       
   217         									return;
       
   218         									}
       
   219         									
       
   220         									aliasObj = CPermission::NewL();
       
   221         									aliasObj->SetPermName(attrVal);
       
   222         								}
       
   223         							else
       
   224         								{
       
   225         								//alias name is invalid
       
   226         								//check whether TPerm / alias name should be deleted
       
   227         								if(aliasObj)
       
   228         									delete aliasObj;
       
   229         								iPolicyInfo.ResetAndDestroy ();
       
   230         								iAliasGroup.ResetAndDestroy ();
       
   231         								iLastError = EErrInvalidAliasName;
       
   232         								CleanupStack::PopAndDestroy(documentNode);
       
   233         								return;
       
   234         								}
       
   235         							
       
   236         							if ( aliasNode->HasChildNodes ())
       
   237         								{
       
   238         									CPermissionSet *aliasCaps = CPermissionSet::NewL();
       
   239         						
       
   240         									TBool result = GetCapabilitiesForGroup(
       
   241         														(CMDXMLElement*)aliasNode,
       
   242         														*aliasCaps, *aliasObj); //get list of uncond caps
       
   243         									
       
   244         									if(result < 0)
       
   245         									{
       
   246         										//Invalid capability string in policy file
       
   247         										if(aliasObj)
       
   248         											delete aliasObj;
       
   249         										if(aliasCaps)
       
   250         											delete aliasCaps;
       
   251         										iPolicyInfo.ResetAndDestroy ();
       
   252         										iAliasGroup.ResetAndDestroy ();
       
   253         										iLastError = result;  //EErrInvalidCapability or EErrRepeatedCaps
       
   254         										CleanupStack::PopAndDestroy(documentNode);
       
   255         										return;
       
   256         									}
       
   257         									if(aliasCaps)
       
   258         										delete aliasCaps;
       
   259         									
       
   260         								}
       
   261         							}
       
   262         							
       
   263         							if(aliasObj)
       
   264         								iAliasGroup.AppendL(aliasObj); 
       
   265         						}
       
   266         						
       
   267         					//check if the domain name is already present..
       
   268         					//need to check to avoid repeated domain tags..
       
   269         					if ( nodeName.CompareF (KDomain)== 0)
       
   270         						{
       
   271         						CMDXMLElement
       
   272         								* domainNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   273         						CProtectionDomain* pPolicy=  NULL;
       
   274         
       
   275         							{//Get the attribute "name" to get the domain name		    								    		
       
   276         							domainNode->GetAttribute (KNameAttr, attrVal);
       
   277         
       
   278         							if ( attrVal.Length ()>0)
       
   279         								{
       
   280         								if ( isDomainPresent (attrVal))
       
   281         									{
       
   282         									//invalid xml file
       
   283         									//delete pPolicy;
       
   284         									iPolicyInfo.ResetAndDestroy ();
       
   285         									iAliasGroup.ResetAndDestroy ();
       
   286         									iLastError = EErrRepeatedDomainTag;
       
   287         									CleanupStack::PopAndDestroy(documentNode);
       
   288         									return;
       
   289         									}
       
   290         
       
   291         								pPolicy = CProtectionDomain::NewL ();
       
   292         								pPolicy->SetDomainName (attrVal);
       
   293         								}
       
   294         							else
       
   295         								{
       
   296         								//domain name is invalid
       
   297         								if ( pPolicy)
       
   298         									delete pPolicy;
       
   299         								iPolicyInfo.ResetAndDestroy ();
       
   300         								iAliasGroup.ResetAndDestroy ();
       
   301         								iLastError = EErrInvalidDomainName;
       
   302         								CleanupStack::PopAndDestroy(documentNode);
       
   303         								return;
       
   304         								}
       
   305         
       
   306         							if ( domainNode->HasChildNodes ())
       
   307         								{
       
   308         								CPermissionSet* caps = CPermissionSet::NewL ();
       
   309         
       
   310         								CMDXMLNode
       
   311         										* domainChildNode = domainNode->FirstChild ();
       
   312         								for (; domainChildNode;domainChildNode=domainChildNode->NextSibling ())
       
   313         									{
       
   314         									if ( domainChildNode->NodeType ()==CMDXMLNode::EElementNode)
       
   315         										{
       
   316         										if ( 0==domainChildNode->NodeName().CompareF (KAllow))//<Allow> unconditional node
       
   317         											{
       
   318         												TBool result = GetCapabilities(
       
   319         														(CMDXMLElement*)domainChildNode,
       
   320         														*caps); //get list of uncond caps
       
   321         														
       
   322         												if(result < 0)
       
   323         												{
       
   324         													//Invalid capability string in policy file
       
   325         													if ( pPolicy)
       
   326         														delete pPolicy;
       
   327         													if(caps)
       
   328         														delete caps;
       
   329         													iPolicyInfo.ResetAndDestroy ();
       
   330         													iAliasGroup.ResetAndDestroy ();
       
   331         													iLastError = result;  //EErrInvalidCapability or EErrRepeatedCaps
       
   332         													CleanupStack::PopAndDestroy(documentNode);
       
   333         													return;
       
   334         												}
       
   335         											}	
       
   336         										else if ( 0==domainChildNode->NodeName().CompareF (KUser))//<User> Conditional node
       
   337         											{
       
   338         												//Get UGCaps
       
   339         												TUserPromptOption upOption(RTUserPrompt_UnDefined);
       
   340         												
       
   341         												TInt error = GetConditions ((CMDXMLElement*)domainChildNode, upOption);//Get conditions
       
   342         												
       
   343         												if(EErrInvalidPermission == error)
       
   344         												{
       
   345         													// invalid permission is specified
       
   346         													if ( pPolicy)
       
   347         														delete pPolicy;
       
   348         													if(caps)
       
   349         														delete caps;
       
   350         													iLastError = EErrInvalidPermission;
       
   351         													iPolicyInfo.ResetAndDestroy ();
       
   352         													iAliasGroup.ResetAndDestroy ();
       
   353         													CleanupStack::PopAndDestroy(documentNode);
       
   354         													return;
       
   355         												}
       
   356         													
       
   357         												TUserPromptOption
       
   358         														defCond = GetDefaultCondition ((CMDXMLElement*)domainChildNode);
       
   359         
       
   360         												if(RTUserPrompt_UnDefined == defCond)
       
   361         													{
       
   362         													//default condition is not specified
       
   363         													if ( pPolicy)
       
   364         														delete pPolicy;
       
   365         													if(caps)
       
   366         														delete caps;
       
   367         													iLastError = EErrNoDefaultCondition;
       
   368         													iPolicyInfo.ResetAndDestroy ();
       
   369         													iAliasGroup.ResetAndDestroy ();
       
   370         													CleanupStack::PopAndDestroy(documentNode);
       
   371         													return;
       
   372         													}
       
   373         													
       
   374         												//default condition is specified
       
   375         												error = GetCapabilities (
       
   376         															(CMDXMLElement*)domainChildNode,
       
   377         															*caps,
       
   378         															upOption,
       
   379         															ETrue, defCond); //get list of ug caps	
       
   380         												if(error < 0)
       
   381         												{
       
   382         													//Invalid capability string in policy file
       
   383         													if ( pPolicy)
       
   384         														delete pPolicy;
       
   385         													if(caps)
       
   386         														delete caps;
       
   387         													iPolicyInfo.ResetAndDestroy ();
       
   388         													iAliasGroup.ResetAndDestroy ();
       
   389         													iLastError = error;  //EErrInvalidCapability or EErrRepeatedCaps
       
   390         													CleanupStack::PopAndDestroy(documentNode);
       
   391         													return;
       
   392         												}
       
   393         											}
       
   394         											else  	// neither USER nor ALLOW node
       
   395         											{
       
   396         												//checking for misplaced alias tag
       
   397         												if((KErrNone == domainChildNode->NodeName().CompareF(KAlias)))
       
   398         												{
       
   399         													if ( pPolicy)
       
   400         														delete pPolicy;
       
   401         													if(caps)
       
   402         														delete caps;
       
   403         													iLastError = EErrMisplacedAlias;
       
   404         													iAliasGroup.ResetAndDestroy ();
       
   405         													iPolicyInfo.ResetAndDestroy ();
       
   406         													CleanupStack::PopAndDestroy(documentNode);
       
   407         													return;										
       
   408         												}
       
   409         											}
       
   410         										}
       
   411         										else
       
   412         										{
       
   413         											//the node is not an element node
       
   414         											//hence it should be comment / junk content
       
   415         											if((KErrNone == domainChildNode->NodeName().CompareF(KText)))
       
   416         											{
       
   417         												if ( pPolicy)
       
   418         													delete pPolicy;
       
   419         												if (caps)
       
   420         													delete caps;
       
   421         												iLastError = EErrJunkContent;
       
   422         												iPolicyInfo.ResetAndDestroy ();
       
   423         												iAliasGroup.ResetAndDestroy ();
       
   424         												CleanupStack::PopAndDestroy(documentNode);
       
   425         												return;										
       
   426         											}
       
   427         											else if(KErrNone == domainChildNode->NodeName().CompareF(KComment))
       
   428         												continue;
       
   429         										}
       
   430         									}									
       
   431         								if ( pPolicy)
       
   432         									pPolicy->SetCapInfo (caps);
       
   433         
       
   434         								}
       
   435         							}
       
   436         
       
   437         						if ( pPolicy)
       
   438         							iPolicyInfo.AppendL (pPolicy);
       
   439         						}
       
   440         						else if((KErrNone == nodeName.CompareF(KText)))
       
   441         						{
       
   442         							iLastError = EErrJunkContent;
       
   443         							iPolicyInfo.ResetAndDestroy ();
       
   444         							iAliasGroup.ResetAndDestroy ();
       
   445         							CleanupStack::PopAndDestroy(documentNode);
       
   446         							return;										
       
   447         						}
       
   448         						
       
   449         					childNode = childNode->NextSibling (); //traverse to the next sibling node			    			
       
   450         
       
   451         					}
       
   452         				} //rootNode->HasChildNodes()
       
   453         			else
       
   454         				{
       
   455         				//No policy information in the xml file
       
   456         				//Treat this as an error
       
   457         				iLastError = EErrNoPolicyInfo;
       
   458         				iPolicyInfo.ResetAndDestroy ();
       
   459         				iAliasGroup.ResetAndDestroy ();
       
   460         				CleanupStack::PopAndDestroy(documentNode);
       
   461         				return;
       
   462         				}
       
   463         
       
   464         			CleanupStack::PopAndDestroy(documentNode);
       
   465         			}
       
   466         		else
       
   467         			{
       
   468         			iLastError = EErrInvalidDocNode;
       
   469         			return;
       
   470         			}
       
   471         		}
       
   472 	    }
       
   473 	}
       
   474 
       
   475 TInt CPolicyParser::GetCapsNode(CMDXMLElement* aParentDomainNode,
       
   476 		RPointerArray<CMDXMLElement>& aCapNodes, const TDesC& aNodeName)
       
   477 	{
       
   478 	if ( aParentDomainNode->HasChildNodes ())
       
   479 		{
       
   480 		CMDXMLNode* childNode = aParentDomainNode->FirstChild ();
       
   481 
       
   482 		do
       
   483 			{
       
   484 			if ( childNode)
       
   485 				{
       
   486 				if ( 0==childNode->NodeName().Compare (aNodeName))
       
   487 					{
       
   488 					CMDXMLElement* pAllowNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   489 					aCapNodes.AppendL (pAllowNode);
       
   490 					}
       
   491 
       
   492 				childNode = childNode->NextSibling ();
       
   493 				}
       
   494 
       
   495 			}
       
   496 		while (NULL != childNode);
       
   497 		}
       
   498 
       
   499 	return EErrNone; //No other possible return err code...
       
   500 	}
       
   501 
       
   502 CMDXMLElement* CPolicyParser::GetCapsNode(CMDXMLElement* aParentDomainNode,
       
   503 		const TDesC& aNodeName)
       
   504 	{
       
   505 	if ( aParentDomainNode->HasChildNodes ())
       
   506 		{
       
   507 		CMDXMLNode* childNode = aParentDomainNode->FirstChild ();
       
   508 
       
   509 		do
       
   510 			{
       
   511 			if ( childNode)
       
   512 				{
       
   513 				if ( 0==childNode->NodeName().Compare (aNodeName))
       
   514 					{
       
   515 					CMDXMLElement* pAllowNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   516 					return pAllowNode;
       
   517 					}
       
   518 
       
   519 				childNode = childNode->NextSibling ();
       
   520 				}
       
   521 
       
   522 			}
       
   523 		while (NULL != childNode);
       
   524 		}
       
   525 	return NULL;
       
   526 	}
       
   527 
       
   528 TInt CPolicyParser::GetCapabilities(CMDXMLElement* aParentNode,
       
   529 		CPermissionSet& aCapInfo, TUserPromptOption aUpOpt, TBool aUGCaps,
       
   530 		TUserPromptOption aDefUpOpt)
       
   531 	{
       
   532 	if ( aParentNode->HasChildNodes ())
       
   533 		{
       
   534 		CMDXMLNode* childNode = aParentNode->FirstChild ();
       
   535 
       
   536 		do
       
   537 			{
       
   538 			if ( childNode)
       
   539 				{
       
   540 				CMDXMLNode* capabilitiesNode=  NULL;
       
   541 				if ( 0==childNode->NodeName().CompareF (KCapabilities))
       
   542 					{
       
   543 					capabilitiesNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   544 
       
   545 					//traverse <capability/> nodes under <capabilities/>
       
   546 					if ( capabilitiesNode->HasChildNodes ())
       
   547 						{
       
   548 						RCapabilityArray capabilities;
       
   549 						
       
   550 						for (CMDXMLNode
       
   551 								* capabilityNode=capabilitiesNode->FirstChild (); capabilityNode;capabilityNode=capabilityNode->NextSibling ())
       
   552 							{
       
   553 							//Get the textnode under <capability/>
       
   554 							if ( capabilityNode)
       
   555 								{
       
   556 								for (CMDXMLNode
       
   557 										* capChildNode=capabilityNode->FirstChild (); capChildNode;capChildNode=capChildNode->NextSibling ())
       
   558 									{
       
   559 									//Get the textnode under <capability/>
       
   560 									if ( CMDXMLNode::ETextNode==capChildNode->NodeType ())
       
   561 										{
       
   562 										CMDXMLText
       
   563 												* capTextNode = dynamic_cast<CMDXMLText*>(capChildNode);
       
   564 										//Extract text out of this node
       
   565 										TCapability
       
   566 												cap = GetCapability (capTextNode->Data ());
       
   567 
       
   568 										if ((ECapability_None!=cap))
       
   569 											{
       
   570 											
       
   571 											// Get LOGIC for the storage of Group capability
       
   572 											
       
   573 											if ( aUGCaps)
       
   574 												{
       
   575 												if ( (!IsPresent(aCapInfo,cap)) && (!IsUserGrantPresent(aCapInfo,cap)))
       
   576 													{
       
   577 														capabilities.Append(cap);
       
   578 														
       
   579 														if ( RTUserPrompt_UnDefined==aUpOpt)								
       
   580 															{
       
   581 																CPermission* perm = CPermission::NewL(capabilities, aDefUpOpt,aDefUpOpt);
       
   582 																aCapInfo.AppendPermission (*perm);
       
   583 															}
       
   584 																
       
   585 															else
       
   586 															{
       
   587 																CPermission* perm = CPermission::NewL(capabilities, aDefUpOpt,aUpOpt);
       
   588 																aCapInfo.AppendPermission (*perm);
       
   589 															}
       
   590 													}
       
   591 													else
       
   592 													{
       
   593 														return EErrRepeatedCaps;	
       
   594 													}
       
   595 												}
       
   596 											else
       
   597 												{
       
   598 													//Check before appending unconditional capability that
       
   599 													//whether the capability is already added or not...
       
   600 													if ( (!IsPresent(aCapInfo,cap)) && (!IsUserGrantPresent(aCapInfo,cap)))
       
   601 													{
       
   602 														aCapInfo.AppendUncondCap (cap);	
       
   603 													}
       
   604 													else
       
   605 													{
       
   606 														//capability string given more than once.. hence invalid
       
   607 														return EErrRepeatedCaps;		
       
   608 													}
       
   609 												}
       
   610 											}
       
   611 										else
       
   612 											{
       
   613 												TBool invalidCaps = ETrue;
       
   614 												
       
   615 												for(TInt idx = 0; idx < iAliasGroup.Count(); idx++)
       
   616 												{
       
   617 													CPermission *tempObj = iAliasGroup[idx];
       
   618 													TPtrC name = tempObj->PermName();
       
   619 													
       
   620 													if ((KErrNone == capTextNode->Data().CompareF(name)))
       
   621 													{
       
   622 													//it is not a text node. so is an alias group
       
   623 													
       
   624 													//alias group name is found. so obtain the caps
       
   625 													TUint32 capAlias = tempObj->PermissionData();
       
   626 													invalidCaps = EFalse;
       
   627 													if( aUGCaps)
       
   628 														{
       
   629 															for(TInt idx1 = 0; idx1 < 20; idx1++)
       
   630 															{
       
   631 																TUint32 tempCapToCheck(KDefaultEnableBit);
       
   632 																tempCapToCheck = tempCapToCheck << (idx1);
       
   633 																
       
   634 																if(capAlias & tempCapToCheck)
       
   635 																	capabilities.Append((TCapability)idx1);
       
   636 																
       
   637 															}
       
   638 															if ( RTUserPrompt_UnDefined==aUpOpt)								
       
   639 															{
       
   640 																CPermission* perm = CPermission::NewL(capabilities, aDefUpOpt,aDefUpOpt);
       
   641 																perm->SetPermName(name);
       
   642 																aCapInfo.AppendPermission (*perm);
       
   643 															}
       
   644 																
       
   645 															else
       
   646 															{
       
   647 																CPermission* perm = CPermission::NewL(capabilities, aDefUpOpt,aUpOpt);
       
   648 																perm->SetPermName(name);
       
   649 																aCapInfo.AppendPermission (*perm);
       
   650 															}
       
   651 														}
       
   652 													else
       
   653 														{
       
   654 														TUint32 capPresent = aCapInfo.UnconditionalCaps();
       
   655 														TUint32 capAfter = capAlias | capPresent;
       
   656 														TUint32 capRep = capAlias ^ capPresent;
       
   657 														TUint32 errChk = capRep & capAfter; //error if capAfter and error are different
       
   658 														
       
   659 														if(!(errChk & capAfter))
       
   660 															{
       
   661 															//capability string given more than once.. hence invalid
       
   662 															return EErrRepeatedCaps;
       
   663 															}
       
   664 															
       
   665 														aCapInfo.AppendUncondCapabilities(capAfter);	
       
   666 														}
       
   667 													}
       
   668 												}
       
   669 
       
   670 												if (invalidCaps)
       
   671 												{
       
   672 												//just log the error message
       
   673 												RDebug::Print (_L ("CPolicyParser::GetCapabilities : Invalid capability string"));
       
   674 												return EErrInvalidCapability;
       
   675 												}
       
   676 											}
       
   677 
       
   678 										}
       
   679 									}
       
   680 								}
       
   681 								capabilities.Close();						
       
   682 							}
       
   683 						}
       
   684 					else
       
   685 						{
       
   686 						RDebug::Print (_L ("CPolicyParser::GetCapabilities : No <capability> under <capabilities/>"));
       
   687 						}
       
   688 
       
   689 					}
       
   690 
       
   691 				childNode = childNode->NextSibling ();
       
   692 				}
       
   693 			else
       
   694 				{
       
   695 				RDebug::Print (_L ("CPolicyParser::GetCapabilities : Childnode NULL"));
       
   696 				}
       
   697 			}
       
   698 		while (NULL != childNode);
       
   699 		}
       
   700 		
       
   701 	return EErrNone;
       
   702 	}
       
   703 
       
   704 TInt CPolicyParser::GetCapabilitiesForGroup(CMDXMLElement* aParentNode,
       
   705 		CPermissionSet& aCapInfo, CPermission& aAliasInfo, TUserPromptOption aUpOpt, TBool aUGCaps,
       
   706 		TUserPromptOption aDefUpOpt)
       
   707 	{
       
   708 	if ( aParentNode->HasChildNodes ())
       
   709 		{
       
   710 		CMDXMLNode* childNode = aParentNode->FirstChild ();
       
   711 
       
   712 		do
       
   713 			{
       
   714 			if ( childNode)
       
   715 				{
       
   716 				CMDXMLNode* capabilitiesNode=  NULL;
       
   717 				if ( 0==childNode->NodeName().CompareF (KCapabilities))
       
   718 					{
       
   719 					capabilitiesNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   720 
       
   721 					//traverse <capability/> nodes under <capabilities/>
       
   722 					if ( capabilitiesNode->HasChildNodes ())
       
   723 						{
       
   724 						RCapabilityArray capabilities;
       
   725 						
       
   726 						for (CMDXMLNode
       
   727 								* capabilityNode=capabilitiesNode->FirstChild (); capabilityNode;capabilityNode=capabilityNode->NextSibling ())
       
   728 							{
       
   729 							//Get the textnode under <capability/>
       
   730 							if ( capabilityNode)
       
   731 								{
       
   732 								for (CMDXMLNode
       
   733 										* capChildNode=capabilityNode->FirstChild (); capChildNode;capChildNode=capChildNode->NextSibling ())
       
   734 									{
       
   735 									//Get the textnode under <capability/>
       
   736 									if ( CMDXMLNode::ETextNode==capChildNode->NodeType ())
       
   737 										{
       
   738 										CMDXMLText
       
   739 												* capTextNode = dynamic_cast<CMDXMLText*>(capChildNode);
       
   740 										//Extract text out of this node
       
   741 										TCapability
       
   742 												cap = GetCapability (capTextNode->Data ());
       
   743 
       
   744 										
       
   745 										if ((ECapability_None!=cap))
       
   746 											{
       
   747 											
       
   748 											// Get LOGIC for the storage of Group capability
       
   749 											
       
   750 											if ( aUGCaps)
       
   751 												{
       
   752 												//For ALIAS GROUP this condition is not true and foll stm are not executed
       
   753 												if ( (!IsPresent(aCapInfo,cap)) && (!IsUserGrantPresent(aCapInfo,cap)))
       
   754 													{
       
   755 													capabilities.Append(cap);
       
   756 													
       
   757 													if ( RTUserPrompt_UnDefined==aUpOpt)								
       
   758 															{
       
   759 																CPermission* perm = CPermission::NewL(capabilities, aDefUpOpt,aDefUpOpt);
       
   760 																
       
   761 																aCapInfo.AppendPermission (*perm);
       
   762 															}
       
   763 																
       
   764 															else
       
   765 															{
       
   766 																CPermission* perm = CPermission::NewL(capabilities, aDefUpOpt,aUpOpt);
       
   767 																
       
   768 																aCapInfo.AppendPermission (*perm);
       
   769 															}
       
   770 													}
       
   771 													else
       
   772 													{
       
   773 													return EErrRepeatedCaps;	
       
   774 													}
       
   775 												}
       
   776 											else
       
   777 												{
       
   778 													//Check before appending unconditional capability that
       
   779 													//whether the capability is already added or not...
       
   780 													if ( (!IsPresent(aCapInfo,cap) && (!IsUserGrantPresent(aCapInfo,cap))) && (!IsCapsAliasPresent(aAliasInfo,cap)))
       
   781 													{
       
   782 														aCapInfo.AppendUncondCap (cap);	
       
   783 														aAliasInfo.AppendCapPermData(cap);
       
   784 													}
       
   785 													else
       
   786 													{
       
   787 														//capability string given more than once.. hence invalid
       
   788 														return EErrRepeatedCaps;		
       
   789 													}
       
   790 												}
       
   791 											}
       
   792 										else
       
   793 											{
       
   794 												TBool invalidCaps = ETrue;
       
   795 												for(TInt idx = 0; idx < iAliasGroup.Count(); idx++)
       
   796 												{
       
   797 													CPermission *tempObj = iAliasGroup[idx];
       
   798 													TPtrC name = tempObj->PermName();
       
   799 													
       
   800 													if ((KErrNone == capTextNode->Data().CompareF(name)))
       
   801 													{
       
   802 													invalidCaps = EFalse;
       
   803 													//it is not a text node. so is an alias group
       
   804 													
       
   805 													//alias group name is found. so obtain the caps
       
   806 													TUint32 capAlias = tempObj->PermissionData();
       
   807 													TUint32 capPresent = aCapInfo.UnconditionalCaps();
       
   808 													TUint32 capAfter = capAlias | capPresent;
       
   809 													TUint32 capRep = capAlias ^ capPresent;
       
   810 													TUint32 errChk = capRep & capAfter; //error if capAfter and error are different
       
   811 													
       
   812 													if(!(errChk & capAfter))
       
   813 														{
       
   814 														//capability string given more than once.. hence invalid
       
   815 														return EErrRepeatedCaps;
       
   816 														}
       
   817 														
       
   818 													aCapInfo.AppendUncondCapabilities(capAfter);
       
   819 													aAliasInfo.SetPermissionData(capAfter);
       
   820 													}
       
   821 												}
       
   822 
       
   823 												if (invalidCaps)
       
   824 												{
       
   825 												//just log the error message
       
   826 												RDebug::Print (_L ("CPolicyParser::GetCapabilities : Invalid capability string"));
       
   827 												return EErrInvalidCapability;
       
   828 												}
       
   829 											}
       
   830 
       
   831 										}										
       
   832 									}
       
   833 								}
       
   834 						
       
   835 							
       
   836 							capabilities.Close();						
       
   837 							}
       
   838 						}
       
   839 					else
       
   840 						{
       
   841 						RDebug::Print (_L ("CPolicyParser::GetCapabilities : No <capability> under <capabilities/>"));
       
   842 						}
       
   843 
       
   844 					}
       
   845 
       
   846 				childNode = childNode->NextSibling ();
       
   847 				}
       
   848 			else
       
   849 				{
       
   850 				RDebug::Print (_L ("CPolicyParser::GetCapabilities : Childnode NULL"));
       
   851 				}
       
   852 
       
   853 			}
       
   854 		while (NULL != childNode);
       
   855 		}
       
   856 		
       
   857 		return EErrNone;
       
   858 	}
       
   859 
       
   860 TInt CPolicyParser::GetConditions(CMDXMLElement* aParentNode, TUserPromptOption& aUserPromptOpt)
       
   861 	{
       
   862 	TUint32 ret(RTUserPrompt_UnDefined);
       
   863 	
       
   864 	if ( aParentNode->HasChildNodes ())
       
   865 		{
       
   866 		CMDXMLNode* childNode = aParentNode->FirstChild ();
       
   867 
       
   868 		do
       
   869 			{
       
   870 			if ( childNode)
       
   871 				{
       
   872 				CMDXMLNode* condNode=  NULL;
       
   873 				if ( 0==childNode->NodeName().CompareF (KCondition))
       
   874 					{
       
   875 					condNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   876 
       
   877 					if ( condNode->HasChildNodes ())
       
   878 						{
       
   879 						for (CMDXMLNode* condChildNode=condNode->FirstChild (); condChildNode;condChildNode=condChildNode->NextSibling ())
       
   880 							{
       
   881 							//Get the textnode under <condition/>
       
   882 							if ( CMDXMLNode::ETextNode==condChildNode->NodeType ())
       
   883 								{
       
   884 								CMDXMLText
       
   885 										* condition = dynamic_cast<CMDXMLText*>(condChildNode);
       
   886 
       
   887 								if ( condition)
       
   888 									{
       
   889 									TInt32 returnVal = GetUserPromptOption (condition->Data ());
       
   890 									
       
   891 										if(RTUserPrompt_UnDefined == returnVal)
       
   892 										{
       
   893 										  aUserPromptOpt = (TUserPromptOption)returnVal;
       
   894 										  return EErrInvalidPermission;						
       
   895 										}
       
   896 									
       
   897 									ret |= returnVal;
       
   898 									}
       
   899 								}
       
   900 							}
       
   901 						}
       
   902 					}
       
   903 				childNode = childNode->NextSibling ();
       
   904 				}
       
   905 
       
   906 			}
       
   907 		while (NULL != childNode);
       
   908 		}
       
   909 
       
   910 	aUserPromptOpt = (TUserPromptOption)ret;
       
   911 	return EErrNone;
       
   912 	}
       
   913 
       
   914 TBool CPolicyParser::isDomainPresent(const TDesC& aDomainName)
       
   915 	{
       
   916 	TBool isPresent(EFalse);
       
   917 
       
   918 	for (TInt i(0); i!=iPolicyInfo.Count ();++i)
       
   919 		{
       
   920 		if ( 0==iPolicyInfo[i]->DomainName().CompareF (aDomainName))
       
   921 			{
       
   922 			isPresent=ETrue;
       
   923 			break;
       
   924 			}
       
   925 		}
       
   926 
       
   927 	return isPresent;
       
   928 	}
       
   929 	
       
   930 TBool CPolicyParser::IsPresent(const CPermissionSet& aCapInfo, TCapability aCap)
       
   931 	{
       
   932 	TUint32 uncondCaps = aCapInfo.UnconditionalCaps ();
       
   933 
       
   934 	TUint32 tempCapToCheck(KDefaultEnableBit);
       
   935 	tempCapToCheck = tempCapToCheck << (aCap);
       
   936 
       
   937 	return (uncondCaps & tempCapToCheck);
       
   938 	}
       
   939 
       
   940 TBool CPolicyParser::IsCapsAliasPresent(const CPermission& aAliasInfo, TCapability aCap)
       
   941 	{
       
   942 	TUint32 uncondCaps = aAliasInfo.PermissionData ();
       
   943 
       
   944 	TUint32 tempCapToCheck(KDefaultEnableBit);
       
   945 	tempCapToCheck = tempCapToCheck << (aCap);
       
   946 
       
   947 	return (uncondCaps & tempCapToCheck);
       
   948 	}
       
   949 
       
   950 TBool CPolicyParser::IsUserGrantPresent(const CPermissionSet& aCapInfo,
       
   951 		TCapability aCap)
       
   952 	{
       
   953 	TUint32 perms(KDefaultNullBit);
       
   954 	//RPermissions tempPerms = aCapInfo.Permissions();
       
   955 	for (TInt i(0); i!=(aCapInfo.Permissions()).Count ();++i)
       
   956 		{
       
   957 		TUint32 temp(KDefaultEnableBit);
       
   958 		RCapabilityArray capabilities;
       
   959 		(aCapInfo.Permissions())[i]->Capabilitilites(capabilities);
       
   960 		for(TInt capIdx(0);capIdx!=capabilities.Count();++capIdx)
       
   961 			{
       
   962 			temp = temp << (capabilities[capIdx]);
       
   963 			perms |= temp;
       
   964 			}		
       
   965 		capabilities.Close();
       
   966 		}
       
   967 
       
   968 	TUint32 temp(KDefaultEnableBit);
       
   969 	temp = temp << (aCap);
       
   970 	return (perms & temp);
       
   971 	}
       
   972 
       
   973 TUserPromptOption CPolicyParser::GetDefaultCondition(CMDXMLElement* aParentNode)
       
   974 	{
       
   975 	if ( aParentNode->HasChildNodes ())
       
   976 		{
       
   977 		CMDXMLNode* childNode = aParentNode->FirstChild ();
       
   978 
       
   979 		do
       
   980 			{
       
   981 			if ( childNode)
       
   982 				{
       
   983 				CMDXMLNode* condNode=  NULL;
       
   984 				if ( 0==childNode->NodeName().CompareF (KDefCondition))
       
   985 					{
       
   986 					condNode = dynamic_cast<CMDXMLElement*>(childNode);
       
   987 
       
   988 					if ( condNode->HasChildNodes ())
       
   989 						{
       
   990 						for (CMDXMLNode* condChildNode=condNode->FirstChild (); condChildNode;condChildNode=condChildNode->NextSibling ())
       
   991 							{
       
   992 							if ( CMDXMLNode::ETextNode==condChildNode->NodeType ())
       
   993 								{
       
   994 								CMDXMLText
       
   995 										* condition = dynamic_cast<CMDXMLText*>(condChildNode);
       
   996 
       
   997 								if ( condition)
       
   998 									{
       
   999 									return GetUserPromptOption (condition->Data ());
       
  1000 									}
       
  1001 								}
       
  1002 							}
       
  1003 						}
       
  1004 					}
       
  1005 
       
  1006 				childNode = childNode->NextSibling ();
       
  1007 				}
       
  1008 
       
  1009 			}
       
  1010 		while (NULL != childNode);
       
  1011 		}
       
  1012 
       
  1013 	return RTUserPrompt_UnDefined;
       
  1014 	}
       
  1015 
       
  1016 TCapability CPolicyParser::GetCapability(const TDesC& aUserPromptOpt)
       
  1017 	{
       
  1018 	TCapability cap(ECapability_None);
       
  1019 
       
  1020 	if ( 0==aUserPromptOpt.CompareF (KCapabilityTCB))
       
  1021 		{
       
  1022 		cap=ECapabilityTCB;
       
  1023 		}
       
  1024 	else
       
  1025 		if ( 0==aUserPromptOpt.CompareF (KCapabilityCommDD))
       
  1026 			{
       
  1027 			cap=ECapabilityCommDD;
       
  1028 			}
       
  1029 		else
       
  1030 			if ( 0==aUserPromptOpt.CompareF (KCapabilityPowerMgmt))
       
  1031 				{
       
  1032 				cap=ECapabilityPowerMgmt;
       
  1033 				}
       
  1034 			else
       
  1035 				if ( 0==aUserPromptOpt.CompareF (KCapabilityMultimediaDD))
       
  1036 					{
       
  1037 					cap=ECapabilityMultimediaDD;
       
  1038 					}
       
  1039 				else
       
  1040 					if ( 0==aUserPromptOpt.CompareF (KCapabilityReadDeviceData))
       
  1041 						{
       
  1042 						cap=ECapabilityReadDeviceData;
       
  1043 						}
       
  1044 					else
       
  1045 						if ( 0==aUserPromptOpt.CompareF (KCapabilityWriteDeviceData))
       
  1046 							{
       
  1047 							cap=ECapabilityWriteDeviceData;
       
  1048 							}
       
  1049 						else
       
  1050 							if ( 0==aUserPromptOpt.CompareF (KCapabilityDRM))
       
  1051 								{
       
  1052 								cap=ECapabilityDRM;
       
  1053 								}
       
  1054 							else
       
  1055 								if ( 0==aUserPromptOpt.CompareF (KCapabilityTrustedUI))
       
  1056 									{
       
  1057 									cap=ECapabilityTrustedUI;
       
  1058 									}
       
  1059 								else
       
  1060 									if ( 0==aUserPromptOpt.CompareF (KCapabilityProtServ))
       
  1061 										{
       
  1062 										cap=ECapabilityProtServ;
       
  1063 										}
       
  1064 									else
       
  1065 										if ( 0==aUserPromptOpt.CompareF (KCapabilityDiskAdmin))
       
  1066 											{
       
  1067 											cap=ECapabilityDiskAdmin;
       
  1068 											}
       
  1069 										else
       
  1070 											if ( 0==aUserPromptOpt.CompareF (KCapabilityNetworkControl))
       
  1071 												{
       
  1072 												cap=ECapabilityNetworkControl;
       
  1073 												}
       
  1074 											else
       
  1075 												if ( 0==aUserPromptOpt.CompareF (KCapabilityAllFiles))
       
  1076 													{
       
  1077 													cap=ECapabilityAllFiles;
       
  1078 													}
       
  1079 												else
       
  1080 													if ( 0==aUserPromptOpt.CompareF (KCapabilitySwEvent))
       
  1081 														{
       
  1082 														cap=ECapabilitySwEvent;
       
  1083 														}
       
  1084 													else
       
  1085 														if ( 0==aUserPromptOpt.CompareF (KCapabilityNetworkServices))
       
  1086 															{
       
  1087 															cap=ECapabilityNetworkServices;
       
  1088 															}
       
  1089 														else
       
  1090 															if ( 0==aUserPromptOpt.CompareF (KCapabilityLocalServices))
       
  1091 																{
       
  1092 																cap=ECapabilityLocalServices;
       
  1093 																}
       
  1094 															else
       
  1095 																if ( 0==aUserPromptOpt.CompareF (KCapabilityReadUserData))
       
  1096 																	{
       
  1097 																	cap=ECapabilityReadUserData;
       
  1098 																	}
       
  1099 																else
       
  1100 																	if ( 0==aUserPromptOpt.CompareF (KCapabilityWriteUserData))
       
  1101 																		{
       
  1102 																		cap=ECapabilityWriteUserData;
       
  1103 																		}
       
  1104 																	else
       
  1105 																		if ( 0==aUserPromptOpt.CompareF (KCapabilityLocation))
       
  1106 																			{
       
  1107 																			cap=ECapabilityLocation;
       
  1108 																			}
       
  1109 																		else
       
  1110 																			if ( 0==aUserPromptOpt.CompareF (KCapabilitySurroundingsDD))
       
  1111 																				{
       
  1112 																				cap=ECapabilitySurroundingsDD;
       
  1113 																				}
       
  1114 																			else
       
  1115 																				if ( 0==aUserPromptOpt.CompareF (KCapabilityUserEnvironment))
       
  1116 																					{
       
  1117 																					cap=ECapabilityUserEnvironment;
       
  1118 																					}
       
  1119 	return cap;
       
  1120 	}
       
  1121 
       
  1122 TUserPromptOption CPolicyParser::GetUserPromptOption(const TDesC& aUserPromptOpt)
       
  1123 	{
       
  1124 	TUserPromptOption userPromptOpt = RTUserPrompt_UnDefined;
       
  1125 
       
  1126 	if ( 0==aUserPromptOpt.CompareF (KUserPromptOneShot))
       
  1127 		{
       
  1128 		userPromptOpt=RTUserPrompt_OneShot;
       
  1129 		}
       
  1130 	else
       
  1131 		if ( 0==aUserPromptOpt.CompareF (KUserPromptSession))
       
  1132 			{
       
  1133 			userPromptOpt=RTUserPrompt_Session;
       
  1134 			}
       
  1135 		else
       
  1136 			if ( 0==aUserPromptOpt.CompareF (KUserPromptBlanket))
       
  1137 				{
       
  1138 				userPromptOpt=RTUserPrompt_Permanent;
       
  1139 				}
       
  1140 	return userPromptOpt;
       
  1141 	}
       
  1142 
       
  1143 TBool CPolicyParser::isAliasPresent(const TDesC& aAliasName)
       
  1144 	{
       
  1145 		TBool isPresent(EFalse);
       
  1146 
       
  1147 		for(TInt idx = 0; idx < iAliasGroup.Count(); idx++)
       
  1148 		{
       
  1149 			if( KErrNone == iAliasGroup[idx]->PermName().CompareF(aAliasName))
       
  1150 			{
       
  1151 				isPresent = ETrue;
       
  1152 				break;
       
  1153 			}
       
  1154 			
       
  1155 		}
       
  1156 		
       
  1157 		return isPresent;
       
  1158 	}