| 1 |
// Copyright (C) MongoDB, Inc. 2017-present. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
// not use this file except in compliance with the License. You may obtain |
| 5 |
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
|
| 7 |
//go:build gssapi && (linux || darwin) |
| 8 |
// +build gssapi |
| 9 |
// +build linux darwin |
| 10 |
|
| 11 |
package gssapi |
| 12 |
|
| 13 |
/* |
| 14 |
#cgo linux CFLAGS: -DGOOS_linux |
| 15 |
#cgo linux LDFLAGS: -lgssapi_krb5 -lkrb5 |
| 16 |
#cgo darwin CFLAGS: -DGOOS_darwin |
| 17 |
#cgo darwin LDFLAGS: -framework GSS |
| 18 |
#include "gss_wrapper.h" |
| 19 |
*/ |
| 20 |
import "C" |
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"fmt" |
| 24 |
"runtime" |
| 25 |
"strings" |
| 26 |
"unsafe" |
| 27 |
) |
| 28 |
|
| 29 |
// New creates a new SaslClient. The target parameter should be a hostname with no port. |
| 30 |
func New(target, username, password string, passwordSet bool, props map[string]string) (*SaslClient, error) { |
| 31 |
serviceName := "mongodb" |
| 32 |
|
| 33 |
for key, value := range props { |
| 34 |
switch strings.ToUpper(key) { |
| 35 |
case "CANONICALIZE_HOST_NAME": |
| 36 |
return nil, fmt.Errorf("CANONICALIZE_HOST_NAME is not supported when using gssapi on %s", runtime.GOOS) |
| 37 |
case "SERVICE_REALM": |
| 38 |
return nil, fmt.Errorf("SERVICE_REALM is not supported when using gssapi on %s", runtime.GOOS) |
| 39 |
case "SERVICE_NAME": |
| 40 |
serviceName = value |
| 41 |
case "SERVICE_HOST": |
| 42 |
target = value |
| 43 |
default: |
| 44 |
return nil, fmt.Errorf("unknown mechanism property %s", key) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
servicePrincipalName := fmt.Sprintf("%s@%s", serviceName, target) |
| 49 |
|
| 50 |
return &SaslClient{ |
| 51 |
servicePrincipalName: servicePrincipalName, |
| 52 |
username: username, |
| 53 |
password: password, |
| 54 |
passwordSet: passwordSet, |
| 55 |
}, nil |
| 56 |
} |
| 57 |
|
| 58 |
type SaslClient struct { |
| 59 |
servicePrincipalName string |
| 60 |
username string |
| 61 |
password string |
| 62 |
passwordSet bool |
| 63 |
|
| 64 |
// state |
| 65 |
state C.gssapi_client_state |
| 66 |
contextComplete bool |
| 67 |
done bool |
| 68 |
} |
| 69 |
|
| 70 |
func (sc *SaslClient) Close() { |
| 71 |
C.gssapi_client_destroy(&sc.state) |
| 72 |
} |
| 73 |
|
| 74 |
func (sc *SaslClient) Start() (string, []byte, error) { |
| 75 |
const mechName = "GSSAPI" |
| 76 |
|
| 77 |
cservicePrincipalName := C.CString(sc.servicePrincipalName) |
| 78 |
defer C.free(unsafe.Pointer(cservicePrincipalName)) |
| 79 |
var cusername *C.char |
| 80 |
var cpassword *C.char |
| 81 |
if sc.username != "" { |
| 82 |
cusername = C.CString(sc.username) |
| 83 |
defer C.free(unsafe.Pointer(cusername)) |
| 84 |
if sc.passwordSet { |
| 85 |
cpassword = C.CString(sc.password) |
| 86 |
defer C.free(unsafe.Pointer(cpassword)) |
| 87 |
} |
| 88 |
} |
| 89 |
status := C.gssapi_client_init(&sc.state, cservicePrincipalName, cusername, cpassword) |
| 90 |
|
| 91 |
if status != C.GSSAPI_OK { |
| 92 |
return mechName, nil, sc.getError("unable to initialize client") |
| 93 |
} |
| 94 |
|
| 95 |
payload, err := sc.Next(nil, nil) |
| 96 |
|
| 97 |
return mechName, payload, err |
| 98 |
} |
| 99 |
|
| 100 |
func (sc *SaslClient) Next(_ context.Context, challenge []byte) ([]byte, error) { |
| 101 |
|
| 102 |
var buf unsafe.Pointer |
| 103 |
var bufLen C.size_t |
| 104 |
var outBuf unsafe.Pointer |
| 105 |
var outBufLen C.size_t |
| 106 |
|
| 107 |
if sc.contextComplete { |
| 108 |
if sc.username == "" { |
| 109 |
var cusername *C.char |
| 110 |
status := C.gssapi_client_username(&sc.state, &cusername) |
| 111 |
if status != C.GSSAPI_OK { |
| 112 |
return nil, sc.getError("unable to acquire username") |
| 113 |
} |
| 114 |
defer C.free(unsafe.Pointer(cusername)) |
| 115 |
sc.username = C.GoString((*C.char)(unsafe.Pointer(cusername))) |
| 116 |
} |
| 117 |
|
| 118 |
bytes := append([]byte{1, 0, 0, 0}, []byte(sc.username)...) |
| 119 |
buf = unsafe.Pointer(&bytes[0]) |
| 120 |
bufLen = C.size_t(len(bytes)) |
| 121 |
status := C.gssapi_client_wrap_msg(&sc.state, buf, bufLen, &outBuf, &outBufLen) |
| 122 |
if status != C.GSSAPI_OK { |
| 123 |
return nil, sc.getError("unable to wrap authz") |
| 124 |
} |
| 125 |
|
| 126 |
sc.done = true |
| 127 |
} else { |
| 128 |
if len(challenge) > 0 { |
| 129 |
buf = unsafe.Pointer(&challenge[0]) |
| 130 |
bufLen = C.size_t(len(challenge)) |
| 131 |
} |
| 132 |
|
| 133 |
status := C.gssapi_client_negotiate(&sc.state, buf, bufLen, &outBuf, &outBufLen) |
| 134 |
switch status { |
| 135 |
case C.GSSAPI_OK: |
| 136 |
sc.contextComplete = true |
| 137 |
case C.GSSAPI_CONTINUE: |
| 138 |
default: |
| 139 |
return nil, sc.getError("unable to negotiate with server") |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if outBuf != nil { |
| 144 |
defer C.free(outBuf) |
| 145 |
} |
| 146 |
|
| 147 |
return C.GoBytes(outBuf, C.int(outBufLen)), nil |
| 148 |
} |
| 149 |
|
| 150 |
func (sc *SaslClient) Completed() bool { |
| 151 |
return sc.done |
| 152 |
} |
| 153 |
|
| 154 |
func (sc *SaslClient) getError(prefix string) error { |
| 155 |
var desc *C.char |
| 156 |
|
| 157 |
status := C.gssapi_error_desc(sc.state.maj_stat, sc.state.min_stat, &desc) |
| 158 |
if status != C.GSSAPI_OK { |
| 159 |
if desc != nil { |
| 160 |
C.free(unsafe.Pointer(desc)) |
| 161 |
} |
| 162 |
|
| 163 |
return fmt.Errorf("%s: (%v, %v)", prefix, sc.state.maj_stat, sc.state.min_stat) |
| 164 |
} |
| 165 |
defer C.free(unsafe.Pointer(desc)) |
| 166 |
|
| 167 |
return fmt.Errorf("%s: %v(%v,%v)", prefix, C.GoString(desc), int32(sc.state.maj_stat), int32(sc.state.min_stat)) |
| 168 |
} |