0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

パスワード入力画面を作る

「chpasswd コマンドを使ってパスワードを非対話的に変更する」
https://qiita.com/nanbuwks/items/9c860f658799ba73ae4b

に、

「TkEasyGUI を触ってみる」
https://qiita.com/nanbuwks/items/260276047aa807374433

の皮をかぶせてかんたんな GUI 画面を作ってみます。

環境

Debian Testing(Debian 13: 2024/07/06 時点)

実行画面

image.png

パスワードを2箇所に入れてOKを押すと、実行しているユーザのパスワードが更新されます。

プログラム

#!/usr/bin/env python3
#import PySimpleGUI as sg
import TkEasyGUI as sg
from subprocess import Popen, PIPE, STDOUT
import os
username = os.getlogin()
layout = [
           [sg.Text("Password:")],[ sg.Input("",size=(25,1),password_char="*",key="-password-")],
           [sg.Text("Password(retype):")],[ sg.Input("",size=(25,1),password_char="*",key="-password2-")],
#          for PySimpleGUI
#           [sg.Checkbox("show password", enable_events=True, key="-toggle_password-")],
           [sg.Text("",key="-message-")],
           [sg.Button('Ok'), sg.Button('Cancel')],
         ]

window = sg.Window('Pleasae setup Password', layout)

# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()

    # if user closes window or clicks cancel
    if event == sg.WIN_CLOSED or event == 'Cancel':
        break

    elif event == "Ok":
       if (values["-password-"]==values["-password2-"]):
#         print (username+":"+values["-password-"]+"\n")
          p = Popen(['sudo chpasswd'], stdout=PIPE, stdin=PIPE, stderr=PIPE, text=True, shell=True)
          p.stdin.write(username+":"+values["-password-"]+"\n")
          p.stdin.close()
          break
       else:
          window["-message-"].update("does not natch!")
          window["-password-"].update("")
          window["-password2-"].update("")

    elif event == "-toggle_password-":
        if values["-toggle_password-"]:
            window["-password-"].update(password_char="")
            window["-password2-"].update(password_char="")
        else:
            window["-password-"].update(password_char="*")
            window["-password2-"].update(password_char="*")

window.close()

パスワードの受け渡し

パイプを開き、 chpasswd に STDIN で受け渡しするようにしています。

パスワード表示確認機能は断念

[sg.Checkbox("show password", enable_events=True, key="-toggle_password-")],

として、パスワードの表示確認機能をつけようとしましたが、TkEasyGUI には以下の機能は使えないようで、コメントアウトしています。

 window["-password-"].update(password_char="")

問題点

パスワードなし sudo が有効である必要があります。

  • 内部で sudo chpasswd を実行しています。
  • このプログラムの想定は Live CD 環境で、それCD だとパスワードなし sudo が有効になっています。
  • しかしそうでない環境の場合はパスワードなし sudo を有効にするか、このプログラム自体を sudo で動かすようにして、ユーザ名を外部から取得するなどの対応が必要になります。

パスワードの変更結果をチェックしていません。

  • 有効なパスワードかどうかチェックしていません。
  • chpasswd の実行結果をパイプで取得して、正常終了でなければ警告を出すなどの処理が必要になります。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?