home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / ngdwtrst.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.1 KB  |  286 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "stdafx.h"
  20.  
  21. #include "ngdwtrst.h"
  22. #include "helper.h"
  23. #include "viewerse.h"
  24.  
  25. CSpawnList::CSpawnList()    {
  26. //    Purpose:    Create our spawn list, by reading in entries out of the INI file.
  27. //    Arguments:  void
  28. //    Returns:    none
  29. //    Comments:   Must be called after correct resolution of the INI file is completed.
  30. //    Revision History:
  31. //      04-10-95    created GAB
  32. //
  33.     CString csProfile = AfxGetApp()->m_pszProfileName;  //  Profile file.
  34.     CString csEntry, csRelationship;    //  Entry, actual value.
  35.  
  36.     DWORD dwRead;
  37.     DWORD dwBuffSize = 0;   //  Size of read buffer.
  38.     char *pBuffer = NULL;
  39.     do  {
  40.         dwBuffSize += 16;  //  Increment the buffer, if we already had one, get rid of it.
  41.         if(pBuffer != NULL) {
  42.             delete[] pBuffer;
  43.         }
  44.  
  45.         pBuffer = new char[dwBuffSize];    //  keep off stack.
  46.         if(pBuffer == NULL) {
  47.             //  Well, there's not enough memory to do anything.
  48.             //  We're just skipping out with no registrations whatsoever.
  49.             pBuffer = new char[2];
  50.             *pBuffer = '\0';
  51.             break;
  52.         }
  53.  
  54.         //  Read in an entire section, to get all the possible app names.
  55.         dwRead = theApp.GetPrivateProfileString(
  56.             SZ_IN_GOD_WE_TRUST, NULL, "", pBuffer, dwBuffSize, csProfile);
  57.  
  58.         //  Continue the loop if the buffer was too small.
  59.     }   while(dwRead == (dwBuffSize - 2));
  60.  
  61.     //  Loop through the entries.
  62.     //  End of list is double NULL.
  63.     char *pTraverse = pBuffer;
  64.     CTrust *pTrust;
  65.     while(*pTraverse != '\0') {
  66.         csEntry = pTraverse;
  67.  
  68.         //  Look up to see if this entry is friend or foe.
  69.         csRelationship = theApp.GetProfileString(SZ_IN_GOD_WE_TRUST, csEntry, SZ_NO);
  70.  
  71.         //  Add it to the list (Default is no).
  72.         if(csRelationship.CompareNoCase(SZ_YES) == 0) {
  73.             pTrust = new CTrust(csEntry, CTrust::m_Friend);
  74.         }
  75.         else    {
  76.             pTrust = new CTrust(csEntry, CTrust::m_Stranger);
  77.         }
  78.         m_cplOpinions.AddTail(pTrust);
  79.  
  80.         //  Increment beyond the length of the entry, +1 for the NULL.
  81.         pTraverse += csEntry.GetLength() + 1;
  82.     }
  83.  
  84.     //  Get rid of our buffer.
  85.     delete[] pBuffer;
  86. }
  87.  
  88. CSpawnList::~CSpawnList()    {
  89. //    Purpose:    Write our spawn list to the INI file.
  90. //    Arguments:  void
  91. //    Returns:    none
  92. //    Comments:   Must be called before the INI file name is lost.
  93. //    Revision History:
  94. //      04-10-95    created GAB
  95. //
  96.  
  97.     //  Go through our list, and write each value out to the INI file.
  98.     CTrust *pTrust;
  99.     while(m_cplOpinions.IsEmpty() == FALSE) {
  100.         pTrust = (CTrust *)m_cplOpinions.RemoveHead();
  101.  
  102.         if(pTrust->GetRelationship() == CTrust::m_Stranger)   {
  103.             theApp.WriteProfileString(SZ_IN_GOD_WE_TRUST, pTrust->GetExeName(), SZ_NO);
  104.         }
  105.         else    {
  106.             theApp.WriteProfileString(SZ_IN_GOD_WE_TRUST, pTrust->GetExeName(), SZ_YES);
  107.         }
  108.  
  109.         delete pTrust;
  110.     }
  111. }
  112.  
  113. BOOL CSpawnList::CanSpawn(CString& csExeName, CWnd *pWnd)   {
  114. //    Purpose:    Determines if an EXE can be spawned off or not.
  115. //    Arguments:  csExeName   The application in question.
  116. //              pWnd        The owner of the dialog box.
  117. //    Returns:    BOOL    TRUE    Can spawn.
  118. //                      FALSE   Can't spawn.
  119. //    Comments:   If the EXE isn't in the list, then we'll popup a generic dialog box asking
  120. //                  the user what to do.  If they ask for the entry to be persistant, then
  121. //                  we'll add it to the list.
  122. //              All spawned executables, wether external viewers, or implicit embeds, must be
  123. //                  validated by the user.
  124. //    Revision History:
  125. //      04-10-95    created GAB
  126. //
  127.  
  128.     //  Can't spawn anything without a name!
  129.     if(csExeName.IsEmpty()) {
  130.         return(FALSE);
  131.     }
  132.  
  133.     //  We immediately have to trust anything that is marked as good in the
  134.     //      registry.
  135.  
  136.     //  First, check to see if the executable is by default trusted by the user (set up
  137.     //      as an external viewer through preferences).
  138.     POSITION rIndex = CHelperApp::m_cplHelpers.GetHeadPosition();
  139.     CHelperApp *pApp;
  140.     while(rIndex != NULL)   {
  141.         pApp = (CHelperApp *)CHelperApp::m_cplHelpers.GetNext(rIndex);
  142.  
  143.         if(pApp->csCmd.CompareNoCase(csExeName) == 0)   {
  144.             //  We're going to be comparing a mime type below, make sure the app has one assigned.
  145.             if(pApp->cd_item != NULL && pApp->cd_item->ci.type != NULL) {
  146.                 //  Before we go off validating this one, make sure it's not a fake association from
  147.                 //      the registry or win.ini file.
  148.                 if(strncmp(pApp->cd_item->ci.type, SZ_WINASSOC, strlen(SZ_WINASSOC)) == 0)    {
  149.                     //  It's fake, we won't let this through.
  150.                     //  We continue, because there very well could be duplicate apps, some which
  151.                     //      may be valid.
  152.                     continue;
  153.                 }
  154.  
  155.                 //  Make sure that the app is set up to handle as an external viewer.
  156.                 //  If not, then we'll continue along our merry way, there may be a duplicate
  157.                 //      setting allowing the app to function as an external viewer.
  158.                 if(pApp->how_handle == HANDLE_EXTERNAL) {
  159.                     //  Trusted since the user has set this app up to be an external viewer.
  160.                     return(TRUE);
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.     //  Loop through the entries that we have.
  167.     //  Do a comparison without case.
  168.     //  If it exists, then we check wether or not we will trust it.
  169.     rIndex = m_cplOpinions.GetHeadPosition();
  170.     CTrust *pTrust;
  171.     while(rIndex != NULL)   {
  172.         pTrust = (CTrust *)m_cplOpinions.GetNext(rIndex);
  173.  
  174.         if(pTrust->GetExeName().CompareNoCase(csExeName) == 0)    {
  175.             if(pTrust->GetRelationship() == CTrust::m_Stranger) {
  176.                 return(FALSE);
  177.             }
  178.             else    {
  179.                 return(TRUE);
  180.             }
  181.         }
  182.     }
  183.  
  184.     //  Finally, it doesn't exist in our list.  We need to prompt the user, and possibly
  185.     //      add the entry to our list if they want the setting to be persistant.
  186.     CViewerSecurity dlgSecurity(pWnd);
  187.  
  188.     //  Initialize the dialog to some defaults.
  189.     char *pBuf = new char[512]; //  Keep it off the stack.
  190.     CString csBuf;
  191.  
  192.     csBuf.LoadString(IDS_VIEWER_SEC_MESSAGE);
  193.     sprintf(pBuf, csBuf, (const char *)csExeName);
  194.     dlgSecurity.m_csMessage = pBuf;
  195.  
  196.     csBuf.LoadString(IDS_VIEWER_SEC_DONTASK);
  197.     sprintf(pBuf, csBuf, (const char *)csExeName);
  198.     dlgSecurity.m_csDontAskText = pBuf;
  199.     delete[] pBuf;
  200.  
  201.     //  Have the user decide.
  202.     dlgSecurity.DoModal();
  203.     if(dlgSecurity.m_bCanceled == FALSE)   {
  204.         //  Check for persistance.
  205.         if(dlgSecurity.m_bAskNoMore == TRUE)    {
  206.             m_cplOpinions.AddTail(new CTrust(csExeName, CTrust::m_Friend));
  207.         }
  208.  
  209.         //  They want to go ahead and become the recepticle of a virus.
  210.         return(TRUE);
  211.     }
  212.     else {
  213.         //  Check for persistance.
  214.         if(dlgSecurity.m_bAskNoMore == TRUE)    {
  215.             m_cplOpinions.AddTail(new CTrust(csExeName, CTrust::m_Stranger));
  216.         }
  217.  
  218.         //  They want no part of corrupting their machine, and are good little children of the
  219.         //      internet.
  220.         return(FALSE);
  221.     }
  222. }
  223.  
  224. // Returns TRUE if we should ask the user before downloading a file of this type or
  225. // FALSE if the user has indicated we trust it
  226. BOOL CSpawnList::PromptBeforeOpening(LPCSTR lpszApp)
  227. {
  228.     BOOL        bPrompt = TRUE;
  229.     POSITION    rIndex;
  230.  
  231.     // Loop through the entries that we have. Do a comparison without case.
  232.     // If it exists, then we check wether or not we will trust it.
  233.     rIndex = m_cplOpinions.GetHeadPosition();
  234.  
  235.     while (rIndex != NULL)   {
  236.         CTrust *pTrust = (CTrust *)m_cplOpinions.GetNext(rIndex);
  237.  
  238.         if (pTrust->GetExeName().CompareNoCase(lpszApp) == 0) {
  239.             bPrompt = pTrust->GetRelationship() == CTrust::m_Stranger;
  240.             break;
  241.         }
  242.     }
  243.  
  244.     return bPrompt;
  245. }
  246.  
  247. void
  248. CSpawnList::SetPromptBeforeOpening(LPCSTR lpszApp, BOOL bPrompt)
  249. {
  250.     POSITION rIndex, rCurrent;
  251.  
  252.     // Loop through the entries that we have. Do a comparison without case.
  253.     rIndex = m_cplOpinions.GetHeadPosition();
  254.  
  255.     while (rIndex != NULL)   {
  256.         rCurrent = rIndex;
  257.  
  258.         CTrust *pTrust = (CTrust *)m_cplOpinions.GetNext(rIndex);
  259.  
  260.         if (pTrust->GetExeName().CompareNoCase(lpszApp) == 0) {
  261.             if (bPrompt) {
  262.                 // Remove this entry from the list
  263.                 theApp.WriteProfileString(SZ_IN_GOD_WE_TRUST, pTrust->GetExeName(), NULL);
  264.                 m_cplOpinions.RemoveAt(rCurrent);
  265.                 delete pTrust;
  266.  
  267.             } else {
  268.                 // User no longer wants to be prompted before opening downloaded
  269.                 // files of this type. Mark it as being trusted
  270.                 pTrust->SetRelationship(CTrust::m_Friend);
  271.             }
  272.  
  273.             return;
  274.         }
  275.     }
  276.  
  277.     // We didn't find an existing entry in the list
  278.     if (!bPrompt) {
  279.         CString    strApp(lpszApp);
  280.  
  281.         // User no longer wants to be prompted before opening downloaded files
  282.         // of this type
  283.         m_cplOpinions.AddTail(new CTrust(strApp, CTrust::m_Friend));
  284.     }
  285. }
  286.