setpass.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/bash
  2. TYPE="$1"
  3. USER="$2"
  4. PASS="$3"
  5. if [ "$USER" = "" -o "$PASS" = "" -o "$TYPE" = "" ]; then
  6. echo "syntax: $0 user-type login pass" >&2
  7. exit 1
  8. fi
  9. if [ "$TYPE" != "student" -a "$TYPE" != "teacher" ]; then
  10. echo "Unknown user type: $TYPE" >&2
  11. exit 4
  12. fi
  13. TYPE="${TYPE//[\/]/}"
  14. USER="${USER//[\/]/}"
  15. PASS="${PASS//[\/]/}"
  16. TMPDIR="$(mktemp -d)"
  17. if [ "$TMPDIR" = "" ]; then
  18. echo "error: cannot create temporary directory" >&2
  19. exit 2
  20. fi
  21. LDAP_LOGINPASS=($(awk -F '"' '{if ($1 == "masterDN=") {LOGIN=$2} if ($1 == "masterPw=") {PASS=$2}} END {print LOGIN"\t"PASS}' < /etc/smbldap-tools/smbldap_bind.conf))
  22. LDAP_LOGIN="${LDAP_LOGINPASS[0]}"
  23. LDAP_PASS="${LDAP_LOGINPASS[1]}"
  24. if [ "$LDAP_LOGIN" = "" -o "$LDAP_PASS" = "" ]; then
  25. echo "error: cannot parse ldap login/pass from /etc/smbldap-tools/smbldap_bind.conf" >&2
  26. exit 3
  27. fi
  28. sed -e "s/@USER@/$USER/g" -e "s/@PASS@/$PASS/g" < /usr/local/share/ldap-workarounds/samba-adduser-"$TYPE"-fix-add.ldif > "$TMPDIR"/fix.ldif
  29. ldapmodify -x -D "$LDAP_LOGIN" -w "$LDAP_PASS" -f "$TMPDIR"/fix.ldif -h ldap.ut.mephi.ru 2>/dev/null
  30. sed -e "s/@USER@/$USER/g" -e "s/@PASS@/$PASS/g" < /usr/local/share/ldap-workarounds/samba-adduser-"$TYPE"-fix-replace.ldif > "$TMPDIR"/fix.ldif
  31. ldapmodify -x -D "$LDAP_LOGIN" -w "$LDAP_PASS" -f "$TMPDIR"/fix.ldif -h ldap.ut.mephi.ru
  32. echo "$PASS" | smbldap-passwd -p "$USER"
  33. #smbpasswd "$USER" -w "$PASS"
  34. rm -f "$TMPDIR"/{,.}* 2>/dev/null
  35. rmdir "$TMPDIR"
  36. exit 0