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
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
14 # An unknown ID_NUM or NAME results in an empty return value.
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.
20 import sys, argparse, pwd, grp
23 for line in sys.stdin:
25 req, arg = line.rstrip().split(' ', 1)
30 ans = pwd.getpwuid(int(arg)).pw_name
32 ans = grp.getgrgid(int(arg)).gr_name
34 ans = pwd.getpwnam(arg).pw_uid
36 ans = grp.getgrnam(arg).gr_gid
38 print("Invalid request", file=sys.stderr)
42 print(ans, flush=True)
45 if __name__ == '__main__':
46 parser = argparse.ArgumentParser(description="Convert users & groups between names & numbers for an rsync daemon.")
47 args = parser.parse_args()