misc/scripts/chpasswd.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 11 Jun 2014 17:14:32 +0200
changeset 10365 21461f80f348
parent 9492 c7fc56eecd1a
permissions -rw-r--r--
[connection] remove ensure_cnx_set context manager uses It has been deprecated in a previous changeset and is now a no-op. Related to #2919309.
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:
9492
c7fc56eecd1a English typography
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents: 8483
diff changeset
    29
    login = raw_input("login? ")
7250
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
9492
c7fc56eecd1a English typography
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents: 8483
diff changeset
    36
pass1 = getpass.getpass(prompt='Enter new password? ')
c7fc56eecd1a English typography
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents: 8483
diff changeset
    37
pass2 = getpass.getpass(prompt='Confirm? ')
7250
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)
8483
4ba11607d84a [entity api] unify set_attributes / set_relations into a cw_set method. Closes #2423719
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7250
diff changeset
    45
cwuser.cw_set(upassword=Binary(crypted))
7250
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.")