📄 rlm_preprocess.c
字号:
if (name == NULL || name[0] == 0) /* * No name, nothing to do. */ return RLM_MODULE_NOOP; for (i = hints; i; i = i->next) { if (matches(name, i, newname)) { DEBUG2(" hints: Matched %s at %d", i->name, i->lineno); break; } } if (i == NULL) return RLM_MODULE_NOOP; add = paircopy(i->reply);#if 0 /* DEBUG */ printf("In hints_setup, newname is %s\n", newname);#endif /* * See if we need to adjust the name. */ do_strip = 1; if ((tmp = pairfind(i->reply, PW_STRIP_USER_NAME)) != NULL && tmp->lvalue == 0) do_strip = 0; if ((tmp = pairfind(i->check, PW_STRIP_USER_NAME)) != NULL && tmp->lvalue == 0) do_strip = 0; if (do_strip) { tmp = pairfind(request_pairs, PW_STRIPPED_USER_NAME); if (tmp) { strcpy((char *)tmp->strvalue, newname); tmp->length = strlen((char *)tmp->strvalue); } else { /* * No Stripped-User-Name exists: add one. */ tmp = paircreate(PW_STRIPPED_USER_NAME, PW_TYPE_STRING); if (!tmp) { radlog(L_ERR|L_CONS, "no memory"); exit(1); } strcpy((char *)tmp->strvalue, newname); tmp->length = strlen((char *)tmp->strvalue); pairadd(&request_pairs, tmp); } request->username = tmp; } /* * Now add all attributes to the request list, * except the PW_STRIP_USER_NAME one. */ pairdelete(&add, PW_STRIP_USER_NAME); for(last = request_pairs; last && last->next; last = last->next) ; if (last) last->next = add; return RLM_MODULE_UPDATED;}/* * See if the huntgroup matches. This function is * tied to the "Huntgroup" keyword. */static int huntgroup_cmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check, VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs){ PAIR_LIST *i; char *huntgroup; rlm_preprocess_t *data = (rlm_preprocess_t *) instance; check_pairs = check_pairs; /* shut the compiler up */ reply_pairs = reply_pairs; huntgroup = (char *)check->strvalue; for (i = data->huntgroups; i; i = i->next) { if (strcmp(i->name, huntgroup) != 0) continue; if (paircmp(req, request, i->check, NULL) == 0) { DEBUG2(" huntgroups: Matched %s at %d", i->name, i->lineno); break; } } /* * paircmp() expects to see zero on match, so let's * keep it happy. */ if (i == NULL) { return -1; } return 0;}/* * See if we have access to the huntgroup. */static int huntgroup_access(PAIR_LIST *huntgroups, VALUE_PAIR *request_pairs){ PAIR_LIST *i; int r = RLM_MODULE_OK; /* * We're not controlling access by huntgroups: * Allow them in. */ if (huntgroups == NULL) return RLM_MODULE_OK; for(i = huntgroups; i; i = i->next) { /* * See if this entry matches. */ if (paircmp(NULL, request_pairs, i->check, NULL) != 0) continue; /* * Now check for access. */ r = RLM_MODULE_REJECT; if (hunt_paircmp(request_pairs, i->reply) == 0) { VALUE_PAIR *vp; /* * We've matched the huntgroup, so add it in * to the list of request pairs. */ vp = pairfind(request_pairs, PW_HUNTGROUP_NAME); if (!vp) { vp = paircreate(PW_HUNTGROUP_NAME, PW_TYPE_STRING); if (!vp) { radlog(L_ERR, "No memory"); exit(1); } strNcpy(vp->strvalue, i->name, sizeof(vp->strvalue)); vp->length = strlen(vp->strvalue); pairadd(&request_pairs, vp); } r = RLM_MODULE_OK; } break; } return r;}/* * If the NAS wasn't smart enought to add a NAS-IP-Address * to the request, then add it ourselves. */static void add_nas_attr(REQUEST *request){ VALUE_PAIR *nas; nas = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS); if (!nas) { nas = paircreate(PW_NAS_IP_ADDRESS, PW_TYPE_IPADDR); if (!nas) { radlog(L_ERR, "No memory"); exit(1); } nas->lvalue = request->packet->src_ipaddr; ip_hostname(nas->strvalue, sizeof(nas->strvalue), nas->lvalue); pairadd(&request->packet->vps, nas); } /* * Add in a Client-IP-Address, to tell the user * the source IP of the request. That is, the client, * * Note that this MAY BE different from the NAS-IP-Address, * especially if the request is being proxied. * * Note also that this is a server configuration item, * and will NOT make it to any packets being sent from * the server. */ nas = paircreate(PW_CLIENT_IP_ADDRESS, PW_TYPE_IPADDR); if (!nas) { radlog(L_ERR, "No memory"); exit(1); } nas->lvalue = request->packet->src_ipaddr; ip_hostname(nas->strvalue, sizeof(nas->strvalue), nas->lvalue); pairadd(&request->packet->vps, nas);}/* * Initialize. */static int preprocess_instantiate(CONF_SECTION *conf, void **instance){ int rcode; rlm_preprocess_t *data; /* * Allocate room to put the module's instantiation data. */ data = (rlm_preprocess_t *) rad_malloc(sizeof(*data)); memset(data, 0, sizeof(*data)); /* * Read this modules configuration data. */ if (cf_section_parse(conf, data, module_config) < 0) { free(data); return -1; } data->huntgroups = NULL; data->hints = NULL; /* * Read the huntgroups file. */ rcode = pairlist_read(data->huntgroup_file, &(data->huntgroups), 0); if (rcode < 0) { radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s", data->huntgroup_file); return -1; } /* * Read the hints file. */ rcode = pairlist_read(data->hints_file, &(data->hints), 0); if (rcode < 0) { radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s", data->hints_file); return -1; } /* * Register the huntgroup comparison operation. */ paircompare_register(PW_HUNTGROUP_NAME, 0, huntgroup_cmp, data); /* * Save the instantiation data for later. */ *instance = data; return 0;}/* * Preprocess a request. */static int preprocess_authorize(void *instance, REQUEST *request){ char buf[1024]; rlm_preprocess_t *data = (rlm_preprocess_t *) instance; /* * Mangle the username, to get rid of stupid implementation * bugs. */ rad_mangle(data, request); if (data->with_ascend_hack) { /* * If we're using Ascend systems, hack the NAS-Port-Id * in place, to go from Ascend's weird values to something * approaching rationality. */ ascend_nasport_hack(pairfind(request->packet->vps, PW_NAS_PORT), data->ascend_channels_per_line); } if (data->with_cisco_vsa_hack) { /* * We need to run this hack because the h323-conf-id * attribute should be used. */ cisco_vsa_hack(request->packet->vps); } /* * Note that we add the Request-Src-IP-Address to the request * structure BEFORE checking huntgroup access. This allows * the Request-Src-IP-Address to be used for huntgroup * comparisons. */ add_nas_attr(request); hints_setup(data->hints, request); /* * If there is a PW_CHAP_PASSWORD attribute but there * is PW_CHAP_CHALLENGE we need to add it so that other * modules can use it as a normal attribute. */ if (pairfind(request->packet->vps, PW_CHAP_PASSWORD) && pairfind(request->packet->vps, PW_CHAP_CHALLENGE) == NULL) { VALUE_PAIR *vp; vp = paircreate(PW_CHAP_CHALLENGE, PW_TYPE_OCTETS); if (!vp) { radlog(L_ERR|L_CONS, "no memory"); exit(1); } vp->length = AUTH_VECTOR_LEN; memcpy(vp->strvalue, request->packet->vector, AUTH_VECTOR_LEN); pairadd(&request->packet->vps, vp); } if (huntgroup_access(data->huntgroups, request->packet->vps) != RLM_MODULE_OK) { radlog(L_AUTH, "No huntgroup access: [%s] (%s)", request->username->strvalue, auth_name(buf, sizeof(buf), request, 1)); return RLM_MODULE_REJECT; } return RLM_MODULE_OK; /* Meaning: try next authorization module */}/* * Preprocess a request before accounting */static int preprocess_preaccounting(void *instance, REQUEST *request){ int r; rlm_preprocess_t *data = (rlm_preprocess_t *) instance; /* * Ensure that we have the SAME user name for both * authentication && accounting. */ rad_mangle(data, request); if (data->with_cisco_vsa_hack) { /* * We need to run this hack because the h323-conf-id * attribute should be used. */ cisco_vsa_hack(request->packet->vps); } /* * Ensure that we log the NAS IP Address in the packet. */ add_nas_attr(request); r = hints_setup(data->hints, request); return r;}/* * Clean up the module's instance. */static int preprocess_detach(void *instance){ rlm_preprocess_t *data = (rlm_preprocess_t *) instance; paircompare_unregister(PW_HUNTGROUP_NAME, huntgroup_cmp); pairlist_free(&(data->huntgroups)); pairlist_free(&(data->hints)); free(data->huntgroup_file); free(data->hints_file); free(data); return 0;}/* globally exported name */module_t rlm_preprocess = { "preprocess", 0, /* type: reserved */ NULL, /* initialization */ preprocess_instantiate, /* instantiation */ { NULL, /* authentication */ preprocess_authorize, /* authorization */ preprocess_preaccounting, /* pre-accounting */ NULL, /* accounting */ NULL, /* checksimul */ NULL, /* pre-proxy */ NULL, /* post-proxy */ NULL /* post-auth */ }, preprocess_detach, /* detach */ NULL, /* destroy */};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -