How do I add users using Python scripts on a Linux machine

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramdas

    #1

    How do I add users using Python scripts on a Linux machine

    How do I add users using Python scripts on a Linux machine?

    Someone has a script?

  • Daniel Klein

    #2
    Re: How do I add users using Python scripts on a Linux machine

    On 1 Jan 2007 11:33:42 -0800, "Ramdas" <ramdaz@gmail.c omwrote:
    >How do I add users using Python scripts on a Linux machine?
    >
    >Someone has a script?
    This should be as easy as something like:

    os.system("/usr/sbin/useradd -m -d /home/newuser -s /bin/ksh")

    Dan

    Comment

    • Hari Sekhon

      #3
      Re: How do I add users using Python scripts on a Linux machine

      That is shell scripting with a python layer on top. Is there a
      specific reason you have to use python? Why not just use shell, that's
      what it's designed for? Unless you have some complex maths/networking
      requirement or something on top.

      -h

      On 01/01/07, Daniel Klein <danielkleinad@ gmail.comwrote:
      On 1 Jan 2007 11:33:42 -0800, "Ramdas" <ramdaz@gmail.c omwrote:
      >
      How do I add users using Python scripts on a Linux machine?

      Someone has a script?
      >
      This should be as easy as something like:
      >
      os.system("/usr/sbin/useradd -m -d /home/newuser -s /bin/ksh")
      >
      Dan
      --

      >

      --
      Hari Sekhon

      Comment

      • Ramdas

        #4
        Re: How do I add users using Python scripts on a Linux machine

        Well,

        I need to add users from a web interface for a web server, which runs
        only Python. I need to add users, set quotas and in future even look at
        managing ip tables to limit bandwidth.

        I know os.system(), but this has to be done through a form entry
        through a web interface.

        Anyways thanks, do advise if there more pythonic solutions

        Ramdas


        Hari Sekhon wrote:
        That is shell scripting with a python layer on top. Is there a
        specific reason you have to use python? Why not just use shell, that's
        what it's designed for? Unless you have some complex maths/networking
        requirement or something on top.
        >
        -h
        >
        On 01/01/07, Daniel Klein <danielkleinad@ gmail.comwrote:
        On 1 Jan 2007 11:33:42 -0800, "Ramdas" <ramdaz@gmail.c omwrote:
        >How do I add users using Python scripts on a Linux machine?
        >
        >Someone has a script?
        This should be as easy as something like:

        os.system("/usr/sbin/useradd -m -d /home/newuser -s /bin/ksh")

        Dan
        --
        >
        >
        --
        Hari Sekhon

        Comment

        • Ivan Voras

          #5
          Re: How do I add users using Python scripts on a Linux machine

          Ramdas wrote:
          Well,
          >
          I need to add users from a web interface for a web server, which runs
          only Python. I need to add users, set quotas and in future even look at
          managing ip tables to limit bandwidth.
          >
          I know os.system(), but this has to be done through a form entry
          through a web interface.
          >
          Anyways thanks, do advise if there more pythonic solutions
          What you're looking for is actually a pretty complex thing. You *could*
          in theory manage /etc/passwd (and its "shadow" file) - you can find
          crypto primitives like MD5 and DES on the 'net, but note that you must
          run your script under the 'root' account in order to write (and even
          read!) the passwd database. The same goes for using os.system and the
          built-in OS utility. Be aware of security implications if you're running
          your web server under the root account.

          Comment

          • Ravi Teja

            #6
            Re: How do I add users using Python scripts on a Linux machine


            Ivan Voras wrote:
            Ramdas wrote:
            Well,

            I need to add users from a web interface for a web server, which runs
            only Python. I need to add users, set quotas and in future even look at
            managing ip tables to limit bandwidth.

            I know os.system(), but this has to be done through a form entry
            through a web interface.

            Anyways thanks, do advise if there more pythonic solutions
            >
            What you're looking for is actually a pretty complex thing. You *could*
            in theory manage /etc/passwd (and its "shadow" file) - you can find
            crypto primitives like MD5 and DES on the 'net, but note that you must
            run your script under the 'root' account in order to write (and even
            read!) the passwd database. The same goes for using os.system and the
            built-in OS utility. Be aware of security implications if you're running
            your web server under the root account.
            How about invoking scripts with SUID root set?

            Comment

            • Jan Dries

              #7
              Re: How do I add users using Python scripts on a Linux machine

              Ivan Voras wrote:
              Ramdas wrote:
              >Well,
              >>
              >I need to add users from a web interface for a web server, which runs
              >only Python. I need to add users, set quotas and in future even look at
              >managing ip tables to limit bandwidth.
              >>
              >I know os.system(), but this has to be done through a form entry
              >through a web interface.
              >>
              >Anyways thanks, do advise if there more pythonic solutions
              >
              What you're looking for is actually a pretty complex thing. You *could*
              in theory manage /etc/passwd (and its "shadow" file) - you can find
              crypto primitives like MD5 and DES on the 'net, but note that you must
              run your script under the 'root' account in order to write (and even
              read!) the passwd database. The same goes for using os.system and the
              built-in OS utility. Be aware of security implications if you're running
              your web server under the root account.
              A solution that is both more pythonic and avoids the problems listed
              above would be to migrate user management from /etc/passwd to an LDAP
              (though pam_ldap). That's the approach I took in a similar situation.
              Sure, it adds the overhead of setting up and running an LDAP, but
              managing users and their quota through python_ldap is much cleaner and
              more flexible than doing so using os.system(), certainly from within a
              web application.
              That doesn't alter the fact though that security must be properly
              considered in any application that can add users.

              Regards,
              Jan

              Comment

              • Sebastian 'lunar' Wiesner

                #8
                Re: How do I add users using Python scripts on a Linux machine

                Ravi Teja <webraviteja@gm ail.comtyped
                >
                Ivan Voras wrote:
                >Ramdas wrote:
                Well,
                >
                I need to add users from a web interface for a web server, which
                runs only Python. I need to add users, set quotas and in future
                even look at managing ip tables to limit bandwidth.
                >
                I know os.system(), but this has to be done through a form entry
                through a web interface.
                >
                Anyways thanks, do advise if there more pythonic solutions
                >>
                >What you're looking for is actually a pretty complex thing. You
                >*could* in theory manage /etc/passwd (and its "shadow" file) - you
                >can find crypto primitives like MD5 and DES on the 'net, but note
                >that you must run your script under the 'root' account in order to
                >write (and even read!) the passwd database. The same goes for using
                >os.system and the built-in OS utility. Be aware of security
                >implications if you're running your web server under the root
                >account.
                >
                How about invoking scripts with SUID root set?
                Linux seems to ignore SUID bit on scripts:

                [lunar@nargond]-[17:03:23] >~/test
                --cat uidtest.py
                #!/usr/bin/python
                import os

                print 'uid:', os.getuid()
                print 'effective uid:', os.geteuid()
                os.system('whoa mi')

                [lunar@nargond]-[17:03:28] >~/test
                --ls -l uidtest.py
                -rwsr-xr-x 1 root root 112 2007-01-02 17:03 uidtest.py

                [lunar@nargond]-[17:03:46] >~/test
                --/home/lunar/test/uidtest.py
                uid: 1000
                effective uid: 1000
                lunar

                Anyway, you should definitely think about security issues. Not all
                people out there are friendly...

                --
                Freedom is always the freedom of dissenters.
                (Rosa Luxemburg)

                Comment

                • Carsten Haese

                  #9
                  Re: How do I add users using Python scripts on a Linux machine

                  On Tue, 2007-01-02 at 17:17 +0100, Sebastian 'lunar' Wiesner wrote:
                  Ravi Teja <webraviteja@gm ail.comtyped
                  >

                  Ivan Voras wrote:
                  Ramdas wrote:
                  Well,

                  I need to add users from a web interface for a web server, which
                  runs only Python. I need to add users, set quotas and in future
                  even look at managing ip tables to limit bandwidth.

                  I know os.system(), but this has to be done through a form entry
                  through a web interface.

                  Anyways thanks, do advise if there more pythonic solutions
                  >
                  What you're looking for is actually a pretty complex thing. You
                  *could* in theory manage /etc/passwd (and its "shadow" file) - you
                  can find crypto primitives like MD5 and DES on the 'net, but note
                  that you must run your script under the 'root' account in order to
                  write (and even read!) the passwd database. The same goes for using
                  os.system and the built-in OS utility. Be aware of security
                  implications if you're running your web server under the root
                  account.
                  How about invoking scripts with SUID root set?
                  >
                  Linux seems to ignore SUID bit on scripts:
                  I don't think that that has anything to do with Linux or not. The script
                  is not the actual executable, hence its suid bit is irrelevant. You'd
                  have to set the suid bit on the python executable, but that would affect
                  all python scripts, which is probably bad.

                  -Carsten


                  Comment

                  • Sebastian 'lunar' Wiesner

                    #10
                    Re: How do I add users using Python scripts on a Linux machine

                    Carsten Haese <carsten@uniqsy s.comtyped
                    On Tue, 2007-01-02 at 17:17 +0100, Sebastian 'lunar' Wiesner wrote:
                    >Ravi Teja <webraviteja@gm ail.comtyped
                    >>
                    >
                    Ivan Voras wrote:
                    >Ramdas wrote:
                    Well,
                    >
                    I need to add users from a web interface for a web server, which
                    runs only Python. I need to add users, set quotas and in future
                    even look at managing ip tables to limit bandwidth.
                    >
                    I know os.system(), but this has to be done through a form entry
                    through a web interface.
                    >
                    Anyways thanks, do advise if there more pythonic solutions
                    >>
                    >What you're looking for is actually a pretty complex thing. You
                    >*could* in theory manage /etc/passwd (and its "shadow" file) - you
                    >can find crypto primitives like MD5 and DES on the 'net, but note
                    >that you must run your script under the 'root' account in order to
                    >write (and even read!) the passwd database. The same goes for
                    >using os.system and the built-in OS utility. Be aware of security
                    >implications if you're running your web server under the root
                    >account.
                    >
                    How about invoking scripts with SUID root set?
                    >>
                    >Linux seems to ignore SUID bit on scripts:
                    >
                    I don't think that that has anything to do with Linux or not. The
                    script is not the actual executable, hence its suid bit is irrelevant.
                    I don't think so. From what I know, the script is passed as executable
                    to the kernel loader, which interprets the shebang and feeds the script
                    through the correct interpreter. So the kernel loader sees the script
                    itself as executable instead of the interpreter binary. I've heard of
                    other Unix systems, which handle this differently (meaning that the
                    SUID bit on scripts has an effect), but I may be wrong.
                    You'd have to set the suid bit on the python executable, but that
                    would affect all python scripts, which is probably bad.
                    It _is_ bad!

                    --
                    Freedom is always the freedom of dissenters.
                    (Rosa Luxemburg)

                    Comment

                    • Ravi Teja

                      #11
                      Re: How do I add users using Python scripts on a Linux machine

                      How about invoking scripts with SUID root set?
                      >
                      Linux seems to ignore SUID bit on scripts:
                      Yes. My bad. The work around was to use native launchers. I don't
                      remember the details. Perhaps with the interpreter embedded to launch
                      it in-process and to hard code the script paths (or at least a config
                      file/script pointing to them) for security.
                      Anyway, you should definitely think about security issues. Not all
                      people out there are friendly...
                      I agree. SUID is often risky.

                      Web applications such as webmin that do administrative functions
                      through a web interface require extra precautions for security such as
                      restricting access to specific IPs.

                      Comment

                      • Piet van Oostrum

                        #12
                        Re: How do I add users using Python scripts on a Linux machine

                        >>>>Sebastian 'lunar' Wiesner <basti.wiesner@ gmx.net(SW) wrote:
                        >SWLinux seems to ignore SUID bit on scripts:
                        The reason is that obeying SUID bits on scripts would be a security risk.
                        --
                        Piet van Oostrum <piet@cs.uu.n l>
                        URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C 4]
                        Private email: piet@vanoostrum .org

                        Comment

                        • Tim Roberts

                          #13
                          Re: How do I add users using Python scripts on a Linux machine

                          "Ramdas" <ramdaz@gmail.c omwrote:
                          >
                          >I need to add users from a web interface for a web server, which runs
                          >only Python. I need to add users, set quotas and in future even look at
                          >managing ip tables to limit bandwidth.
                          >
                          >I know os.system(), but this has to be done through a form entry
                          >through a web interface.
                          >
                          >Anyways thanks, do advise if there more pythonic solutions
                          os.system is perfectly Pythonic, and can be executed from a CGI script. The
                          challenge is becoming root, which is necessary to do what you ask. You can
                          write a simple C program that is setuid root that calls your script for
                          you.
                          --
                          Tim Roberts, timr@probo.com
                          Providenza & Boekelheide, Inc.

                          Comment

                          • Sebastian 'lunar' Wiesner

                            #14
                            Re: How do I add users using Python scripts on a Linux machine

                            Piet van Oostrum <piet@cs.uu.nlt yped
                            >>>>>Sebastia n 'lunar' Wiesner <basti.wiesner@ gmx.net(SW) wrote:
                            >
                            >>SWLinux seems to ignore SUID bit on scripts:
                            >
                            The reason is that obeying SUID bits on scripts would be a security
                            risk.
                            I don't see a problem with SUID on scripts. If you restrict write access
                            to the owner, modification is hardly possible.
                            However, if you allow world-wide write access to your binaries and
                            scripts, both can easily be modified...

                            --
                            Freedom is always the freedom of dissenters.
                            (Rosa Luxemburg)

                            Comment

                            • Ivan Voras

                              #15
                              Re: How do I add users using Python scripts on a Linux machine

                              Sebastian 'lunar' Wiesner wrote:
                              Carsten Haese <carsten@uniqsy s.comtyped
                              >I don't think that that has anything to do with Linux or not. The
                              >script is not the actual executable, hence its suid bit is irrelevant.
                              >
                              I don't think so. From what I know, the script is passed as executable
                              to the kernel loader, which interprets the shebang and feeds the script
                              through the correct interpreter. So the kernel loader sees the script
                              itself as executable instead of the interpreter binary. I've heard of
                              other Unix systems, which handle this differently (meaning that the
                              SUID bit on scripts has an effect), but I may be wrong.
                              Yes, the kernel parses #! but the suid-ness is still controlled by the
                              target interpreter (i.e. python executable). At least BSD systems also
                              behave this way.

                              Comment

                              Working...