/// <summary> /// Create a principal for a Unix user. /// </summary> /// <param name="userInfo">The user information.</param> /// <returns>The claims principal.</returns> public static ClaimsPrincipal CreateUnixPrincipal(UnixUserInfo userInfo) { var groups = UnixGroupInfo.GetLocalGroups(); var userGroups = groups .Where(x => x.GetMemberNames().Any(memberName => memberName == userInfo.UserName)) .ToList(); var claims = new List <Claim>() { new Claim(ClaimsIdentity.DefaultNameClaimType, userInfo.GroupName), new Claim(FtpClaimTypes.UserId, userInfo.UserId.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64), new Claim(ClaimTypes.AuthenticationMethod, "pam"), }; if (!string.IsNullOrWhiteSpace(userInfo.HomeDirectory)) { claims.Add(new Claim(FtpClaimTypes.HomePath, userInfo.HomeDirectory)); } foreach (var userGroup in userGroups) { claims.Add(new Claim(FtpClaimTypes.GroupId, userGroup.GroupId.ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64)); claims.Add(new Claim(ClaimsIdentity.DefaultRoleClaimType, userGroup.GroupName)); } return(new ClaimsPrincipal(new ClaimsIdentity(claims, "pam"))); }
[Category("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it public void ReentrantConstructors() { var seen = new Dictionary <string, object> (); foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups()) { if (seen.ContainsKey(group.GroupName)) { continue; } seen.Add(group.GroupName, null); try { UnixGroupInfo byName = new UnixGroupInfo(group.GroupName); UnixGroupInfo byId = new UnixGroupInfo(group.GroupId); Assert.AreEqual(group, byName, "#TRC: construct by name"); Assert.AreEqual(group, byId, "#TRC: construct by gid"); Assert.AreEqual(byName, byId, "#TRC: name == gid?"); } catch (Exception e) { Assert.Fail( string.Format("#TRC: Exception constructing UnixGroupInfo: {0}", e.ToString())); } } }
[Category("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it public void NonReentrantSyscalls() { var seen = new Dictionary <string, object> (); foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups()) { if (seen.ContainsKey(group.GroupName)) { continue; } seen.Add(group.GroupName, null); try { Group byName = Syscall.getgrnam(group.GroupName); Group byId = Syscall.getgrgid((uint)group.GroupId); Assert.IsNotNull(byName, "#TNRS: access by name"); Assert.IsNotNull(byId, "#TNRS: access by gid"); UnixGroupInfo n = new UnixGroupInfo(byName); UnixGroupInfo u = new UnixGroupInfo(byId); Assert.AreEqual(group, n, "#TNRS: construct by name"); Assert.AreEqual(group, u, "#TNRS: construct by gid"); Assert.AreEqual(n, u, "#TNRS: name == gid?"); } catch (Exception e) { Assert.Fail( string.Format("#TRC: Exception constructing UnixGroupInfo: {0}", e.ToString())); } } }
public void NonReentrantSyscalls() { foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups()) { try { Group byName = Syscall.getgrnam(group.GroupName); Group byId = Syscall.getgrgid((uint)group.GroupId); Assert.IsNotNull(byName, "#TNRS: access by name"); Assert.IsNotNull(byId, "#TNRS: access by gid"); UnixGroupInfo n = new UnixGroupInfo(byName); UnixGroupInfo u = new UnixGroupInfo(byId); Assert.AreEqual(group, n, "#TNRS: construct by name"); Assert.AreEqual(group, u, "#TNRS: construct by gid"); Assert.AreEqual(n, u, "#TNRS: name == gid?"); } catch (Exception e) { Assert.Fail( string.Format("#TRC: Exception constructing UnixGroupInfo: {0}", e.ToString())); } } }
static uint GetGid(string group) { var info = new UnixGroupInfo(group); var gid = info.GroupId; if (gid > UInt32.MaxValue || gid <= 0) { throw new ArgumentOutOfRangeException("group", String.Format("Gid for {0} ({1}) not in range for sgid", group, gid)); } return((uint)gid); }
public RootUserInfo() { var currentUser = UnixUserInfo.GetRealUser(); Info = currentUser; IsUserRoot = currentUser.UserId == 0; var rootGroup = new UnixGroupInfo(0); IsGroupRoot = rootGroup.GetMembers().Any(x => x.UserId == currentUser.UserId); var groups = UnixGroupInfo.GetLocalGroups(); var sudoGroup = groups.FirstOrDefault(x => x.GroupName == "sudo"); IsSudo = sudoGroup != null && sudoGroup.GetMembers().Any(x => x.UserId == currentUser.UserId); }
static void CreateWithPerm(string path, string permissions, string groupName = null) { Directory.CreateDirectory(path); uint perm = Convert.ToUInt32(permissions, 8); Syscall.chmod(path, NativeConvert.ToFilePermissions(perm)); if (groupName == null) { return; } var group = new UnixGroupInfo(groupName); Syscall.chown(path, 0, (uint)group.GroupId); }
[Category("AndroidNotWorking")] // API 21 conditionally has setgrent in the NDK headers, but bionic doesn't export it public void ListAllGroups_ToString() { try { Console.WriteLine("Listing all groups"); foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups()) { Console.WriteLine("\t{0}", group); } } catch (Exception e) { Assert.Fail( string.Format("#TLAU_TS: Exception listing local groups: {0}", e.ToString())); } }
/// <summary> /// Initializes a new instance of the <see cref="PamFtpUser"/> class. /// </summary> /// <param name="userInfo">The Unix PAM user information.</param> internal PamFtpUser([NotNull] UnixUserInfo userInfo) { var groups = UnixGroupInfo.GetLocalGroups(); var userGroups = groups .Where(x => x.GetMemberNames().Any(memberName => memberName == userInfo.UserName)) .ToList(); _userGroupNames = new HashSet <string>( userGroups.Select(x => x.GroupName), StringComparer.Ordinal); Name = userInfo.UserName; HomeDirectory = userInfo.HomeDirectory; UserId = userInfo.UserId; GroupId = userInfo.GroupId; }
public UnixUserIdentifier(int uid, int gid) { UserId = (uint)uid; GroupId = (uint)gid; try { var user_info = new UnixUserInfo(uid); Username = user_info.UserName; var group_info = new UnixGroupInfo(GroupId); Group = group_info.GroupName; } catch { } }
public UnixUserIdentifier(string username, string group) { Username = username; Group = group; try { var user_info = new UnixUserInfo(username); UserId = (uint)user_info.UserId; var group_info = new UnixGroupInfo(group); GroupId = (uint)group_info.GroupId; } catch { } }
static void Main(string[] args) { try { restrictedUser = new UnixUserInfo(username); restrictedGroup = new UnixGroupInfo(groupname); } catch (ArgumentException argException) { Console.WriteLine($"Cannot create security objects for the user/group."); Environment.Exit(1); } Recurse(targetPath); Environment.Exit(0); }
public void ReentrantConstructors() { foreach (UnixGroupInfo group in UnixGroupInfo.GetLocalGroups()) { try { UnixGroupInfo byName = new UnixGroupInfo(group.GroupName); UnixGroupInfo byId = new UnixGroupInfo(group.GroupId); Assert.AreEqual(group, byName, "#TRC: construct by name"); Assert.AreEqual(group, byId, "#TRC: construct by gid"); Assert.AreEqual(byName, byId, "#TRC: name == gid?"); } catch (Exception e) { Assert.Fail( string.Format("#TRC: Exception constructing UnixGroupInfo: {0}", e.ToString())); } } }