PrivilegedActionDeclaration class

Namespace Runic.Platform in Runic.Platform 0.23.0

public sealed class PrivilegedActionDeclaration

A privileged step the application declared at build time: a fixed program, run as the administrator with the platform's authorization, and the arguments it accepts.

Remarks

Declare actions with RunicPrivilegedAction items, which generate these declarations and the platform artifacts (a Polkit policy on Linux). The privileged program must check its own arguments again, with the provider's helper (on Linux PolkitHelper.Match, which also resets the process state the caller chose) and its own rules: any local user can start it through the platform's tool with any arguments.

Constructors

PrivilegedActionDeclaration

public PrivilegedActionDeclaration(string id, string program)

Declares an action.

Parameters

id

A lowercase reverse-DNS identifier such as org.example.app.activate; see PrivilegedActionDeclaration.IsValidId.

program

The absolute, normalized path of the program that runs as the administrator.

Fields

ArgumentLengthLimit

public const int ArgumentLengthLimit = 4096

The longest argument, in UTF-16 code units, that a declaration accepts.

ArgumentLimit

public const int ArgumentLimit = 64

The most arguments a declaration can accept after its verb.

PatternOptions

public const RegexOptions PatternOptions = 1536

The options PrivilegedActionDeclaration.ArgumentPattern is compiled with, at build time and at run time.

Properties

ArgumentPattern

public string? ArgumentPattern { get; init; }

A .NET regular expression that every argument after the verb must match completely; null accepts any argument within the length limit. It is evaluated with RegexOptions.NonBacktracking, so backreferences and lookarounds are not available.

Remarks

.NET character classes are Unicode-wide: \d and \w match digits and letters of every script. A negated class such as [^/] also matches a line break. An inline (?i) makes [a-z] match the Kelvin sign (U+212A) too, and (?x) turns # into a comment. Spell out the allowed characters, for example [A-Za-z0-9._+-], without inline options.

The pattern is compiled when PrivilegedActionDeclaration.AcceptsArguments first runs, not when it is set, which reports an invalid pattern. RunicPrivilegedAction items are checked the same way at build time (RUNICPA008), so a generated declaration's pattern is always valid.

Commands

public ImmutableArray<PrivilegedActionCommand> Commands { get; init; }

The commands the action runs: when there are any, the first argument after the verb names one, and the arguments after it must be within that command. Empty, the default, accepts arguments without commands.

Exceptions

ArgumentException

A command is null, or two commands have the same name.

Remarks

Commands let several operations of one program share one action, and so one authorization that auth_admin_keep retains, while each operation still declares the arguments it takes at each position. The arguments must also be within PrivilegedActionDeclaration.MaximumArguments and PrivilegedActionDeclaration.ArgumentPattern, which count and match the command name too. RunicPrivilegedCommand items generate the commands and the maximum.

Id

public string Id { get; }

The action's identifier, the same on every platform.

MaximumArguments

public int MaximumArguments { get; init; }

How many arguments the action accepts after its verb, from 0 to PrivilegedActionDeclaration.ArgumentLimit.

Program

public string Program { get; }

The absolute path of the program that runs as the administrator.

Verb

public string? Verb { get; init; }

The first argument, which selects this action when several actions run the same program; null for none. On Linux it is the policy's org.freedesktop.policykit.exec.argv1.

Methods

AcceptsArguments

public bool AcceptsArguments(IReadOnlyList<string> arguments)

Whether arguments, the arguments after the verb, are within this declaration: at most PrivilegedActionDeclaration.MaximumArguments, each without NUL characters, at most PrivilegedActionDeclaration.ArgumentLengthLimit long and matching PrivilegedActionDeclaration.ArgumentPattern; and, when the action has PrivilegedActionDeclaration.Commands, a command's name followed by arguments within that command.

Exceptions

ArgumentException

PrivilegedActionDeclaration.ArgumentPattern or a command's pattern is invalid on its own, for example with an unbalanced group, or once anchored.

NotSupportedException

PrivilegedActionDeclaration.ArgumentPattern or a command's pattern needs backtracking (backreferences or lookarounds).

CreateArgumentVector

public ImmutableArray<string> CreateArgumentVector(IReadOnlyList<string> arguments)

The complete argument vector after the program: the verb, when declared, then arguments.

Exceptions

ArgumentException

The arguments are not within this declaration, or PrivilegedActionDeclaration.ArgumentPattern is invalid.

NotSupportedException

PrivilegedActionDeclaration.ArgumentPattern needs backtracking.

IsValidId

public static bool IsValidId(string id)

Whether id is a valid action ID: lowercase ASCII letters, digits and hyphens in two or more dot-separated segments, each starting with a letter or digit, the first with a letter, at most 255 characters.

ToString

public override string ToString()

Inherits documentation from Object.ToString.