def main(stdscr, *args, **kw): """ The main function hook passed to the curses wrapper """ global screen screen = stdscr main_menu() while True: try: screen.addstr(14, 0, "What would you like to do?\n") c = screen.getch() if c == ord('1'): cname_from = get_input("Adding CNAME: From where?") if not validation.is_fqdn(cname_from): raise CLIException("Invalid syntax: please use a fully-qualified domain name") cname_to = get_input("To where?") if not validation.is_fqdn(cname_to): raise CLIException("Invalid syntax: please use a fully-qualified domain name") record = db.get_dns_records(name=cname_from) if record: raise CLIException("Record already found: %s" % record) domain = db.get_domains(contains=cname_from) if not domain: raise CLIException("No domain found, couldn't add CNAME.") db.add_dns_record(name=cname_from, tid=5, text_content=cname_to, add_ptr=False) done_message("Done! Added CNAME: %s --> %s" % (cname_from, cname_to)) elif c == ord('2'): domain = get_input("Adding domain:") if not validation.is_fqdn(domain): raise CLIException("Invalid syntax: please use a fully-qualified domain name") address = get_input("To where?") if not validation.is_ip(address): raise CLIException("Invalid syntax: please use an IP address") db._begin_transaction() try: db.add_domain( name=domain, type='MASTER', master=None ) db.add_soa_record( name=domain, primary='root1.usu.edu', hostmaster='*****@*****.**' ) db.add_dns_record( name=domain, tid=1, ip_content=address ) db._commit() except: db._rollback() raise done_message("Done! Added domain %s --> %s" % (domain, address)) elif c == ord('7'): sys.exit(0) except Exception, e: error_message(e)
def byip(ip): print 'byip(%s)' % ip if type(ip) == types.ListType: for i in ip: i = str(i) print i if not validation.is_ip(i): raise error.InvalidArgument('Not a valid ip address: %s' % i) cond = "ip IN ('%s')" % "','".join(ip) else: if not validation.is_ip(ip): raise error.InvalidArgument('Not a valid ip address: %s' % ip) cond = "ip = '%s'" % ip data = execute("SELECT ip, mac, COALESCE(NOW() - stopstamp, interval '0 minutes') AS last_seen FROM arpentries JOIN iplastarp ON arpentries.id = iplastarp.arpid WHERE %s;" % cond) return mk_dicts(data)
def byip(ip): print 'byip(%s)' % ip if type(ip) == types.ListType: for i in ip: i = str(i) print i if not validation.is_ip(i): raise error.InvalidArgument('Not a valid ip address: %s' % i) cond = "ip IN ('%s')" % "','".join(ip) else: if not validation.is_ip(ip): raise error.InvalidArgument('Not a valid ip address: %s' % ip) cond = "ip = '%s'" % ip data = execute( "SELECT ip, mac, COALESCE(NOW() - stopstamp, interval '0 minutes') AS last_seen FROM arpentries JOIN iplastarp ON arpentries.id = iplastarp.arpid WHERE %s;" % cond) return mk_dicts(data)
def search(self, q='', expiring=False, page=0, order_by='hostname', success=False, **kw): ''' The search page where the search form POSTs ''' # Confirm user authentication self.check_session() # Initialization values = {} page = int(page) if re.search(r'[^a-zA-Z.,_ ]',order_by): raise Exception('Who do you think you are?') cherrypy.session.acquire_lock() try: if not q and not kw.keys(): if not expiring: raise cherrypy.InternalRedirect('/hosts') else: kw['username'] = cherrypy.session['username'] limit = cherrypy.session['hosts_limit'] finally: cherrypy.session.release_lock() if success: values['global_success'] = 'Hosts Updated Successfully' if expiring: kw['expiring'] = expiring if page: kw['page'] = page special_search = { 'ip':'ip', 'mac':'mac', 'user':'******', 'username':'******', 'net':'network', 'network':'network', 'hostname':'namesearch', 'desc':'descriptionsearch','description':'descriptionsearch', 'name':'namesearch', 'group':'groupname', } for element in q.split( ): if validation.is_mac(element): kw['mac'] = element elif validation.is_ip(element): kw['ip'] = element elif validation.is_cidr(element): kw['network'] = element elif ':' in element: # I strongly recommend that we do this next to last... stype,value = element.split(':',1) if special_search.has_key(stype): kw[special_search[stype]] = value else: raise error.InvalidArgument('Unrecognized special search type: %s (value: %s)' % (stype, value)) if stype == 'mac' and '*' not in value and len(value) >= 24: # range specified rawmacs = re.sub(r'[:.-]','',value.strip()) if not re.match(r"([0-9a-fA-F]{6})[0-9a-fA-F]{6}\1[0-9a-fA-F]{6}", rawmacs): raise error.InvalidArgument("Invalid mac range: %s (%s)" % (value, rawmacs)) kw['mac'] = rawmacs[:12] kw['endmac'] = rawmacs[12:] else: # Let's assume it's a hostname. if '.' in element or '*' in element or '%' in element: namesearch = element.replace('%','*') else: namesearch = '*%s*' % element.replace('%','*') if kw.has_key('namesearch'): raise error.InvalidArgument('Invalid search string -- more than one name (%s, %s)' % (kw['namesearch'], namesearch)) kw['namesearch'] = namesearch # FIXME: this might break with special characters # FIXME: need more thorough input validation kw_elements = [] kw_keys = kw.keys() kw_keys.sort() for k in kw_keys: v = kw[k] if hasattr(v, '__contains__') and '&' in v: raise error.InvalidArgument('& is not valid here') if k != 'page': kw_elements.append('%s=%s' % (k,v)) search_str = '/search/?%s&' % '&'.join(kw_elements) print search_str if q: # we are ignoring order_by here, but this should only happen with a new search anyway... self.redirect('/hosts%s' % ( search_str[:-1] ) ) kw['order_by'] = order_by values['search'] = search_str values['page'] = int(page) cherrypy.session.acquire_lock() try: values['show_all_hosts'] = cherrypy.session['show_all_hosts'] values['username'] = cherrypy.session['username'] finally: cherrypy.session.release_lock() values['num_hosts'],values['hosts'] = self.get_hosts( count=True, **kw ) values['len_hosts'] = len(values['hosts']) values['num_pages'] = int( (values['num_hosts'] + limit - 1) / limit ) values['first_host'] = page * limit + 1 values['last_host'] = page * limit + len(values['hosts']) values['limit'] = limit values['order_by'] = order_by values['url'] = cherrypy.url() values['groups'] = self.webservice.get_groups( { 'ignore_usergroups' : True, 'order_by' : 'name' } ) values['dhcp_group_dict'] = {} dhcp_groups = self.webservice.get_dhcp_groups( {'order_by' : 'name' } ) for g in dhcp_groups: values['dhcp_group_dict'][g['id']] = dict(g) return self.__template.wrap(leftcontent=self.get_leftnav(), filename='%s/templates/hosts.tmpl'%frontend.static_dir, values=values)
def main(stdscr, *args, **kw): """ The main function hook passed to the curses wrapper """ global screen screen = stdscr main_menu() while True: try: screen.addstr(14, 0, "What would you like to do?\n") c = screen.getch() if c == ord('1'): cname_from = get_input("Adding CNAME: From where?") if not validation.is_fqdn(cname_from): raise CLIException( "Invalid syntax: please use a fully-qualified domain name" ) cname_to = get_input("To where?") if not validation.is_fqdn(cname_to): raise CLIException( "Invalid syntax: please use a fully-qualified domain name" ) record = db.get_dns_records(name=cname_from) if record: raise CLIException("Record already found: %s" % record) domain = db.get_domains(contains=cname_from) if not domain: raise CLIException("No domain found, couldn't add CNAME.") db.add_dns_record(name=cname_from, tid=5, text_content=cname_to, add_ptr=False) done_message("Done! Added CNAME: %s --> %s" % (cname_from, cname_to)) elif c == ord('2'): domain = get_input("Adding domain:") if not validation.is_fqdn(domain): raise CLIException( "Invalid syntax: please use a fully-qualified domain name" ) address = get_input("To where?") if not validation.is_ip(address): raise CLIException( "Invalid syntax: please use an IP address") db._begin_transaction() try: db.add_domain(name=domain, type='MASTER', master=None) db.add_soa_record(name=domain, primary='root1.usu.edu', hostmaster='*****@*****.**') db.add_dns_record(name=domain, tid=1, ip_content=address) db._commit() except: db._rollback() raise done_message("Done! Added domain %s --> %s" % (domain, address)) elif c == ord('7'): sys.exit(0) except Exception, e: error_message(e)