static void parse_options(int argc, char** const argv) { long c; add_defaults(); while ((c = getopt_long(argc, argv, "hvU:", options, NULL)) != EOF) { switch (c) { case 'h': fputs(USAGE, stdout); exit(EXIT_SUCCESS); case 'v': printf("%s\n", "irw " VERSION); exit(EXIT_SUCCESS); case 'U': options_set_opt("lircd:plugindir", optarg); break; case '?': fprintf(stderr, "unrecognized option: -%c\n", optopt); fputs("Try `irsimsend -h' for more information.\n", stderr); exit(EXIT_FAILURE); } } if (argc != optind + 2) { fputs(USAGE, stderr); exit(EXIT_FAILURE); } }
static rlm_rcode_t reset_db(rlm_counter_t *inst) { int cache_size = inst->cache_size; rlm_rcode_t rcode; DEBUG2("rlm_counter: reset_db: Closing database"); gdbm_close(inst->gdbm); /* * Open a completely new database. */ { char *filename; memcpy(&filename, &inst->filename, sizeof(filename)); inst->gdbm = gdbm_open(filename, sizeof(int), GDBM_NEWDB | GDBM_COUNTER_OPTS, 0600, NULL); } if (!inst->gdbm) { ERROR("rlm_counter: Failed to open file %s: %s", inst->filename, fr_syserror(errno)); return RLM_MODULE_FAIL; } if (gdbm_setopt(inst->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(cache_size)) == -1) { ERROR("rlm_counter: Failed to set cache size"); } DEBUG2("rlm_counter: reset_db: Opened new database"); /* * Add defaults */ rcode = add_defaults(inst); if (rcode != RLM_MODULE_OK) return rcode; DEBUG2("rlm_counter: reset_db ended"); return RLM_MODULE_OK; }
void ft_reset(int in_x) { reset_all_list(); add_defaults(in_x); }
/* * Do any per-module initialization that is separate to each * configured instance of the module. e.g. set up connections * to external databases, read configuration files, set up * dictionary entries, etc. * * If configuration information is given in the config section * that must be referenced in later calls, store a handle to it * in *instance otherwise put a null pointer there. */ static int mod_instantiate(CONF_SECTION *conf, void *instance) { rlm_counter_t *inst = instance; DICT_ATTR const *da; DICT_VALUE *dval; ATTR_FLAGS flags; time_t now; int cache_size; int ret; datum key_datum; datum time_datum; char const *default1 = "DEFAULT1"; char const *default2 = "DEFAULT2"; cache_size = inst->cache_size; da = dict_attrbyname(inst->key_name); rad_assert(da != NULL); inst->key_attr = da; /* * Discover the attribute number of the counter. */ da = dict_attrbyname(inst->count_attribute); rad_assert(da != NULL); inst->count_attr = da; /* * Discover the attribute number of the reply attribute. */ if (inst->reply_name != NULL) { da = dict_attrbyname(inst->reply_name); if (!da) { cf_log_err_cs(conf, "No such attribute %s", inst->reply_name); return -1; } if (da->type != PW_TYPE_INTEGER) { cf_log_err_cs(conf, "Reply attribute' %s' is not of type integer", inst->reply_name); return -1; } inst->reply_attr = da; } else { inst->reply_attr = NULL; } /* * Create a new attribute for the counter. */ rad_assert(inst->counter_name && *inst->counter_name); memset(&flags, 0, sizeof(flags)); if (dict_addattr(inst->counter_name, -1, 0, PW_TYPE_INTEGER, flags) < 0) { ERROR("rlm_counter: Failed to create counter attribute %s: %s", inst->counter_name, fr_strerror()); return -1; } da = dict_attrbyname(inst->counter_name); if (!da) { cf_log_err_cs(conf, "Failed to find counter attribute %s", inst->counter_name); return -1; } inst->dict_attr = da; DEBUG2("rlm_counter: Counter attribute %s is number %d", inst->counter_name, inst->dict_attr->attr); /* * Create a new attribute for the check item. */ rad_assert(inst->check_name && *inst->check_name); if (dict_addattr(inst->check_name, -1, 0, PW_TYPE_INTEGER, flags) < 0) { ERROR("rlm_counter: Failed to create check attribute %s: %s", inst->counter_name, fr_strerror()); return -1; } da = dict_attrbyname(inst->check_name); if (!da) { ERROR("rlm_counter: Failed to find check attribute %s", inst->counter_name); return -1; } inst->check_attr = da; /* * Find the attribute for the allowed protocol */ if (inst->service_type != NULL) { if ((dval = dict_valbyname(PW_SERVICE_TYPE, 0, inst->service_type)) == NULL) { ERROR("rlm_counter: Failed to find attribute number for %s", inst->service_type); return -1; } inst->service_val = dval->value; } /* * Find when to reset the database. */ rad_assert(inst->reset && *inst->reset); now = time(NULL); inst->reset_time = 0; inst->last_reset = now; if (find_next_reset(inst,now) == -1) { ERROR("rlm_counter: find_next_reset() returned -1. Exiting"); return -1; } { char *filename; memcpy(&filename, &inst->filename, sizeof(filename)); inst->gdbm = gdbm_open(filename, sizeof(int), GDBM_NEWDB | GDBM_COUNTER_OPTS, 0600, NULL); } if (!inst->gdbm) { ERROR("rlm_counter: Failed to open file %s: %s", inst->filename, fr_syserror(errno)); return -1; } if (gdbm_setopt(inst->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(cache_size)) == -1) { ERROR("rlm_counter: Failed to set cache size"); } /* * Look for the DEFAULT1 entry. This entry if it exists contains the * time of the next database reset. This time is set each time we reset * the database. If next_reset < now then we reset the database. * That way we can overcome the problem where radiusd is down during a database * reset time. If we did not keep state information in the database then the reset * would be extended and that would create problems. * * We also store the time of the last reset in the DEFAULT2 entry. * * If DEFAULT1 and DEFAULT2 do not exist (new database) we add them to the database */ memcpy(&key_datum.dptr, &default1, sizeof(key_datum.dptr)); key_datum.dsize = strlen(key_datum.dptr); time_datum = gdbm_fetch(inst->gdbm, key_datum); if (time_datum.dptr != NULL) { time_t next_reset = 0; memcpy(&next_reset, time_datum.dptr, sizeof(time_t)); free(time_datum.dptr); time_datum.dptr = NULL; if (next_reset && next_reset <= now) { inst->last_reset = now; ret = reset_db(inst); if (ret != RLM_MODULE_OK) { ERROR("rlm_counter: reset_db() failed"); return -1; } } else { inst->reset_time = next_reset; } memcpy(&key_datum.dptr, &default2, sizeof(key_datum.dptr)); key_datum.dsize = strlen(key_datum.dptr); time_datum = gdbm_fetch(inst->gdbm, key_datum); if (time_datum.dptr != NULL) { memcpy(&inst->last_reset, time_datum.dptr, sizeof(time_t)); free(time_datum.dptr); } } else { ret = add_defaults(inst); if (ret != RLM_MODULE_OK) { ERROR("rlm_counter: add_defaults() failed"); return -1; } } /* * Register the counter comparison operation. * FIXME: move all attributes to DA */ paircompare_register(inst->dict_attr, NULL, true, counter_cmp, inst); /* * Init the mutex */ pthread_mutex_init(&inst->mutex, NULL); return 0; }