// On disposal, cease to be root. public void Dispose() { setreuid(this.previousUid, this.previousUid); setregid(this.previousGid, this.previousGid); this.previousUid = 0; this.previousGid = 0; }
// Revert from root. private static void Unrootify() { // if (previousUid == 0) // throw new InvalidOperationException("Cannot unrootify unroot."); setreuid(previousUid, previousUid); setregid(previousGid, previousGid); previousUid = 0; previousGid = 0; }
// Become root. internal RootPrivilege() { this.previousUid = getuid(); this.previousGid = getgid(); setreuid(0, 0); setregid(0, 0); this.IsRoot = true; }
// Revert from root. private static void Unrootify() { if (previousUid == 0) { throw new InvalidOperationException("Cannot unrootify unroot."); } setuid(previousUid); previousUid = 0; }
// Become root. private static void Rootify() { if (previousUid != 0) { throw new InvalidOperationException("Cannot rootify root."); } previousUid = getuid(); setuid(0); }
internal static passwd getpwuid(uid_t uid) { var pwd = _getpwuid(uid); if (pwd == IntPtr.Zero) { throw new NullReferenceException($"getpwuid returned null for uuid_t ({uid})"); } return((passwd)Marshal.PtrToStructure(pwd, typeof(passwd))); }
// Become root. internal RootPrivilege() { if (getuid() == 0) { throw new InvalidOperationException("Cannot rootify root."); } this.previousUid = getuid(); this.previousGid = getgid(); setreuid(0, 0); setregid(0, 0); }
// Become root. private static void Rootify() { if (previousUid != 0) { throw new InvalidOperationException("Cannot rootify root."); } previousUid = getuid(); previousGid = getgid(); setreuid(0, 0); setregid(0, 0); // Console.WriteLine ($"uid={getuid()} gid={getgid()} euid={geteuid()} egid={getegid()}"); }
private static extern IntPtr _getpwuid(uid_t uid);
public static extern int fchownat(int dirfd, byte *path, uid_t owner, gid_t group, int flags);
public static extern int lchown(byte *path, uid_t owner, gid_t group);
public static extern int fchown(int fd, uid_t owner, gid_t group);
public static extern int setresuid(uid_t ruid, uid_t euid, uid_t suid);
public static extern int seteuid(uid_t uid);