Mercurial > prosody-hg
annotate tools/openfire2prosody.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parents | 879a6a33c21b |
| children |
| rev | line source |
|---|---|
|
5089
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env lua |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
2 -- Prosody IM |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2009 Waqas Hussain |
|
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
5696
diff
changeset
|
4 -- |
|
5089
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
5 -- This project is MIT/X11 licensed. Please see the |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
6 -- COPYING file in the source package for more information. |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
7 -- |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
8 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
9 package.path = package.path..";../?.lua"; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
10 package.cpath = package.cpath..";../?.so"; -- needed for util.pposix used in datamanager |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
11 |
|
5696
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5217
diff
changeset
|
12 local my_name = arg[0]; |
|
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5217
diff
changeset
|
13 if my_name:match("[/\\]") then |
|
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5217
diff
changeset
|
14 package.path = package.path..";"..my_name:gsub("[^/\\]+$", "../?.lua"); |
|
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5217
diff
changeset
|
15 package.cpath = package.cpath..";"..my_name:gsub("[^/\\]+$", "../?.so"); |
|
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5217
diff
changeset
|
16 end |
|
9fba74a28e0c
package{,c}path fixes for migration tools
Vadim Misbakh-Soloviov <mva@mva.name>
parents:
5217
diff
changeset
|
17 |
|
13142
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
18 if not pcall(require, "prosody.loader") then |
|
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
19 pcall(require, "loader"); |
|
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
20 end |
|
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
21 |
|
5089
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
22 -- ugly workaround for getting datamanager to work outside of prosody :( |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
23 prosody = { }; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
24 prosody.platform = "unknown"; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
25 if os.getenv("WINDIR") then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
26 prosody.platform = "windows"; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
27 elseif package.config:sub(1,1) == "/" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
28 prosody.platform = "posix"; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
29 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
30 |
|
13142
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
31 local parse_xml = require "prosody.util.xml".parse; |
|
5089
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
32 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
33 ----------------------------------------------------------------------- |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
34 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
35 package.loaded["util.logger"] = {init = function() return function() end; end} |
|
13142
879a6a33c21b
tools: Update imports to use new prosody.* namespace
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
36 local dm = require "prosody.util.datamanager" |
|
5089
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
37 dm.set_data_path("data"); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
38 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
39 local arg = ...; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
40 local help = "/? -? ? /h -h /help -help --help"; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
41 if not arg or help:find(arg, 1, true) then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
42 print([[Openfire importer for Prosody |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
43 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
44 Usage: openfire2prosody.lua filename.xml hostname |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
45 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
46 ]]); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
47 os.exit(1); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
48 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
49 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
50 local host = select(2, ...) or "localhost"; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
51 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
52 local file = assert(io.open(arg)); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
53 local data = assert(file:read("*a")); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
54 file:close(); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
55 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
56 local xml = assert(parse_xml(data)); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
57 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
58 assert(xml.name == "Openfire", "The input file is not an Openfire XML export"); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
59 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
60 local substatus_mapping = { ["0"] = "none", ["1"] = "to", ["2"] = "from", ["3"] = "both" }; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
61 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
62 for _,tag in ipairs(xml.tags) do |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
63 if tag.name == "User" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
64 local username, password, roster; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
65 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
66 for _,tag in ipairs(tag.tags) do |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
67 if tag.name == "Username" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
68 username = tag:get_text(); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
69 elseif tag.name == "Password" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
70 password = tag:get_text(); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
71 elseif tag.name == "Roster" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
72 roster = {}; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
73 local pending = {}; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
74 for _,tag in ipairs(tag.tags) do |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
75 if tag.name == "Item" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
76 local jid = assert(tag.attr.jid, "Roster item has no JID"); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
77 if tag.attr.substatus ~= "-1" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
78 local item = {}; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
79 item.name = tag.attr.name; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
80 item.subscription = assert(substatus_mapping[tag.attr.substatus], "invalid substatus"); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
81 item.ask = tag.attr.askstatus == "0" and "subscribe" or nil; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
82 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
83 local groups = {}; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
84 for _,tag in ipairs(tag) do |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
85 if tag.name == "Group" then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
86 groups[tag:get_text()] = true; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
87 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
88 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
89 item.groups = groups; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
90 roster[jid] = item; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
91 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
92 if tag.attr.recvstatus == "1" then pending[jid] = true; end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
93 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
94 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
95 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
96 if next(pending) then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
97 roster[false] = { pending = pending }; |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
98 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
99 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
100 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
101 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
102 assert(username and password, "No username or password"); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
103 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
104 local ret, err = dm.store(username, host, "accounts", {password = password}); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
105 print("["..(err or "success").."] stored account: "..username.."@"..host.." = "..password); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
106 |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
107 if roster then |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
108 local ret, err = dm.store(username, host, "roster", roster); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
109 print("["..(err or "success").."] stored roster: "..username.."@"..host.." = "..password); |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
110 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
111 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
112 end |
|
a5b683909f79
tools/openfire2prosody: Initial commit.
Waqas Hussain <waqas20@gmail.com>
parents:
diff
changeset
|
113 |
