1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gio.Credentials;
26 
27 private import glib.ConstructionException;
28 private import glib.ErrorG;
29 private import glib.GException;
30 private import glib.Str;
31 private import gobject.ObjectG;
32 private import gtkc.gio;
33 public  import gtkc.giotypes;
34 
35 
36 /**
37  * The #GCredentials type is a reference-counted wrapper for native
38  * credentials. This information is typically used for identifying,
39  * authenticating and authorizing other processes.
40  * 
41  * Some operating systems supports looking up the credentials of the
42  * remote peer of a communication endpoint - see e.g.
43  * g_socket_get_credentials().
44  * 
45  * Some operating systems supports securely sending and receiving
46  * credentials over a Unix Domain Socket, see
47  * #GUnixCredentialsMessage, g_unix_connection_send_credentials() and
48  * g_unix_connection_receive_credentials() for details.
49  * 
50  * On Linux, the native credential type is a struct ucred - see the
51  * unix(7) man page for details. This corresponds to
52  * %G_CREDENTIALS_TYPE_LINUX_UCRED.
53  * 
54  * On FreeBSD, Debian GNU/kFreeBSD, and GNU/Hurd, the native
55  * credential type is a struct cmsgcred. This corresponds
56  * to %G_CREDENTIALS_TYPE_FREEBSD_CMSGCRED.
57  * 
58  * On NetBSD, the native credential type is a struct unpcbid.
59  * This corresponds to %G_CREDENTIALS_TYPE_NETBSD_UNPCBID.
60  * 
61  * On OpenBSD, the native credential type is a struct sockpeercred.
62  * This corresponds to %G_CREDENTIALS_TYPE_OPENBSD_SOCKPEERCRED.
63  * 
64  * On Solaris (including OpenSolaris and its derivatives), the native
65  * credential type is a ucred_t. This corresponds to
66  * %G_CREDENTIALS_TYPE_SOLARIS_UCRED.
67  *
68  * Since: 2.26
69  */
70 public class Credentials : ObjectG
71 {
72 	/** the main Gtk struct */
73 	protected GCredentials* gCredentials;
74 
75 	/** Get the main Gtk struct */
76 	public GCredentials* getCredentialsStruct(bool transferOwnership = false)
77 	{
78 		if (transferOwnership)
79 			ownedRef = false;
80 		return gCredentials;
81 	}
82 
83 	/** the main Gtk struct as a void* */
84 	protected override void* getStruct()
85 	{
86 		return cast(void*)gCredentials;
87 	}
88 
89 	protected override void setStruct(GObject* obj)
90 	{
91 		gCredentials = cast(GCredentials*)obj;
92 		super.setStruct(obj);
93 	}
94 
95 	/**
96 	 * Sets our main struct and passes it to the parent class.
97 	 */
98 	public this (GCredentials* gCredentials, bool ownedRef = false)
99 	{
100 		this.gCredentials = gCredentials;
101 		super(cast(GObject*)gCredentials, ownedRef);
102 	}
103 
104 
105 	/** */
106 	public static GType getType()
107 	{
108 		return g_credentials_get_type();
109 	}
110 
111 	/**
112 	 * Creates a new #GCredentials object with credentials matching the
113 	 * the current process.
114 	 *
115 	 * Returns: A #GCredentials. Free with g_object_unref().
116 	 *
117 	 * Since: 2.26
118 	 *
119 	 * Throws: ConstructionException GTK+ fails to create the object.
120 	 */
121 	public this()
122 	{
123 		auto p = g_credentials_new();
124 		
125 		if(p is null)
126 		{
127 			throw new ConstructionException("null returned by new");
128 		}
129 		
130 		this(cast(GCredentials*) p, true);
131 	}
132 
133 	/**
134 	 * Gets a pointer to native credentials of type @native_type from
135 	 * @credentials.
136 	 *
137 	 * It is a programming error (which will cause an warning to be
138 	 * logged) to use this method if there is no #GCredentials support for
139 	 * the OS or if @native_type isn't supported by the OS.
140 	 *
141 	 * Params:
142 	 *     nativeType = The type of native credentials to get.
143 	 *
144 	 * Returns: The pointer to native credentials or %NULL if the
145 	 *     operation there is no #GCredentials support for the OS or if
146 	 *     @native_type isn't supported by the OS. Do not free the returned
147 	 *     data, it is owned by @credentials.
148 	 *
149 	 * Since: 2.26
150 	 */
151 	public void* getNative(GCredentialsType nativeType)
152 	{
153 		return g_credentials_get_native(gCredentials, nativeType);
154 	}
155 
156 	/**
157 	 * Tries to get the UNIX process identifier from @credentials. This
158 	 * method is only available on UNIX platforms.
159 	 *
160 	 * This operation can fail if #GCredentials is not supported on the
161 	 * OS or if the native credentials type does not contain information
162 	 * about the UNIX process ID.
163 	 *
164 	 * Returns: The UNIX process ID, or -1 if @error is set.
165 	 *
166 	 * Since: 2.36
167 	 *
168 	 * Throws: GException on failure.
169 	 */
170 	public pid_t getUnixPid()
171 	{
172 		GError* err = null;
173 		
174 		auto p = g_credentials_get_unix_pid(gCredentials, &err);
175 		
176 		if (err !is null)
177 		{
178 			throw new GException( new ErrorG(err) );
179 		}
180 		
181 		return p;
182 	}
183 
184 	/**
185 	 * Tries to get the UNIX user identifier from @credentials. This
186 	 * method is only available on UNIX platforms.
187 	 *
188 	 * This operation can fail if #GCredentials is not supported on the
189 	 * OS or if the native credentials type does not contain information
190 	 * about the UNIX user.
191 	 *
192 	 * Returns: The UNIX user identifier or -1 if @error is set.
193 	 *
194 	 * Since: 2.26
195 	 *
196 	 * Throws: GException on failure.
197 	 */
198 	public uid_t getUnixUser()
199 	{
200 		GError* err = null;
201 		
202 		auto p = g_credentials_get_unix_user(gCredentials, &err);
203 		
204 		if (err !is null)
205 		{
206 			throw new GException( new ErrorG(err) );
207 		}
208 		
209 		return p;
210 	}
211 
212 	/**
213 	 * Checks if @credentials and @other_credentials is the same user.
214 	 *
215 	 * This operation can fail if #GCredentials is not supported on the
216 	 * the OS.
217 	 *
218 	 * Params:
219 	 *     otherCredentials = A #GCredentials.
220 	 *
221 	 * Returns: %TRUE if @credentials and @other_credentials has the same
222 	 *     user, %FALSE otherwise or if @error is set.
223 	 *
224 	 * Since: 2.26
225 	 *
226 	 * Throws: GException on failure.
227 	 */
228 	public bool isSameUser(Credentials otherCredentials)
229 	{
230 		GError* err = null;
231 		
232 		auto p = g_credentials_is_same_user(gCredentials, (otherCredentials is null) ? null : otherCredentials.getCredentialsStruct(), &err) != 0;
233 		
234 		if (err !is null)
235 		{
236 			throw new GException( new ErrorG(err) );
237 		}
238 		
239 		return p;
240 	}
241 
242 	/**
243 	 * Copies the native credentials of type @native_type from @native
244 	 * into @credentials.
245 	 *
246 	 * It is a programming error (which will cause an warning to be
247 	 * logged) to use this method if there is no #GCredentials support for
248 	 * the OS or if @native_type isn't supported by the OS.
249 	 *
250 	 * Params:
251 	 *     nativeType = The type of native credentials to set.
252 	 *     native = A pointer to native credentials.
253 	 *
254 	 * Since: 2.26
255 	 */
256 	public void setNative(GCredentialsType nativeType, void* native)
257 	{
258 		g_credentials_set_native(gCredentials, nativeType, native);
259 	}
260 
261 	/**
262 	 * Tries to set the UNIX user identifier on @credentials. This method
263 	 * is only available on UNIX platforms.
264 	 *
265 	 * This operation can fail if #GCredentials is not supported on the
266 	 * OS or if the native credentials type does not contain information
267 	 * about the UNIX user. It can also fail if the OS does not allow the
268 	 * use of "spoofed" credentials.
269 	 *
270 	 * Params:
271 	 *     uid = The UNIX user identifier to set.
272 	 *
273 	 * Returns: %TRUE if @uid was set, %FALSE if error is set.
274 	 *
275 	 * Since: 2.26
276 	 *
277 	 * Throws: GException on failure.
278 	 */
279 	public bool setUnixUser(uid_t uid)
280 	{
281 		GError* err = null;
282 		
283 		auto p = g_credentials_set_unix_user(gCredentials, uid, &err) != 0;
284 		
285 		if (err !is null)
286 		{
287 			throw new GException( new ErrorG(err) );
288 		}
289 		
290 		return p;
291 	}
292 
293 	/**
294 	 * Creates a human-readable textual representation of @credentials
295 	 * that can be used in logging and debug messages. The format of the
296 	 * returned string may change in future GLib release.
297 	 *
298 	 * Returns: A string that should be freed with g_free().
299 	 *
300 	 * Since: 2.26
301 	 */
302 	public override string toString()
303 	{
304 		auto retStr = g_credentials_to_string(gCredentials);
305 		
306 		scope(exit) Str.freeString(retStr);
307 		return Str.toString(retStr);
308 	}
309 }