Beispiel #1
0
        public Task <int> Execute()
        {
            try
            {
                if (CredentialsCacheLifetimeManager.ClearCredentialsCache())
                {
                    if (Verbose)
                    {
                        Logger.Instance.Log("All credentials cache were invalidated.", LogLevel.Info);
                    }
                }
                else
                {
                    if (Verbose)
                    {
                        Logger.Instance.Log("No active credentials found to invalidate.", LogLevel.Info);
                    }
                }

                return(Task.FromResult(0));
            }
            catch (Exception ex)
            {
                Logger.Instance.Log($"Failed to invalidate Credentials Cache: {ex.ToString()}", LogLevel.Error);
                return(Task.FromResult(Constants.GSUDO_ERROR_EXITCODE));
            }
        }
Beispiel #2
0
        public async Task <int> Execute()
        {
            // service mode
            if (LogLvl.HasValue)
            {
                Settings.LogLevel.Value = LogLvl.Value;
            }

            Console.Title = "gsudo Service";
            var cacheLifetime = new CredentialsCacheLifetimeManager(AllowedPid);

            Logger.Instance.Log("Service started", LogLevel.Info);

            using (IRpcServer server = CreateServer())
            {
                try
                {
                    cacheLifetime.OnCacheClear += server.Close;
                    ShutdownTimer              = new Timer((o) => server.Close(), null, Timeout.Infinite, Timeout.Infinite); // 10 seconds for initial connection or die.
                    server.ConnectionAccepted += (o, connection) => AcceptConnection(connection).ConfigureAwait(false).GetAwaiter().GetResult();
                    server.ConnectionClosed   += (o, connection) => EnableTimer();

                    Logger.Instance.Log($"Service will shutdown if idle for {CacheDuration}", LogLevel.Debug);
                    EnableTimer();
                    await server.Listen().ConfigureAwait(false);
                }
                catch (System.OperationCanceledException) { }
                finally
                {
                    cacheLifetime.OnCacheClear -= server.Close;
                }
            }

            Logger.Instance.Log("Service stopped", LogLevel.Info);
            return(0);
        }
Beispiel #3
0
        public async Task <int> Execute()
        {
            if (!AllowedPid.HasValue)
            {
                AllowedPid = Process.GetCurrentProcess().GetParentProcessExcludingShim().Id;
            }

            if (!Action.HasValue || Action == CacheCommandAction.Help)
            {
                return(await CacheHelp().ConfigureAwait(false));
            }
            else if (Action == CacheCommandAction.Off)
            {
                if (InputArguments.KillCache)
                {
                    return(await new KillCacheCommand(true).Execute().ConfigureAwait(false));
                }
                if (CredentialsCacheLifetimeManager.ClearCredentialsCache(AllowedPid))
                {
                    Logger.Instance.Log("Cache session closed.", LogLevel.Info);
                }
                else
                {
                    Logger.Instance.Log(
                        "No cache session available for this process. (Use `-k' to close all sessions)`",
                        LogLevel.Info);
                }

                return(0);
            }
            else // CacheCommandAction.On
            {
                if (Settings.CacheMode.Value == CacheMode.Disabled ||
                    Math.Abs(Settings.CacheDuration.Value.TotalSeconds) < 1)
                {
                    Logger.Instance.Log("Unable to start a gsudo Cache session because CacheMode setting is 'Disabled' or CacheDuration is 0. Run `gsudo cache help` for more information.", LogLevel.Error);
                    return(1);
                }


                if (!ProcessHelper.IsAdministrator() && NamedPipeClient.IsServiceAvailable())
                {
                    var commandToRun = new List <string>();
                    commandToRun.Add(ProcessHelper.GetOwnExeName());
                    if (InputArguments.Debug)
                    {
                        commandToRun.Add("--debug");
                    }

                    commandToRun.AddRange(new[]
                                          { "cache", "on", "--pid", AllowedPid.ToString() });

                    return(await new RunCommand()
                    {
                        CommandToRun = commandToRun
                    }
                           .Execute().ConfigureAwait(false));
                }
                else
                {
                    if (!RunCommand.StartElevatedService(AllowedPid.Value, CacheDuration))
                    {
                        return(Constants.GSUDO_ERROR_EXITCODE);
                    }
                    if (AllowedPid.Value != 0)
                    {
                        Logger.Instance.Log($"Elevation allowed for process Id {AllowedPid.Value} and children.",
                                            LogLevel.Info);
                    }
                    else
                    {
                        Logger.Instance.Log($"Elevation allowed for any process from same-user.", LogLevel.Warning);
                    }

                    Logger.Instance.Log("Cache is a security risk. Use `gsudo cache off` (or `-k`) to go back to safety.",
                                        LogLevel.Warning);
                }

                return(0);
            }
        }