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 	/**
91 	 * Sets our main struct and passes it to the parent class.
92 	 */
93 	public this (GCredentials* gCredentials, bool ownedRef = false)
94 	{
95 		this.gCredentials = gCredentials;
96 		super(cast(GObject*)gCredentials, ownedRef);
97 	}
98 
99 
100 	/** */
101 	public static GType getType()
102 	{
103 		return g_credentials_get_type();
104 	}
105 
106 	/**
107 	 * Creates a new #GCredentials object with credentials matching the
108 	 * the current process.
109 	 *
110 	 * Returns: A #GCredentials. Free with g_object_unref().
111 	 *
112 	 * Since: 2.26
113 	 *
114 	 * Throws: ConstructionException GTK+ fails to create the object.
115 	 */
116 	public this()
117 	{
118 		auto p = g_credentials_new();
119 
120 		if(p is null)
121 		{
122 			throw new ConstructionException("null returned by new");
123 		}
124 
125 		this(cast(GCredentials*) p, true);
126 	}
127 
128 	/**
129 	 * Gets a pointer to native credentials of type @native_type from
130 	 * @credentials.
131 	 *
132 	 * It is a programming error (which will cause an warning to be
133 	 * logged) to use this method if there is no #GCredentials support for
134 	 * the OS or if @native_type isn't supported by the OS.
135 	 *
136 	 * Params:
137 	 *     nativeType = The type of native credentials to get.
138 	 *
139 	 * Returns: The pointer to native credentials or %NULL if the
140 	 *     operation there is no #GCredentials support for the OS or if
141 	 *     @native_type isn't supported by the OS. Do not free the returned
142 	 *     data, it is owned by @credentials.
143 	 *
144 	 * Since: 2.26
145 	 */
146 	public void* getNative(GCredentialsType nativeType)
147 	{
148 		return g_credentials_get_native(gCredentials, nativeType);
149 	}
150 
151 	/**
152 	 * Tries to get the UNIX process identifier from @credentials. This
153 	 * method is only available on UNIX platforms.
154 	 *
155 	 * This operation can fail if #GCredentials is not supported on the
156 	 * OS or if the native credentials type does not contain information
157 	 * about the UNIX process ID.
158 	 *
159 	 * Returns: The UNIX process ID, or -1 if @error is set.
160 	 *
161 	 * Since: 2.36
162 	 *
163 	 * Throws: GException on failure.
164 	 */
165 	public pid_t getUnixPid()
166 	{
167 		GError* err = null;
168 
169 		auto p = g_credentials_get_unix_pid(gCredentials, &err);
170 
171 		if (err !is null)
172 		{
173 			throw new GException( new ErrorG(err) );
174 		}
175 
176 		return p;
177 	}
178 
179 	/**
180 	 * Tries to get the UNIX user identifier from @credentials. This
181 	 * method is only available on UNIX platforms.
182 	 *
183 	 * This operation can fail if #GCredentials is not supported on the
184 	 * OS or if the native credentials type does not contain information
185 	 * about the UNIX user.
186 	 *
187 	 * Returns: The UNIX user identifier or -1 if @error is set.
188 	 *
189 	 * Since: 2.26
190 	 *
191 	 * Throws: GException on failure.
192 	 */
193 	public uid_t getUnixUser()
194 	{
195 		GError* err = null;
196 
197 		auto p = g_credentials_get_unix_user(gCredentials, &err);
198 
199 		if (err !is null)
200 		{
201 			throw new GException( new ErrorG(err) );
202 		}
203 
204 		return p;
205 	}
206 
207 	/**
208 	 * Checks if @credentials and @other_credentials is the same user.
209 	 *
210 	 * This operation can fail if #GCredentials is not supported on the
211 	 * the OS.
212 	 *
213 	 * Params:
214 	 *     otherCredentials = A #GCredentials.
215 	 *
216 	 * Returns: %TRUE if @credentials and @other_credentials has the same
217 	 *     user, %FALSE otherwise or if @error is set.
218 	 *
219 	 * Since: 2.26
220 	 *
221 	 * Throws: GException on failure.
222 	 */
223 	public bool isSameUser(Credentials otherCredentials)
224 	{
225 		GError* err = null;
226 
227 		auto p = g_credentials_is_same_user(gCredentials, (otherCredentials is null) ? null : otherCredentials.getCredentialsStruct(), &err) != 0;
228 
229 		if (err !is null)
230 		{
231 			throw new GException( new ErrorG(err) );
232 		}
233 
234 		return p;
235 	}
236 
237 	/**
238 	 * Copies the native credentials of type @native_type from @native
239 	 * into @credentials.
240 	 *
241 	 * It is a programming error (which will cause an warning to be
242 	 * logged) to use this method if there is no #GCredentials support for
243 	 * the OS or if @native_type isn't supported by the OS.
244 	 *
245 	 * Params:
246 	 *     nativeType = The type of native credentials to set.
247 	 *     native = A pointer to native credentials.
248 	 *
249 	 * Since: 2.26
250 	 */
251 	public void setNative(GCredentialsType nativeType, void* native)
252 	{
253 		g_credentials_set_native(gCredentials, nativeType, native);
254 	}
255 
256 	/**
257 	 * Tries to set the UNIX user identifier on @credentials. This method
258 	 * is only available on UNIX platforms.
259 	 *
260 	 * This operation can fail if #GCredentials is not supported on the
261 	 * OS or if the native credentials type does not contain information
262 	 * about the UNIX user. It can also fail if the OS does not allow the
263 	 * use of "spoofed" credentials.
264 	 *
265 	 * Params:
266 	 *     uid = The UNIX user identifier to set.
267 	 *
268 	 * Returns: %TRUE if @uid was set, %FALSE if error is set.
269 	 *
270 	 * Since: 2.26
271 	 *
272 	 * Throws: GException on failure.
273 	 */
274 	public bool setUnixUser(uid_t uid)
275 	{
276 		GError* err = null;
277 
278 		auto p = g_credentials_set_unix_user(gCredentials, uid, &err) != 0;
279 
280 		if (err !is null)
281 		{
282 			throw new GException( new ErrorG(err) );
283 		}
284 
285 		return p;
286 	}
287 
288 	/**
289 	 * Creates a human-readable textual representation of @credentials
290 	 * that can be used in logging and debug messages. The format of the
291 	 * returned string may change in future GLib release.
292 	 *
293 	 * Returns: A string that should be freed with g_free().
294 	 *
295 	 * Since: 2.26
296 	 */
297 	public override string toString()
298 	{
299 		auto retStr = g_credentials_to_string(gCredentials);
300 
301 		scope(exit) Str.freeString(retStr);
302 		return Str.toString(retStr);
303 	}
304 }