📄 resources.c
字号:
* Function: range_ok * * Purpose: This function checks whether the specified range is too large * for the given base IP address * *****************************************************************************/static intrange_ok (ip_address, netmask, range)UINT4 ip_address;UINT4 netmask;int range;{ UINT4 first_host; UINT4 broadcast; static char *func = "range_ok"; dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); if (netmask == 0xffffffff) { if (range != 1) { return (0); } return (1); } first_host = ip_address & (~netmask); broadcast = ~netmask; if ((first_host + range - 1) >= broadcast) { return (0); } return (1);} /* end of range_ok () *//****************************************************************************** * * Function: pool_overlap * * Purpose: Checks if the given IP address lies within one of the pools * defined in the given list of pools. * *****************************************************************************/static charpool_overlap (pool, name, ipaddr)ADDR_POOL **pool;char *name;UINT4 ipaddr;{ ADDR_POOL *pptr; for (pptr = *pool; pptr; pptr = pptr->next) { if (strcmp (name, pptr->name) == 0) { return (1); } if (ipaddr >= pptr->ip_address && ipaddr < (pptr->ip_address + pptr->range)) { return (1); } } return (0);} /* end of pool_overlap () *//****************************************************************************** * * Function: resource_mgmt * * Purpose: This function assigns an IP address from one of the address * pools defined in the users file. It also keeps track of all * the addresses that have been assigned and of all the users * who have more than one session allowed at a time. * *****************************************************************************/intresource_mgmt (authreq)AUTH_REQ *authreq;{ VALUE_PAIR *user_name; VALUE_PAIR *nas_addr; VALUE_PAIR *nas_port; VALUE_PAIR *framed_ip; USER_ENTRY *user_ent; ADDR_POOL *aptr; ASSIGNED_IP *p; ASSIGNED_IP *q; UINT4 new_address = 1; ASSIGNED_IP *new_node; char head = 0; ASSIGNED_IP **ipptr; FILE_LIST *file_ent; static char *func = "resource_mgmt"; dprintf(2, (LOG_AUTH, LOG_DEBUG, "%s: entered", func)); if ((user_name = get_vp (authreq->request, PW_USER_NAME)) == (VALUE_PAIR *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: Missing User Name; resource management not possible", func); return EV_NAK; } if ((nas_addr = get_vp (authreq->request, PW_NAS_IP_ADDRESS)) == (VALUE_PAIR *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: Missing NAS IP address, cannot assign %s an IP address", func, user_name->strvalue); return EV_NAK; } if ((nas_port = get_vp (authreq->request, PW_NAS_PORT)) == (VALUE_PAIR *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: Missing NAS Port, cannot assign %s an IP address", func, user_name->strvalue); return EV_NAK; } if ((file_ent = find_file_ent (authreq->client->file_pfx)) == (FILE_LIST *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: NULL file_ent", func); return EV_NAK; } for (user_ent = file_ent->user_list; user_ent; user_ent = user_ent->next) { if (strcmp (user_ent->name, user_name->strvalue) == 0) { break; /* Found user. */ } } if (user_ent == (USER_ENTRY *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: Invalid user %s", func, user_name); return EV_NAK; } if (user_ent->count >= user_ent->sessions) { logit (LOG_DAEMON, LOG_INFO, "%s: User %s has maximum allowed sessions going", func, user_name); return EV_NAK; } aptr = file_ent->pool_list; /* The default pool *//* if (user_ent->pool_name[0] != '\0') */ if (strcmp (user_ent->pool_name, DEF_POOL_NAME) != 0) { for (; aptr != (ADDR_POOL *) NULL; aptr = aptr->next) { if (strcmp (aptr->name, user_ent->pool_name) == 0) { break; } } if (aptr == (ADDR_POOL *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: Could not find an Address Pool for %s.", func, user_name); return EV_NAK; } if (aptr->count >= aptr->range) { logit (LOG_DAEMON, LOG_ERR, "%s: Address Pool is full; can't assign address to %s", func, user_ent->name); return EV_NAK; } p = aptr->user_q; /* Processing address pool list. */ if (p != (ASSIGNED_IP *) NULL) { if (p->ip_address != aptr->ip_address) {/* new_address = aptr->ip_address; */ head = 1; } else { for (q = p->next; q; q = q->next) { if ((q->ip_address - p->ip_address) > 1) { break; } p = p->next; } new_address = p->ip_address + 1; } } else { head = 1;/* new_address = aptr->ip_address; */ } new_node = (ASSIGNED_IP *) malloc (sizeof (ASSIGNED_IP)); if (new_node == (ASSIGNED_IP *) NULL) { logit (LOG_DAEMON, LOG_ALERT, "%s: OUT OF MEMORY", func); return EV_NAK; } if (head == 1) { new_address = aptr->ip_address; new_node->next = aptr->user_q;/* new_node->next = (ASSIGNED_IP *) NULL; */ aptr->user_q = new_node; } else { new_node->next = p->next; p->next = new_node; } new_node->ip_address = new_address; new_node->user_ent = user_ent; new_node->nas_ip = nas_addr->lvalue; new_node->nas_port = nas_port->lvalue; user_ent->count++; aptr->count++; if (avpair_add (&authreq->cur_request, PW_FRAMED_IP_ADDRESS, &new_address, 0) == (VALUE_PAIR *) NULL) { logit (LOG_DAEMON, LOG_ERR, "%s: Problem adding Framed-IP-Address for user %s", func, user_ent->name); return EV_NAK; } authreq->cur_count++; return EV_ACK; } else /* if (user_ent->sessions > 1) */ /* * No Address Pool defined, but multiple sessions allowed. * Put session in the default pool. */ { for (ipptr = &aptr->user_q; *ipptr != (ASSIGNED_IP *) NULL; ipptr = &(*ipptr)->next) { ; /* continue */ } new_node = (ASSIGNED_IP *) malloc (sizeof (ASSIGNED_IP)); if (new_node == (ASSIGNED_IP *) NULL) { logit (LOG_DAEMON, LOG_ALERT, "%s: OUT OF MEMORY", func); return EV_NAK; } new_node->user_ent = user_ent; if ((framed_ip = get_vp (authreq->cur_request, PW_FRAMED_IP_ADDRESS)) != (VALUE_PAIR *) NULL) { new_node->ip_address = framed_ip->lvalue; } else { new_node->ip_address = 0; } new_node->nas_ip = nas_addr->lvalue; new_node->nas_port = nas_port->lvalue; new_node->next = (ASSIGNED_IP *) NULL; *ipptr = new_node; user_ent->count++; aptr->count++; return EV_ACK; }} /* end of resource_mgmt () *//*************************************************************************** * * Function: free_resources * * Purpose: Free the resource (given IP address) allocated to the * given user. * **************************************************************************/intfree_resources (name, ipaddr, nas, nas_port)char *name;UINT4 ipaddr;UINT4 nas;UINT4 nas_port;{ char default_pool = FALSE; FILE_LIST *file_ent; ADDR_POOL *aptr; ASSIGNED_IP **assign_prev; ASSIGNED_IP *assign_cur; char *func = "free_resources"; dprintf(2, (LOG_DAEMON, LOG_DEBUG, "%s: entered", func)); file_ent = get_default_file_entry (); if (nas == 0 || name == (char *) NULL || nas_port == 0) { logit (LOG_DAEMON, LOG_ERR, "%s: Insufficient information in resource free request", func); return EV_NAK; } for (; file_ent != (FILE_LIST *) NULL; file_ent = file_ent->next) { for (aptr = file_ent->pool_list; aptr != (ADDR_POOL *) NULL; aptr = aptr->next) { if (strcmp (aptr->name, DEF_POOL_NAME) != 0) { if (aptr->count == 0) { continue; } if ((ipaddr & aptr->netmask) != aptr->network) { continue; } } else { default_pool = TRUE; } assign_prev = &aptr->user_q; assign_cur = *assign_prev; for ( ; assign_cur != (ASSIGNED_IP *) NULL; assign_cur = assign_cur->next) { if ((default_pool == TRUE || assign_cur->ip_address == ipaddr) && assign_cur->nas_ip == nas && assign_cur->nas_port == nas_port && strcmp (name, assign_cur->user_ent->name) == 0) { *assign_prev = assign_cur->next; (assign_cur->user_ent)->count--; aptr->count--; free (assign_cur); return EV_ACK; } assign_prev = &assign_cur->next; } } }/* logit (LOG_DAEMON, LOG_ERR, "%s: No such resource", func); */ return EV_NAK;} /* end of free_resources () */#endif /* USR_CCA */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -