-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.go
153 lines (134 loc) · 4.89 KB
/
main.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
151
152
153
package main
import (
"os/user"
"github.com/nohupped/ADtoLDAP/syncer"
"os"
"reflect"
"flag"
"gopkg.in/ldap.v2"
"runtime"
"time"
"fmt"
)
/*func init() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func(){
for sig := range c {
fmt.Println(sig.String(), "received, terminating.")
os.Exit(1)
}
}()
}*/
func main() {
checkSafety := flag.Bool("safe", true, "Set it to false to skip config file securitycheck")
configFile := flag.String("configfile", "/etc/ldapsync.ini", "Path to the config file")
logfile := flag.String("logfile", "/var/log/ldapsync.log", "Path to log file. Defaults to /var/log/ldapsync.log")
sampleConfig := flag.Bool("showSampleConfig", false, "Prints a sample config to STDOUT")
flag.Parse()
if *sampleConfig == true {
fmt.Println(syncer.SampleConfig)
os.Exit(0)
}
username, err := user.Current()
syncer.CheckForError(err)
log := syncer.StartLog(*logfile)
defer syncer.LoggerClose()
log.Infoln("Running program as", username)
log.Infoln("safe option set to", *checkSafety)
log.Infoln("Config file is, ", *configFile)
if *checkSafety == true {
syncer.CheckPerm(*configFile)
} else {
log.Infoln("Skipping file permission check on", *configFile)
}
r := syncer.NewRuntimeConfig(*configFile)
//End of variable declaration
log.Debugf("%+v\n", *r)
log.Infoln("Initiating sync")
for {
AddChan := make(chan syncer.Action)
log.Debugln("Created", reflect.TypeOf(AddChan))
DelChan := make(chan syncer.Action)
log.Debugln("Created", reflect.TypeOf(DelChan))
shutdownAddChan := make(chan string)
shutdownDelChan := make(chan string)
shutdownChannel := make(chan string)
LdapConnectionChan := make(chan *ldap.Conn)
log.Debugln("Created channel of type", reflect.TypeOf(LdapConnectionChan))
ADElementsChan := make(chan *[]syncer.LDAPElement)
log.Debugln("Created channel of type", reflect.TypeOf(ADElementsChan))
LDAPElementsChan := make(chan *[]syncer.LDAPElement)
log.Debugln("Created channel of type", reflect.TypeOf(LDAPElementsChan))
go syncer.SyncRunLDAP(r.LDAPServer.Host, r.LDAPServer.Port, r.LDAPServer.Username, r.LDAPServer.Password,
r.LDAPServer.BaseDN, r.LDAPServer.Filter, r.LDAPServer.Attributes, r.LDAPServer.Page,
r.LDAPServer.ConnTimeOut, r.LDAPServer.UseTLS, r.LDAPServer.CRTInsecureSkipVerify,
r.LDAPServer.CRTValidFor, r.LDAPServer.CRTPath, shutdownChannel, LDAPElementsChan, LdapConnectionChan,
r.ReplaceAttributes, r.MapAttributes)
go syncer.SyncRunAD(r.ADServer.Host, r.ADServer.Port, r.ADServer.Username, r.ADServer.Password,
r.ADServer.BaseDN, r.ADServer.Filter, r.ADServer.Attributes, r.ADServer.Page,
r.ADServer.ConnTimeOut, r.ADServer.UseTLS, r.ADServer.CRTInsecureSkipVerify,
r.ADServer.CRTValidFor, r.ADServer.CRTPath, shutdownChannel, ADElementsChan)
ADElements := <-ADElementsChan
LDAPElements := <-LDAPElementsChan
LDAPConnection := <-LdapConnectionChan
log.Debugln(<-shutdownChannel)
log.Debugln(<-shutdownChannel)
ADElementsConverted := syncer.InitialPopulateToLdap(ADElements, LDAPConnection, r.ReplaceAttributes, r.MapAttributes, true)
LDAPElementsConverted := syncer.InitialPopulateToLdap(LDAPElements, LDAPConnection, r.ReplaceAttributes, r.MapAttributes, true)
syncer.ConvertRealmToLower(ADElementsConverted)
log.Debugln("Converted AD Realms to lowercase")
go syncer.FindAdds(&ADElementsConverted, &LDAPElementsConverted, LDAPConnection, AddChan, shutdownAddChan)
go syncer.FindDels(&LDAPElementsConverted, &ADElementsConverted, DelChan, shutdownDelChan)
counter := 0
for {
select {
case Add := <-AddChan:
for k, v := range Add {
log.Debugln(k, ":", v)
err := LDAPConnection.Add(v)
if err != nil {
log.Errorln(err)
}
}
case Del := <-DelChan:
for k, v := range Del {
log.Debugln(k, ":", v)
deleteRecord := ldap.NewDelRequest(v.DN, []ldap.Control{})
err := LDAPConnection.Del(deleteRecord)
if err != nil {
log.Errorln(err)
}
}
case shutdownAdd := <-shutdownAddChan:
log.Debugln(shutdownAdd)
counter ++
case shutdownDel := <-shutdownDelChan:
log.Debugln(shutdownDel)
counter ++
}
if counter == 2 {
log.Debugln("Counter reached")
break
}
}
LDAPConnection.Close()
//Sleep the daemon
close(shutdownDelChan)
close(shutdownAddChan)
close(shutdownChannel)
close(LdapConnectionChan)
close(ADElementsChan)
close(LDAPElementsChan)
log.Infoln("Sleeping for", r.Delay, "seconds, and iterating again...")
log.Infoln("Currently active goroutines: ", runtime.NumGoroutine())
//Thanks to profiling, that helped finding a goroutine leak.
/*buf1 := new(bytes.Buffer)
pprof.Lookup("goroutine").WriteTo(buf1, 1)
fmt.Println("pprof.Lookup.WriteTo report:\n", string(buf1.Bytes()[:buf1.Len()]))
var buf [10240]byte
out := buf[:runtime.Stack(buf[:], true)]
fmt.Println("runtime.Stack report:\n", string(out))*/
time.Sleep(time.Second * time.Duration(r.Delay))
}
}