~ubuntu-branches/ubuntu/trusty/juju-core/trusty-proposed

« back to all changes in this revision

Viewing changes to src/gopkg.in/mgo.v2/internal/sasl/sasl_windows.go

  • Committer: Package Import Robot
  • Author(s): Robie Basak
  • Date: 2015-07-15 13:09:07 UTC
  • mfrom: (35.1.15 wily-proposed)
  • Revision ID: package-import@ubuntu.com-20150715130907-wqak1zpebzzdvy3q
Tags: 1.22.6-0ubuntu1~14.04.1
* No change backport to 14.04 (LP: #1469744). This results in the
  following packaging delta from the previous 1.20.11-0ubuntu0.14.04.1
  in trusty-updates:
  - distro-info added and libgo5 removed from Build-Depends.
  - Standards-Version bumped.
  - cloud-image-utils | cloud-utils added to juju-local Depends.
  - d/copyright updated.
  - dep8 tests updated.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package sasl
 
2
 
 
3
// #include "sasl_windows.h"
 
4
import "C"
 
5
 
 
6
import (
 
7
        "fmt"
 
8
        "strings"
 
9
        "sync"
 
10
        "unsafe"
 
11
)
 
12
 
 
13
type saslStepper interface {
 
14
        Step(serverData []byte) (clientData []byte, done bool, err error)
 
15
        Close()
 
16
}
 
17
 
 
18
type saslSession struct {
 
19
        // Credentials
 
20
        mech          string
 
21
        service       string
 
22
        host          string
 
23
        userPlusRealm string
 
24
        target        string
 
25
        domain        string
 
26
 
 
27
        // Internal state
 
28
        authComplete bool
 
29
        errored      bool
 
30
        step         int
 
31
 
 
32
        // C internal state
 
33
        credHandle C.CredHandle
 
34
        context    C.CtxtHandle
 
35
        hasContext C.int
 
36
 
 
37
        // Keep track of pointers we need to explicitly free
 
38
        stringsToFree []*C.char
 
39
}
 
40
 
 
41
var initError error
 
42
var initOnce sync.Once
 
43
 
 
44
func initSSPI() {
 
45
        rc := C.load_secur32_dll()
 
46
        if rc != 0 {
 
47
                initError = fmt.Errorf("Error loading libraries: %v", rc)
 
48
        }
 
49
}
 
50
 
 
51
func New(username, password, mechanism, service, host string) (saslStepper, error) {
 
52
        initOnce.Do(initSSPI)
 
53
        ss := &saslSession{mech: mechanism, hasContext: 0, userPlusRealm: username}
 
54
        if service == "" {
 
55
                service = "mongodb"
 
56
        }
 
57
        if i := strings.Index(host, ":"); i >= 0 {
 
58
                host = host[:i]
 
59
        }
 
60
        ss.service = service
 
61
        ss.host = host
 
62
 
 
63
        usernameComponents := strings.Split(username, "@")
 
64
        if len(usernameComponents) < 2 {
 
65
                return nil, fmt.Errorf("Username '%v' doesn't contain a realm!", username)
 
66
        }
 
67
        user := usernameComponents[0]
 
68
        ss.domain = usernameComponents[1]
 
69
        ss.target = fmt.Sprintf("%s/%s", ss.service, ss.host)
 
70
 
 
71
        var status C.SECURITY_STATUS
 
72
        // Step 0: call AcquireCredentialsHandle to get a nice SSPI CredHandle
 
73
        if len(password) > 0 {
 
74
                status = C.sspi_acquire_credentials_handle(&ss.credHandle, ss.cstr(user), ss.cstr(password), ss.cstr(ss.domain))
 
75
        } else {
 
76
                status = C.sspi_acquire_credentials_handle(&ss.credHandle, ss.cstr(user), nil, ss.cstr(ss.domain))
 
77
        }
 
78
        if status != C.SEC_E_OK {
 
79
                ss.errored = true
 
80
                return nil, fmt.Errorf("Couldn't create new SSPI client, error code %v", status)
 
81
        }
 
82
        return ss, nil
 
83
}
 
84
 
 
85
func (ss *saslSession) cstr(s string) *C.char {
 
86
        cstr := C.CString(s)
 
87
        ss.stringsToFree = append(ss.stringsToFree, cstr)
 
88
        return cstr
 
89
}
 
90
 
 
91
func (ss *saslSession) Close() {
 
92
        for _, cstr := range ss.stringsToFree {
 
93
                C.free(unsafe.Pointer(cstr))
 
94
        }
 
95
}
 
96
 
 
97
func (ss *saslSession) Step(serverData []byte) (clientData []byte, done bool, err error) {
 
98
        ss.step++
 
99
        if ss.step > 10 {
 
100
                return nil, false, fmt.Errorf("too many SSPI steps without authentication")
 
101
        }
 
102
        var buffer C.PVOID
 
103
        var bufferLength C.ULONG
 
104
        if len(serverData) > 0 {
 
105
                buffer = (C.PVOID)(unsafe.Pointer(&serverData[0]))
 
106
                bufferLength = C.ULONG(len(serverData))
 
107
        }
 
108
        var status C.int
 
109
        if ss.authComplete {
 
110
                // Step 3: last bit of magic to use the correct server credentials
 
111
                status = C.sspi_send_client_authz_id(&ss.context, &buffer, &bufferLength, ss.cstr(ss.userPlusRealm))
 
112
        } else {
 
113
                // Step 1 + Step 2: set up security context with the server and TGT
 
114
                status = C.sspi_step(&ss.credHandle, ss.hasContext, &ss.context, &buffer, &bufferLength, ss.cstr(ss.target))
 
115
        }
 
116
        if buffer != C.PVOID(nil) {
 
117
                defer C.free(unsafe.Pointer(buffer))
 
118
        }
 
119
        if status != C.SEC_E_OK && status != C.SEC_I_CONTINUE_NEEDED {
 
120
                ss.errored = true
 
121
                return nil, false, ss.handleSSPIErrorCode(status)
 
122
        }
 
123
 
 
124
        clientData = C.GoBytes(unsafe.Pointer(buffer), C.int(bufferLength))
 
125
        if status == C.SEC_E_OK {
 
126
                ss.authComplete = true
 
127
                return clientData, true, nil
 
128
        } else {
 
129
                ss.hasContext = 1
 
130
                return clientData, false, nil
 
131
        }
 
132
}
 
133
 
 
134
func (ss *saslSession) handleSSPIErrorCode(code C.int) error {
 
135
        switch {
 
136
        case code == C.SEC_E_TARGET_UNKNOWN:
 
137
                return fmt.Errorf("Target %v@%v not found", ss.target, ss.domain)
 
138
        }
 
139
        return fmt.Errorf("Unknown error doing step %v, error code %v", ss.step, code)
 
140
}