Changeset 48

Show
Ignore:
Timestamp:
02/12/04 15:55:33 (5 years ago)
Author:
jajcus
Message:

- mapping file support
- some debug messages removed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/spidentd.py

    r45 r48  
    3838        def lookup(self,localport,localip,remoteport,remoteip): 
    3939                return self.reply 
     40 
     41class Mapping: 
     42        def __init__(self): 
     43                pass 
     44        def lookup(self,user): 
     45                return user 
     46 
     47class FileMapping(Mapping): 
     48        def __init__(self,path): 
     49                self.mapping={} 
     50                for l in file(path).xreadlines(): 
     51                        l=l.strip() 
     52                        if not l: 
     53                                continue 
     54                        key,val=l.split(":") 
     55                        self.mapping[key]=val 
     56        def lookup(self,user): 
     57                return self.mapping.get(user,user) 
    4058 
    4159class RealDriver(Driver): 
     
    6179                        remip=socket.htonl(long(remip,16))&0xffffffffL 
    6280                        remport=int(remport,16) 
    63                         print "%r:%r,%r:%r vs %r:%r,%r:%r" % ( 
    64                                 localip,localport,remoteip,remoteport, 
    65                                 locip,locport,remip,remport) 
    6681                        if (localip,localport,remoteip,remoteport)!=(locip,locport,remip,remport): 
    6782                                continue 
     
    140155class SocketDriver(Driver): 
    141156        def __init__(self,path): 
     157                global socketmode,socketgroup,user,group 
    142158                self.socket=socket.socket(socket.AF_UNIX) 
    143159                try: 
     
    146162                        pass 
    147163                self.socket.bind(path) 
     164                if socketmode is not None: 
     165                        os.chmod(path,socketmode) 
     166                if os.getuid()==0: 
     167                        gid=None 
     168                        if socketgroup is not None: 
     169                                gid=grp.getgrnam(socketgroup)[2] 
     170                        elif group is not None: 
     171                                gid=grp.getgrnam(group)[2] 
     172                        elif user is not None: 
     173                                gid=pwd.getpwname(user)[3] 
     174                        if gid: 
     175                                os.chown(path,0,gid) 
    148176                self.socket.listen(1) 
    149177                self.clients=[] 
     
    209237                return 
    210238        reply=None 
     239        mapping=None 
    211240        for d in drivers: 
     241                if isinstance(d,Mapping): 
     242                        mapping=d 
     243                        continue 
     244                if not isinstance(d,Driver): 
     245                        continue 
    212246                r=d.lookup(local,localip,remote,remoteip) 
    213                 print `r` 
    214247                if r is None: 
    215248                        continue 
    216                 elif r.startswith("ERROR:"): 
     249                if mapping and not r.startswith("ERROR:"): 
     250                        oldr=r 
     251                        r=mapping.lookup(r) 
     252                        print "%r -> %r" % (oldr,r) 
     253                if r.startswith("ERROR:"): 
    217254                        reply="%i,%i:%s" % (local,remote,r) 
    218255                        break 
     
    232269        print >>sys.stderr,"Signal %i received, exiting." % (signum,) 
    233270 
    234  
    235271def accept_connection(sock): 
    236272        th=threading.Thread(target=input_thread,args=sock.accept()) 
     
    249285        print "    -i ADDR --ip=ADDR      bind to IP address ADDR." 
    250286        print "    -p PORT --port=PORT    bind to port PORT (default: 113)." 
    251         print "    -p USER --user=USER    when started with uid=0, switch " 
     287        print "    -u USER --user=USER    when started with uid=0, switch " 
    252288        print "                           to user USER (default: 'nobody')" 
    253289        print "    -p GROUP --group=GROUP when started with uid=0, switch " 
    254290        print "                           to group GROUP (default: nobody's group)" 
     291        print "    --socketmode=MODE      access mode for listening socket (--socket driver)" 
     292        print "    --socketgroup=MODE     owner group for listening socket (--socket driver)" 
    255293        print "Drivers:" 
    256294        print "    --nouser               always reply with NO-USER error." 
    257295        print "    --hidden               always reply with HIDDN-USER error." 
    258296        print "    --fail                 always reply with UNKNOWN-ERROR error." 
    259         print "    --real                 reply with real connection user name (currently Linux only)." 
     297        print "    --real                 reply with real connection user name (currently Linux" 
     298        print "                           only)." 
     299        print "    --map=FILE             user mapping file FILE for results of the following " 
     300        print "                           drivers" 
    260301        print "    --static=USER          always reply with the same USER reply." 
    261302        print "    --socket=PATH          listen on UNIX socket PATH for other servers" 
     
    268309port=113 
    269310drivers=[] 
     311socketmode=0775 
     312socketgroup=None 
    270313 
    271314try: 
    272         opts,args=getopt.getopt(sys.argv[1:], "hi:p:u:g:", ["help","ip=","port=","user=","group=", 
     315        opts,args=getopt.getopt(sys.argv[1:], "hi:p:u:g:", ["help","ip=","port=","user=","group=","map=","socketmode=","socketgroup=", 
    273316                "nouser","hidden","real","fail","static=","socket="]) 
    274 except: 
     317except Exception,e: 
     318        print e 
    275319        usage() 
    276320        sys.exit(2) 
     
    287331        if o in ("-g","--group"): 
    288332                group=a 
     333        if o in ("--socketmode",): 
     334                socketmode=int(a,8) 
     335        if o in ("--socketgroup",): 
     336                socketgroup=a 
    289337        if o in ("--nouser",): 
    290338                drivers.append(NoUserDriver()) 
     
    295343        if o in ("--real",): 
    296344                drivers.append(RealDriver()) 
     345        if o in ("--map",): 
     346                drivers.append(FileMapping(a)) 
    297347        if o in ("--static",): 
    298348                drivers.append(StaticDriver(a))