Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60974010
keep_cred.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, May 3, 17:42
Size
1 KB
Mime Type
text/x-python
Expires
Sun, May 5, 17:42 (2 d)
Engine
blob
Format
Raw Data
Handle
17447813
Attached To
R8811 EPFL Roaming
keep_cred.py
View Options
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author : Bancal Samuel
# Created : 100831
# Updates : 110103
# Requisite :
# Description :
# - get username/password
# - encode it
# - store it to tmp file
import
os
import
sys
import
pwd
import
pickle
import
ldap
import
re
SERVICE
=
os
.
environ
[
"PAM_SERVICE"
]
TYPE
=
os
.
environ
[
"PAM_TYPE"
]
USERNAME
=
os
.
environ
[
"PAM_USER"
]
class
UserIdentity
():
"""
Become user or back to root
"""
def
__init__
(
self
):
pass
def
__enter__
(
self
):
pw
=
pwd
.
getpwnam
(
USERNAME
)
os
.
seteuid
(
pw
.
pw_uid
)
def
__exit__
(
self
,
type
,
value
,
traceback
):
os
.
seteuid
(
0
)
#~ DOMAIN = get_domain()
#TTY = os.environ["PAM_TTY"]
print
"USERNAME
%s
"
%
USERNAME
print
"SERVICE
%s
"
%
SERVICE
print
"TYPE
%s
"
%
TYPE
CRED_FILENAME
=
"/tmp/
%s
_epfl_cred"
%
USERNAME
if
TYPE
!=
"auth"
:
sys
.
exit
(
0
)
PASSWORD
=
sys
.
stdin
.
readline
()
.
rstrip
(
chr
(
0
))
def
save_credentials
(
username
=
USERNAME
,
password
=
PASSWORD
):
# Encode credential
def
encode
(
username
,
password
):
username
=
unicode
(
username
,
'utf-8'
)
password
=
unicode
(
password
,
'utf-8'
)
factor
=
len
(
password
)
/
len
(
username
)
+
1
key
=
username
*
factor
enc_password
=
""
.
join
([
unichr
(
ord
(
password
[
i
])
+
ord
(
key
[
i
]))
for
i
in
range
(
0
,
len
(
password
))
])
return
enc_password
try
:
with
open
(
CRED_FILENAME
,
"wb"
)
as
f
:
enc_password
=
encode
(
username
,
password
)
pickle
.
dump
(
enc_password
,
f
)
except
IOError
:
print
"Error: Could not write to file
%s
"
%
CRED_FILENAME
sys
.
exit
(
1
)
save_credentials
(
USERNAME
,
PASSWORD
)
pw
=
pwd
.
getpwnam
(
USERNAME
)
os
.
chown
(
CRED_FILENAME
,
pw
.
pw_uid
,
pw
.
pw_gid
)
os
.
chmod
(
CRED_FILENAME
,
0600
)
sys
.
exit
(
0
)
Event Timeline
Log In to Comment