misc/scripts/chpasswd.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 14 Oct 2011 13:36:39 +0200
changeset 7956 db49658b2812
parent 7250 fe27390bf2f8
child 8483 4ba11607d84a
permissions -rw-r--r--
add missing imports
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7250
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     1
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     3
#
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     4
# This file is part of CubicWeb.
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     5
#
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
     9
# any later version.
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    10
#
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    14
# details.
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    15
#
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    18
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    19
import sys
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    20
import getpass
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    21
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    22
from cubicweb import Binary
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    23
from cubicweb.server.utils import crypt_password
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    24
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    25
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    26
if __args__:
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    27
    login = __args__.pop()
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    28
else:
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    29
    login = raw_input("login ? ")
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    30
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    31
rset = rql('Any U WHERE U is CWUser, U login %(login)s', {'login': login})
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    32
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    33
if len(rset) != 1:
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    34
    sys.exit("user '%s' does not exist!" % login)
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    35
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    36
pass1 = getpass.getpass(prompt='Enter new password ? ')
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    37
pass2 = getpass.getpass(prompt='Confirm ? ')
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    38
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    39
if pass1 != pass2:
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    40
    sys.exit("passwords don't match!")
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    41
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    42
crypted = crypt_password(pass1)
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    43
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    44
cwuser = rset.get_entity(0,0)
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    45
cwuser.set_attributes(upassword=Binary(crypted))
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    46
commit()
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    47
fe27390bf2f8 [book] fix documentation + add chpasswd.py script
Julien Jehannet <julien.jehannet@logilab.fr>
parents:
diff changeset
    48
print("password updated.")