예제 #1
0
        /// <summary>
        /// Connects the unc path.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="credentials">The credentials.</param>
        /// <returns>System.Int32.</returns>
        private int ConnectUncPath(string path, NetworkCredential credentials)
        {
            CDFMonitor.LogOutputHandler("DEBUG:ConnectUncPath:enter:" + path);
            string userName = credentials.UserName;

            if (!string.IsNullOrEmpty(credentials.Domain))
            {
                userName = string.Format("{0}\\{1}", credentials.Domain, credentials.UserName);
            }

            if (GetPathType(path) != ResourceType.Unc)
            {
                return(-1);
            }

            DeterminePathObjType objectType = DeterminePathObj(path);

            if (objectType == DeterminePathObjType.File)
            {
                path = Path.GetDirectoryName(FileManager.GetFullPath(path));
                CDFMonitor.LogOutputHandler("DEBUG:ConnectUncPath:new path:" + path);
            }

            int ret = WindowsNetworking.ConnectToRemote(path,
                                                        userName,
                                                        credentials.Password, false);

            CDFMonitor.LogOutputHandler(string.Format("DEBUG:ConnectToRemote:return:{0}:{1}", path, ret == 0 ? true : false));
            return(ret);
        }
예제 #2
0
 /// <summary>
 /// Disconnects the unc path.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
 public bool DisconnectUncPath(string path)
 {
     try
     {
         return(WindowsNetworking.DisconnectRemote(path) == 0);
     }
     catch (Exception e)
     {
         CDFMonitor.LogOutputHandler("DEBUG:DisconnectUncPath:Exception:" + e.ToString());
         return(false);
     }
 }
예제 #3
0
        /// <summary>
        /// Checks unc resource for access. Looks in cache, tries windows cache, and then prompts.
        /// Returns CommandResults.Successful on success.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="force">if set to <c>true</c> [force].</param>
        /// <param name="credentials">The credentials.</param>
        /// <returns>CommandResults.</returns>
        private CommandResults CheckResourceCredentialsUnc(string path, bool force, NetworkCredential credentials = null)
        {
            try
            {
                string uriHost = new Uri(path).Host;
                int    ret     = 0;

                CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:treating as UNC");

                if (CredentialsList.ContainsKey(uriHost) && !force)
                {
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:returning cached authentication status");

                    // Already tried authenticating previously so just return false
                    return(CredentialsList[uriHost].Status);
                }
                else if (CredentialsList.ContainsKey(uriHost))
                {
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:trying locally cached credentials");

                    // Authenticate with creds
                    if ((TestUncPath(path, CredentialsList[uriHost])))
                    {
                        return(CommandResults.Successful);
                    }
                    else
                    {
                        CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:removing locally cached credentials. force = true");
                        CredentialsList.Remove(uriHost);
                    }
                }
                else
                {
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:trying passed in credentials");
                    if (TestUncPath(path, credentials))
                    {
                        return(AddToCredentialList(credentials, uriHost));
                    }

                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:trying default credentials");

                    // no credentials so try as default
                    if (Directory.Exists(path))
                    {
                        return(AddToCredentialList(CredentialCache.DefaultNetworkCredentials, uriHost, true,
                                                   CommandResults.Successful));
                    }

                    // try with utility credentials if available
                    if (CredentialsList.ContainsKey(Resources.SessionName))
                    {
                        CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:trying utility credentials");
                        if (TestUncPath(path, CredentialsList[Resources.SessionName]))
                        {
                            return(AddToCredentialList(CredentialsList[Resources.SessionName], uriHost));
                        }
                    }
                }

                if (CanPromptForCredentials())
                {
                    CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:trying with prompted credentials");

                    // this queries windows credential manager for cached credential
                    credentials = Credentials.PromptForCredentials(credentials.UserName, credentials.Password, uriHost);

                    if (TestUncPath(path, credentials))
                    {
                        return(AddToCredentialList(credentials, uriHost));
                    }
                    else
                    {
                        return(CommandResults.Fail);
                    }
                }

                // bad set as bad for future check
                CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:CheckResourceCredentials return:" +
                                            WindowsNetworking.GetErrorForNumber(ret));
                if (ret != 53) // path not found
                {
                    AddToCredentialList(credentials, uriHost, false, CommandResults.Fail);
                    return(CommandResults.Error);
                }
                else
                {
                    return(CommandResults.InvalidPath);
                }
            }
            catch (Exception e)
            {
                CDFMonitor.LogOutputHandler("DEBUG:CheckResourceCredentialsUnc:Exception:" + e.ToString());

                return(AddToCredentialList(credentials, path, false, CommandResults.Fail));
            }
        }