📄 rlm_preprocess.c
字号:
add = paircopy(i->reply); /* * Now add all attributes to the request list, * except the PW_STRIP_USER_NAME one, and * xlat them. */ pairdelete(&add, PW_STRIP_USER_NAME); pairxlatmove(request, &request->packet->vps, &add); pairfree(&add); return RLM_MODULE_UPDATED;}/* * See if we have access to the huntgroup. */static int huntgroup_access(REQUEST *request, 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(request, request_pairs, i->check, NULL) != 0) continue; /* * Now check for access. */ r = RLM_MODULE_REJECT; if (hunt_paircmp(request, 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"); r = RLM_MODULE_FAIL; } 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 int 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"); return -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"); return -1; } nas->lvalue = request->packet->src_ipaddr; ip_hostname(nas->strvalue, sizeof(nas->strvalue), nas->lvalue); pairadd(&request->packet->vps, nas); return 0;}/* * 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; } /* * Save the instantiation data for later. */ *instance = data; return 0;}/* * Preprocess a request. */static int preprocess_authorize(void *instance, REQUEST *request){ char buf[1024]; int r; 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. */ if (add_nas_attr(request) < 0) { return RLM_MODULE_FAIL; } 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"); return RLM_MODULE_FAIL; } vp->length = AUTH_VECTOR_LEN; memcpy(vp->strvalue, request->packet->vector, AUTH_VECTOR_LEN); pairadd(&request->packet->vps, vp); } if ((r = huntgroup_access(request, 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 r; } 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. */ if (add_nas_attr(request) < 0) { return RLM_MODULE_FAIL; } 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; 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 + -