Debian Codegraph @

support/nameconvert

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 # This implements a simple protocol to do user & group conversions between
4 # names & ids. All input and output consists of simple strings with a
5 # terminating newline.
6 #
7 # The requests can be:
8 #
9 # uid ID_NUM\n -> NAME\n
10 # gid ID_NUM\n -> NAME\n
11 # usr NAME\n -> ID_NUM\n
12 # grp NAME\n -> ID_NUM\n
13 #
14 # An unknown ID_NUM or NAME results in an empty return value.
15 #
16 # This is used by an rsync daemon when configured with the "name converter" and
17 # (often) "use chroot = true". While this converter uses real user & group
18 # lookups you could change it to use any mapping idiom you'd like.
19
20 import sys, argparse, pwd, grp
21
22 def main():
23 for line in sys.stdin:
24 try:
25 req, arg = line.rstrip().split(' ', 1)
26 except:
27 req = None
28 try:
29 if req == 'uid':
30 ans = pwd.getpwuid(int(arg)).pw_name
31 elif req == 'gid':
32 ans = grp.getgrgid(int(arg)).gr_name
33 elif req == 'usr':
34 ans = pwd.getpwnam(arg).pw_uid
35 elif req == 'grp':
36 ans = grp.getgrnam(arg).gr_gid
37 else:
38 print("Invalid request", file=sys.stderr)
39 sys.exit(1)
40 except KeyError:
41 ans = ''
42 print(ans, flush=True)
43
44
45 if __name__ == '__main__':
46 parser = argparse.ArgumentParser(description="Convert users & groups between names & numbers for an rsync daemon.")
47 args = parser.parse_args()
48 main()
49
50 # vim: sw=4 et