social_stream-presence 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -52,8 +52,8 @@ stop(Host) ->
52
52
  on_register_connection(_SID, _JID, _Info) ->
53
53
  {_A,User,_B,_C,_D,_E,_F} = _JID,
54
54
  ?INFO_MSG("mod_sspresence: on_register_connection (~p)", [User]),
55
- Login_path = string:concat(getOptionValue("scripts_path="), "/set_connection_script "),
56
- os:cmd(string:join([Login_path, User ], " ")),
55
+ Rest_api_script_path = string:concat(getOptionValue("scripts_path="), "/rest_api_client_script "),
56
+ os:cmd(string:join([Rest_api_script_path, "setConnection", User ], " ")),
57
57
  ok.
58
58
 
59
59
  on_remove_connection(_SID, _JID, _SessionInfo) ->
@@ -62,8 +62,8 @@ on_remove_connection(_SID, _JID, _SessionInfo) ->
62
62
  Connected = isConnected(User),
63
63
  case Connected of
64
64
  true -> ok;
65
- _ -> Logout_path = string:concat(getOptionValue("scripts_path="), "/unset_connection_script "),
66
- os:cmd(string:join([Logout_path, User ], " "))
65
+ _ -> Rest_api_script_path = string:concat(getOptionValue("scripts_path="), "/rest_api_client_script "),
66
+ os:cmd(string:join([Rest_api_script_path, "unsetConnection", User ], " "))
67
67
  end,
68
68
  ok.
69
69
 
@@ -73,19 +73,19 @@ on_presence(User, _Server, _Resource, Packet) ->
73
73
 
74
74
  case Type of
75
75
  "presence" -> Status = getStatusFromSubel(Subel),
76
- Presence_path = string:concat(getOptionValue("scripts_path="), "/set_presence_script "),
76
+ Rest_api_script_path = string:concat(getOptionValue("scripts_path="), "/rest_api_client_script "),
77
77
  ?INFO_MSG("mod_sspresence: set_presence_script call with user (~p) and status (~p)", [User,Status]),
78
- os:cmd(string:join([Presence_path, User , Status], " "));
78
+ os:cmd(string:join([Rest_api_script_path, "setPresence", User , Status], " "));
79
79
  _ -> ok
80
80
  end,
81
81
  ok.
82
82
 
83
83
  on_unset_presence(User, _Server, _Resource, _Status) ->
84
84
  ?INFO_MSG("mod_sspresence: on_unset_presence (~p)", [User]),
85
- _UPresence_path = string:concat(getOptionValue("scripts_path="), "/unset_presence_script "),
85
+ _Rest_api_script_path = string:concat(getOptionValue("scripts_path="), "/rest_api_client_script "),
86
86
  %% Wait for on_remove_connection
87
87
  %% ?INFO_MSG("mod_sspresence: unset_presence_script call with user (~p)", [User]),
88
- %%os:cmd(string:join([UPresence_path, User , Status], " "));
88
+ %%os:cmd(string:join([_Rest_api_script_path, "unsetPresence", User , Status], " ")),
89
89
  ok.
90
90
 
91
91
  on_packet_send(_From, _To, {xmlelement, Type, _Attr, _Subel} = _Packet) ->
@@ -185,8 +185,8 @@ end.
185
185
 
186
186
  %Reset all connections
187
187
  reset_connections() ->
188
- Reset_path = string:concat(getOptionValue("scripts_path="), "/reset_connection_script "),
189
- os:cmd(Reset_path),
188
+ Rest_api_script_path = string:concat(getOptionValue("scripts_path="), "/rest_api_client_script "),
189
+ os:cmd(string:join([Rest_api_script_path, "resetConnection"], " ")),
190
190
  ok.
191
191
 
192
192
 
@@ -6,13 +6,15 @@ SocialStream::Presence.setup do |config|
6
6
  #Configures Authentication Method: "cookie" or "password"
7
7
  config.auth_method = "cookie"
8
8
  #Configures XMPP Server Password
9
- config.xmpp_server_password = <%= SecureRandom.hex(64).inspect %>
9
+ config.xmpp_server_password = <%= SecureRandom.hex(16).inspect %>
10
+ #Uncomment to enable REST API Security
11
+ #config.secure_rest_api = true
10
12
  #Remote or local mode
11
13
  config.remote_xmpp_server = false
12
14
  #Scripts path to execute ejabberd scripts: local or remote
13
15
  config.scripts_path = "/scripts_path"
14
16
  #Ejabberd module path in the xmpp server
15
- config.ejabberd_module_path = "/lib/ejabberd/ebin"
17
+ config.ejabberd_module_path = "/usr/lib/ejabberd/ebin"
16
18
  #Uncomment to enable Social Stream Presence
17
19
  #config.enable = true
18
20
 
@@ -27,6 +29,6 @@ SocialStream::Presence.setup do |config|
27
29
  #Username of the the Social Stream Admin sid
28
30
  config.social_stream_presence_username = "social_stream-presence"
29
31
  #Configures Social Stream Rails App Password
30
- config.password = <%= SecureRandom.hex(64).inspect %>
32
+ config.password = <%= SecureRandom.hex(32).inspect %>
31
33
 
32
- end
34
+ end
@@ -20,6 +20,7 @@ module SocialStream
20
20
  mattr_accessor :bosh_service
21
21
  mattr_accessor :auth_method
22
22
  mattr_accessor :xmpp_server_password
23
+ mattr_accessor :secure_rest_api
23
24
  mattr_accessor :remote_xmpp_server
24
25
  mattr_accessor :scripts_path
25
26
  mattr_accessor :ejabberd_module_path
@@ -34,6 +35,7 @@ module SocialStream
34
35
 
35
36
  @@auth_method = "cookie"
36
37
  @@remote_xmpp_server = false
38
+ @@secure_rest_api = false
37
39
  @@enable = false
38
40
 
39
41
  class << self
@@ -1,5 +1,5 @@
1
1
  module Socialstream
2
2
  module Presence
3
- VERSION = "0.1.4"
3
+ VERSION = "0.1.6"
4
4
  end
5
5
  end
@@ -5,6 +5,9 @@ require 'xmpp4r/client'
5
5
  require 'xmpp4r/message'
6
6
  require 'net/ssh'
7
7
  require 'net/sftp'
8
+ require 'openssl'
9
+ require 'digest/md5'
10
+
8
11
 
9
12
  module SocialStream
10
13
  module Presence
@@ -111,18 +114,18 @@ module SocialStream
111
114
 
112
115
  #Installation methods
113
116
 
114
- def copyFolder(oPath,dPath)
117
+ def copyFolderToXmppServer(oPath,dPath)
115
118
  if SocialStream::Presence.remote_xmpp_server
116
119
  #Remote mode
117
- copyRemoteFolder(oPath,dPath)
120
+ copyFolderToXmppServerRemote(oPath,dPath)
118
121
  else
119
122
  #Local mode
120
- SocialStream::Presence::XmppServerOrder::executeCommand("cp -r " + oPath + "/* " + dPath)
123
+ executeCommand("cp -r " + oPath + "/* " + dPath)
121
124
  end
122
125
  end
123
126
 
124
127
 
125
- def copyRemoteFolder(localPath,remotePath)
128
+ def copyFolderToXmppServerRemote(localPath,remotePath)
126
129
  begin
127
130
  if SocialStream::Presence.ssh_password
128
131
 
@@ -150,7 +153,15 @@ module SocialStream
150
153
  end
151
154
 
152
155
 
153
- def recursiveCopyFolder(localPath,remotePath,sftp)
156
+ def recursiveCopyFolder(localPath,remotePath,sftp)
157
+
158
+ #Check if localPath is a file
159
+ if File.file?(localPath) and File.file?(remotePath)
160
+ puts "Copy files..."
161
+ sftp.upload(localPath, remotePath)
162
+ return
163
+ end
164
+
154
165
  # Create directory if not exits
155
166
  sftp.mkdir(remotePath)
156
167
  # Upload files to the remote host
@@ -172,6 +183,7 @@ module SocialStream
172
183
 
173
184
  autoconf.push("scripts_path=" + SocialStream::Presence.scripts_path)
174
185
  autoconf.push("ejabberd_password=" + SocialStream::Presence.xmpp_server_password)
186
+ autoconf.push("secure_rest_api=" + SocialStream::Presence.secure_rest_api.to_s)
175
187
  autoconf.push("server_domain=" + SocialStream::Presence.domain)
176
188
  autoconf.push("cookie_name=" + Rails.application.config.session_options[:key])
177
189
 
@@ -220,6 +232,53 @@ module SocialStream
220
232
  end
