18 #include <sys/types.h> 27 #define CIB_FLAG_DIRTY 0x00001 28 #define CIB_FLAG_LIVE 0x00002 30 typedef struct cib_file_opaque_s {
37 xmlNode *
data, xmlNode ** output_data,
int call_options);
40 xmlNode *
data, xmlNode ** output_data,
int call_options,
41 const char *user_name);
48 cib_file_inputfd(
cib_t * cib)
50 return -EPROTONOSUPPORT;
54 cib_file_set_connection_dnotify(
cib_t * cib,
void (*dnotify) (gpointer user_data))
56 return -EPROTONOSUPPORT;
60 cib_file_register_notification(
cib_t * cib,
const char *callback,
int enabled)
62 return -EPROTONOSUPPORT;
75 cib_file_verify_digest(xmlNode *root,
const char *sigfile)
77 gboolean passed = FALSE;
80 if (expected == NULL) {
83 crm_err(
"On-disk digest at %s is empty", sigfile);
86 crm_warn(
"No on-disk digest present at %s", sigfile);
89 crm_perror(LOG_ERR,
"Could not read on-disk digest from %s", sigfile);
118 char *local_sigfile = NULL;
119 xmlNode *local_root = NULL;
127 s_res = stat(filename, &buf);
129 crm_perror(LOG_WARNING,
"Could not verify cluster configuration file %s", filename);
131 }
else if (buf.st_size == 0) {
132 crm_warn(
"Cluster configuration file %s is corrupt (size is zero)", filename);
138 if (local_root == NULL) {
139 crm_warn(
"Cluster configuration file %s is corrupt (unparseable as XML)", filename);
144 if (sigfile == NULL) {
145 sigfile = local_sigfile = crm_concat(filename,
"sig",
'.');
149 if (cib_file_verify_digest(local_root, sigfile) == FALSE) {
164 #define CIB_SERIES "cib" 165 #define CIB_SERIES_MAX 100 166 #define CIB_SERIES_BZIP FALSE 170 #define CIB_LIVE_NAME CIB_SERIES ".xml" 181 cib_file_is_live(
const char *filename)
183 gboolean same = FALSE;
185 if (filename != NULL) {
189 if (real_filename != NULL) {
193 if (real_livename && !strcmp(real_filename, real_livename)) {
207 static uid_t cib_file_owner = 0;
208 static uid_t cib_file_group = 0;
209 static gboolean cib_do_chown = FALSE;
221 cib_file_backup(
const char *cib_dirname,
const char *cib_filename)
224 char *cib_path = crm_concat(cib_dirname, cib_filename,
'/');
225 char *cib_digest = crm_concat(cib_path,
"sig",
'.');
231 char *backup_digest = crm_concat(backup_path,
"sig",
'.');
233 CRM_ASSERT((cib_path != NULL) && (cib_digest != NULL)
234 && (backup_path != NULL) && (backup_digest != NULL));
238 unlink(backup_digest);
241 if ((link(cib_path, backup_path) < 0) && (errno != ENOENT)) {
242 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
243 cib_path, backup_path);
247 }
else if ((link(cib_digest, backup_digest) < 0) && (errno != ENOENT)) {
248 crm_perror(LOG_ERR,
"Could not archive %s by linking to %s",
249 cib_digest, backup_digest);
256 if ((chown(backup_path, cib_file_owner, cib_file_group) < 0)
257 && (errno != ENOENT)) {
258 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_path);
261 if ((chown(backup_digest, cib_file_owner, cib_file_group) < 0)
262 && (errno != ENOENT)) {
263 crm_perror(LOG_ERR,
"Could not set owner of %s", backup_digest);
267 cib_file_group) < 0) {
269 "Could not set owner of %s last sequence file",
275 crm_info(
"Archived previous version as %s", backup_path);
297 cib_file_prepare_xml(xmlNode *root)
299 xmlNode *cib_status_root = NULL;
309 if (cib_status_root != NULL) {
329 const char *cib_filename)
341 char *cib_path = crm_concat(cib_dirname, cib_filename,
'/');
342 char *digest_path = crm_concat(cib_path,
"sig",
'.');
348 CRM_ASSERT((cib_path != NULL) && (digest_path != NULL)
349 && (tmp_cib != NULL) && (tmp_digest != NULL));
352 crm_trace(
"Reading cluster configuration file %s", cib_path);
354 if ((rc !=
pcmk_ok) && (rc != -ENOENT)) {
355 crm_err(
"%s was manually modified while the cluster was active!",
362 if (cib_file_backup(cib_dirname, cib_filename) < 0) {
368 umask(S_IWGRP | S_IWOTH | S_IROTH);
369 cib_file_prepare_xml(cib_root);
372 fd = mkstemp(tmp_cib);
374 crm_perror(LOG_ERR,
"Couldn't open temporary file %s for writing CIB",
381 if (fchmod(fd, S_IRUSR | S_IWUSR) < 0) {
382 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
387 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
388 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
396 crm_err(
"Changes couldn't be written to %s", tmp_cib);
404 crm_info(
"Wrote version %s.%s.0 of the CIB to disk (digest: %s)",
405 (admin_epoch ? admin_epoch :
"0"), (epoch ? epoch :
"0"), digest);
408 fd = mkstemp(tmp_digest);
410 crm_perror(LOG_ERR,
"Could not create temporary file for CIB digest");
414 if (cib_do_chown && (fchown(fd, cib_file_owner, cib_file_group) < 0)) {
415 crm_perror(LOG_ERR,
"Couldn't protect temporary file %s for writing CIB",
422 crm_perror(LOG_ERR,
"Could not write digest to file %s", tmp_digest);
428 crm_debug(
"Wrote digest %s to disk", digest);
431 crm_info(
"Reading cluster configuration file %s (digest: %s)",
432 tmp_cib, tmp_digest);
438 if (rename(tmp_cib, cib_path) < 0) {
439 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_cib, cib_path);
442 if (rename(tmp_digest, digest_path) < 0) {
443 crm_perror(LOG_ERR,
"Couldn't rename %s as %s", tmp_digest,
465 CRM_ASSERT((cib != NULL) && (
private != NULL));
470 if (cib_location == NULL) {
471 cib_location = getenv(
"CIB_file");
474 if (cib_file_is_live(cib_location)) {
476 crm_trace(
"File %s detected as live CIB", cib_location);
478 private->filename = strdup(cib_location);
493 static xmlNode *in_mem_cib = NULL;
510 load_file_cib(
const char *filename)
513 xmlNode *root = NULL;
516 if (stat(filename, &buf) < 0) {
535 crm_err(
"CIB does not validate against %s", schema);
551 if (private->filename == NULL) {
554 rc = load_file_cib(private->filename);
558 crm_debug(
"%s: Opened connection to local file '%s'", name, private->filename);
563 fprintf(stderr,
"%s: Connection to local file '%s' failed: %s\n",
579 cib_file_write_live(
char *path)
581 uid_t uid = geteuid();
582 struct passwd *daemon_pwent;
583 char *sep = strrchr(path,
'/');
584 const char *cib_dirname, *cib_filename;
590 if (daemon_pwent == NULL) {
599 if ((uid != 0) && (uid != daemon_pwent->pw_uid)) {
600 crm_perror(LOG_ERR,
"Must be root or %s to modify live CIB",
612 }
else if (sep == path) {
614 cib_filename = path + 1;
618 cib_filename = sep + 1;
623 cib_file_owner = daemon_pwent->pw_uid;
624 cib_file_group = daemon_pwent->pw_gid;
636 cib_do_chown = FALSE;
640 if ((sep != NULL) && (*sep ==
'\0')) {
666 crm_debug(
"Disconnecting from the CIB manager");
675 if (cib_file_write_live(private->filename) < 0) {
683 if (
write_xml_file(in_mem_cib, private->filename, do_bzip) <= 0) {
689 crm_info(
"Wrote CIB to %s", private->filename);
692 crm_err(
"Could not write CIB to %s", private->filename);
714 free(private->filename);
720 fprintf(stderr,
"Couldn't sign off: %d\n", rc);
726 struct cib_func_entry {
733 static struct cib_func_entry cib_file_ops[] = {
748 xmlNode *
data, xmlNode ** output_data,
int call_options)
756 xmlNode *
data, xmlNode ** output_data,
int call_options,
757 const char *user_name)
760 char *effective_user = NULL;
761 gboolean query = FALSE;
762 gboolean changed = FALSE;
763 xmlNode *request = NULL;
764 xmlNode *output = NULL;
765 xmlNode *cib_diff = NULL;
766 xmlNode *result_cib = NULL;
769 static int max_msg_types =
DIMOF(cib_file_ops);
779 if (output_data != NULL) {
787 for (lpc = 0; lpc < max_msg_types; lpc++) {
789 fn = &(cib_file_ops[lpc].fn);
790 query = cib_file_ops[lpc].read_only;
796 return -EPROTONOSUPPORT;
800 request =
cib_create_op(cib->
call_id,
"dummy-token", op, host, section, data, call_options, user_name);
805 crm_trace(
"Performing %s operation as %s", op, user_name);
814 section, request, data, TRUE, &changed, in_mem_cib, &result_cib, &cib_diff,
825 }
else if (query == FALSE) {
828 in_mem_cib = result_cib;
838 if (output_data && output) {
839 if(output == in_mem_cib) {
842 *output_data = output;
845 }
else if(output != in_mem_cib) {
849 free(effective_user);
xmlNode * find_xml_node(xmlNode *cib, const char *node_path, gboolean must_find)
#define pcmk_err_schema_validation
char * crm_compat_realpath(const char *path)
const char * pcmk_strerror(int rc)
#define XML_ATTR_NUMUPDATES
int get_last_sequence(const char *directory, const char *series)
int cib_process_delete(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_process_replace(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int(* signoff)(cib_t *cib)
xmlNode * get_object_root(const char *object_type, xmlNode *the_root)
int cib_process_erase(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_file_perform_op_delegate(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options, const char *user_name)
int cib_process_bump(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int write_xml_file(xmlNode *xml_node, const char *filename, gboolean compress)
Write XML to a file.
gboolean crm_digest_verify(xmlNode *input, const char *expected)
#define CRM_LOG_ASSERT(expr)
int(* inputfd)(cib_t *cib)
#define clear_bit(word, bit)
int cib_file_write_with_digest(xmlNode *cib_root, const char *cib_dirname, const char *cib_filename)
xmlNode * filename2xml(const char *filename)
struct cib_file_opaque_s cib_file_opaque_t
#define pcmk_err_cib_backup
char * generate_series_filename(const char *directory, const char *series, int sequence, gboolean bzip)
#define XML_ATTR_GENERATION
char * crm_read_contents(const char *filename)
gboolean validate_xml(xmlNode *xml_blob, const char *validation, gboolean to_logs)
gboolean validate_xml_verbose(xmlNode *xml_blob)
xmlNode * cib_create_op(int call_id, const char *token, const char *op, const char *host, const char *section, xmlNode *data, int call_options, const char *user_name)
int cib_file_perform_op(cib_t *cib, const char *op, const char *host, const char *section, xmlNode *data, xmlNode **output_data, int call_options)
xmlNode * copy_xml(xmlNode *src_node)
cib_t * cib_new_variant(void)
int(* set_connection_dnotify)(cib_t *cib, void(*dnotify)(gpointer user_data))
#define crm_warn(fmt, args...)
#define pcmk_err_cib_save
#define set_bit(word, bit)
cib_api_operations_t * cmds
#define crm_debug(fmt, args...)
int cib_process_diff(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
#define CIB_OP_APPLY_DIFF
int(* signon)(cib_t *cib, const char *name, enum cib_conn_type type)
int cib_file_signoff(cib_t *cib)
int(* cib_op_t)(const char *, int, const char *, xmlNode *, xmlNode *, xmlNode *, xmlNode **, xmlNode **)
#define crm_trace(fmt, args...)
void(* op_callback)(const xmlNode *msg, int call_id, int rc, xmlNode *output)
Wrappers for and extensions to libxml2.
xmlNode * create_xml_node(xmlNode *parent, const char *name)
#define XML_ATTR_VALIDATION
#define pcmk_err_cib_modified
void free_xml(xmlNode *child)
gboolean crm_str_eq(const char *a, const char *b, gboolean use_case)
gboolean crm_ends_with_ext(const char *s, const char *match)
const char * crm_element_value(const xmlNode *data, const char *name)
int(* register_notification)(cib_t *cib, const char *callback, int enabled)
char * calculate_on_disk_digest(xmlNode *local_cib)
Calculate and return digest of XML tree, suitable for storing on disk.
void write_last_sequence(const char *directory, const char *series, int sequence, int max)
const char * crm_xml_add(xmlNode *node, const char *name, const char *value)
int cib_process_query(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_file_read_and_verify(const char *filename, const char *sigfile, xmlNode **root)
cib_t * cib_file_new(const char *cib_location)
#define crm_perror(level, fmt, args...)
Log a system error message.
#define crm_err(fmt, args...)
int cib_perform_op(const char *op, int call_options, cib_op_t *fn, gboolean is_query, const char *section, xmlNode *req, xmlNode *input, gboolean manage_counters, gboolean *config_changed, xmlNode *current_cib, xmlNode **result_cib, xmlNode **diff, xmlNode **output)
int crm_write_sync(int fd, const char *contents)
#define XML_ATTR_GENERATION_ADMIN
int crm_chown_last_sequence(const char *directory, const char *series, uid_t uid, gid_t gid)
Wrappers for and extensions to libqb IPC.
int cib_file_free(cib_t *cib)
#define XML_CIB_TAG_STATUS
int cib_file_signon(cib_t *cib, const char *name, enum cib_conn_type type)
void crm_sync_directory(const char *name)
void xml_log_patchset(uint8_t level, const char *function, xmlNode *xml)
#define safe_str_eq(a, b)
int write_xml_fd(xmlNode *xml_node, const char *filename, int fd, gboolean compress)
Write XML to a file descriptor.
char * crm_strdup_printf(char const *format,...) __attribute__((__format__(__printf__
#define pcmk_err_cib_corrupt
int cib_process_upgrade(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
#define crm_info(fmt, args...)
const char * crm_xml_add_last_written(xmlNode *xml_node)
int cib_process_modify(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
int cib_process_create(const char *op, int options, const char *section, xmlNode *req, xmlNode *input, xmlNode *existing_cib, xmlNode **result_cib, xmlNode **answer)
enum crm_ais_msg_types type