CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    May 2005
    Posts
    25

    executing the passwd command w/ exec or system (php)

    I'm trying to execute the passwd command using either exec or system in php. I've tried many different ways but cannot get any of them to work. It seems like "sudo passwd username\npassword\npassword\n" would work, but apparently the return key is different than a "\n"?

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: executing the passwd command w/ exec or system (php)

    What operating system are you working with?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Sep 2002
    Location
    All around the World
    Posts
    99

    Re: executing the passwd command w/ exec or system (php)

    Quote Originally Posted by peejavery
    What operating system are you working with?
    I think it's quite obvious that it's non-Windows OS when you need to execute sudo and passwd.

    @dave18285: I'm not sure whether this is possible or even are you suppose to do so. As allowing this command to run on the web will bring some risks with it if you get what i mean. But i try to see whether i can figure it out later.

  4. #4
    Join Date
    Sep 2002
    Location
    All around the World
    Posts
    99

    Re: executing the passwd command w/ exec or system (php)

    Quote Originally Posted by dave18285
    I'm trying to execute the passwd command using either exec or system in php. I've tried many different ways but cannot get any of them to work. It seems like "sudo passwd username\npassword\npassword\n" would work, but apparently the return key is different than a "\n"?
    correct me if i'm wrong....but if you need to pass in variables to "sudo passwd".... it should be something like this...

    PHP Code:
    <?php
        $pass 
    = <yourpassword>;
        
    $user = <yourusername>;
        
    $shell exec('echo -e  "' $pass '\n' $pass '\n' $pass '"| sudo passwd ' $user)
    ?>
    I haven't test out the code yet so i'm not sure whether you need to do a crypt on the pass in order for it to work as there are no returned values for this case. Another thing which i'm not sure is....i think you need to add "noody" as part of the sudoers in "/etc/sudoers".....otherwise you can't execute the command. Hope other gurus can correct me if i'm wrong here.
    Last edited by e_har; June 19th, 2006 at 12:48 AM.

  5. #5
    Join Date
    May 2002
    Posts
    10,943

    Re: executing the passwd command w/ exec or system (php)

    Quote Originally Posted by e_har
    I think it's quite obvious that it's non-Windows OS when you need to execute sudo and passwd.
    Well, I know that. But there are many other operating systems out there. It could be Mac OS, which is what I use, or it could be Linux. That is why it is a valid question. Mac OS X and Linux are very different.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  6. #6
    Join Date
    May 2005
    Posts
    25

    Re: executing the passwd command w/ exec or system (php)

    Hey thanks for the help. I'm using Fedora 4...I tried the above code, but still haven't been able to get it to work. I tried crypting the password and have also tried it uncrypted. I also tried putting in \r's instead of \n's thinking that maybe it wanted a carrage return...any other tips would be greatly appreciated.
    Dave

  7. #7
    Join Date
    May 2002
    Posts
    10,943

    Re: executing the passwd command w/ exec or system (php)

    If you were using the Fedora 4 console, what would be the exact way that you would type it?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  8. #8
    Join Date
    May 2005
    Posts
    25

    Re: executing the passwd command w/ exec or system (php)

    if i wanted to set the password to "1234" for a user called "user1234" i would type:

    passwd user1234
    1234
    1234

    which would look like this on the console:
    [root@admin ~]# passwd user1234
    Changing password for user user1234.
    New UNIX password:
    BAD PASSWORD: it is too short
    Retype new UNIX password:
    passwd: all authentication tokens updated successfully.

  9. #9
    Join Date
    May 2002
    Posts
    10,943

    Re: executing the passwd command w/ exec or system (php)

    Wait, after you type a command, then Fedora will ask you for the password? The executing commands can only execute one line commands. They do not wait for a response.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  10. #10
    Join Date
    May 2005
    Posts
    25

    Re: executing the passwd command w/ exec or system (php)

    Seems like theres gotta be someway to do this though. What I'm trying to do is allow users to change their email passwords. Could this be done with something else maybe like a perl script?

  11. #11
    Join Date
    Sep 2002
    Location
    All around the World
    Posts
    99

    Re: executing the passwd command w/ exec or system (php)

    Quote Originally Posted by dave18285
    if i wanted to set the password to "1234" for a user called "user1234" i would type:

    passwd user1234
    1234
    1234

    which would look like this on the console:
    [root@admin ~]# passwd user1234
    Changing password for user user1234.
    New UNIX password:
    BAD PASSWORD: it is too short
    Retype new UNIX password:
    passwd: all authentication tokens updated successfully.
    Pardon me if i'm wrong here but the example here you're trying to show is using your root to change password....this will definitely work but if you use "sudo passwd <username>".....it won't work for a web....

    Firstly, the user changing the password via the script will be termed "nobody" and this user is not in the list of "/etc/sudoers" so it can't change password via "sudo".

    Secondly, if you use my above sample and try it on your console with users that have access to sudo...something like this
    echo -e "<newpasspassword>\n<newpassword>\n" | sudo passwd <username>

    It will definitely work but if you issue \r\n....it won't work...i'm not sure why as i'm not a Linux guru.

    So what you need to find out is how to make your users able to execute "sudo" command via the web. Do post it here if you managed to find out as i feel it'll be helpful to others if they want to do something similar.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured