forked from liam-middlebrook/csh-plug
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathldap.go
150 lines (130 loc) · 3.69 KB
/
ldap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"crypto/tls"
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/ldap.v2"
"strconv"
)
type LDAPConnection struct {
app *PlugApplication
con *ldap.Conn
host string
bind_dn string
bind_pw string
}
func (c *LDAPConnection) Init(
app *PlugApplication,
host,
bind_dn,
bind_pw string) {
c.app = app
c.host = host
c.bind_dn = bind_dn
c.bind_pw = bind_pw
c.reconnectToLDAP()
}
func (c *LDAPConnection) reconnectToLDAP() {
lcon, err := ldap.DialTLS("tcp", c.host,
&tls.Config{ServerName: "ldap.csh.rit.edu"})
if err != nil {
c.app.db.AddLog(0, "ldap connection error: "+err.Error())
log.Fatal(err)
}
err = lcon.Bind(c.bind_dn, c.bind_pw)
if err != nil {
c.app.db.AddLog(0, "ldap bind error: "+err.Error())
log.Fatal(err)
}
c.con = lcon
}
func (c LDAPConnection) pingLDAPAlive() {
searchReq := ldap.NewSearchRequest(
"dc=csh,dc=rit,dc=edu",
ldap.ScopeBaseObject, ldap.NeverDerefAliases, 0, 0, false,
"(objectClass=top)",
[]string{"dn"},
nil,
)
_, err := c.con.Search(searchReq)
if err != nil {
c.reconnectToLDAP()
}
}
func (c LDAPConnection) CheckIfAdmin(username string) bool {
c.pingLDAPAlive()
searchRequest := ldap.NewSearchRequest(
"cn=users,cn=accounts,dc=csh,dc=rit,dc=edu",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(|(memberof=cn=drink,cn=groups,cn=accounts,dc=csh,dc=rit,dc=edu)(memberof=cn=rtp,cn=groups,cn=accounts,dc=csh,dc=rit,dc=edu)(memberof=cn=eboard,cn=groups,cn=accounts,dc=csh,dc=rit,dc=edu))(uid="+username+"))",
[]string{"uid"},
nil,
)
sr, err := c.con.Search(searchRequest)
if err != nil {
c.app.db.AddLog(0, "ldap search error: "+err.Error())
log.Fatal(err)
return false
}
return len(sr.Entries) > 0
}
func (c LDAPConnection) CheckIfIntroMember(username string) bool {
c.pingLDAPAlive()
searchRequest := ldap.NewSearchRequest(
"cn=users,cn=accounts,dc=csh,dc=rit,dc=edu",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(memberof=cn=intromembers,cn=groups,cn=accounts,dc=csh,dc=rit,dc=edu)(uid="+username+"))",
[]string{"uid"},
nil,
)
sr, err := c.con.Search(searchRequest)
if err != nil {
c.app.db.AddLog(0, "ldap search error: "+err.Error())
log.Fatal(err)
return false
}
return len(sr.Entries) > 0
}
func (c LDAPConnection) CheckCredits(username string) int {
c.pingLDAPAlive()
searchRequest := ldap.NewSearchRequest(
"uid="+username+",cn=users,cn=accounts,dc=csh,dc=rit,dc=edu",
ldap.ScopeBaseObject, ldap.NeverDerefAliases, 0, 0, false,
"(objectClass=*)",
[]string{"drinkBalance"},
nil,
)
sr, err := c.con.Search(searchRequest)
if err != nil {
c.app.db.AddLog(0, "ldap search error: "+err.Error())
log.Fatal(err)
}
balance, err := strconv.Atoi(sr.Entries[0].GetAttributeValue("drinkBalance"))
if err != nil {
c.app.db.AddLog(0, "ldap result parse error: "+err.Error())
log.Fatal(err)
}
log.Info("current balance for %s is %d", username, balance)
return balance
}
func (c LDAPConnection) HasEnoughCredits(username string, credits int) bool {
balance := c.CheckCredits(username)
return balance >= credits
}
func (c LDAPConnection) DecrementCredits(username string, credits int) bool {
balance := c.CheckCredits(username)
newBalance := balance - credits
if newBalance < 0 {
log.Info("Insufficient Credits! %d", balance)
return false
}
modifyRequest := ldap.NewModifyRequest("uid=" + username + ",cn=users,cn=accounts,dc=csh,dc=rit,dc=edu")
modifyRequest.Replace("drinkBalance", []string{fmt.Sprintf("%d", newBalance)})
err := c.con.Modify(modifyRequest)
if err != nil {
c.app.db.AddLog(0, "ldap modification error: "+err.Error())
log.Fatal(err)
}
log.Info("current balance for %s is %d", username, newBalance)
return true
}