Source file pam.ml

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
300
301
302
303
304
305
306
307
open! Core

(* [t] is an abstract type which holds the pam context *)
type t

module Pam_error = struct
  type t = int
end

module Pam_conv = struct
  module Message = struct
    type style_t =
      | PAM_PROMPT_ECHO_OFF
      | PAM_PROMPT_ECHO_ON
      | PAM_ERROR_MSG
      | PAM_TEXT_INFO
    [@@deriving bin_io]

    type t =
      { style : style_t
      ; message : string
      }
    [@@deriving bin_io, fields]

    (* The value here should not be changed without updating the code
       and constant values in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| 47df5cd77358f7105834f9fc0e807676 |}]
    ;;

    let create = Fields.create
  end

  module Response = struct
    type t =
      { resp : string option
      ; resp_retcode : int
      }
    [@@deriving bin_io, fields]

    (* Use custom create function as [pam] expects a constant [resp_retcode] *)
    let create ~resp = Fields.create ~resp ~resp_retcode:0
  end

  module Result = struct
    type error_t =
      | PAM_BUF_ERR
      | PAM_CONV_ERR
    [@@deriving bin_io]

    type t = (Response.t list, error_t) Result.t [@@deriving bin_io]

    (* The value here should not be changed without updating the return
       code and ordering in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| 622288696dcfeb2b5599101c06a18a26 |}]
    ;;
  end
end

module Pam_result = struct
  type 'a t = ('a, Pam_error.t) Result.t
end

module Pam_auth = struct
  module Flag = struct
    type t =
      | PAM_SILENT
      | PAM_DISALLOW_NULL_AUTHTOK
    [@@deriving bin_io]

    (* The value here should not be changed without updating the return
       code and ordering in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| 1d56cbf13292909f3ae9b88709273566 |}]
    ;;
  end
end

module Pam_acct = struct
  module Flag = struct
    type t =
      | PAM_SILENT
      | PAM_DISALLOW_NULL_AUTHTOK
    [@@deriving bin_io]

    (* The value here should not be changed without updating the return
       code and ordering in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| 1d56cbf13292909f3ae9b88709273566 |}]
    ;;
  end
end

module Pam_cred = struct
  module Flag = struct
    type t =
      | PAM_ESTABLISH_CRED
      | PAM_DELETE_CRED
      | PAM_REINITIALIZE_CRED
      | PAM_REFRESH_CRED
    [@@deriving bin_io]

    (* The value here should not be changed without updating the return
       code and ordering in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| f303df589fc8138228a35e94265f8eba |}]
    ;;
  end
end

module Pam_authtok = struct
  module Flag = struct
    type t =
      | PAM_SILENT
      | PAM_CHANGE_EXPIRED_AUTHTOK
    [@@deriving bin_io]

    (* The value here should not be changed without updating the return
       code and ordering in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| 99753ea70855ca8f8bf962a186395393 |}]
    ;;
  end
end

type pam_conv
type pam_fail_delay
type pam_xauth_data

module Pam_item_type = struct
  (* The value here should not be changed or re-ordred without updating the return
     code and ordering in [pam_stub.c]. [bin_io] does not work with gadt so we cannot
     create unit test case to check like others *)
  type _ t =
    | PAM_SERVICE : string t
    | PAM_USER : string t
    | PAM_USER_PROMPT : string t
    | PAM_TTY : string t
    | PAM_RUSER : string t
    | PAM_RHOST : string t
    | PAM_AUTHTOK : string t
    | PAM_OLDAUTHTOK : string t
    | PAM_XDISPLAY : string t
    | PAM_XAUTHDATA : pam_xauth_data t
    | PAM_AUTHTOK_TYPE : string t
    | PAM_CONV : pam_conv t
    | PAM_FAIL_DELAY : pam_fail_delay t
end

module Pam_session = struct
  module Flag = struct
    type t = PAM_SILENT [@@deriving bin_io]

    (* The value here should not be changed without updating the return
       code and ordering in [pam_stub.c] *)
    let%expect_test _ =
      print_endline [%bin_digest: t];
      [%expect {| 5c9ce93eee7315a34bbf88ee63a24a3e |}]
    ;;
  end
end

external pam_start_c
  :  string
  -> string
  -> ((Pam_conv.Message.t, string) Result.t list -> Pam_conv.Result.t)
  -> (t, t * Pam_error.t) Result.t
  = "caml_pam_start"

external pam_end_c : t -> unit Pam_result.t = "caml_pam_end"

external pam_authenticate_c
  :  t
  -> Pam_auth.Flag.t list
  -> unit Pam_result.t
  = "caml_pam_authenticate"

