Merge 0.10->trunk
[prosody.git] / util / dependencies.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9 local function softreq(...) local ok, lib =  pcall(require, ...); if ok then return lib; else return nil, lib; end end
10
11 -- Required to be able to find packages installed with luarocks
12 if not softreq "luarocks.loader" then -- LuaRocks 2.x
13         softreq "luarocks.require"; -- LuaRocks <1.x
14 end
15
16 local function missingdep(name, sources, msg)
17         print("");
18         print("**************************");
19         print("Prosody was unable to find "..tostring(name));
20         print("This package can be obtained in the following ways:");
21         print("");
22         local longest_platform = 0;
23         for platform in pairs(sources) do
24                 longest_platform = math.max(longest_platform, #platform);
25         end
26         for platform, source in pairs(sources) do
27                 print("", platform..":"..(" "):rep(4+longest_platform-#platform)..source);
28         end
29         print("");
30         print(msg or (name.." is required for Prosody to run, so we will now exit."));
31         print("More help can be found on our website, at https://prosody.im/doc/depends");
32         print("**************************");
33         print("");
34 end
35
36 -- COMPAT w/pre-0.8 Debian: The Debian config file used to use
37 -- util.ztact, which has been removed from Prosody in 0.8. This
38 -- is to log an error for people who still use it, so they can
39 -- update their configs.
40 package.preload["util.ztact"] = function ()
41         if not package.loaded["core.loggingmanager"] then
42                 error("util.ztact has been removed from Prosody and you need to fix your config "
43                     .."file. More information can be found at https://prosody.im/doc/packagers#ztact", 0);
44         else
45                 error("module 'util.ztact' has been deprecated in Prosody 0.8.");
46         end
47 end;
48
49 local function check_dependencies()
50         if _VERSION < "Lua 5.1" then
51                 print "***********************************"
52                 print("Unsupported Lua version: ".._VERSION);
53                 print("At least Lua 5.1 is required.");
54                 print "***********************************"
55                 return false;
56         end
57
58         local fatal;
59
60         local lxp = softreq "lxp"
61
62         if not lxp then
63                 missingdep("luaexpat", {
64                                 ["Debian/Ubuntu"] = "sudo apt-get install liblua5.1-expat0";
65                                 ["luarocks"] = "luarocks install luaexpat";
66                                 ["Source"] = "http://www.keplerproject.org/luaexpat/";
67                         });
68                 fatal = true;
69         end
70
71         local socket = softreq "socket"
72
73         if not socket then
74                 missingdep("luasocket", {
75                                 ["Debian/Ubuntu"] = "sudo apt-get install liblua5.1-socket2";
76                                 ["luarocks"] = "luarocks install luasocket";
77                                 ["Source"] = "http://www.tecgraf.puc-rio.br/~diego/professional/luasocket/";
78                         });
79                 fatal = true;
80         end
81
82         local lfs, err = softreq "lfs"
83         if not lfs then
84                 missingdep("luafilesystem", {
85                                 ["luarocks"] = "luarocks install luafilesystem";
86                                 ["Debian/Ubuntu"] = "sudo apt-get install liblua5.1-filesystem0";
87                                 ["Source"] = "http://www.keplerproject.org/luafilesystem/";
88                         });
89                 fatal = true;
90         end
91
92         local ssl = softreq "ssl"
93
94         if not ssl then
95                 missingdep("LuaSec", {
96                                 ["Debian/Ubuntu"] = "https://prosody.im/download/start#debian_and_ubuntu";
97                                 ["luarocks"] = "luarocks install luasec";
98                                 ["Source"] = "http://www.inf.puc-rio.br/~brunoos/luasec/";
99                         }, "SSL/TLS support will not be available");
100         end
101
102         local encodings, err = softreq "util.encodings"
103         if not encodings then
104                 if err:match("module '[^']*' not found") then
105                         missingdep("util.encodings", { ["Windows"] = "Make sure you have encodings.dll from the Prosody distribution in util/";
106                                                 ["GNU/Linux"] = "Run './configure' and 'make' in the Prosody source directory to build util/encodings.so";
107                                         });
108                 else
109                         print "***********************************"
110                         print("util/encodings couldn't be loaded. Check that you have a recent version of libidn");
111                         print ""
112                         print("The full error was:");
113                         print(err)
114                         print "***********************************"
115                 end
116                 fatal = true;
117         end
118
119         local hashes, err = softreq "util.hashes"
120         if not hashes then
121                 if err:match("module '[^']*' not found") then
122                         missingdep("util.hashes", { ["Windows"] = "Make sure you have hashes.dll from the Prosody distribution in util/";
123                                                 ["GNU/Linux"] = "Run './configure' and 'make' in the Prosody source directory to build util/hashes.so";
124                                         });
125                 else
126                         print "***********************************"
127                         print("util/hashes couldn't be loaded. Check that you have a recent version of OpenSSL (libcrypto in particular)");
128                         print ""
129                         print("The full error was:");
130                         print(err)
131                         print "***********************************"
132                 end
133                 fatal = true;
134         end
135         return not fatal;
136 end
137
138 local function log_warnings()
139         if _VERSION > "Lua 5.1" then
140                 prosody.log("warn", "Support for %s is experimental, please report any issues", _VERSION);
141         end
142         local ssl = softreq"ssl";
143         if ssl then
144                 local major, minor, veryminor, patched = ssl._VERSION:match("(%d+)%.(%d+)%.?(%d*)(M?)");
145                 if not major or ((tonumber(major) == 0 and (tonumber(minor) or 0) <= 3 and (tonumber(veryminor) or 0) <= 2) and patched ~= "M") then
146                         prosody.log("error", "This version of LuaSec contains a known bug that causes disconnects, see https://prosody.im/doc/depends");
147                 end
148         end
149         local lxp = softreq"lxp";
150         if lxp then
151                 if not pcall(lxp.new, { StartDoctypeDecl = false }) then
152                         prosody.log("error", "The version of LuaExpat on your system leaves Prosody "
153                                 .."vulnerable to denial-of-service attacks. You should upgrade to "
154                                 .."LuaExpat 1.3.0 or higher as soon as possible. See "
155                                 .."https://prosody.im/doc/depends#luaexpat for more information.");
156                 end
157                 if not lxp.new({}).getcurrentbytecount then
158                         prosody.log("error", "The version of LuaExpat on your system does not support "
159                                 .."stanza size limits, which may leave servers on untrusted "
160                                 .."networks (e.g. the internet) vulnerable to denial-of-service "
161                                 .."attacks. You should upgrade to LuaExpat 1.3.0 or higher as "
162                                 .."soon as possible. See "
163                                 .."https://prosody.im/doc/depends#luaexpat for more information.");
164                 end
165         end
166 end
167
168 return {
169         softreq = softreq;
170         missingdep = missingdep;
171         check_dependencies = check_dependencies;
172         log_warnings = log_warnings;
173 };