221
233
  end
222
234
 
235
+ def generateRSAKeys(keysPath)
236
+
237
+ unless File.directory?(keysPath)
238
+ return "Keys path not exists"
239
+ end
240
+
241
+ web_public_key_path=keysPath+"/web_rsa_key_public.pem"
242
+ web_private_key_path=keysPath+"/web_rsa_key_private.pem"
243
+ xmpp_public_key_path=keysPath+"/xmpp_rsa_key_public.pem"
244
+ xmpp_private_key_path=keysPath+"/xmpp_rsa_key_private.pem"
245
+
246
+ # .generate creates an object containing both keys
247
+ web_rsa_key = OpenSSL::PKey::RSA.generate( 1024 )
248
+ xmpp_rsa_key = OpenSSL::PKey::RSA.generate( 1024 )
249
+
250
+ #Write keys as PEM's
251
+
252
+ #Public Key
253
+ web_rsa_key_public = web_rsa_key.public_key
254
+ xmpp_rsa_key_public = xmpp_rsa_key.public_key
255
+ output_public = File.new(web_public_key_path, "w")
256
+ output_public.puts web_rsa_key_public
257
+ output_public.close
258
+ output_public = File.new(xmpp_public_key_path, "w")
259
+ output_public.puts xmpp_rsa_key_public
260
+ output_public.close
261
+ puts "New Web Server public key stored in #{web_public_key_path}\n"
262
+ #puts "New Web Server public key:\n#{web_rsa_key_public}\n"
263
+ puts "New Xmpp Server public key stored in #{xmpp_public_key_path}\n"
264
+ #puts "New Xmpp Server public key:\n#{xmpp_rsa_key_public}\n"
265
+
266
+ #Private Key
267
+ web_rsa_key_private = web_rsa_key.to_pem
268
+ xmpp_rsa_key_private = xmpp_rsa_key.to_pem
269
+ output_private = File.new(web_private_key_path, "w")
270
+ output_private.puts web_rsa_key_private
271
+ output_private.close
272
+ output_private = File.new(xmpp_private_key_path, "w")
273
+ output_private.puts xmpp_rsa_key_private
274
+ output_private.close
275
+ puts "New Web Server private key stored in #{web_private_key_path}\n"
276
+ #puts "New Web Server private key:\n#{web_rsa_key_private}\n"
277
+ puts "New Xmpp Server private key stored in #{xmpp_private_key_path}\n"
278
+ #puts "New Xmpp Server private key:\n#{xmpp_rsa_key_private}\n"
279
+
280
+ puts "Finish"
281
+ end
223
282
 
224
283
  #Execution commands manage
225
284
 
@@ -264,38 +323,56 @@ module SocialStream
264
323
  return output
265
324
  end
266
325
 
326
+ def executeLocalCommand(command)
327
+ puts "Executing " + parsingCommand(command)
328
+ return executeLocalCommands([command])
329
+ end
330
+
267
331
  def executeLocalCommands(commands)
268
- output="No command received";
332
+ if commands.empty?
333
+ return "No command received";
334
+ end
335
+ output=""
269
336
  commands.each do |command|
270
- output = %x[#{command}];
337
+ response = %x[#{command}]
338
+ output = output + "\n" + response;
271
339
  end
272
340
  return output
273
341
  end
274
342
 
275
343
  def executeRemoteCommands(commands)
276
- output="No command received";
344
+ if commands.empty?
345
+ return "No command received";
346
+ end
277
347
 
278
348
  begin
349
+ output="";
279
350
  if SocialStream::Presence.ssh_password
280
351
  Net::SSH.start( SocialStream::Presence.ssh_domain, SocialStream::Presence.ssh_user, :password => SocialStream::Presence.ssh_password, :auth_methods => ["password"]) do |session|
281
352
  commands.each do |command|
282
- output = session.exec!(command)
353
+ response = session.exec!(command)
354
+ if response != nil
355
+ output = output + "\n" + response
356
+ end
283
357
  end
284
358
  end
285
359
  else
286
360
  #SSH with authentication key instead of password
287
361
  Net::SSH.start( SocialStream::Presence.ssh_domain, SocialStream::Presence.ssh_user) do |session|
288
362
  commands.each do |command|
289
- output = session.exec!(command)
363
+ response = session.exec!(command)
364
+ if response != nil
365
+ output = output + "\n" + response
366
+ end
290
367
  end
291
368
  end
292
369
  end
293
370
  rescue Exception => e
294
371
  case e
295
372
  when Net::SSH::AuthenticationFailed
296
- output = "AuthenticationFailed on remote access"
373
+ return "ERROR: AuthenticationFailed on remote access"
297
374
  else
298
- output = "Unknown exception in executeRemoteCommands method: #{e.to_s}"
375
+ return "ERROR: Unknown exception in executeRemoteCommands method: #{e.to_s}"
299
376
  end
300
377
  end
301
378
 
@@ -303,6 +380,97 @@ module SocialStream
303
380
  end
304
381
 
305
382
 
383
+ #Authorization methods
384
+ def authorization(params)
385
+ case SocialStream::Presence.secure_rest_api
386
+ when true
387
+ #Authorization using asymmetric RSA keys
388
+ begin
389
+ presence_root = File.expand_path("../../../../", __FILE__)
390
+ xmpp_public_key_path = presence_root + "/rsa_keys/xmpp_rsa_key_public.pem";
391
+ xmpp_public_key = OpenSSL::PKey::RSA.new(File.read(xmpp_public_key_path))
392
+
393
+ stamp = xmpp_public_key.public_decrypt( params[:password] )
394
+
395
+ #stamp = password#####timestamp#####hash
396
+ stampParams=stamp.split("#####")
397
+ password = stampParams[0]
398
+ timestamp = stampParams[1]
399
+ hash = stampParams[2]
400
+
401
+ myHash = calculateHash(params)
402
+
403
+ #Evaluating Hash
404
+ if (myHash != hash)
405
+ #Hash not valid
406
+ return false
407
+ end
408
+
409
+ #Evaluating Timestamp
410
+ if ((Time.now.utc - Time.parse(timestamp).utc).to_int > (10*60))
411
+ #Timestamp not valid
412
+ return false
413
+ end
414
+
415
+ #Evaluating Password
416
+ return ( password == SocialStream::Presence.xmpp_server_password )
417
+ rescue
418
+ return false
419
+ end
420
+ else
421
+ #Basic authorization
422
+ return ( params[:password] and params[:password] == SocialStream::Presence.xmpp_server_password )
423
+ end
424
+ end
425
+
426
+
427
+ def calculateHash(params)
428
+ if params
429
+ params.delete("password")
430
+ params.delete("controller")
431
+ params.delete("action")
432
+ else
433
+ params = {};
434
+ end
435
+
436
+ hash = "";
437
+ params.each do |key,value|
438
+ hash = hash + key.to_s + value.to_s
439
+ end
440
+ return Digest::MD5.hexdigest(hash)
441
+ end
442
+
443
+
444
+ def decryptParams(params)
445
+ case SocialStream::Presence.secure_rest_api
446
+ when true
447
+ #Secure Mode
448
+ begin
449
+ if params[:encrypted_params]
450
+ presence_root = File.expand_path("../../../../", __FILE__)
451
+ web_private_key_path = presence_root + "/rsa_keys/web_rsa_key_private.pem";
452
+ private_key = OpenSSL::PKey::RSA.new(File.read(web_private_key_path))
453
+
454
+ clear_params_hash_string = private_key.private_decrypt( params[:encrypted_params] )
455
+ clear_params = getHashFromHashString(clear_params_hash_string)
456
+ params.delete("encrypted_params")
457
+
458
+ clear_params.each do |key,value|
459
+ params[key] = value
460
+ end
461
+ end
462
+
463
+ return params
464
+ rescue
465
+ return "Error in function: decryptParam(param)"
466
+ end
467
+
468
+ else
469
+ #Non Secure Mode
470
+ return params
471
+ end
472
+ end
473
+
306
474
 
307
475
  #Help methods
308
476
 
@@ -316,6 +484,16 @@ module SocialStream
316
484
  #Hide passwords on sudo commands: command pattern = "echo password | sudo -S order"
317
485
  return command.gsub(/echo ([aA-zZ]+) [|] sudo -S [.]*/,"echo ****** | sudo -S \\2")
318
486
  end
487
+
488
+ def getHashFromHashString(hashString)
489
+ hash={}
490
+ hashString[1..-2].split(/, /).each {|entry|
491
+ entryMap=entry.split(/=>/);
492
+ value_str = entryMap[1];
493
+ hash[entryMap[0].strip[1..-1].to_sym] = value_str.nil? ? "" : value_str.strip[1..-2]
494
+ }
495
+ return hash
496
+ end
319
497
 
320
498
  #Xmpp client manage methods
321
499
 
@@ -1,6 +1,6 @@
1
1
  namespace :presence do
2
2
  desc 'Copy ejabberd files to the xmpp server and write configuration files'
3
- task :install => [ 'presence:install:copy_xmpp_server_files', 'presence:install_xmpp_server' , 'presence:autoconfigure_xmpp_server' ]
3
+ task :install => [ 'presence:install:copy_xmpp_server_files', 'presence:install_xmpp_server' , 'presence:autoconfigure_xmpp_server' , 'presence:generate_RSA_keys' ]
4
4
 
5
5
  namespace :install do
6
6
 
@@ -15,7 +15,7 @@ namespace :presence do
15
15
  #Cleaning dpath
16
16
  SocialStream::Presence::XmppServerOrder::executeCommands(["rm -r " + dpath,"mkdir -p " + dpath])
17
17
  #Copy folders
18
- SocialStream::Presence::XmppServerOrder::copyFolder(opath,dpath)
18
+ SocialStream::Presence::XmppServerOrder::copyFolderToXmppServer(opath,dpath)
19
19
 
20
20
  puts "Social Stream Ejabberd files copied to " + dpath + " in Xmpp Server"
21
21
  puts "Copy_xmpp_server_files: Task complete"
@@ -63,9 +63,12 @@ namespace :presence do
63
63
  commands << "echo " + password + " | sudo -S " + SocialStream::Presence.scripts_path + "/sstream_ejabberd_files/installer.sh \"ejabberd_module_path=" + SocialStream::Presence.ejabberd_module_path + "\" \"scripts_path=" + SocialStream::Presence.scripts_path + "\" \"" + options + "\""
64
64
 
65
65
  #Execution order
66
- output = SocialStream::Presence::XmppServerOrder::executeCommands(commands)
67
-
66
+ output = SocialStream::Presence::XmppServerOrder::executeCommands(commands)
68
67
  puts output
68
+
69
+ #Generate RSA Keys
70
+ Rake::Task["presence:install:generate_RSA_keys"].execute
71
+
69
72
  puts "Installation complete"
70
73
  end
71
74
 
@@ -96,5 +99,36 @@ namespace :presence do
96
99
  puts "Autoconfigure complete"
97
100
  end
98
101
 
102
+
103
+ desc "Generate and distribute RSA Keys"
104
+ task :generate_RSA_keys => :environment do
105
+ puts "Starting presence:generate_RSA_keys"
106
+
107
+ presence_root = File.expand_path("../../../../", __FILE__)
108
+ webKeysPath = presence_root + "/rsa_keys";
109
+ xmppKeysPath = SocialStream::Presence.scripts_path + "/rsa_keys";
110
+
111
+ puts "Cleaning previous keys"
112
+ #Cleaning Keys path
113
+ SocialStream::Presence::XmppServerOrder::executeLocalCommand("rm -r " + webKeysPath)
114
+ SocialStream::Presence::XmppServerOrder::executeLocalCommand("mkdir -p " + webKeysPath)
115
+ SocialStream::Presence::XmppServerOrder::executeCommands(["rm -r " + xmppKeysPath,"mkdir -p " + xmppKeysPath])
116
+
117
+ puts "Generating new keys"
118
+ #Generating RSA Keys
119
+ SocialStream::Presence::XmppServerOrder::generateRSAKeys(webKeysPath)
120
+
121
+ puts "Copy keys to the Xmpp Server"
122
+ #Copy Xmpp Keys to the Xmpp Server
123
+ SocialStream::Presence::XmppServerOrder::copyFolderToXmppServer(webKeysPath,xmppKeysPath)
124
+
125
+ #Remove not own private keys
126
+ puts "Removing not own private keys"
127
+ SocialStream::Presence::XmppServerOrder::executeLocalCommand("rm -r " + webKeysPath + "/xmpp_rsa_key_private.pem")
128
+ SocialStream::Presence::XmppServerOrder::executeCommands(["rm -r " + xmppKeysPath + "/web_rsa_key_private.pem"])
129
+
130
+ puts "Generate_RSA_keys: Task complete"
131
+ end
132
+
99
133
  end
100
134
  end