static int cleanup_hash_table_mapper (void *key, void *value, void *arg_ignored) { xfree (key); free_specs (value); return 0; }
void res_cleanup (void) { if (registered_specs) { hash_table_iterator iter; for (hash_table_iterate (registered_specs, &iter); hash_table_iter_next (&iter); ) { xfree (iter.key); free_specs (iter.value); } hash_table_destroy (registered_specs); registered_specs = NULL; } }
void res_register_specs (const char *host, int port, struct robot_specs *specs) { struct robot_specs *old; char *hp, *hp_old; SET_HOSTPORT (host, port, hp); if (!registered_specs) registered_specs = make_nocase_string_hash_table (0); if (hash_table_get_pair (registered_specs, hp, &hp_old, &old)) { if (old) free_specs (old); hash_table_put (registered_specs, hp_old, specs); } else { hash_table_put (registered_specs, xstrdup (hp), specs); } }
int main(int argc, char *argv[]) { struct saved_data data; const char *path; char stack_path[PATH_MAX + 1]; int rc; if (argc != 2) { fprintf(stderr, "usage: %s input_file\n", argv[0]); exit(EXIT_FAILURE); } memset(&data, 0, sizeof(data)); path = argv[1]; rc = process_file(&data, path); if (rc < 0) return rc; rc = sort_specs(&data); if (rc) return rc; rc = snprintf(stack_path, sizeof(stack_path), "%s.bin", path); if (rc < 0 || rc >= sizeof(stack_path)) return rc; rc = write_binary_file(&data, stack_path); if (rc < 0) return rc; rc = free_specs(&data); if (rc < 0) return rc; return 0; }
int main(int argc, char *argv[]) { const char *path = NULL; const char *out_file = NULL; char stack_path[PATH_MAX + 1]; char *tmp = NULL; int fd, rc, opt; FILE *policy_fp = NULL; struct stat buf; struct selabel_handle *rec = NULL; struct saved_data *data = NULL; if (argc < 2) usage(argv[0]); while ((opt = getopt(argc, argv, "o:p:")) > 0) { switch (opt) { case 'o': out_file = optarg; break; case 'p': policy_file = optarg; break; default: usage(argv[0]); } } if (optind >= argc) usage(argv[0]); path = argv[optind]; if (stat(path, &buf) < 0) { fprintf(stderr, "Can not stat: %s: %m\n", path); exit(EXIT_FAILURE); } /* Open binary policy if supplied. */ if (policy_file) { policy_fp = fopen(policy_file, "r"); if (!policy_fp) { fprintf(stderr, "Failed to open policy: %s\n", policy_file); exit(EXIT_FAILURE); } if (sepol_set_policydb_from_file(policy_fp) < 0) { fprintf(stderr, "Failed to load policy: %s\n", policy_file); fclose(policy_fp); exit(EXIT_FAILURE); } } /* Generate dummy handle for process_line() function */ rec = (struct selabel_handle *)calloc(1, sizeof(*rec)); if (!rec) { fprintf(stderr, "Failed to calloc handle\n"); if (policy_fp) fclose(policy_fp); exit(EXIT_FAILURE); } rec->backend = SELABEL_CTX_FILE; /* Need to set validation on to get the bin file generated by the * process_line function, however as the bin file being generated * may not be related to the currently loaded policy (that it * would be validated against), then set callback to ignore any * validation - unless the -p option is used in which case if an * error is detected, the process will be aborted. */ rec->validating = 1; selinux_set_callback(SELINUX_CB_VALIDATE, (union selinux_callback)&validate_context); data = (struct saved_data *)calloc(1, sizeof(*data)); if (!data) { fprintf(stderr, "Failed to calloc saved_data\n"); free(rec); if (policy_fp) fclose(policy_fp); exit(EXIT_FAILURE); } rec->data = data; rc = process_file(rec, path); if (rc < 0) goto err; rc = sort_specs(data); if (rc) goto err; if (out_file) rc = snprintf(stack_path, sizeof(stack_path), "%s", out_file); else rc = snprintf(stack_path, sizeof(stack_path), "%s.bin", path); if (rc < 0 || rc >= (int)sizeof(stack_path)) goto err; tmp = malloc(strlen(stack_path) + 7); if (!tmp) goto err; rc = sprintf(tmp, "%sXXXXXX", stack_path); if (rc < 0) goto err; fd = mkstemp(tmp); if (fd < 0) goto err; rc = fchmod(fd, buf.st_mode); if (rc < 0) { perror("fchmod failed to set permission on compiled regexs"); goto err_unlink; } rc = write_binary_file(data, fd); if (rc < 0) goto err_unlink; rc = rename(tmp, stack_path); if (rc < 0) goto err_unlink; rc = 0; out: if (policy_fp) fclose(policy_fp); free_specs(data); free(rec); free(data); free(tmp); return rc; err_unlink: unlink(tmp); err: rc = -1; goto out; }
int main(int argc, char *argv[]) { struct saved_data data; const char *path; char stack_path[PATH_MAX + 1]; int rc; char *tmp= NULL; int fd; struct stat buf; if (argc != 2) { fprintf(stderr, "usage: %s input_file\n", argv[0]); exit(EXIT_FAILURE); } memset(&data, 0, sizeof(data)); path = argv[1]; if (stat(path, &buf) < 0) { fprintf(stderr, "Can not stat: %s: %m\n", path); exit(EXIT_FAILURE); } rc = process_file(&data, path); if (rc < 0) return rc; rc = sort_specs(&data); if (rc) return rc; rc = snprintf(stack_path, sizeof(stack_path), "%s.bin", path); if (rc < 0 || rc >= (int)sizeof(stack_path)) return rc; if (asprintf(&tmp, "%sXXXXXX", stack_path) < 0) return -1; fd = mkstemp(tmp); if (fd < 0) goto err; rc = fchmod(fd, buf.st_mode); if (rc < 0) { perror("fchmod failed to set permission on compiled regexs"); goto err; } rc = write_binary_file(&data, fd); if (rc < 0) goto err; rename(tmp, stack_path); rc = free_specs(&data); if (rc < 0) goto err; rc = 0; out: free(tmp); return rc; err: rc = -1; goto out; }