Created
July 19, 2015 08:22
-
-
Save nansenat16/d871b480475561533ead to your computer and use it in GitHub Desktop.
Import Linux account,passwd transform to LDIF format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/////////////////////////////////////////////////////////// | |
// | |
// Import Linux account,passwd transform to LDIF format. | |
// Ex: move account from openwebmail to Zimbra Mail System | |
// | |
// 2015/06/21 by nansenat16 | |
// | |
/////////////////////////////////////////////////////////// | |
$template=<<<EOF | |
dn: uid=%s,ou=people,dc=zimbra,dc=llc,dc=com,dc=tw | |
zimbraMailQuota: %s | |
zimbraPrefTimeZoneId: Asia/Taipei | |
description: %s | |
objectClass: inetOrgPerson | |
objectClass: zimbraAccount | |
objectClass: amavisAccount | |
zimbraId: %s | |
zimbraCreateTimestamp: 20150615153232Z | |
zimbraAccountStatus: active | |
zimbraMailHost: zimbra.llc.com.tw | |
zimbraMailTransport: lmtp:zimbra.llc.com.tw:7025 | |
zimbraMailStatus: enabled | |
zimbraMailDeliveryAddress: %[email protected] | |
mail: %[email protected] | |
cn: %s | |
sn: %s | |
uid: %s | |
zimbraPasswordModifiedTime: 20150615153232Z | |
userPassword: %s | |
EOF; | |
$file_uid=file('/etc/passwd'); | |
$file_pwd=file('/etc/shadow'); | |
$skip_account=array('admin','nfsnobody'); | |
$mail_Quota=(5000 * 1024 * 1024);//5GB | |
//////////////////////////////////// | |
// | |
// End Setting | |
// | |
//////////////////////////////////// | |
$acc_list=array(); | |
for($n=0;$n<count($file_uid);$n++){ | |
$uid_tmp=explode(':',$file_uid[$n]); | |
//print_r($uid_tmp); | |
if($uid_tmp[2]>'500'){//UID over 500 | |
if(in_array(str_replace('!', '', $uid_tmp[0]),$skip_account)){continue;} | |
$acc_list[str_replace('!', '', $uid_tmp[0])]=array('uid'=>str_replace('!', '', $uid_tmp[0]),'memo'=>trim(iconv('big5','utf8',$uid_tmp[4]))); | |
} | |
} | |
//print_r($acc_list); | |
//print_r($file_pwd); | |
for($n=0;$n<count($file_pwd);$n++){ | |
$pwd_tmp=explode(':', $file_pwd[$n]); | |
//print_r($pwd_tmp); | |
if(array_key_exists($pwd_tmp[0], $acc_list)){ | |
$acc_list[$pwd_tmp[0]]['pwd']=$pwd_tmp[1]; | |
$acc_list[$pwd_tmp[0]]['uuid']=gen_uuid(); | |
$acc_list[$pwd_tmp[0]]['quota']=$mail_Quota; | |
if(strlen($acc_list[$pwd_tmp[0]]['memo'])==0){ | |
$acc_list[$pwd_tmp[0]]['memo']='LDAP Import'; | |
} | |
} | |
} | |
//print_r($acc_list); | |
foreach ($acc_list as $uid => $val) { | |
echo sprintf($template,$uid,$val['quota'],$val['memo'],$val['uuid'],$uid,$uid,$uid,$uid,$uid,'{CRYPT}'.$val['pwd']); | |
echo "\n\n"; | |
} | |
function gen_uuid() { | |
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', | |
// 32 bits for "time_low" | |
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), | |
// 16 bits for "time_mid" | |
mt_rand( 0, 0xffff ), | |
// 16 bits for "time_hi_and_version", | |
// four most significant bits holds version number 4 | |
mt_rand( 0, 0x0fff ) | 0x4000, | |
// 16 bits, 8 bits for "clk_seq_hi_res", | |
// 8 bits for "clk_seq_low", | |
// two most significant bits holds zero and one for variant DCE1.1 | |
mt_rand( 0, 0x3fff ) | 0x8000, | |
// 48 bits for "node" | |
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) | |
); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment