-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathejabberd-go-auth.go
175 lines (145 loc) · 4.05 KB
/
ejabberd-go-auth.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package main
import (
"flag"
"bufio"
"encoding/binary"
"os"
"strings"
"database/sql"
_ "github.com/lib/pq"
_ "github.com/go-sql-driver/mysql"
"fmt"
"gopkg.in/ini.v1"
"log"
)
type Config struct {
Driver string
Host string
Port string
User string
Pass string
Dbname string
Dbargs string
Table string
UserField string
PassField string
ServerField string
}
func auth(conf *Config, db *sql.DB, user string,
server string, passwd string) (bool, error) {
var query string
var value string
var err error
log.Printf("user: %s, server: %s, passwd: %s\n", user, server, passwd)
if conf.ServerField != "" {
query = fmt.Sprintf(
"select %s from %s where %s = $1 and %s = $2 and %s = $3",
conf.UserField, conf.Table, conf.UserField, conf.PassField,
conf.ServerField)
err = db.QueryRow(query, user, passwd, server).Scan(&value)
} else {
query = fmt.Sprintf(
"select %s from %s where %s = $1 and %s = $2",
conf.UserField, conf.Table, conf.UserField, conf.PassField)
err = db.QueryRow(query, user, passwd).Scan(&value)
}
if err != nil || value == "" {
return false, err
}
return true, nil
}
func isuser(conf *Config, db *sql.DB, user string,
server string) (bool, error) {
var query string
var value string
var err error
if conf.ServerField != "" {
query = fmt.Sprintf(
"select %s from %s where %s = $1 and %s = $2",
conf.UserField, conf.Table, conf.UserField, conf.ServerField)
err = db.QueryRow(query, user, server).Scan(&value)
} else {
query = fmt.Sprintf(
"select %s from %s where %s = $1",
conf.UserField, conf.Table, conf.UserField)
err = db.QueryRow(query, user).Scan(&value)
}
if err != nil || value == "" {
return false, err
}
return true, nil
}
func GetSqlConnectionString(conf *Config) string {
return fmt.Sprintf("%s://%s:%s@%s:%s/%s?%s",
conf.Driver, conf.User, conf.Pass, conf.Host, conf.Port,
conf.Dbname, conf.Dbargs)
}
func OpenSqlConnection(conf *Config) (*sql.DB, error) {
var err error
connectionString := GetSqlConnectionString(conf)
db, err := sql.Open(conf.Driver, connectionString)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}
func AuthLoop(conf *Config) {
db, err := OpenSqlConnection(conf)
bioIn := bufio.NewReader(os.Stdin)
bioOut := bufio.NewWriter(os.Stdout)
var success bool
var length uint16
var result uint16
for {
_ = binary.Read(bioIn, binary.BigEndian, &length)
buf := make([]byte, length)
r, err := bioIn.Read(buf)
if r == 0 || err != nil {
time.Sleep(25 * time.Millisecond)
continue
}
if err != nil {
err = db.Ping()
}
if err == nil {
data := strings.Split(string(buf[:r]), ":")
if data[0] == "auth" {
success, err = auth(conf, db, data[1], data[2], data[3])
} else if data[0] == "isuser" {
success, err = isuser(conf, db, data[1], data[2])
} else {
success = false
}
} else {
success = false
}
length = 2
binary.Write(bioOut, binary.BigEndian, &length)
if success != true {
result = 0
} else {
result = 1
}
binary.Write(bioOut, binary.BigEndian, &result)
bioOut.Flush()
}
}
func main() {
filename := flag.String("conf", "/etc/ejabberd-pg-auth.ini",
"Config file with all the connection infos needed.")
flag.Parse()
cfg, err := ini.Load(*filename)
if err != nil {
log.Fatal(err)
}
conf := new(Config)
err = cfg.MapTo(conf)
if err != nil {
log.Fatal(err)
}
log.SetOutput(os.Stderr)
AuthLoop(conf)
}