Search code examples
shellhaskelliosamba

Haskell - System.Process gives an error when password is required


I am creating a Haskell program to automatically add a user to the system and then add it to samba, but it asks password, and I would like to use the same. I did not find any parameter we can use with smbclient to achieve such goal. What can I do?

Here is my code:

import System.Environment(getArgs)
import System.Process (createProcess, shell)

main = do
  (user:pass:_) <- getArgs
  putStrLn $ "Creating user " ++ user ++ " " ++ pass ++ "..."
  callCommand $ "useradd " ++ user
  callCommand $ "echo \"" ++ user ++ "\":\"" ++ pass ++ "\" | chpasswd"  
  putStrLn $ "User Created. Adding it to samba..."
  callCommand $ "smbpasswd -a " ++ user

callCommand = createProcess . shell

Result:

-bash# runghc test.hs testUser 12345
Creating user testUser 12345...
User Created. Adding it to samba...
chpasswd: (user testUser) pam_chauthtok() failed, error:
Authentication token manipulation error
chpasswd: (line 1, user testUser) password not changed
-bash# New SMB password:
Retype new SMB password:
Failed to add entry for user testUser.

Can I pass the pass argument received from user to the smbclient program (or press Enter automatically), and avoid this error?


Solution

  • Use readCreateProcess:

    readCreateProcess :: CreateProcess -> String -> IO String   
    

    The additional String will be passed to the command as standard input.