aboutsummaryrefslogtreecommitdiff
path: root/src/web/ejabberd_web.erl
blob: 143fbf40706bc1c1d081df684dbf12c4a134da1e (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
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
%%%----------------------------------------------------------------------
%%% File    : ejabberd_web.erl
%%% Author  : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : 
%%% Created : 28 Feb 2004 by Alexey Shchepin <alexey@sevcom.net>
%%% Id      : $Id$
%%%----------------------------------------------------------------------

-module(ejabberd_web).
-author('alexey@sevcom.net').
-vsn('$Revision$  ').

%% External exports
-export([make_xhtml/1,
	 process_get/2]).

-include("ejabberd.hrl").
-include("jlib.hrl").
-include("ejabberd_http.hrl").


make_xhtml(Els) ->
    {xmlelement, "html", [{"xmlns", "http://www.w3.org/1999/xhtml"},
			  {"xml:lang", "en"},
			  {"lang", "en"}],
     [{xmlelement, "head", [],
       [{xmlelement, "meta", [{"http-equiv", "Content-Type"},
			      {"content", "text/html; charset=utf-8"}], []}]},
      {xmlelement, "body", [], Els}
     ]}.


-define(X(Name), {xmlelement, Name, [], []}).
-define(XA(Name, Attrs), {xmlelement, Name, Attrs, []}).
-define(XE(Name, Els), {xmlelement, Name, [], Els}).
-define(XAE(Name, Attrs, Els), {xmlelement, Name, Attrs, Els}).
-define(C(Text), {xmlcdata, Text}).
-define(XC(Name, Text), ?XE(Name, [?C(Text)])).
-define(XAC(Name, Attrs, Text), ?XAE(Name, Attrs, [?C(Text)])).

-define(LI(Els), ?XE("li", Els)).
-define(A(URL, Els), ?XAE("a", [{"href", URL}], Els)).
-define(AC(URL, Text), ?A(URL, [?C(Text)])).
-define(P, ?X("p")).
-define(BR, ?X("br")).
-define(INPUT(Type, Name, Value),
	?XA("input", [{"type", Type},
		      {"name", Name},
		      {"value", Value}])).


process_get({_, true},
	    #request{auth = Auth,
		     path = ["admin", "server", SHost | RPath],
		     q = Query,
		     lang = Lang} = Request) ->
    Host = jlib:nameprep(SHost),
    case lists:member(Host, ?MYHOSTS) of
	true ->
	    US = case Auth of
		     {SJID, P} ->
			 case jlib:string_to_jid(SJID) of
			     error ->
				 unauthorized;
			     #jid{user = U, server = S} ->
				 case ejabberd_auth:check_password(U, S, P) of
				     true ->
					 {U, S};
				     false ->
					 unauthorized
				 end
			 end;
		     _ ->
			 unauthorized
		 end,
	    case US of
		{User, Server} ->
		    case acl:match_rule(
			   Host, configure, jlib:make_jid(User, Server, "")) of
			deny ->
			    {401, [], make_xhtml([?XC("h1", "Not Allowed")])};
			allow ->
			    ejabberd_web_admin:process_admin(
			      Host, Request#request{path = RPath,
						    us = US})
		    end;
		unauthorized ->
		    {401,
		     [{"WWW-Authenticate", "basic realm=\"ejabberd\""}],
		     ejabberd_web:make_xhtml([{xmlelement, "h1", [],
					       [{xmlcdata, "401 Unauthorized"}]}])}
	    end;
	false ->
	    {404, [], make_xhtml([?XC("h1", "Not found")])}
    end;

process_get({_, true},
	    #request{auth = Auth,
		     path = ["admin" | RPath],
		     q = Query,
		     lang = Lang} = Request) ->
    US = case Auth of
	     {SJID, P} ->
		 case jlib:string_to_jid(SJID) of
		     error ->
			 unauthorized;
		     #jid{user = U, server = S} ->
			 case ejabberd_auth:check_password(U, S, P) of
			     true ->
				 {U, S};
			     false ->
				 unauthorized
			 end
		 end;
	     _ ->
		 unauthorized
	 end,
    case US of
	{User, Server} ->
	    case acl:match_rule(
		   global, configure, jlib:make_jid(User, Server, "")) of
		deny ->
		    {401, [], make_xhtml([?XC("h1", "Not Allowed")])};
		allow ->
		    ejabberd_web_admin:process_admin(
		      global, Request#request{path = RPath,
					      us = US})
	    end;
	unauthorized ->
	    {401,
	     [{"WWW-Authenticate", "basic realm=\"ejabberd\""}],
	     ejabberd_web:make_xhtml([{xmlelement, "h1", [],
				       [{xmlcdata, "401 Unauthorized"}]}])}
    end;

process_get({true, _},
	    #request{path = ["http-poll" | RPath],
		     q = _Query,
		     lang = _Lang} = Request) ->
    ejabberd_http_poll:process_request(Request#request{path = RPath});

process_get(_, _Request) ->
    {404, [], make_xhtml([?XC("h1", "Not found")])}.