-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
219 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package eldap | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
var ErrGroupAdd = errors.New("[FAIL] group name exist") | ||
var ErrGroupDel = errors.New("[FAIL] group name not found or more than one same name") | ||
var ErrGroupMod = errors.New("[FAIL] modify attr for group fail") | ||
|
||
var ErrTeamAdd = errors.New("[FAIL] team name exist") | ||
var ErrTeamDel = errors.New("[FAIL] team name not found or more than one same name") | ||
var ErrTeamMod = errors.New("[FAIL] modify attr for team fail") | ||
|
||
var ErrUserAdd = errors.New("[FAIL] user name exist") | ||
var ErrUserDel = errors.New("[FAIL] user name not dount or more than one same name") | ||
var ErrUserMod = errors.New("[FAIL] modify attr for user fail") |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package eldap | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
var MinNumber = 10000 | ||
var MaxNumber = 100000 | ||
|
||
func NewGidNumber(min int, max int) (int, error) { | ||
o := NewOption() | ||
for i := min; i < max; i++ { | ||
arr, err := o.SearchAllEntryDNByAttr(Group, "gidNumber", strconv.Itoa(i)) | ||
if err != nil { | ||
return -1, err | ||
} | ||
if len(arr) != 0 { | ||
continue | ||
} | ||
return i, nil | ||
} | ||
return -1, fmt.Errorf("not found unique gidNumber in %d and %d", min, max) | ||
|
||
} | ||
|
||
func NewPrivateGidNumber(min int, max int, gidNumber int) (int, error) { | ||
o := NewOption() | ||
arr, err := o.SearchAllEntryDNByAttr(Group, "gidNumber", strconv.Itoa(gidNumber)) | ||
if err != nil { | ||
return -1, err | ||
} | ||
if len(arr) != 0 { | ||
return NewGidNumber(min, max) | ||
} | ||
return gidNumber, nil | ||
} | ||
|
||
func NewUidNumber(min int, max int) (int, error) { | ||
o := NewOption() | ||
for i := min; i < max; i++ { | ||
arr, err := o.SearchAllEntryDNByAttr(User, "uidNumber", strconv.Itoa(i)) | ||
if err != nil { | ||
return -1, err | ||
} | ||
if len(arr) != 0 { | ||
continue | ||
} | ||
return i, nil | ||
} | ||
return -1, fmt.Errorf("not found unique uidNumber in %d and %d", min, max) | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#!/bin/bash | ||
# Author: Han Hao | ||
# Email: [email protected] | ||
# Only Run Once When You First Install OpenLDAP | ||
|
||
|
||
function usage(){ | ||
cat <<EOF | ||
$0 -D <domain> -u <username> -s <password> | ||
Example: $0 -D home.org -u admin -s 111111 | ||
EOF | ||
} | ||
|
||
|
||
while getopts "D:s:u:" opt;do | ||
case $opt in | ||
D) domain=$OPTARG | ||
;; | ||
s) rootpw=$OPTARG | ||
;; | ||
u) cn=$OPTARG | ||
;; | ||
*) echo "Invalid option: " $OPTARG | ||
;; | ||
esac | ||
done | ||
|
||
# check args | ||
[ -z $domain ] && usage && exit 1 | ||
[ -z $rootpw ] && usage && exit 1 | ||
[ -z $cn ] && usage && exit 1 | ||
|
||
# domain is home.org ==> olcSuffix is dc=home,dc=org | ||
olcSuffix=`echo $domain |awk -F '.' '{print "dc="$1",dc="$2}'` | ||
# olcRootDN cn=admin,dc=home,dc=org | ||
olcRootDN="cn=$cn,$olcSuffix" | ||
# get password to sha password | ||
shaRootPW=`slappasswd -h {SHA} -s $rootpw` | ||
|
||
cat <<EOF | ldapmodify -Y EXTERNAL -H ldapi:/// | ||
dn: olcDatabase={0}config,cn=config | ||
changetype: modify | ||
add: olcRootPW | ||
olcRootPW: $shaRootPW | ||
EOF | ||
|
||
# add default schema | ||
if [ ! -e /root/.openldap-shell ];then | ||
echo "Add default schema" | ||
ls /etc/openldap/schema/*.ldif | xargs -I {} ldapadd -Y EXTERNAL -H ldapi:/// -f {} > /dev/null | ||
fi | ||
# modify olcSuffix olcRootDN | ||
cat <<EOF | ldapmodify -Y EXTERNAL -H ldapi:/// | ||
dn: olcDatabase={1}monitor,cn=config | ||
changetype: modify | ||
replace: olcAccess | ||
olcAccess: {0} to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.base="$olcRootDN" read by * none | ||
dn: olcDatabase={2}hdb,cn=config | ||
changetype: modify | ||
replace: olcSuffix | ||
olcSuffix: $olcSuffix | ||
dn: olcDatabase={2}hdb,cn=config | ||
changetype: modify | ||
replace: olcRootDN | ||
olcRootDN: $olcRootDN | ||
dn: olcDatabase={2}hdb,cn=config | ||
changetype: modify | ||
add: olcRootPW | ||
olcRootPW: $shaRootPW | ||
dn: olcDatabase={2}hdb,cn=config | ||
changetype: modify | ||
add: olcAccess | ||
olcAccess: {0}to attrs=userPassword,shadowLastChange by dn="$olcRootDN" write by anonymous auth by self write by * none | ||
olcAccess: {1}to dn.base="" by * read | ||
olcAccess: {2}to * by dn="$olcRootDN" write by * read | ||
EOF | ||
|
||
echo "Add $olcRootDN Manager" | ||
|
||
# add user and group | ||
echo "Add users and groups" | ||
left_dc=`echo $olcSuffix |awk -F ',' '{print $1}'| awk -F '=' '{print $2}'` | ||
cat << EOF | ldapadd -D "$olcRootDN" -H ldapi:/// -w $rootpw | ||
dn: $olcSuffix | ||
objectClass: top | ||
objectClass: dcObject | ||
objectclass: organization | ||
o: root_ldap | ||
dc: $left_dc | ||
EOF | ||
|
||
cat <<EOF | ||
You olcSuffix is $olcSuffix | ||
You olcRootDN is $olcRootDN | ||
You olcRootPW is $rootpw | ||
EOF |