File: Init.hsc

package info (click to toggle)
ldap-haskell 0.6.11-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 164 kB
  • sloc: haskell: 83; ansic: 34; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 4,469 bytes parent folder | download | duplicates (3)
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
{- -*- Mode: haskell; -*-
Haskell LDAP Interface
Copyright (C) 2005 John Goerzen <jgoerzen@complete.org>

This code is under a 3-clause BSD license; see COPYING for details.
-}

{- |
   Module     : LDAP.Init
   Copyright  : Copyright (C) 2005 John Goerzen
   License    : BSD

   Maintainer : John Goerzen,
   Maintainer : jgoerzen\@complete.org
   Stability  : provisional
   Portability: portable

Initialization and shutdown for LDAP programs

Written by John Goerzen, jgoerzen\@complete.org
-}

module LDAP.Init(ldapOpen,
                 ldapInit,
                 ldapInitialize,
                 ldapSimpleBind,
                 ldapExternalSaslBind)
where

import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.C.String
import Foreign.Marshal.Alloc
import Foreign.Storable
import LDAP.Types
import Foreign.C.Types
import LDAP.Utils
import Foreign.Marshal.Utils

#include <ldap.h>


ldapSetVersion3 :: LDAPPtr -> IO LDAPInt
ldapSetVersion3 cld =
    with ((#{const LDAP_VERSION3})::LDAPInt) $ \copt ->
    ldap_set_option cld #{const LDAP_OPT_PROTOCOL_VERSION} (castPtr copt)

ldapSetRestart :: LDAPPtr -> IO LDAPInt
ldapSetRestart cld =
    with ((#{const LDAP_OPT_ON})::LDAPInt) $ \copt ->
    ldap_set_option cld #{const LDAP_OPT_RESTART} (castPtr copt)

{- | Preferred way to initialize a LDAP connection. 
The default port is given in 'LDAP.Constants.ldapPort'.

Could throw IOError on failure. -}
ldapInit :: String              -- ^ Host
         -> LDAPInt             -- ^ Port
         -> IO LDAP             -- ^ New LDAP Obj
ldapInit host port =
    withCString host $ \cs ->
       do rv <- fromLDAPPtr "ldapInit" (cldap_init cs port)
          withForeignPtr rv $ \cld -> do
              ldapSetVersion3 cld
              ldapSetRestart cld
          return rv

{- | Like 'ldapInit', but establish network connection immediately. -}
ldapOpen :: String              -- ^ Host
            -> LDAPInt          -- ^ Port
            -> IO LDAP          -- ^ New LDAP Obj
ldapOpen host port =
    withCString host (\cs ->
        do rv <- fromLDAPPtr "ldapOpen" (cldap_open cs port)
           withForeignPtr rv ldapSetRestart
           return rv)

{- | Like 'ldapInit', but accepts a URI (or whitespace/comma separated
list of URIs) which can contain a schema, a host and a port.  Besides
ldap, valid schemas are ldaps (LDAP over TLS), ldapi (LDAP over IPC),
and cldap (connectionless LDAP). -}
ldapInitialize :: String        -- ^ URI
                  -> IO LDAP    -- ^ New LDAP Obj
ldapInitialize uri =
    withCString uri $ \cs ->
    alloca $ \pp -> do
    r <- ldap_initialize pp cs
    ldap <- fromLDAPPtr "ldapInitialize" (peek pp)
    _ <- checkLE "ldapInitialize" ldap (return r)
    withForeignPtr ldap $ \p -> do
        ldapSetVersion3 p
        ldapSetRestart p
    return ldap


{- | Bind to the remote server. -}
ldapSimpleBind :: LDAP          -- ^ LDAP Object
               -> String        -- ^ DN (Distinguished Name)
               -> String        -- ^ Password
               -> IO ()
ldapSimpleBind ld dn passwd =
    withLDAPPtr ld (\ptr ->
     withCString dn (\cdn ->
      withCString passwd (\cpasswd -> 
        do checkLE "ldapSimpleBind" ld
                            (ldap_simple_bind_s ptr cdn cpasswd)
           return ()
                         )))

{- | Bind with the SASL EXTERNAL mechanism. -}
ldapExternalSaslBind :: LDAP   -- ^ LDAP Object
                     -> String -- ^ Authorization identity (UTF-8 encoded; pass "" to derive it from the authentication identity)
                     -> IO ()
ldapExternalSaslBind ld authz =
    withLDAPPtr ld (\ptr ->
     withCStringLen authz (\(authzPtr,authzLen) ->
        do checkLE "ldapExternalSaslBind" ld (external_sasl_bind ptr authzPtr authzLen)
           return ()
      ))

foreign import ccall unsafe "ldap.h ldap_init"
  cldap_init :: CString -> CInt -> IO LDAPPtr


foreign import ccall safe "ldap.h ldap_open"
  cldap_open :: CString -> CInt -> IO LDAPPtr

foreign import ccall unsafe "ldap.h ldap_initialize"
  ldap_initialize :: Ptr LDAPPtr -> CString -> IO LDAPInt

foreign import ccall safe "ldap.h ldap_simple_bind_s"
  ldap_simple_bind_s :: LDAPPtr -> CString -> CString -> IO LDAPInt

foreign import ccall safe "sasl_external.h external_sasl_bind"
  external_sasl_bind :: LDAPPtr -> CString -> Int -> IO LDAPInt

foreign import ccall unsafe "ldap.h ldap_set_option"
  ldap_set_option :: LDAPPtr -> LDAPInt -> Ptr () -> IO LDAPInt