1 /*****
2 *
3 * Copyright (C) 2004-2015 CS-SI. All Rights Reserved.
4 * Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
5 *
6 * This file is part of the Prelude library.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 *
22 *****/
23 
24 #include "config.h"
25 #include "libmissing.h"
26 
27 #include <stdio.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 
32 #include <gnutls/gnutls.h>
33 
34 #include "relocatable.h"
35 #include "glthread/lock.h"
36 
37 #define PRELUDE_ERROR_SOURCE_DEFAULT PRELUDE_ERROR_SOURCE_CLIENT_PROFILE
38 
39 #include "prelude-error.h"
40 #include "prelude-client-profile.h"
41 #include "tls-auth.h"
42 #include "common.h"
43 
44 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
45 # define geteuid(x) (0)
46 # define getegid(x) (0)
47 #endif
48 
49 
50 #define PRELUDE_PROFILE_DIR PRELUDE_CONFIG_DIR "/profile"
51 #define PRELUDE_CONFIG_DEFAULT_DIR PRELUDE_CONFIG_DIR "/default"
52 #define TLS_CONFIG PRELUDE_CONFIG_DEFAULT_DIR "/tls.conf"
53 
54 
55 /*
56  * directory where TLS private keys file are stored.
57  */
58 #define TLS_KEY_DIR PRELUDE_CONFIG_DIR "/keys"
59 
60 /*
61  * directory where TLS client certificate file are stored.
62  */
63 #define TLS_CLIENT_CERT_DIR PRELUDE_CONFIG_DIR "/tls/client"
64 
65 /*
66  * directory where TLS server certificate file are stored.
67  */
68 #define TLS_SERVER_CERT_DIR PRELUDE_CONFIG_DIR "/tls/server"
69 
70 
71 
72 struct prelude_client_profile {
73         int refcount;
74         prelude_uid_t uid;
75         prelude_gid_t gid;
76         char *name;
77         uint64_t analyzerid;
78         gnutls_certificate_credentials_t credentials;
79 };
80 
81 
82 extern char *_prelude_prefix;
83 static char *user_prefix = NULL;
84 static const char *relocated_prefix;
85 static const char *relative_spool_dir = NULL;
86 static const char *relative_config_default_dir = NULL;
87 static const char *relative_profile_dir = NULL;
88 
89 static gl_lock_t lock = gl_lock_initializer;
90 gl_once_define(static, relocate_once);
91 
92 
93 
get_relpath(const char * path)94 static const char *get_relpath(const char *path)
95 {
96         return ( strstr(path, INSTALLPREFIX) ) ? path + sizeof(INSTALLPREFIX) : NULL;
97 }
98 
99 
100 
_get_dir_once(void)101 static void _get_dir_once(void)
102 {
103         relocated_prefix = (_prelude_prefix) ? _prelude_prefix : relocate(INSTALLPREFIX);
104 
105         relative_spool_dir = get_relpath(PRELUDE_SPOOL_DIR);
106         relative_profile_dir = get_relpath(PRELUDE_PROFILE_DIR);
107         relative_config_default_dir = get_relpath(PRELUDE_CONFIG_DEFAULT_DIR);
108 
109         prelude_log_debug(2, "install   prefix=%s", INSTALLPREFIX);
110         prelude_log_debug(2, "relocated prefix=%s\n", relocated_prefix);
111         prelude_log_debug(2, "relative   spool=%s\n", relative_spool_dir ? relative_spool_dir : PRELUDE_SPOOL_DIR);
112         prelude_log_debug(2, "relative  config=%s\n", relative_config_default_dir ? relative_config_default_dir : PRELUDE_CONFIG_DEFAULT_DIR);
113         prelude_log_debug(2, "relative profile=%s\n", relative_profile_dir ? relative_profile_dir : PRELUDE_PROFILE_DIR);
114 }
115 
116 
init_once_and_get_prefix(void)117 static const char *init_once_and_get_prefix(void)
118 {
119         gl_once(relocate_once, _get_dir_once);
120         return (user_prefix) ? user_prefix : relocated_prefix;
121 }
122 
123 
get_profile_analyzerid(prelude_client_profile_t * cp)124 static int get_profile_analyzerid(prelude_client_profile_t *cp)
125 {
126         int ret;
127         FILE *fd;
128         char *ptr, filename[256], buf[256];
129 
130         prelude_client_profile_get_profile_dirname(cp, filename, sizeof(filename));
131         if ( access(filename, R_OK|X_OK) < 0 ) {
132                 if ( errno == ENOENT )
133                         return prelude_error_verbose(PRELUDE_ERROR_PROFILE, "profile '%s' does not exist", cp->name);
134 
135                 else if ( errno == EACCES )
136                         return prelude_error_verbose(PRELUDE_ERROR_PROFILE, "could not open profile '%s': insufficient permission", cp->name);
137         }
138 
139         prelude_client_profile_get_analyzerid_filename(cp, filename, sizeof(filename));
140 
141         fd = fopen(filename, "r");
142         if ( ! fd )
143                 return prelude_error_verbose(PRELUDE_ERROR_PROFILE, "could not open '%s' for reading", filename);
144 
145         ptr = fgets(buf, sizeof(buf), fd);
146         fclose(fd);
147 
148         if ( ! ptr )
149                 return prelude_error_verbose(PRELUDE_ERROR_PROFILE, "could not read analyzerID from '%s'", filename);
150 
151         ret = sscanf(buf, "%" PRELUDE_PRIu64, &cp->analyzerid);
152         if ( ret != 1 )
153                 return prelude_error_verbose(PRELUDE_ERROR_PROFILE, "'%s' is not a valid analyzerID", buf);
154 
155         return 0;
156 }
157 
158 
159 /**
160  * prelude_client_profile_set_prefix:
161  * @cp: pointer on a #prelude_client_profile_t object.
162  * @prefix: Prefix to use for various libprelude files.
163  *
164  * This function allow to dynamically change the prefix used to acess
165  * libprelude related file. This is particularly usefull in case of
166  * application running under certain condition (chroot).
167  *
168  * Returns: 0 on success, a negative value if an error occured.
169  */
prelude_client_profile_set_prefix(prelude_client_profile_t * cp,const char * prefix)170 int prelude_client_profile_set_prefix(prelude_client_profile_t *cp, const char *prefix)
171 {
172         char *n;
173 
174         n = strdup(prefix);
175 
176         gl_lock_lock(lock);
177 
178         if ( user_prefix )
179                 free(user_prefix);
180 
181         user_prefix = n;
182 
183         gl_lock_unlock(lock);
184 
185         return (n) ? 0 : prelude_error_from_errno(errno);
186 }
187 
188 
189 /**
190  * prelude_client_profile_get_prefix:
191  * @cp: pointer on a #prelude_client_profile_t object.
192  * @buf: buffer to write the returned filename to.
193  * @size: size of @buf.
194  *
195  * Retrieve current prefix used with this profile.
196  */
prelude_client_profile_get_prefix(const prelude_client_profile_t * cp,char * buf,size_t size)197 void prelude_client_profile_get_prefix(const prelude_client_profile_t *cp, char *buf, size_t size)
198 {
199         const char *prefix;
200 
201         prelude_return_if_fail(buf);
202 
203         gl_lock_lock(lock);
204 
205         prefix = init_once_and_get_prefix();
206         snprintf(buf, size, "%s", prefix);
207 
208         gl_lock_unlock(lock);
209 }
210 
211 
212 
213 /**
214  * prelude_client_profile_get_analyzerid_filename:
215  * @cp: pointer on a #prelude_client_profile_t object.
216  * @buf: buffer to write the returned filename to.
217  * @size: size of @buf.
218  *
219  * Writes the filename used to store @cp unique and permanent analyzer ident.
220  */
prelude_client_profile_get_default_config_dirname(const prelude_client_profile_t * cp,char * buf,size_t size)221 void prelude_client_profile_get_default_config_dirname(const prelude_client_profile_t *cp, char *buf, size_t size)
222 {
223         const char *prefix;
224 
225         prelude_return_if_fail(buf);
226 
227         gl_lock_lock(lock);
228 
229         prefix = init_once_and_get_prefix();
230         if ( ! relative_config_default_dir )
231                 snprintf(buf, size, "%s", PRELUDE_CONFIG_DEFAULT_DIR);
232         else
233                 snprintf(buf, size, "%s/%s", prefix, relative_config_default_dir);
234 
235         gl_lock_unlock(lock);
236 }
237 
238 
239 
240 /**
241  * prelude_client_profile_get_analyzerid_filename:
242  * @cp: pointer on a #prelude_client_profile_t object.
243  * @buf: buffer to write the returned filename to.
244  * @size: size of @buf.
245  *
246  * Writes the filename used to store @cp unique and permanent analyzer ident.
247  */
prelude_client_profile_get_analyzerid_filename(const prelude_client_profile_t * cp,char * buf,size_t size)248 void prelude_client_profile_get_analyzerid_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
249 {
250         const char *prefix;
251 
252         prelude_return_if_fail(cp);
253         prelude_return_if_fail(buf);
254 
255         gl_lock_lock(lock);
256 
257         prefix = init_once_and_get_prefix();
258         if ( ! relative_profile_dir )
259                 snprintf(buf, size, "%s/%s/analyzerid", PRELUDE_PROFILE_DIR, cp->name);
260         else
261                 snprintf(buf, size, "%s/%s/%s/analyzerid", prefix, relative_profile_dir, cp->name);
262 
263         gl_lock_unlock(lock);
264 }
265 
266 
267 
268 /**
269  * prelude_client_profile_get_config_filename:
270  * @cp: pointer on a #prelude_client_profile_t object.
271  * @buf: buffer to write the returned filename to.
272  * @size: size of @buf.
273  *
274  * Writes the filename used to store @cp configuration template.
275  */
prelude_client_profile_get_config_filename(const prelude_client_profile_t * cp,char * buf,size_t size)276 void prelude_client_profile_get_config_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
277 {
278         const char *prefix;
279 
280         prelude_return_if_fail(cp);
281         prelude_return_if_fail(buf);
282 
283         gl_lock_lock(lock);
284 
285         prefix = init_once_and_get_prefix();
286         if ( ! relative_profile_dir )
287                 snprintf(buf, size, "%s/%s/config", PRELUDE_PROFILE_DIR, cp->name);
288         else
289                 snprintf(buf, size, "%s/%s/%s/config", prefix, relative_profile_dir, cp->name);
290 
291         gl_lock_unlock(lock);
292 }
293 
294 
295 /**
296  * prelude_client_profile_get_tls_key_filename:
297  * @cp: pointer on a #prelude_client_profile_t object.
298  * @buf: buffer to write the returned filename to.
299  * @size: size of @buf.
300  *
301  * Writes the filename used to store @cp private key.
302  */
prelude_client_profile_get_tls_key_filename(const prelude_client_profile_t * cp,char * buf,size_t size)303 void prelude_client_profile_get_tls_key_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
304 {
305         const char *prefix;
306 
307         prelude_return_if_fail(cp);
308         prelude_return_if_fail(buf);
309 
310         gl_lock_lock(lock);
311 
312         prefix = init_once_and_get_prefix();
313         if ( ! relative_profile_dir )
314                 snprintf(buf, size, "%s/%s/key", PRELUDE_PROFILE_DIR, cp->name);
315         else
316                 snprintf(buf, size, "%s/%s/%s/key", prefix, relative_profile_dir, cp->name);
317 
318         gl_lock_unlock(lock);
319 }
320 
321 
322 
323 /**
324  * prelude_client_profile_get_tls_server_ca_cert_filename:
325  * @cp: pointer on a #prelude_client_profile_t object.
326  * @buf: buffer to write the returned filename to.
327  * @size: size of @buf.
328  *
329  * Writes the filename used to store @cp related CA certificate.
330  * This only apply to @cp receiving connection from analyzer (server).
331  */
prelude_client_profile_get_tls_server_ca_cert_filename(const prelude_client_profile_t * cp,char * buf,size_t size)332 void prelude_client_profile_get_tls_server_ca_cert_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
333 {
334         const char *prefix;
335 
336         prelude_return_if_fail(cp);
337         prelude_return_if_fail(buf);
338 
339         gl_lock_lock(lock);
340 
341         prefix = init_once_and_get_prefix();
342         if ( ! relative_profile_dir )
343                 snprintf(buf, size, "%s/%s/server.ca", PRELUDE_PROFILE_DIR, cp->name);
344         else
345                 snprintf(buf, size, "%s/%s/%s/server.ca", prefix, relative_profile_dir, cp->name);
346 
347         gl_lock_unlock(lock);
348 }
349 
350 
351 
352 /**
353  * prelude_client_profile_get_tls_server_keycert_filename:
354  * @cp: pointer on a #prelude_client_profile_t object.
355  * @buf: buffer to write the returned filename to.
356  * @size: size of @buf.
357  *
358  * Writes the filename used to store certificate for @cp server.
359  * This only apply to @cp receiving connection from analyzer (server).
360  */
prelude_client_profile_get_tls_server_keycert_filename(const prelude_client_profile_t * cp,char * buf,size_t size)361 void prelude_client_profile_get_tls_server_keycert_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
362 {
363         const char *prefix;
364 
365         prelude_return_if_fail(cp);
366         prelude_return_if_fail(buf);
367 
368         gl_lock_lock(lock);
369 
370         prefix = init_once_and_get_prefix();
371         if ( ! relative_profile_dir )
372                 snprintf(buf, size, "%s/%s/server.keycrt", PRELUDE_PROFILE_DIR, cp->name);
373         else
374                 snprintf(buf, size, "%s/%s/%s/server.keycrt", prefix, relative_profile_dir, cp->name);
375 
376         gl_lock_unlock(lock);
377 }
378 
379 
380 
381 /**
382  * prelude_client_profile_get_tls_server_crl_filename:
383  * @cp: pointer on a #prelude_client_profile_t object.
384  * @buf: buffer to write the returned filename to.
385  * @size: size of @buf.
386  *
387  * Writes the filename used to store CRL for @cp server.
388  * This only apply to @cp receiving connection from analyzer (server).
389  */
prelude_client_profile_get_tls_server_crl_filename(const prelude_client_profile_t * cp,char * buf,size_t size)390 void prelude_client_profile_get_tls_server_crl_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
391 {
392         const char *prefix;
393 
394         prelude_return_if_fail(cp);
395         prelude_return_if_fail(buf);
396 
397         gl_lock_lock(lock);
398 
399         prefix = init_once_and_get_prefix();
400         if ( ! relative_profile_dir )
401                 snprintf(buf, size, "%s/%s/server.crl", PRELUDE_PROFILE_DIR, cp->name);
402         else
403                 snprintf(buf, size, "%s/%s/%s/server.crl", prefix, relative_profile_dir, cp->name);
404 
405         gl_lock_unlock(lock);
406 }
407 
408 
409 
410 /**
411  * prelude_client_profile_get_tls_client_trusted_cert_filename:
412  * @cp: pointer on a #prelude_client_profile_t object.
413  * @buf: buffer to write the returned filename to.
414  * @size: size of @buf.
415  *
416  * Writes the filename used to store peers public certificates that @cp trust.
417  * This only apply to client connecting to a peer.
418  */
prelude_client_profile_get_tls_client_trusted_cert_filename(const prelude_client_profile_t * cp,char * buf,size_t size)419 void prelude_client_profile_get_tls_client_trusted_cert_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
420 {
421         const char *prefix;
422 
423         prelude_return_if_fail(cp);
424         prelude_return_if_fail(buf);
425 
426         gl_lock_lock(lock);
427 
428         prefix = init_once_and_get_prefix();
429         if ( ! relative_profile_dir )
430                 snprintf(buf, size, "%s/%s/client.trusted", PRELUDE_PROFILE_DIR, cp->name);
431         else
432                 snprintf(buf, size, "%s/%s/%s/client.trusted", prefix, relative_profile_dir, cp->name);
433 
434         gl_lock_unlock(lock);
435 }
436 
437 
438 
439 
440 /**
441  * prelude_client_profile_get_tls_client_keycert_filename:
442  * @cp: pointer on a #prelude_client_profile_t object.
443  * @buf: buffer to write the returned filename to.
444  * @size: size of @buf.
445  *
446  * Writes the filename used to store public certificate for @cp private key.
447  * This only apply to client connecting to a peer.
448  */
prelude_client_profile_get_tls_client_keycert_filename(const prelude_client_profile_t * cp,char * buf,size_t size)449 void prelude_client_profile_get_tls_client_keycert_filename(const prelude_client_profile_t *cp, char *buf, size_t size)
450 {
451         const char *prefix;
452 
453         prelude_return_if_fail(cp);
454         prelude_return_if_fail(buf);
455 
456         gl_lock_lock(lock);
457 
458         prefix = init_once_and_get_prefix();
459         if ( ! relative_profile_dir )
460                 snprintf(buf, size, "%s/%s/client.keycrt", PRELUDE_PROFILE_DIR, cp->name);
461         else
462                 snprintf(buf, size, "%s/%s/%s/client.keycrt", prefix, relative_profile_dir, cp->name);
463 
464         gl_lock_unlock(lock);
465 }
466 
467 
468 
469 /**
470  * prelude_client_profile_get_backup_dirname:
471  * @cp: pointer on a #prelude_client_profile_t object.
472  * @buf: buffer to write the returned filename to.
473  * @size: size of @buf.
474  *
475  * Writes the directory name where message sent by @cp will be stored,
476  * in case writing the message to the peer fail.
477  */
prelude_client_profile_get_backup_dirname(const prelude_client_profile_t * cp,char * buf,size_t size)478 void prelude_client_profile_get_backup_dirname(const prelude_client_profile_t *cp, char *buf, size_t size)
479 {
480         const char *prefix;
481 
482         prelude_return_if_fail(cp);
483         prelude_return_if_fail(buf);
484 
485         gl_lock_lock(lock);
486 
487         prefix = init_once_and_get_prefix();
488         if ( ! relative_spool_dir )
489                 snprintf(buf, size, "%s/%s", PRELUDE_SPOOL_DIR, cp->name);
490         else
491                 snprintf(buf, size, "%s/%s/%s", prefix, relative_spool_dir, cp->name);
492 
493         gl_lock_unlock(lock);
494 }
495 
496 
497 /**
498  * prelude_client_profile_get_backup_dirname:
499  * @cp: pointer on a #prelude_client_profile_t object.
500  * @buf: buffer to write the returned filename to.
501  * @size: size of @buf.
502  *
503  * Writes the directory name where the profile for @cp is stored. If
504  * @cp is NULL or has no name, then this function will provide the main
505  * profile directory.
506  */
prelude_client_profile_get_profile_dirname(const prelude_client_profile_t * cp,char * buf,size_t size)507 void prelude_client_profile_get_profile_dirname(const prelude_client_profile_t *cp, char *buf, size_t size)
508 {
509         const char *prefix, *name_sep = "", *name = "";
510 
511         prelude_return_if_fail(buf);
512 
513         if ( cp && cp->name ) {
514                 name_sep = "/";
515                 name = cp->name;
516         }
517 
518         gl_lock_lock(lock);
519 
520         prefix = init_once_and_get_prefix();
521         if ( ! relative_profile_dir )
522                 snprintf(buf, size, "%s/%s%s", PRELUDE_PROFILE_DIR, name_sep, name);
523         else
524                 snprintf(buf, size, "%s/%s%s%s", prefix, relative_profile_dir, name_sep, name);
525 
526         gl_lock_unlock(lock);
527 }
528 
529 
_prelude_client_profile_new(prelude_client_profile_t ** ret)530 int _prelude_client_profile_new(prelude_client_profile_t **ret)
531 {
532         *ret = calloc(1, sizeof(**ret));
533         if ( ! *ret )
534                 return prelude_error_from_errno(errno);
535 
536         (*ret)->refcount = 1;
537         (*ret)->uid = geteuid();
538         (*ret)->gid = getegid();
539 
540         return 0;
541 }
542 
543 
544 
_prelude_client_profile_init(prelude_client_profile_t * cp)545 int _prelude_client_profile_init(prelude_client_profile_t *cp)
546 {
547         int ret;
548 
549         prelude_return_val_if_fail(cp, prelude_error(PRELUDE_ERROR_ASSERTION));
550 
551         ret = get_profile_analyzerid(cp);
552         if ( ret < 0 )
553                 return ret;
554 
555         return 0;
556 }
557 
558 
559 
560 /**
561  * prelude_client_profile_new:
562  * @ret: Pointer where to store the address of the created object.
563  * @name: Name for this profile.
564  *
565  * Creates a new #prelude_client_profile_t object and store its
566  * address into @ret.
567  *
568  * Returns: 0 on success or a negative value if an error occured.
569  */
prelude_client_profile_new(prelude_client_profile_t ** ret,const char * name)570 int prelude_client_profile_new(prelude_client_profile_t **ret, const char *name)
571 {
572         int retval;
573         prelude_client_profile_t *cp;
574 
575         prelude_return_val_if_fail(name, prelude_error(PRELUDE_ERROR_ASSERTION));
576 
577         retval = _prelude_client_profile_new(&cp);
578         if ( retval < 0 )
579                 return retval;
580 
581         cp->name = strdup(name);
582         if ( ! cp->name ) {
583                 free(cp);
584                 return prelude_error_from_errno(errno);
585         }
586 
587         retval = _prelude_client_profile_init(cp);
588         if ( retval < 0 )
589                 return retval;
590 
591         *ret = cp;
592 
593         return 0;
594 }
595 
596 
597 
598 /**
599  * prelude_client_profile_destroy:
600  * @cp: Pointer to a #prelude_client_profile_t.
601  *
602  * Destroys @cp.
603  */
prelude_client_profile_destroy(prelude_client_profile_t * cp)604 void prelude_client_profile_destroy(prelude_client_profile_t *cp)
605 {
606         prelude_return_if_fail(cp);
607 
608         if ( --cp->refcount )
609                 return;
610 
611         if ( cp->credentials )
612                 gnutls_certificate_free_credentials(cp->credentials);
613 
614         if ( cp->name )
615                 free(cp->name);
616 
617         free(cp);
618 }
619 
620 
621 
622 /**
623  * prelude_client_profile_get_uid:
624  * @cp: Pointer to a #prelude_client_profile_t object.
625  *
626  * Gets the UID associated with @cp.
627  *
628  * Returns: the UID associated used by @cp.
629  */
prelude_client_profile_get_uid(const prelude_client_profile_t * cp)630 prelude_uid_t prelude_client_profile_get_uid(const prelude_client_profile_t *cp)
631 {
632         prelude_return_val_if_fail(cp, 0);
633         return cp->uid;
634 }
635 
636 
637 
638 /**
639  * prelude_client_profile_set_uid:
640  * @cp: Pointer to a #prelude_client_profile_t object.
641  * @uid: UID to be used by @cp.
642  *
643  * Sets the UID used by @cp to @uid.
644  */
prelude_client_profile_set_uid(prelude_client_profile_t * cp,prelude_uid_t uid)645 void prelude_client_profile_set_uid(prelude_client_profile_t *cp, prelude_uid_t uid)
646 {
647         prelude_return_if_fail(cp);
648         cp->uid = uid;
649 }
650 
651 
652 
653 /**
654  * prelude_client_profile_get_gid:
655  * @cp: Pointer to a #prelude_client_profile_t object.
656  *
657  * Gets the GID associated with @cp.
658  *
659  * Returns: the GID associated used by @cp.
660  */
prelude_client_profile_get_gid(const prelude_client_profile_t * cp)661 prelude_gid_t prelude_client_profile_get_gid(const prelude_client_profile_t *cp)
662 {
663         prelude_return_val_if_fail(cp, 0);
664         return cp->gid;
665 }
666 
667 
668 
669 /**
670  * prelude_client_profile_set_gid:
671  * @cp: Pointer to a #prelude_client_profile_t object.
672  * @gid: GID to be used by @cp.
673  *
674  * Sets the GID used by @cp to @gid.
675  */
prelude_client_profile_set_gid(prelude_client_profile_t * cp,prelude_gid_t gid)676 void prelude_client_profile_set_gid(prelude_client_profile_t *cp, prelude_gid_t gid)
677 {
678         prelude_return_if_fail(cp);
679         cp->gid = gid;
680 }
681 
682 
683 
684 /**
685  * prelude_client_profile_set_analyzerid:
686  * @cp: Pointer to a #prelude_client_profile_t object.
687  * @analyzerid: Analyzer ID to be used by @cp.
688  *
689  * Sets the Analyzer ID used by @cp to @analyzerid.
690  */
prelude_client_profile_set_analyzerid(prelude_client_profile_t * cp,uint64_t analyzerid)691 void prelude_client_profile_set_analyzerid(prelude_client_profile_t *cp, uint64_t analyzerid)
692 {
693         prelude_return_if_fail(cp);
694         cp->analyzerid = analyzerid;
695 }
696 
697 
698 
699 /**
700  * prelude_client_profile_get_analyzerid:
701  * @cp: Pointer to a #prelude_client_profile_t object.
702  *
703  * Gets the unique and permanent analyzer ident associated with @cp.
704  *
705  * Returns: the analyzer ident used by @cp.
706  */
prelude_client_profile_get_analyzerid(const prelude_client_profile_t * cp)707 uint64_t prelude_client_profile_get_analyzerid(const prelude_client_profile_t *cp)
708 {
709         prelude_return_val_if_fail(cp, 0);
710         return cp->analyzerid;
711 }
712 
713 
714 
715 /**
716  * prelude_client_profile_get_name:
717  * @cp: Pointer to a #prelude_client_profile_t object.
718  *
719  * Gets the name of @cp client profile.
720  *
721  * Returns: the name used by @cp.
722  */
prelude_client_profile_get_name(const prelude_client_profile_t * cp)723 const char *prelude_client_profile_get_name(const prelude_client_profile_t *cp)
724 {
725         prelude_return_val_if_fail(cp, NULL);
726         return cp->name;
727 }
728 
729 
730 /**
731  * prelude_client_profile_set_name:
732  * @cp: Pointer to a #prelude_client_profile_t object.
733  * @name: Name to associate the profile with.
734  *
735  * Sets the prelude client profile name.
736  *
737  * Returns: 0 on success or a negative value if an error occured.
738  */
prelude_client_profile_set_name(prelude_client_profile_t * cp,const char * name)739 int prelude_client_profile_set_name(prelude_client_profile_t *cp, const char *name)
740 {
741         prelude_return_val_if_fail(cp, prelude_error(PRELUDE_ERROR_ASSERTION));
742         prelude_return_val_if_fail(name, prelude_error(PRELUDE_ERROR_ASSERTION));
743 
744         if ( cp->name )
745                 free(cp->name);
746 
747         cp->name = strdup(name);
748         if ( ! cp->name )
749                 return prelude_error_from_errno(errno);
750 
751         return 0;
752 }
753 
754 
755 
756 /**
757  * prelude_client_profile_get_crendentials:
758  * @cp: Pointer to a #prelude_client_profile_t object.
759  * @credentials: The GNU TLS certificate credentials retrieved.
760  *
761  * Gets the prelude client profile credentials
762  *
763  * Returns: 0 on success or a negative value if an error occured.
764  */
prelude_client_profile_get_credentials(prelude_client_profile_t * cp,void ** credentials)765 int prelude_client_profile_get_credentials(prelude_client_profile_t *cp, void **credentials)
766 {
767         int ret;
768 
769         prelude_return_val_if_fail(cp, prelude_error(PRELUDE_ERROR_ASSERTION));
770 
771         if ( cp->credentials ) {
772                 *credentials = cp->credentials;
773                 return 0;
774         }
775 
776         ret = tls_auth_init(cp, &cp->credentials);
777         if ( ret < 0 )
778                 return ret;
779 
780         *credentials = cp->credentials;
781 
782         return 0;
783 }
784 
785 
prelude_client_profile_ref(prelude_client_profile_t * cp)786 prelude_client_profile_t *prelude_client_profile_ref(prelude_client_profile_t *cp)
787 {
788         prelude_return_val_if_fail(cp, NULL);
789 
790         cp->refcount++;
791         return cp;
792 }
793