Advertisement
Guest User

Shell script + CGI

a guest
Sep 13th, 2011
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.80 KB | None | 0 0
  1. #!/bin/bash --norc
  2. #
  3.  
  4. # SEGURANÇA: Força o uso do caminho COMPLETO para os comandos do sistema.
  5. unset PATH
  6.  
  7. set -e
  8.  
  9. declare post usuario senha
  10.  
  11. # Método POST é passado para o CGI via STDIN
  12. post=$(/bin/sed -n '1,$p')
  13. # Pega os valores dos campos do formulário de acordo com a tag 'name'
  14. usuario=$(echo $post | /bin/sed 's/\(login=\)\(.*\)\(\&senha=.*\)/\2/;s/+/ /g')
  15. senha=$(echo $post | /bin/sed 's/\(.*&senha=\)\(.*\)\(\&submit=.*\)/\2/g')
  16.  
  17. # *** AVISO DE SEGURANÇA ***
  18. # Sempre, sempre e sempre!!! trate os dados fornecidos pelos usuários, principalmente
  19. # ao usar CGI. De preferência remova caracteres potencialmente perigosos como:
  20. # ; | & * $ < > ( ) `
  21.  
  22. echo -e "Content-Type: text/html; charset=utf-8\n\n"
  23.  
  24. if [ -z "$post" ]; then
  25.     # Formulario basico
  26.     /bin/cat <<-FIM
  27.     <html>
  28.         <head><title>CGI + Shell Script</title></head>
  29.         <body>
  30.             <form name="formulario" method="POST" action="">
  31.                 <table align="center">
  32.                     <tr><td>Login:&nbsp;</td><td><input type="text" size="30" name="login" id="login"></td><br>
  33.                     <tr><td>Senha:&nbsp;</td><td><input type="password" size="30" name="senha" id="senha"></td><br>
  34.                 </table>
  35.                 <p align="center"><input type="submit" name="submit" value="Alterar"></p>
  36.             </form>
  37.         </body>
  38.     </html>
  39.     FIM
  40. else
  41.     [ -z "$usuario" -o -z "$senha" ] && { echo "<strong>Preencha todos os campos!</strong>"; exit 1; }
  42.     # Altera a senha (Atente para as devidas permissoes!)
  43.     echo "${usuario}:$senha" | /usr/sbin/chpasswd
  44.     if [ $? -eq 0 ]; then
  45.         echo -e "$senha\n$senha" | /usr/bin/smbpasswd -s -a $usuario
  46.         if [ $? -eq 0 ]; then
  47.             echo "<p>Senha alterada com sucesso.</p>"
  48.             exit 0
  49.         else
  50.             echo "<strong>Erro ao cadastrar senha no Samba!</strong>"
  51.             exit 1
  52.         fi
  53.     else
  54.         echo "<strong>Nao foi possivel alterar sua senha!</strong>"
  55.         exit 1
  56.     fi
  57. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement