This file is indexed.

/usr/share/lua/5.1/xmlrpc/init.lua is in liblua5.1-xmlrpc0 1.2.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
---------------------------------------------------------------------
-- XML-RPC implementation for Lua.
-- See Copyright Notice in license.html
---------------------------------------------------------------------

local lxp = require "lxp"
local lom = require "lxp.lom"

local assert, error, ipairs, pairs, select, type, tonumber, unpack = assert, error, ipairs, pairs, select, type, tonumber, unpack
local format, gsub, strfind, strsub = string.format, string.gsub, string.find, string.sub
local concat, tinsert = table.concat, table.insert
local ceil = math.ceil
local parse = lxp.lom.parse

module (...)

_COPYRIGHT = "Copyright (C) 2003-2010 Kepler Project"
_DESCRIPTION = "LuaXMLRPC is a library to make remote procedure calls using XML-RPC"
_PKGNAME = "LuaXMLRPC"
_VERSION_MAJOR = 1
_VERSION_MINOR = 2
_VERSION_MICRO = 1
_VERSION = _VERSION_MAJOR .. "." .. _VERSION_MINOR .. "." .. _VERSION_MICRO

---------------------------------------------------------------------
-- XML-RPC Parser
---------------------------------------------------------------------

---------------------------------------------------------------------
local function trim (s)
	return (type(s) == "string" and gsub (s, "^%s*(.-)%s*$", "%1"))
end

---------------------------------------------------------------------
local function is_space (s)
	return type(s) == "string" and trim(s) == ""
end

---------------------------------------------------------------------
-- Get next non-space element from tab starting from index i.
-- @param tab Table.
-- @param i Numeric index.
-- @return Object and its position on table; nil and an invalid index
--	when there is no more elements.
---------------------------------------------------------------------
function next_nonspace (tab, i)
	if not i then i = 1 end
	while is_space (tab[i]) do i = i+1 end
	return tab[i], i
end

---------------------------------------------------------------------
-- Get next element of tab with the given tag starting from index i.
-- @param tab Table.
-- @param tag String with the name of the tag.
-- @param i Numeric index.
-- @return Object and its position on table; nil and an invalid index
--	when there is no more elements.
---------------------------------------------------------------------
local function next_tag (tab, tag, i)
	if not i then i = 1 end
	while tab[i] do
		if type (tab[i]) == "table" and tab[i].tag == tag then
			return tab[i], i
		end
		i = i + 1
	end
	return nil, i
end

---------------------------------------------------------------------
local function x2number (tab)
	if tab.tag == "int" or tab.tag == "i4" or tab.tag == "double" then
		return tonumber (next_nonspace (tab, 1), 10)
	end
end

---------------------------------------------------------------------
local function x2boolean (tab)
	if tab.tag == "boolean" then
		local v = next_nonspace (tab, 1)
		return v == true or v == "true" or tonumber (v) == 1 or false
	end
end

---------------------------------------------------------------------
local function x2string (tab)
	return tab.tag == "string" and (tab[1] or "")
end

---------------------------------------------------------------------
local function x2date (tab)
	return tab.tag == "dateTime.iso8601" and next_nonspace (tab, 1)
end

---------------------------------------------------------------------
local function x2base64 (tab)
	return tab.tag == "base64" and next_nonspace (tab, 1)
end

---------------------------------------------------------------------
local function x2name (tab)
	return tab.tag == "name" and next_nonspace (tab, 1)
end

local x2value

---------------------------------------------------------------------
-- Disassemble a member object in its name and value parts.
-- @param tab Table with a DOM representation.
-- @return String (name) and Object (value).
-- @see x2name, x2value.
---------------------------------------------------------------------
local function x2member (tab)
	return
		x2name (next_tag(tab,"name")),
		x2value (next_tag(tab,"value"))
end

---------------------------------------------------------------------
-- Disassemble a struct object into a Lua table.
-- @param tab Table with DOM representation.
-- @return Table with "name = value" pairs.
---------------------------------------------------------------------
local function x2struct (tab)
	if tab.tag == "struct" then
		local res = {}
		for i = 1, #tab do
			if not is_space (tab[i]) then
				local name, val = x2member (tab[i])
				res[name] = val
			end
		end
		return res
	end
end

---------------------------------------------------------------------
-- Disassemble an array object into a Lua table.
-- @param tab Table with DOM representation.
-- @return Table.
---------------------------------------------------------------------
local function x2array (tab)
	if tab.tag == "array" then
		local d = next_tag (tab, "data")
		local res = {}
		for i = 1, #d do
			if not is_space (d[i]) then
				tinsert (res, x2value (d[i]))
			end
		end
		return res
	end
end

---------------------------------------------------------------------
local xmlrpc_types = {
	int = x2number,
	i4 = x2number,
	boolean = x2boolean,
	string = x2string,
	double = x2number,
	["dateTime.iso8601"] = x2date,
	base64 = x2base64,
	struct = x2struct,
	array = x2array,
}

local x2param, x2fault

---------------------------------------------------------------------
-- Disassemble a methodResponse into a Lua object.
-- @param tab Table with DOM representation.
-- @return Boolean (indicating wether the response was successful)
--	and (a Lua object representing the return values OR the fault
--	string and the fault code).
---------------------------------------------------------------------
local function x2methodResponse (tab)
	assert (type(tab) == "table", "Not a table")
	assert (tab.tag == "methodResponse",
		"Not a `methodResponse' tag: "..tab.tag)
	local t = next_nonspace (tab, 1)
	if t.tag == "params" then
		return true, unpack (x2param (t))
	elseif t.tag == "fault" then
		local f = x2fault (t)
		return false, f.faultString, f.faultCode
	else
		error ("Couldn't find a <params> nor a <fault> element")
	end
end

---------------------------------------------------------------------
-- Disassemble a value element into a Lua object.
-- @param tab Table with DOM representation.
-- @return Object.
---------------------------------------------------------------------
x2value = function (tab)
	local t = tab.tag
	assert (t == "value", "Not a `value' tag: "..t)
	local n = next_nonspace (tab)
	if type(n) == "string" or type(n) == "number" then
		return n
	elseif type (n) == "table" then
		local t = n.tag
		local get = xmlrpc_types[t]
		if not get then error ("Invalid <"..t.."> element") end
		return get (next_nonspace (tab))
        elseif type(n) == "nil" then
                -- the next best thing is to assume it's an empty string
               return ""

	end
end

---------------------------------------------------------------------
-- Disassemble a fault element into a Lua object.
-- @param tab Table with DOM representation.
-- @return Object.
---------------------------------------------------------------------
x2fault = function (tab)
	assert (tab.tag == "fault", "Not a `fault' tag: "..tab.tag)
	return x2value (next_nonspace (tab))
end

---------------------------------------------------------------------
-- Disassemble a param element into a Lua object.
-- Ignore white spaces between elements.
-- @param tab Table with DOM representation.
-- @return Object.
---------------------------------------------------------------------
x2param = function (tab)
	assert (tab.tag == "params", "Not a `params' tag")
	local res = {}
	local p, i = next_nonspace (tab, 1)
	while p do
		if p.tag == "param" then
			tinsert (res, x2value (next_tag (p, "value")))
		end
		p, i = next_nonspace (tab, i+1)
	end
	return res
end

---------------------------------------------------------------------
-- Disassemble a methodName element into a Lua object.
-- @param tab Table with DOM representation.
-- @return Object.
---------------------------------------------------------------------
local function x2methodName (tab)
	assert (tab.tag == "methodName", "Not a `methodName' tag: "..tab.tag)
	return (next_nonspace (tab, 1))
end

---------------------------------------------------------------------
-- Disassemble a methodCall element into its name and a list of parameters.
-- @param tab Table with DOM representation.
-- @return Object.
---------------------------------------------------------------------
local function x2methodCall (tab)
	assert (tab.tag == "methodCall", "Not a `methodCall' tag: "..tab.tag)
	return
		x2methodName (next_tag (tab,"methodName")),
		x2param (next_tag (tab,"params"))
end

---------------------------------------------------------------------
-- End of XML-RPC Parser
---------------------------------------------------------------------

---------------------------------------------------------------------
-- Convert a Lua Object into an XML-RPC string.
---------------------------------------------------------------------

---------------------------------------------------------------------
local formats = {
	boolean = "<boolean>%d</boolean>",
	number = "<double>%d</double>",
	string = "<string>%s</string>",

	array = "<array><data>\n%s\n</data></array>",
	double = "<double>%s</double>",
	int = "<int>%s</int>",
	struct = "<struct>%s</struct>",

	member = "<member><name>%s</name>%s</member>",
	value = "<value>%s</value>",

	param = "<param>%s</param>",

	params = [[
    <params>
      %s
    </params>]],

	fault = [[
    <fault>
      %s
    </fault>]],

	methodCall = [[
<?xml version="1.0"?>
<methodCall>
  <methodName>%s</methodName>
%s
</methodCall>
]],

	methodResponse = [[
<?xml version="1.0"?>
<methodResponse>
%s
</methodResponse>]],
}
formats.table = formats.struct

local toxml = {}
toxml.double = function (v,t) return format (formats.double, v) end
toxml.int = function (v,t) return format (formats.int, v) end
toxml.string = function (v,t) return format (formats.string, v) end

---------------------------------------------------------------------
-- Build a XML-RPC representation of a boolean.
-- @param v Object.
-- @return String.
---------------------------------------------------------------------
function toxml.boolean (v)
	local n = (v and 1) or 0
	return format (formats.boolean, n)
end

---------------------------------------------------------------------
-- Build a XML-RPC representation of a number.
-- @param v Object.
-- @param t Object representing the XML-RPC type of the value.
-- @return String.
---------------------------------------------------------------------
function toxml.number (v, t)
	local tt = (type(t) == "table") and t["*type"]
	if tt == "int" or tt == "i4" then
		return toxml.int (v, t)
	elseif tt == "double" then
		return toxml.double (v, t)
	elseif v == ceil(v) then
		return toxml.int (v, t)
	else
		return toxml.double (v, t)
	end
end

---------------------------------------------------------------------
-- @param typ Object representing a type.
-- @return Function that generate an XML element of the given type.
-- The object could be a string (as usual in Lua) or a table with
-- a field named "type" that should be a string with the XML-RPC
-- type name.
---------------------------------------------------------------------
local function format_func (typ)
	if type (typ) == "table" then
		return toxml[typ.type]
	else
		return toxml[typ]
	end
end

---------------------------------------------------------------------
-- @param val Object representing an array of values.
-- @param typ Object representing the type of the value.
-- @return String representing the equivalent XML-RPC value.
---------------------------------------------------------------------
function toxml.array (val, typ)
	local ret = {}
	local et = typ.elemtype
	local f = format_func (et)
	for i,v in ipairs (val) do
		if et and et ~= "array" then
			tinsert (ret, format (formats.value, f (v, et)))
		else
			local ct,cv = type_val(v)
			local cf = format_func(ct)
			tinsert (ret, format (formats.value, cf(cv, ct)))
		end

	end
	return format (formats.array, concat (ret, '\n'))
end

---------------------------------------------------------------------
---------------------------------------------------------------------
function toxml.struct (val, typ)
	local ret = {}
	if type (typ) == "table" then
		for n,t in pairs (typ.elemtype) do
			local f = format_func (t)
			tinsert (ret, format (formats.member, n, f (val[n], t)))
		end
	else
		for i, v in pairs (val) do
			tinsert (ret, toxml.member (i, v))
		end
	end
	return format (formats.struct, concat (ret))
end

toxml.table = toxml.struct

---------------------------------------------------------------------
---------------------------------------------------------------------
function toxml.member (n, v)
	return format (formats.member, n, toxml.value (v))
end

---------------------------------------------------------------------
-- Get type and value of object.
---------------------------------------------------------------------
function type_val (obj)
	local t = type (obj)
	local v = obj
	if t == "table" then
		t = obj["*type"] or "table"
		v = obj["*value"] or obj
	end
	return t, v
end

---------------------------------------------------------------------
-- Convert a Lua object to a XML-RPC object (plain string).
---------------------------------------------------------------------
function toxml.value (obj)
	local to, val = type_val (obj)
	if type(to) == "table" then
		return format (formats.value, toxml[to.type] (val, to))
	else
		-- primitive (not structured) types.
		--return format (formats[to], val)
		return format (formats.value, toxml[to] (val, to))
	end
end

---------------------------------------------------------------------
-- @param ... List of parameters.
-- @return String representing the `params' XML-RPC element.
---------------------------------------------------------------------
function toxml.params (...)
	local params_list = {}
	for i = 1, select ("#", ...) do
		params_list[i] = format (formats.param, toxml.value (select (i, ...)))
	end
	return format (formats.params, concat (params_list, '\n      '))
end

---------------------------------------------------------------------
-- @param method String with method's name.
-- @param ... List of parameters.
-- @return String representing the `methodCall' XML-RPC element.
---------------------------------------------------------------------
function toxml.methodCall (method, ...)
	local idx = strfind (method, "[^A-Za-z_.:/0-9]")
	if idx then
		error (format ("Invalid character `%s'", strsub (method, idx, idx)))
	end
	return format (formats.methodCall, method, toxml.params (...))
end

---------------------------------------------------------------------
-- @param err String with error message.
-- @return String representing the `fault' XML-RPC element.
---------------------------------------------------------------------
function toxml.fault (err)
	local code
	local message = err
	if type (err) == "table" then
		code = err.code
		message = err.message
	end
	return format (formats.fault, toxml.value {
		faultCode = { ["*type"] = "int", ["*value"] = code or err.faultCode or 1 },
		faultString = message or err.faultString or "fatal error",
	})
end

---------------------------------------------------------------------
-- @param ok Boolean indicating if the response was correct or a
--	fault one.
-- @param params Object containing the response contents.
-- @return String representing the `methodResponse' XML-RPC element.
---------------------------------------------------------------------
function toxml.methodResponse (ok, params)
	local resp
	if ok then
		resp = toxml.params (params)
	else
		resp = toxml.fault (params)
	end
	return format (formats.methodResponse, resp)
end

---------------------------------------------------------------------
-- End of converter from Lua to XML-RPC.
---------------------------------------------------------------------


---------------------------------------------------------------------
-- Create a representation of an array with the given element type.
---------------------------------------------------------------------
function newArray (elemtype)
	return { type = "array", elemtype = elemtype, }
end

---------------------------------------------------------------------
-- Create a representation of a structure with the given members.
---------------------------------------------------------------------
function newStruct (members)
	return { type = "struct", elemtype = members, }
end

---------------------------------------------------------------------
-- Create a representation of a value according to a type.
-- @param val Any Lua value.
-- @param typ A XML-RPC type.
---------------------------------------------------------------------
function newTypedValue (val, typ)
	return { ["*type"] = typ, ["*value"] = val }
end

---------------------------------------------------------------------
-- Create the XML-RPC string used to call a method.
-- @param method String with method name.
-- @param ... Parameters to the call.
-- @return String with the XML string/document.
---------------------------------------------------------------------
function clEncode (method, ...)
	return toxml.methodCall (method, ...)
end

---------------------------------------------------------------------
-- Convert the method response document to a Lua table.
-- @param meth_resp String with XML document.
-- @return Boolean indicating whether the call was successful or not;
--	and a Lua object with the converted response element.
---------------------------------------------------------------------
function clDecode (meth_resp)
	local d = parse (meth_resp)
	if type(d) ~= "table" then
		error ("Not an XML document: "..meth_resp)
	end
	return x2methodResponse (d)
end

---------------------------------------------------------------------
-- Convert the method call (client request) document to a name and
--	a list of parameters.
-- @param request String with XML document.
-- @return String with method's name AND the table of arguments.
---------------------------------------------------------------------
function srvDecode (request)
	local d = parse (request)
	if type(d) ~= "table" then
		error ("Not an XML document: "..request)
	end
	return x2methodCall (d)
end

---------------------------------------------------------------------
-- Convert a table into an XML-RPC methodReponse element.
-- @param obj Lua object.
-- @param is_fault Boolean indicating wether the result should be
--	a `fault' element (default = false).
-- @return String with XML-RPC response.
---------------------------------------------------------------------
function srvEncode (obj, is_fault)
	local ok = not (is_fault or false)
	return toxml.methodResponse (ok, obj)
end

---------------------------------------------------------------------
-- Register the methods.
-- @param tab_or_func Table or mapping function.
-- If a table is given, it can have one level of objects and then the
-- methods;
-- if a function is given, it will be used as the dispatcher.
-- The given function should return a Lua function that implements.
---------------------------------------------------------------------
dispatch = error
function srvMethods (tab_or_func)
	local t = type (tab_or_func)
	if t == "function" then
		dispatch = tab_or_func
	elseif t == "table" then
		dispatch = function (name)
			local ok, _, obj, method = strfind (name, "^([^.]+)%.(.+)$")
			if not ok then
				return tab_or_func[name]
			else
				if tab_or_func[obj] and tab_or_func[obj][method] then
					return function (...)
						return tab_or_func[obj][method] (obj, ...)
					end
				else
					return nil
				end
			end
		end
	else
		error ("Argument is neither a table nor a function")
	end
end