Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
sbws
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
juga
sbws
Commits
2dddc9a7
Commit
2dddc9a7
authored
7 years ago
by
Matt Traudt
Browse files
Options
Downloads
Patches
Plain Diff
Log the client name who authenticated to the server
parent
50f02f29
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
sbws/commands/server.py
+7
-7
7 additions, 7 deletions
sbws/commands/server.py
sbws/util/simpleauth.py
+25
-19
25 additions, 19 deletions
sbws/util/simpleauth.py
with
32 additions
and
26 deletions
sbws/commands/server.py
+
7
−
7
View file @
2dddc9a7
...
...
@@ -99,13 +99,15 @@ def write_to_client(sock, conf, amount):
return
True
def
new_thread
(
args
,
conf
,
sock
,
passwords
):
def
new_thread
(
args
,
conf
,
sock
):
def
closure
():
if
not
authenticate_client
(
sock
,
passwords
,
log
.
info
):
client_name
=
authenticate_client
(
sock
,
conf
[
'
server.passwords
'
],
log
.
info
)
if
not
client_name
:
log
.
info
(
'
Client did not provide valid auth
'
)
close_socket
(
sock
)
return
log
.
debug
(
'
Client authed successfully
'
)
log
.
notice
(
client_name
,
'
authenticated on
'
,
sock
.
fileno
()
)
while
True
:
send_amount
=
get_send_amount
(
sock
)
if
send_amount
is
None
:
...
...
@@ -124,9 +126,7 @@ def main(args, conf, log_):
if
not
is_initted
(
args
.
directory
):
fail_hard
(
'
Sbws isn
\'
t initialized. Try sbws init
'
,
log
=
log
)
passwords
=
[
conf
[
'
server.passwords
'
][
key
]
for
key
in
conf
[
'
server.passwords
'
]]
if
len
(
passwords
)
<
1
:
if
len
(
conf
[
'
server.passwords
'
])
<
1
:
fail_hard
(
'
Sbws server needs at least one password
'
,
log
=
log
)
server
=
socket
.
socket
(
socket
.
AF_INET
,
socket
.
SOCK_STREAM
)
...
...
@@ -146,7 +146,7 @@ def main(args, conf, log_):
while
True
:
sock
,
addr
=
server
.
accept
()
log
.
info
(
'
accepting connection from
'
,
addr
,
'
as
'
,
sock
.
fileno
())
t
=
new_thread
(
args
,
conf
,
sock
,
passwords
)
t
=
new_thread
(
args
,
conf
,
sock
)
t
.
start
()
except
KeyboardInterrupt
:
pass
...
...
This diff is collapsed.
Click to expand it.
sbws/util/simpleauth.py
+
25
−
19
View file @
2dddc9a7
...
...
@@ -7,51 +7,52 @@ PW_LEN = 64
WIRE_PROTO_VER
=
b
'
1
'
def
authenticate_client
(
sock
,
passwords
,
log_fn
=
print
):
def
authenticate_client
(
sock
,
conf_section
,
log_fn
=
print
):
'''
Use this on the server side to read bytes from the client and properly
authenticate them. Return True if the client checks out, otherwise False.
authenticate them. Return the name of the client who has authenticated if
they provided a good password, otherwise None.
'''
assert
sock
.
fileno
()
>
0
assert
isinstance
(
passwords
,
list
)
assert
len
(
passwords
)
>
0
assert
len
(
conf_section
)
>
0
try
:
magic
=
sock
.
recv
(
len
(
MAGIC_BYTES
))
except
socket
.
timeout
as
e
:
log_fn
(
e
)
return
Fals
e
return
Non
e
if
magic
!=
MAGIC_BYTES
:
log_fn
(
'
Magic string doesn
\'
t match
'
)
return
Fals
e
return
Non
e
try
:
line
=
read_line
(
sock
,
max_len
=
4
,
log_fn
=
log_fn
)
except
socket
.
timeout
as
e
:
log_fn
(
e
)
return
Fals
e
return
Non
e
if
line
!=
str
(
WIRE_PROTO_VER
,
'
utf-8
'
):
log_fn
(
'
Client gave protocol version {} but we support {}
'
.
format
(
line
,
str
(
WIRE_PROTO_VER
,
'
utf-8
'
)))
return
Fals
e
return
Non
e
try
:
pw
=
str
(
sock
.
recv
(
PW_LEN
),
'
utf-8
'
)
except
UnicodeDecodeError
:
log_fn
(
'
Non-unicode password string received
'
)
return
Fals
e
return
Non
e
except
socket
.
timeout
as
e
:
log_fn
(
e
)
return
Fals
e
return
Non
e
if
not
_is_valid_password
(
pw
,
passwords
):
client_name
=
_is_valid_password
(
pw
,
conf_section
)
if
not
client_name
:
log_fn
(
'
Invalid password
'
)
return
Fals
e
return
Non
e
try
:
sock
.
send
(
SUCCESS_BYTES
)
except
(
ConnectionResetError
,
BrokenPipeError
)
as
e
:
log_fn
(
e
)
return
Fals
e
return
Tru
e
return
Non
e
return
client_nam
e
def
authenticate_to_server
(
sock
,
pw
,
log_fn
=
print
):
...
...
@@ -74,9 +75,14 @@ def authenticate_to_server(sock, pw, log_fn=print):
return
True
def
_is_valid_password
(
pw
,
passwords
):
assert
isinstance
(
passwords
,
list
)
assert
len
(
passwords
)
>
0
if
len
(
pw
)
==
PW_LEN
and
pw
in
passwords
:
return
True
def
_is_valid_password
(
pw
,
conf_section
):
'''
Returns the key in the [server.passwords] section of the config for the
password the client provided (AKA: if the client provided a valid
password). Otherwise return None
'''
assert
len
(
conf_section
)
>
0
if
len
(
pw
)
!=
PW_LEN
:
return
None
for
key
in
conf_section
.
keys
():
if
pw
==
conf_section
[
key
]:
return
key
return
False
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment