-
-
Notifications
You must be signed in to change notification settings - Fork 183
/
Copy pathmain.go
executable file
·93 lines (84 loc) · 2.1 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
package main
import (
"C" // Import cgo to avoid project-wide go fmt failures.
"database/sql"
"flag"
"fmt"
"log"
"os"
"github.com/jcmturner/gokrb5/v8/client"
"github.com/jcmturner/gokrb5/v8/config"
"github.com/jcmturner/gokrb5/v8/credentials"
"github.com/jcmturner/gokrb5/v8/gssapi"
"github.com/jcmturner/gokrb5/v8/spnego"
"github.com/sijms/go-ora/v2/advanced_nego"
)
type KerberosAuth struct{}
func (kerb KerberosAuth) Authenticate(server, service string) ([]byte, error) {
conf, err := config.Load("/etc/krb5.conf")
if err != nil {
return nil, err
}
ccache, err := credentials.LoadCCache("/tmp/krb5cc_1000")
if err != nil {
return nil, err
}
cl, err := client.NewFromCCache(ccache, conf)
if err != nil {
return nil, err
}
ticket, key, err := cl.GetServiceTicket(service + "/" + server)
if err != nil {
return nil, err
}
token, err := spnego.NewKRB5TokenAPREQ(cl, ticket, key, []int{gssapi.ContextFlagMutual}, []int{})
if err != nil {
return nil, err
}
return token.APReq.Marshal()
}
func usage() {
fmt.Println()
fmt.Println("kerberos")
fmt.Println(" a program to test kerberos5 authentication.")
fmt.Println()
fmt.Println("Usage:")
fmt.Println(` kerberos -server server_url`)
flag.PrintDefaults()
fmt.Println()
fmt.Println("Example:")
fmt.Println(` kerberos -server "oracle://user:pass@server/service_name"`)
fmt.Println()
}
func main() {
var server string
flag.StringVar(&server, "server", "", "Server's URL, oracle://user:pass@server/service_name")
flag.Parse()
connStr := os.ExpandEnv(server)
if connStr == "" {
fmt.Println("Missing -server option")
usage()
os.Exit(1)
}
fmt.Println("Connection string: ", connStr)
advanced_nego.SetKerberosAuth(&KerberosAuth{})
//options := map[string]string{
// "TRACE FILE": "trace.log",
// "AUTH TYPE": "KERBEROS",
//}
conn, err := sql.Open("oracle", connStr)
if err != nil {
log.Fatalln("cannot connect: ", err)
}
defer func() {
err = conn.Close()
if err != nil {
fmt.Println("Can't close connection: ", err)
}
}()
err = conn.Ping()
if err != nil {
fmt.Println("Can't ping connection: ", err)
return
}
}