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