37 #define _DARWIN_C_SOURCE 1
41 #define __BSD_VISIBLE 1
48 #include "scheduler/task.h"
57 static const char *module_str =
"policy_resalt_task";
58 static const time_t TIME_INF = ((time_t)-1);
67 generate_salt(
char *buf,
int len)
69 #ifdef HAVE_ARC4RANDOM
70 arc4random_buf(buf, len);
75 for (i = 0; i < len; i++)
76 buf[i] = rand() & 0xFF;
87 to_hex(
const char *buf,
int len,
char *out)
89 const char *h =
"0123456789abcdef";
92 for (i = 0; i < len; i++) {
93 out[2*i] = h[(buf[i]>>4) & 0x0F];
94 out[2*i+1] = h[buf[i] & 0x0F];
104 perform_policy_resalt(task_type* task,
char const *policyname,
void *userdata,
109 time_t resalt_time, now = time_now();
110 char salt[255], salthex[511];
116 ods_log_error(
"[%s] could not fetch policy %s from database,"
117 " rescheduling", module_str, policyname);
120 return schedule_DEFER;
127 return schedule_SUCCESS;
132 if (now >= resalt_time) {
134 if (saltlength <= 0 || saltlength > 255) {
135 ods_log_error(
"[%s] policy %s has an invalid salt length. "
138 return schedule_SUCCESS;
141 #ifndef HAVE_ARC4RANDOM
146 generate_salt(salt, saltlength);
147 to_hex(salt, saltlength, salthex);
153 ods_log_error(
"[%s] db error", module_str);
155 return schedule_DEFER;
158 ods_log_debug(
"[%s] policy %s resalted successfully", module_str,
policy_name(
policy));
167 policy_resalt_task(
char const *owner,
engine_type *engine)
169 return task_create(strdup(owner), TASK_CLASS_ENFORCER, TASK_TYPE_RESALT,
170 perform_policy_resalt, engine, NULL, time_now());
183 int status = ODS_STATUS_OK;
187 ods_log_error(
"[%s] Unable to get list of policies from database",
190 return ODS_STATUS_ERR;
195 status |= schedule_task(engine->
taskq, task, 1, 0);