adduser.sh 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. smbldap-useradd "$USER"
  29. 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
  30. ldapmodify -x -D "$LDAP_LOGIN" -w "$LDAP_PASS" -f "$TMPDIR"/fix.ldif -h ldap.ut.mephi.ru
  31. 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
  32. ldapmodify -x -D "$LDAP_LOGIN" -w "$LDAP_PASS" -f "$TMPDIR"/fix.ldif -h ldap.ut.mephi.ru
  33. echo "$PASS" | smbldap-passwd -p "$USER"
  34. #smbpasswd "$USER" -w "$PASS"
  35. rm -f "$TMPDIR"/{,.}* 2>/dev/null
  36. rmdir "$TMPDIR"
  37. exit 0