| 1 |
|
| 2 |
#include <genders.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <string.h> |
| 5 |
#include <stdio.h> |
| 6 |
|
| 7 |
#include <sys/types.h> |
| 8 |
#include <sys/stat.h> |
| 9 |
#include <unistd.h> |
| 10 |
#include <errno.h> |
| 11 |
|
| 12 |
static int genders_file_exists (const char *genders_file) |
| 13 |
{ |
| 14 |
struct stat st; |
| 15 |
|
| 16 |
if (genders_file == NULL) |
| 17 |
genders_file = GENDERS_DEFAULT_FILE; |
| 18 |
|
| 19 |
if ((stat (genders_file, &st) < 0) && |
| 20 |
((errno == ENOENT) || (errno == ENOTDIR))) |
| 21 |
return 0; |
| 22 |
|
| 23 |
return 1; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
/* |
| 28 |
* This is a helper symbol exported by cfengine to allow |
| 29 |
* modules to export new hard classes. |
| 30 |
*/ |
| 31 |
extern CfRegisterHardClass (const char *class); |
| 32 |
|
| 33 |
int cfagent_module_getclasses (void) |
| 34 |
{ |
| 35 |
genders_t gh; |
| 36 |
char **attrs, **vals; |
| 37 |
int i, nattrs; |
| 38 |
int maxattrs; |
| 39 |
int rc = -1; |
| 40 |
|
| 41 |
char *host = getenv ("GENDERS_HOSTNAME"); |
| 42 |
char *genders_file = getenv ("GENDERS_FILE"); |
| 43 |
|
| 44 |
/* |
| 45 |
* Return silently if genders file does not exist. This |
| 46 |
* shouldn't be an error, there is just nothing to do. |
| 47 |
*/ |
| 48 |
if (!genders_file_exists (genders_file)) |
| 49 |
return (0); |
| 50 |
|
| 51 |
if ((gh = genders_handle_create()) == NULL) |
| 52 |
return (-1); |
| 53 |
|
| 54 |
if (genders_load_data (gh, genders_file) < 0) { |
| 55 |
fprintf (stderr, "genders: Failed to read genders file: %s\n", |
| 56 |
genders_errormsg (gh)); |
| 57 |
return (-1); |
| 58 |
} |
| 59 |
|
| 60 |
if (genders_attrlist_create (gh, &attrs) < 0) { |
| 61 |
fprintf (stderr, "genders: Failed to malloc attrlist: %s\n", |
| 62 |
genders_errormsg (gh)); |
| 63 |
goto out1; |
| 64 |
} |
| 65 |
if (genders_vallist_create (gh, &vals) < 0) { |
| 66 |
fprintf (stderr, "genders: Failed to malloc vallist: %s\n", |
| 67 |
genders_errormsg (gh)); |
| 68 |
goto out2; |
| 69 |
} |
| 70 |
|
| 71 |
maxattrs = genders_getmaxattrs (gh); |
| 72 |
|
| 73 |
if ((nattrs = genders_getattr (gh, attrs, vals, maxattrs, host)) < 0) { |
| 74 |
fprintf (stderr, "genders: Failed to get all node attributes: %s\n", |
| 75 |
genders_errormsg (gh)); |
| 76 |
goto out3; |
| 77 |
} |
| 78 |
|
| 79 |
rc = 0; |
| 80 |
|
| 81 |
for (i = 0; i < nattrs; i++) { |
| 82 |
CfRegisterHardClass (attrs[i]); |
| 83 |
if (vals[i][0] != '\0') { |
| 84 |
char buf [8192]; |
| 85 |
snprintf (buf, sizeof (buf) - 1, "%s_is_%s", attrs[i], vals[i]); |
| 86 |
CfRegisterHardClass (buf); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
out3: |
| 91 |
genders_attrlist_destroy (gh, attrs); |
| 92 |
out2: |
| 93 |
genders_vallist_destroy (gh, vals); |
| 94 |
out1: |
| 95 |
genders_handle_destroy (gh); |
| 96 |
return (rc); |
| 97 |
} |