Changeset 48
- Timestamp:
- 02/12/04 15:55:33 (5 years ago)
- Files:
-
- trunk/spidentd.py (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/spidentd.py
r45 r48 38 38 def lookup(self,localport,localip,remoteport,remoteip): 39 39 return self.reply 40 41 class Mapping: 42 def __init__(self): 43 pass 44 def lookup(self,user): 45 return user 46 47 class 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) 40 58 41 59 class RealDriver(Driver): … … 61 79 remip=socket.htonl(long(remip,16))&0xffffffffL 62 80 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)66 81 if (localip,localport,remoteip,remoteport)!=(locip,locport,remip,remport): 67 82 continue … … 140 155 class SocketDriver(Driver): 141 156 def __init__(self,path): 157 global socketmode,socketgroup,user,group 142 158 self.socket=socket.socket(socket.AF_UNIX) 143 159 try: … … 146 162 pass 147 163 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) 148 176 self.socket.listen(1) 149 177 self.clients=[] … … 209 237 return 210 238 reply=None 239 mapping=None 211 240 for d in drivers: 241 if isinstance(d,Mapping): 242 mapping=d 243 continue 244 if not isinstance(d,Driver): 245 continue 212 246 r=d.lookup(local,localip,remote,remoteip) 213 print `r`214 247 if r is None: 215 248 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:"): 217 254 reply="%i,%i:%s" % (local,remote,r) 218 255 break … … 232 269 print >>sys.stderr,"Signal %i received, exiting." % (signum,) 233 270 234 235 271 def accept_connection(sock): 236 272 th=threading.Thread(target=input_thread,args=sock.accept()) … … 249 285 print " -i ADDR --ip=ADDR bind to IP address ADDR." 250 286 print " -p PORT --port=PORT bind to port PORT (default: 113)." 251 print " - pUSER --user=USER when started with uid=0, switch "287 print " -u USER --user=USER when started with uid=0, switch " 252 288 print " to user USER (default: 'nobody')" 253 289 print " -p GROUP --group=GROUP when started with uid=0, switch " 254 290 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)" 255 293 print "Drivers:" 256 294 print " --nouser always reply with NO-USER error." 257 295 print " --hidden always reply with HIDDN-USER error." 258 296 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" 260 301 print " --static=USER always reply with the same USER reply." 261 302 print " --socket=PATH listen on UNIX socket PATH for other servers" … … 268 309 port=113 269 310 drivers=[] 311 socketmode=0775 312 socketgroup=None 270 313 271 314 try: 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=", 273 316 "nouser","hidden","real","fail","static=","socket="]) 274 except: 317 except Exception,e: 318 print e 275 319 usage() 276 320 sys.exit(2) … … 287 331 if o in ("-g","--group"): 288 332 group=a 333 if o in ("--socketmode",): 334 socketmode=int(a,8) 335 if o in ("--socketgroup",): 336 socketgroup=a 289 337 if o in ("--nouser",): 290 338 drivers.append(NoUserDriver()) … … 295 343 if o in ("--real",): 296 344 drivers.append(RealDriver()) 345 if o in ("--map",): 346 drivers.append(FileMapping(a)) 297 347 if o in ("--static",): 298 348 drivers.append(StaticDriver(a))
