2 // Mono.Unix/UnixUserInfo.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
7 // (C) 2004-2005 Jonathan Pryor
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
36 public sealed class UnixUserInfo
38 private Native.Passwd passwd;
40 public UnixUserInfo (string user)
42 passwd = new Native.Passwd ();
44 int r = Native.Syscall.getpwnam_r (user, passwd, out pw);
45 if (r != 0 || pw == null)
46 throw new ArgumentException (Locale.GetText ("invalid username"), "user");
49 [CLSCompliant (false)]
50 public UnixUserInfo (uint user)
52 passwd = new Native.Passwd ();
54 int r = Native.Syscall.getpwuid_r (user, passwd, out pw);
55 if (r != 0 || pw == null)
56 throw new ArgumentException (Locale.GetText ("invalid user id"), "user");
59 public UnixUserInfo (long user)
61 passwd = new Native.Passwd ();
63 int r = Native.Syscall.getpwuid_r (Convert.ToUInt32 (user), passwd, out pw);
64 if (r != 0 || pw == null)
65 throw new ArgumentException (Locale.GetText ("invalid user id"), "user");
68 public UnixUserInfo (Native.Passwd passwd)
70 this.passwd = CopyPasswd (passwd);
73 private static Native.Passwd CopyPasswd (Native.Passwd pw)
75 Native.Passwd p = new Native.Passwd ();
77 p.pw_name = pw.pw_name;
78 p.pw_passwd = pw.pw_passwd;
81 p.pw_gecos = pw.pw_gecos;
83 p.pw_shell = pw.pw_shell;
88 public string UserName {
89 get {return passwd.pw_name;}
92 public string Password {
93 get {return passwd.pw_passwd;}
97 get {return passwd.pw_uid;}
100 public UnixGroupInfo Group {
101 get {return new UnixGroupInfo (passwd.pw_gid);}
104 public long GroupId {
105 get {return passwd.pw_gid;}
108 public string GroupName {
109 get {return Group.GroupName;}
112 public string RealName {
113 get {return passwd.pw_gecos;}
116 public string HomeDirectory {
117 get {return passwd.pw_dir;}
120 public string ShellProgram {
121 get {return passwd.pw_shell;}
124 public override int GetHashCode ()
126 return passwd.GetHashCode ();
129 public override bool Equals (object obj)
131 if (obj == null || GetType () != obj.GetType())
133 return passwd.Equals (((UnixUserInfo) obj).passwd);
136 public override string ToString ()
138 return passwd.ToString ();
141 public static UnixUserInfo GetRealUser ()
143 return new UnixUserInfo (GetRealUserId ());
146 public static long GetRealUserId ()
148 return Native.Syscall.getuid ();
151 // I would hope that this is the same as GetCurrentUserName, but it is a
152 // different syscall, so who knows.
153 public static string GetLoginName ()
155 StringBuilder buf = new StringBuilder (4);
159 r = Native.Syscall.getlogin_r (buf, (ulong) buf.Capacity);
160 } while (r == (-1) && Native.Stdlib.GetLastError() == Native.Errno.ERANGE);
161 UnixMarshal.ThrowExceptionForLastErrorIf (r);
162 return buf.ToString ();
165 public Native.Passwd ToPasswd ()
167 return CopyPasswd (passwd);
170 public static UnixUserInfo[] GetLocalUsers ()
172 ArrayList entries = new ArrayList ();
173 lock (Native.Syscall.pwd_lock) {
174 if (Native.Syscall.setpwent () != 0) {
175 UnixMarshal.ThrowExceptionForLastError ();
179 while ((p = Native.Syscall.getpwent()) != null)
180 entries.Add (new UnixUserInfo (p));
181 if (Native.Syscall.GetLastError () != (Native.Errno) 0)
182 UnixMarshal.ThrowExceptionForLastError ();
185 Native.Syscall.endpwent ();
188 return (UnixUserInfo[]) entries.ToArray (typeof(UnixUserInfo));