external pam_acct_mgmt_c
  :  t
  -> Pam_acct.Flag.t list
  -> unit Pam_result.t
  = "caml_pam_acct_mgmt"

external pam_setcred_c
  :  t
  -> bool
  -> Pam_cred.Flag.t
  -> unit Pam_result.t
  = "caml_pam_setcred"

external pam_chauthtok_c
  :  t
  -> Pam_authtok.Flag.t list
  -> unit Pam_result.t
  = "caml_pam_chauthtok"

external pam_open_session_c
  :  t
  -> Pam_session.Flag.t list
  -> unit Pam_result.t
  = "caml_pam_open_session"

external pam_close_session_c
  :  t
  -> Pam_session.Flag.t list
  -> unit Pam_result.t
  = "caml_pam_close_session"

external pam_getenv_c : t -> string -> (string, string) Result.t = "caml_pam_getenv"
external pam_putenv_c : t -> string -> unit Pam_result.t = "caml_pam_putenv"
external pam_getenvlist_c : t -> (string list, string) Result.t = "caml_pam_getenvlist"

external pam_get_item_c
  :  t
  -> 'a Pam_item_type.t
  -> 'a option Pam_result.t
  = "caml_pam_get_item"

external pam_set_item_c
  :  t
  -> 'a Pam_item_type.t
  -> 'a
  -> unit Pam_result.t
  = "caml_pam_set_item"

external pam_strerror_c : t -> Pam_error.t -> string = "caml_pam_strerror"

let pam_strerror t errnum = pam_strerror_c t errnum
let pam_errmsg ~op errmsg = sprintf "[%s] %s" op errmsg

let pam_result_to_or_error ~op t result =
  Result.map_error result ~f:(fun errnum ->
    let errmsg = pam_errmsg ~op (pam_strerror t errnum) in
    Error.of_string (sprintf "%s (errnum: %d)" errmsg errnum))
;;

let pam_start ~service ~user ~conv =
  pam_start_c service user conv
  |> Result.map_error ~f:(fun (t, errnum) ->
    let err_desc = pam_strerror t errnum in
    ignore (pam_end_c t);
    Error.of_string err_desc)
;;

let pam_end t =
  (* We cannot use [t] after [pam_end_c] so we have to come up with an error message *)
  pam_end_c t
  |> Result.map_error ~f:(fun errnum ->
    Error.of_string
      (pam_errmsg ~op:"pam_end" (sprintf "failed to release (errnum: %d)" errnum)))
;;

let pam_authenticate t ~flags =
  pam_authenticate_c t flags |> pam_result_to_or_error ~op:"pam_authenticate" t
;;

let pam_acct_mgmt t ~flags =
  pam_acct_mgmt_c t flags |> pam_result_to_or_error ~op:"pam_acct_mgmt" t
;;

let pam_setcred ?(silent = false) t ~flag =
  pam_setcred_c t silent flag |> pam_result_to_or_error ~op:"pam_setcred" t
;;

let pam_chauthtok t ~flags =
  pam_chauthtok_c t flags |> pam_result_to_or_error ~op:"pam_chauthtok" t
;;

let pam_open_session t ~flags =
  pam_open_session_c t flags |> pam_result_to_or_error ~op:"pam_open_session" t
;;

let pam_close_session t ~flags =
  pam_close_session_c t flags |> pam_result_to_or_error ~op:"pam_close_session" t
;;

let pam_getenv t ~key =
  pam_getenv_c t key
  |> Result.map_error ~f:(Fn.compose Error.of_string (pam_errmsg ~op:"pam_getenv"))
;;

let pam_putenv t ~key ~data =
  pam_putenv_c t (sprintf "%s=%s" key data) |> pam_result_to_or_error ~op:"pam_putenv" t
;;

let pam_unsetenv t ~key = pam_putenv_c t key |> pam_result_to_or_error ~op:"pam_putenv" t

let pam_getenvlist t =
  pam_getenvlist_c t
  |> Result.map_error ~f:(Fn.compose Error.of_string (pam_errmsg ~op:"pam_getenvlist"))
;;

let pam_get_item (type a) t ~(item_type : a Pam_item_type.t) : a option Or_error.t =
  pam_get_item_c t item_type |> pam_result_to_or_error ~op:"pam_get_item" t
;;

let pam_set_item (type a) t ~(item_type : a Pam_item_type.t) ~(item : a) : unit Or_error.t
  =
  pam_set_item_c t item_type item |> pam_result_to_or_error ~op:"pam_set_item" t
;;