/usr/share/gocode/src/github.com/mqu/openldap/results.go is in golang-openldap-dev 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | package openldap
/*#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <ldap.h>
int _berval_get_len(struct berval **ber, int i){
return ber[i]->bv_len;
}
char* _berval_get_value(struct berval **ber, int i){
return ber[i]->bv_val;
}
*/
// #cgo CFLAGS: -DLDAP_DEPRECATED=1
// #cgo linux CFLAGS: -DLINUX=1
// #cgo LDFLAGS: -lldap -llber
import "C"
import (
"errors"
"fmt"
"unsafe"
)
// ------------------------------------------ RESULTS methods ---------------------------------------------
/*
openldap C API :
int ldap_count_messages( LDAP *ld, LdapMessage *result )
LdapMessage *ldap_first_message( LDAP *ld, LdapMessage *result )
LdapMessage *ldap_next_message ( LDAP *ld, LdapMessage *message )
int ldap_count_entries( LDAP *ld, LdapMessage *result )
LdapMessage *ldap_first_entry( LDAP *ld, LdapMessage *result )
LdapMessage *ldap_next_entry ( LDAP *ld, LdapMessage *entry )
char *ldap_first_attribute(LDAP *ld, LdapMessage *entry, BerElement **berptr )
char *ldap_next_attribute (LDAP *ld, LdapMessage *entry, BerElement *ber )
char **ldap_get_values(LDAP *ld, LdapMessage *entry, char *attr)
struct berval **ldap_get_values_len(LDAP *ld, LdapMessage *entry,char *attr)
int ldap_count_values(char **vals)
int ldap_count_values_len(struct berval **vals)
void ldap_value_free(char **vals)
void ldap_value_free_len(struct berval **vals)
*/
func (self *LdapMessage) Count() int {
// API : int ldap_count_messages(LDAP *ld, LDAPMessage *chain )
// err : (count = -1)
count := int(C.ldap_count_messages(self.ldap.conn, self.msg))
if count == -1 {
panic("LDAP::Count() (ldap_count_messages) error (-1)")
}
return count
}
func (self *LdapMessage) FirstMessage() *LdapMessage {
var msg *C.LDAPMessage
msg = C.ldap_first_message(self.ldap.conn, self.msg)
if msg == nil {
return nil
}
_msg := new(LdapMessage)
_msg.ldap = self.ldap
_msg.errno = 0
_msg.msg = msg
return _msg
}
func (self *LdapMessage) NextMessage() *LdapMessage {
var msg *C.LDAPMessage
msg = C.ldap_next_message(self.ldap.conn, self.msg)
if msg == nil {
return nil
}
_msg := new(LdapMessage)
_msg.ldap = self.ldap
_msg.errno = 0
_msg.msg = msg
return _msg
}
/* an alias to ldap_count_message() ? */
func (self *LdapEntry) CountEntries() int {
// API : int ldap_count_messages(LDAP *ld, LDAPMessage *chain )
// err : (count = -1)
return int(C.ldap_count_entries(self.ldap.conn, self.entry))
}
func (self *LdapMessage) FirstEntry() *LdapEntry {
var msg *C.LDAPMessage
// API: LdapMessage *ldap_first_entry( LDAP *ld, LdapMessage *result )
msg = C.ldap_first_entry(self.ldap.conn, self.msg)
if msg == nil {
return nil
}
_msg := new(LdapEntry)
_msg.ldap = self.ldap
_msg.errno = 0
_msg.entry = msg
return _msg
}
func (self *LdapEntry) NextEntry() *LdapEntry {
var msg *C.LDAPMessage
// API: LdapMessage *ldap_next_entry ( LDAP *ld, LdapMessage *entry )
msg = C.ldap_next_entry(self.ldap.conn, self.entry)
if msg == nil {
return nil
}
_msg := new(LdapEntry)
_msg.ldap = self.ldap
_msg.errno = 0
_msg.entry = msg
return _msg
}
func (self *LdapEntry) FirstAttribute() (string, error) {
var ber *C.BerElement
// API: char *ldap_first_attribute(LDAP *ld, LdapMessage *entry, BerElement **berptr )
rv := C.ldap_first_attribute(self.ldap.conn, self.entry, &ber)
if rv == nil {
// error
return "", nil
}
self.ber = ber
return C.GoString(rv), nil
}
func (self *LdapEntry) NextAttribute() (string, error) {
// API: char *ldap_next_attribute (LDAP *ld, LdapMessage *entry, BerElement *ber )
rv := C.ldap_next_attribute(self.ldap.conn, self.entry, self.ber)
if rv == nil {
// error
return "", nil
}
return C.GoString(rv), nil
}
// private func
func sptr(p uintptr) *C.char {
return *(**C.char)(unsafe.Pointer(p))
}
// private func used to convert null terminated char*[] to go []string
func cstrings_array(x **C.char) []string {
var s []string
for p := uintptr(unsafe.Pointer(x)); sptr(p) != nil; p += unsafe.Sizeof(uintptr(0)) {
s = append(s, C.GoString(sptr(p)))
}
return s
}
/*
FIXME: does not work for binary attributes
FIXME:
If the attribute values are binary in nature, and thus not suitable to be returned as an array of char *'s, the ldap_get_values_len() routine can be used instead. It
takes the same parameters as ldap_get_values(), but returns a NULL-terminated array of pointers to berval structures, each containing the length of and a pointer to a
value.
*/
// OK for pure ASCI entries.
func (self *LdapEntry) GetValues_ascii(attr string) []string {
_attr := C.CString(attr)
defer C.free(unsafe.Pointer(_attr))
// DEPRECATED
// API: char **ldap_get_values(LDAP *ld, LdapMessage *entry, char *attr)
values := cstrings_array(C.ldap_get_values(self.ldap.conn, self.entry, _attr))
return values
}
// FIXME: need to verify binary values.
func (self *LdapEntry) GetValues(attr string) []string {
var s []string
_attr := C.CString(attr)
defer C.free(unsafe.Pointer(_attr))
var bv **C.struct_berval
//API: struct berval **ldap_get_values_len(LDAP *ld, LDAPMessage *entry, char *attr)
bv = C.ldap_get_values_len(self.ldap.conn, self.entry, _attr)
var i int
count := int(C.ldap_count_values_len(bv))
for i = 0 ; i < count; i++ {
s = append(s, C.GoStringN(C._berval_get_value(bv, C.int(i)), C._berval_get_len(bv, C.int(i))))
}
// free allocated array (bv)
C.ldap_value_free_len(bv)
return s
}
// ------------------------------------------------ RESULTS -----------------------------------------------
/*
int ldap_result ( LDAP *ld, int msgid, int all, struct timeval *timeout, LdapMessage **result );
int ldap_msgfree( LdapMessage *msg );
int ldap_msgtype( LdapMessage *msg );
int ldap_msgid ( LdapMessage *msg );
*/
// Result()
// take care to free LdapMessage result with MsgFree()
//
func (self *Ldap) Result() (*LdapMessage, error) {
var msgid int = 1
var all int = 1
var tv C.struct_timeval
tv.tv_sec = 30
var msg *C.LDAPMessage
// API: int ldap_result( LDAP *ld, int msgid, int all, struct timeval *timeout, LDAPMessage **result );
rv := C.ldap_result(self.conn, C.int(msgid), C.int(all), &tv, &msg)
if rv != LDAP_OPT_SUCCESS {
return nil, errors.New(fmt.Sprintf("LDAP::Result() error : %d (%s)", rv, ErrorToString(int(rv))))
}
_msg := new(LdapMessage)
_msg.ldap = self
_msg.errno = int(rv)
_msg.msg = msg
return _msg, nil
}
// MsgFree() is used to free LDAP::Result() allocated data
//
// returns -1 on error.
//
func (self *LdapMessage) MsgFree() int{
if self.msg != nil {
rv := C.ldap_msgfree(self.msg)
self.msg = nil
return int(rv)
}
return -1
}
// ---------------------------------------- DN Methods ---------------------------------------------------
/*
char *ldap_get_dn( LDAP *ld, LdapMessage *entry)
int ldap_str2dn( const char *str, LDAPDN *dn, unsigned flags)
void ldap_dnfree( LDAPDN dn)
int ldap_dn2str( LDAPDN dn, char **str, unsigned flags)
char **ldap_explode_dn( const char *dn, int notypes)
char **ldap_explode_rdn( const char *rdn, int notypes)
char *ldap_dn2ufn ( const char * dn )
char *ldap_dn2dcedn( const char * dn )
char *ldap_dcedn2dn( const char * dn )
char *ldap_dn2ad_canonical( const char * dn )
*/
// GetDn() return the DN (Distinguish Name) for self LdapEntry
func (self *LdapEntry) GetDn() string {
// API: char *ldap_get_dn( LDAP *ld, LDAPMessage *entry )
rv := C.ldap_get_dn(self.ldap.conn, self.entry)
defer C.free(unsafe.Pointer(rv))
if rv == nil {
err := self.ldap.Errno()
panic(fmt.Sprintf("LDAP::GetDn() error %d (%s)", err, ErrorToString(err)))
}
val := C.GoString(rv)
return val
}
|