def authorization(received, check, reply): """Find user in database Input: (dict) received data, (dict) check data, (dict) reply data Output: (bool) True - success, False - failure """ # translate "-" to "_" in request attributes attrs = translateAttrs(received) queryTpl = radParsedConfig['AUTHORIZATION'].get('authz_query', None) if queryTpl == None: return # Select account data. # It is advisable to select username and password as 1st two attributes # for modules which require them in authentication (chap, digest) dbh = DatabaseConnection.getHandler('mod_sql_1') query = Template(queryTpl).substitute(attrs) # use request items for keyword replacement acctData = dbh.execGetRowsOne(query) if acctData: debug ('Account found') authType = check.get('Auth-Type', [None])[0] if not authType: # just set the auth type to "sql" if no previous modules have touched it. check['Auth-Type'] = ['sql'] elif len(acctData) >= 2: # pass username and password to module which has set up it's auth type. check['User-Name'] = [acctData[0]] check['User-Password'] = [acctData[1]] else: debug ('Account not found') # Return true even if account not found. Leave chance for other modules to # find the user. return True
def shutdown(): """Close database connections Input: none Output: none """ info ("Shutting down database connections") dbh = DatabaseConnection.getHandler('mod_sql_1') dbh.disconnect()
def accounting(received): """Log data of completed session (call). Input: (dict) received data Output: none """ dbh = DatabaseConnection.getHandler('mod_sql_1') acctType = received.get('Acct-Status-Type', [None])[0] if not acctType: raise ModsqlError, 'Acct-Status-Type not found in request' debug('Accounting %s message received' % acctType) supportedAcctTypes = ['Start', 'Alive', 'Stop'] if acctType not in supportedAcctTypes: return # translate "-" to "_" in request attributes attrs = translateAttrs(received) # Parse date and time from cisco format to datetime class when using # mysql. There are no problems with postgresql however. # This should be done for users who want to log call start and end times into # database. Since they can wish to log these attributes during any type of # request, we should always parse them. if config['ACCESS']['type'] != 'postgresql': tmp = { } # use temporary storage to avoid messing up received attributes tmp['h323_setup_time'] = parseDatetime(attrs.get( 'h323_setup_time', '')) tmp['h323_connect_time'] = parseDatetime( attrs.get('h323_connect_time', '')) tmp['h323_disconnect_time'] = parseDatetime( attrs.get('h323_disconnect_time', '')) for key, value in tmp.iteritems(): if value != None: attrs[key] = str(value) # get sql query template based on Acct-Status-Type parameter queryTpl = None if acctType == 'Start': queryTpl = config['ACCOUNTING'].get('acct_start_query', None) elif acctType == 'Alive': queryTpl = config['ACCOUNTING'].get('acct_update_query', None) elif acctType == 'Stop': queryTpl = config['ACCOUNTING'].get('acct_stop_query', None) # check and parse query template if queryTpl == None: return query = Template(queryTpl).substitute( attrs) # use request items for keyword replacement # execute query success = dbh.execute(query) if not success: raise ModsqlError, "Error executing accounting %s SQL query" % acctType
def accounting(received): """Log data of completed session (call). Input: (dict) received data Output: none """ dbh = DatabaseConnection.getHandler('mod_sql_1') acctType = received.get('Acct-Status-Type', [None])[0] if not acctType: raise ModsqlError, 'Acct-Status-Type not found in request' debug ('Accounting %s message received' % acctType) supportedAcctTypes = ['Start', 'Alive', 'Stop'] if acctType not in supportedAcctTypes: return # translate "-" to "_" in request attributes attrs = translateAttrs(received) # Parse date and time from cisco format to datetime class when using # mysql. There are no problems with postgresql however. # This should be done for users who want to log call start and end times into # database. Since they can wish to log these attributes during any type of # request, we should always parse them. if radParsedConfig['ACCESS']['type'] != 'postgresql': tmp = {} # use temporary storage to avoid messing up received attributes tmp['h323_setup_time'] = parseDatetime(attrs.get('h323_setup_time', '')) tmp['h323_connect_time'] = parseDatetime(attrs.get('h323_connect_time', '')) tmp['h323_disconnect_time'] = parseDatetime(attrs.get('h323_disconnect_time', '')) for key, value in tmp.iteritems(): if value != None: attrs[key] = str(value) # get sql query template based on Acct-Status-Type parameter queryTpl = None if acctType == 'Start': queryTpl = radParsedConfig['ACCOUNTING'].get('acct_start_query', None) elif acctType == 'Alive': queryTpl = radParsedConfig['ACCOUNTING'].get('acct_update_query', None) elif acctType == 'Stop': queryTpl = radParsedConfig['ACCOUNTING'].get('acct_stop_query', None) # check and parse query template if queryTpl == None: return query = Template(queryTpl).substitute(attrs) # use request items for keyword replacement # execute query success = dbh.execute(query) if not success: raise ModsqlError, "Error executing accounting %s SQL query" % acctType
def startup(): """Open sql connection Input: none Output: none """ info ('Opening database connections') sqlAccess = radParsedConfig['ACCESS'] # detect database driver name and open connection if sqlAccess['type'] == 'postgresql': dbadapterName = 'psycopg' else: dbadapterName = 'MySQLdb' dbh = DatabaseConnection.getHandler('mod_sql_1', dbadapterName) dbh.connect(host = sqlAccess['host'], user = sqlAccess['user'], password = sqlAccess['pass'], dbname = sqlAccess['name'])