This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
auth.go
207 lines (157 loc) · 5.56 KB
/
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package util
import (
"gopkg.in/macaron.v1"
"os"
"syscall"
_ "github.com/go-sql-driver/mysql"
"database/sql"
"fmt"
"os/user"
"strings"
)
type API_function func(ctx *macaron.Context) (string)
type User struct {
Hostcontrol_id int
System_username string
Privileges string
Owned_by string
Login_token string
Email_address string
HomeDir string
Sudo bool
}
func Auth(ctx *macaron.Context, permission string) (User,bool) {
//api_authid := string(ctx.Query("api_authid"))
//api_token := string(ctx.Query("api_token"))
ckhostcontrol_id := ctx.GetCookie("hostcontrol_id")
cklogin_token := ctx.GetCookie("login_token")
api_token_user_id := ""
api_token_id := ""
raw_api_token := Query(ctx, "api_token")
if raw_api_token != "" {
parsed_token := strings.Split(raw_api_token, "/")
if len(parsed_token) == 2 {
api_token_user_id = parsed_token[0]
api_token_id = parsed_token[1]
}
}
var hcuser User
hcuser.Sudo = false
db, _ := MySQL()
defer db.Close()
if api_token_user_id != "" {
tstmt, err := db.Prepare("SELECT * from hostcontrol_user_tokens LEFT JOIN hostcontrol_users ON hostcontrol_user_tokens.hostcontrol_id=hostcontrol_users.hostcontrol_id WHERE hostcontrol_user_tokens.hostcontrol_id=? and token=?")
if err != nil {
fmt.Println(string(err.Error()))
}
trows, _ := tstmt.Query(api_token_user_id, api_token_id)
tstmt.Close()
// check if we have a row returned...
if trows.Next() {
var token string
var hostcontrol_id string
var description string
var token_id string
trows.Scan(&token, &hostcontrol_id, &description, &token_id, &hcuser.Hostcontrol_id, &hcuser.System_username, &hcuser.Privileges, &hcuser.Owned_by, &hcuser.Login_token, &hcuser.Email_address)
if strings.Contains(hcuser.Privileges, permission) || permission == "any" || strings.Contains(hcuser.Privileges, "all") {
return hcuser, true
}
}
}
// token authentication check
stmt, err := db.Prepare("SELECT * from hostcontrol_users WHERE hostcontrol_id = ? and login_token = ?")
if err != nil {
fmt.Println(string(err.Error()))
}
rows, _ := stmt.Query(ckhostcontrol_id, cklogin_token)
stmt.Close()
// check if we have a row returned...
if rows.Next() {
rows.Scan(&hcuser.Hostcontrol_id, &hcuser.System_username, &hcuser.Privileges, &hcuser.Owned_by, &hcuser.Login_token, &hcuser.Email_address)
}else{
return hcuser, false
}
// check for sudo cookie
cksudo := ctx.GetCookie("sudo")
bkmount := string(ctx.Params("sudo"))
if bkmount != "" {
cksudo = bkmount
}
if (cksudo != "" && ChkPaternity(hcuser.System_username, cksudo)) || bkmount != "" {
stmt, _ = db.Prepare("SELECT * from hostcontrol_users WHERE system_username = ?")
rows, _ = stmt.Query(cksudo)
stmt.Close()
// check if we have a row returned...
if rows.Next() {
rows.Scan(&hcuser.Hostcontrol_id, &hcuser.System_username, &hcuser.Privileges, &hcuser.Owned_by, &hcuser.Login_token, &hcuser.Email_address)
}
hcuser.Sudo = true
}
suser, _ := user.Lookup(hcuser.System_username)
hcuser.HomeDir = suser.HomeDir
if strings.Contains(hcuser.Privileges, permission) || permission == "any" || strings.Contains(hcuser.Privileges, "all") {
return hcuser, true
}
// fail out with death.
return hcuser, false
}
func Sudo(username string, ctx *macaron.Context) {
ctx.SetParams("sudo", username)
}
func ChkPaternity(owner string, child string) (bool) {
db, _ := MySQL();
defer db.Close()
data := make(map[string]map[string]string)
data = Getusers(owner, data, db)
if data[child] == nil{
return false
}
return true
}
func Getusers(username string, data map[string]map[string]string, db *sql.DB) (map[string]map[string]string){
stmt, _ := db.Prepare("SELECT * from hostcontrol_users WHERE owned_by = ? and system_username != owned_by")
rows, _ := stmt.Query(username)
stmt.Close()
for rows.Next() {
var hostcontrol_id string
var system_username string
var privileges string
var owned_by string
var login_token string
var email_address string
rows.Scan(&hostcontrol_id,&system_username,&privileges,&owned_by,&login_token,&email_address)
data[system_username] = make(map[string]string)
data[system_username]["hostcontrol_id"] = hostcontrol_id
data[system_username]["system_username"] = system_username
data[system_username]["privileges"] = privileges
data[system_username]["owned_by"] = owned_by
data[system_username]["login_token"] = login_token
data[system_username]["email_address"] = email_address
data = Getusers(system_username, data, db)
}
return data
}
// true means the user has access to the file/dir
func ChkPerms(selected_object string, uid int, gid int) bool {
if uid == 0 && gid == 0 {
return true
}
fs_object, err := os.Stat(selected_object)
if err != nil {
return false
}
fs_object_uid := int(fs_object.Sys().(*syscall.Stat_t).Uid)
fs_object_gid := int(fs_object.Sys().(*syscall.Stat_t).Gid)
if uid != fs_object_uid || gid != fs_object_gid {
return false
}
return true
}
func Query(ctx *macaron.Context, name string) string {
query := string(ctx.Query(name))
param := string(ctx.Params(name))
if query != "" {
return query
}
return param
}