2018年10月3日 星期三

新增Windows AD user by ldapadd

沒想到這麼快又來自虐了。

今天的任務是用ldapadd來新增AD user。(其實是自找麻煩)

場景:有個AD server且支援ldaps,有個Linux server。

首先要生出來一個ldif檔,叫他aduser.ldif好了,這個檔有點機車....

dn: CN=$user_id, OU=your_ou, DC=your_dc
changetype: add
objectClass: top
objectClass: person
objectClass: organizationalPerson
objectClass: user
cn: $user_id
sn::$sn                       #這是姓
givenName::$gn       #這是名
displayName::$fullname #這是姓名
mail: $mail
description::$fullname
profilePath: \\\\ad_server_name\\userprofile$\\user_id  #指定user profile的路徑
sAMAccountName: $user_id      #看後面說明
userPrincipalName: $user_id@cloud.xxx.edu.tw  #看後面說明

dn: CN=$user_id, OU=your_ou, DC=your_dc
changetype: modify
replace: unicodePwd
unicodePwd::$pass

dn: CN=$user_id, OU=your_ou, DC=your_dc
changetype: modify
replace: userAccountControl
userAccountControl: 66048  #這個鬼數字自己google

對了,不是你眼殘,有些attribute是兩個::,以本人粗淺的認知,如果值是編碼過的,就用兩個::,那不用編碼的怎麼辦,啊就一個:啊!怎麼樣,厲害的廢話嚇到你了吧!

那麼怎麼編碼呢?好孩子問到重點:

密碼的比較麻煩,要用UTF-16LE編碼,再用base64編碼,變成密碼。
pass=`echo -n \""$pass"\"|iconv --to utf-16le|base64`
echo "Encoded Passowrd is $pass"

例:
# echo -n \""GuessWhat"\"|iconv --to utf-16le|base64
IgBHAHUAZQBzAHMAVwBoAGEAdAAiAA==

如果只是一般字串
sn=`echo $sn | iconv -f UTF8 | base64`

例:
sn=`echo 中文嘿嘿嘿 | iconv -f UTF8 | base64`
echo $sn
5Lit5paH5Zi/5Zi/5Zi/Cg==

順帶提示您重點中的重點,sn是姓,我相信不會有人的姓是中文嘿嘿嘿。

## 執行新增
ldapadd -x -H ldap_server -D "CN=Administrator,CN=Users,DC=your_dc" -w GodDamnPassowrd -f aduser.ldif

adding new entry "CN=123123,OU=your_ou, DC=your_dc"

modifying entry "CN=123123,OU=your_ou, DC=your_dc"

modifying entry "CN=123123,OU=your_ou, DC=your_dc"

收工。喊太早了,你應該去AD上面看看結果再說。


剛剛說到兩個attribute...

sAMAccountName: user_id
userPrincipalName: user_id@xxx.edu.tw

這兩個attribute會對應到AD上,帳戶=>內容 => 帳戶,裡的「使用者登入名稱」跟「使用者登入名稱(Windows 2000前版)」

那麼這兩個attribute有甚麼鬼屁用?以沒有設定這兩個attribute的情況下來測試一下:

moodle => 不受影響
vmware vdi => 無法登入

設定之後,VDI就可以正確登入了。

廢話結束,謝謝收看,掌聲不用,下次不用再來沒關係。


2018年10月2日 星期二

ldapmodify 變更 windows AD user 密碼

環境:
1. Windows Server + AD
2. Ubuntu Server 沒有AD (廢話了一下)

本篇目的:用openldap的指令ldapmodify去修改那該死的AD user password(為什麼要搞死自己
呢?)

其實就是達到某種密碼同步啦!

前提:Windows AD必須支援ldaps,怎麼支援?你說說看,我為什麼要跟你說?

廢話不多說,開始:

#!/bin/bash
b64pass=`echo -n \""GodDamnPassword"\"|iconv --to utf-16le|base64`

echo "dn: CN=UserAccount, OU=YourOU, DC=YourDC" > test.ldif
echo "changetype: modify" >> test.ldif
echo "replace: unicodePwd" >> test.ldif
echo "unicodePwd::$b64pass" >> test.ldif

ldapmodify -H ldaps://AD_Server  -D "CN=Administrator,CN=Users,DC=YourDC" -W -f test.ldif

執行時會問你LDAP PASSWORD,請乖乖輸入AD的Administrator密碼,若你有好狗運,會出現以下:


root@server:/path/to/blablavla # ./chpass.sh 
Enter LDAP Password:
modifying entry "CN=UserAccount, OU=YourOU, DC=YourDC" 

然後...還然後?趕快去試試看啊!這還要我教?

OR...

#b64pass=`echo -n \""GodDamnPassword"\"|iconv --to utf-16le|base64`
#ldapmodify -H ldaps://AD.Server -D "CN=Administrator,CN=Users,DC=YourDC" -W <<EOF
> dn: CN=UserAccount, OU=YourOU, DC=YourDC
> changetype: modify
> replace: unicodePwd
> unicodePwd::$b64pass
> EOF

其實這個unicodePwd還蠻該死的,google一下便知一堆G拔毛的鬼啊!