aboutsummaryrefslogtreecommitdiff
path: root/ca/rpkic
blob: 2e215095cde30acc5f1d7c9b2d203994d860c736 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python

# Using a Python script to run sudo to run a Python script is a bit
# silly, but it lets us use rpki.autoconf to locate sudo, lets us
# avoid needing a custom setuid wrapper, lets us avoid another pass
# through the adventures of shell quoting and tokenization, and
# generally is just a lot simpler to implement correctly.
#
# OK, it's probably a few milliseconds slower.  Big deal.

if __name__ == "__main__":

    import os
    import pwd
    import sys
    import rpki.autoconf

    try:
        uid = pwd.getpwnam(rpki.autoconf.RPKI_USER).pw_uid
    except:
        uid = None

    if uid is None or uid == os.geteuid():
        import rpki.rpkic
        rpki.rpkic.main()

    else:
        try:
            argv = [rpki.autoconf.SUDO, "-u", rpki.autoconf.RPKI_USER, sys.executable]
            argv.extend(os.path.abspath(a) if i == 0 else a for i, a in enumerate(sys.argv))
            os.execv(argv[0], argv)
            sys.exit("rpkic startup failure, no exception so don't know why, sorry")

        except Exception as e:
            sys.exit("Couldn't exec sudo python rpkic: {!s}".format(e))