-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pam: also set teleport-specific env vars via pam_putenv #3725
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,7 @@ package pam | |
// extern void writeCallback(int n, int s, char* c); | ||
// extern struct pam_conv *make_pam_conv(int); | ||
// extern int _pam_start(void *, const char *, const char *, const struct pam_conv *, pam_handle_t **); | ||
// extern int _pam_putenv(void *, pam_handle_t *, const char *); | ||
// extern int _pam_end(void *, pam_handle_t *, int); | ||
// extern int _pam_authenticate(void *, pam_handle_t *, int); | ||
// extern int _pam_acct_mgmt(void *, pam_handle_t *, int); | ||
|
@@ -43,7 +44,9 @@ import "C" | |
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
"sync" | ||
"syscall" | ||
|
@@ -280,6 +283,22 @@ func Open(config *Config) (*PAM, error) { | |
return nil, p.codeToError(p.retval) | ||
} | ||
|
||
for k, v := range config.Env { | ||
// Set a regular OS env var on this process which should be available | ||
// to child PAM processes. | ||
os.Setenv(k, v) | ||
|
||
// Also set it via PAM-specific pam_putenv, which is respected by | ||
// pam_exec (and possibly others), where parent env vars are not. | ||
kv := C.CString(fmt.Sprintf("%s=%s", k, v)) | ||
// pam_putenv makes a copy of kv, so we can free it right away. | ||
defer C.free(unsafe.Pointer(kv)) | ||
retval := C._pam_putenv(pamHandle, p.pamh, kv) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://www.linux-pam.org/Linux-PAM-html/adg-interface-by-app-expected.html#adg-pam_putenv doesn't mention any limits and their source code seems to allocate as much as needed. |
||
if retval != C.PAM_SUCCESS { | ||
return nil, p.codeToError(retval) | ||
} | ||
} | ||
|
||
// Check that the *nix account is valid. Checking an account varies based off | ||
// the PAM modules used in the account stack. Typically this consists of | ||
// checking if the account is expired or has access restrictions. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C.CString
does a malloc under the hood. Will PAM free this memory? If not you'll need to callC.free
yourself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, nice catch, added
C.free
.First time using cgo, appreciate all the advice!