aboutsummaryrefslogtreecommitdiff
path: root/src/ejabberd_auth_pam.erl
blob: bc5a0ea2584001d3fc2983a14ab8f15aed43c2c9 (plain) (blame)
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
%%%-------------------------------------------------------------------
%%% File    : ejabberd_auth_pam.erl
%%% Author  : Evgeniy Khramtsov <xram@jabber.ru>
%%% Purpose : PAM authentication
%%% Created : 5 Jul 2007 by Evgeniy Khramtsov <xram@jabber.ru>
%%%
%%%
%%% ejabberd, Copyright (C) 2002-2018   ProcessOne
%%%
%%% This program is free software; you can redistribute it and/or
%%% modify it under the terms of the GNU General Public License as
%%% published by the Free Software Foundation; either version 2 of the
%%% License, or (at your option) any later version.
%%%
%%% This program is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%%% General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License along
%%% with this program; if not, write to the Free Software Foundation, Inc.,
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
%%%
%%%-------------------------------------------------------------------
-module(ejabberd_auth_pam).

-behaviour(ejabberd_config).

-author('xram@jabber.ru').

-behaviour(ejabberd_auth).

-export([start/1, stop/1, check_password/4,
	 user_exists/2, store_type/1, plain_password_required/1,
	 opt_type/1]).

start(_Host) ->
    ejabberd:start_app(epam).

stop(_Host) ->
    ok.

check_password(User, AuthzId, Host, Password) ->
    if AuthzId /= <<>> andalso AuthzId /= User ->
        false;
    true ->
    Service = get_pam_service(Host),
    UserInfo = case get_pam_userinfotype(Host) of
		 username -> User;
		 jid -> <<User/binary, "@", Host/binary>>
	       end,
    case catch epam:authenticate(Service, UserInfo,
				 Password)
	of
      true -> true;
      _ -> false
        end
    end.

user_exists(User, Host) ->
    Service = get_pam_service(Host),
    UserInfo = case get_pam_userinfotype(Host) of
		 username -> User;
		 jid -> <<User/binary, "@", Host/binary>>
	       end,
    case catch epam:acct_mgmt(Service, UserInfo) of
      true -> true;
      false -> false;
      _Err -> {error, db_failure}
    end.

plain_password_required(_) -> true.

store_type(_) -> external.

%%====================================================================
%% Internal functions
%%====================================================================
get_pam_service(Host) ->
    ejabberd_config:get_option({pam_service, Host}, <<"ejabberd">>).

get_pam_userinfotype(Host) ->
    ejabberd_config:get_option({pam_userinfotype, Host}, username).

-spec opt_type(pam_service) -> fun((binary()) -> binary());
	      (pam_userinfotype) -> fun((username | jid) -> username | jid);
	      (atom()) -> [atom()].
opt_type(pam_service) -> fun iolist_to_binary/1;
opt_type(pam_userinfotype) ->
    fun (username) -> username;
	(jid) -> jid
    end;
opt_type(_) -> [pam_service, pam_userinfotype].