1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-11-30 11:18:12 +00:00

add option to set environment variables

This commit is contained in:
Greg Alexander 2018-05-16 16:23:12 -04:00
parent a75d0b0d1c
commit 9efa856065
7 changed files with 61 additions and 5 deletions

1
NOTES
View File

@ -530,7 +530,6 @@ environment...just one name=value per line.
XXX - document su change XXX - document su change
XXX - add environment variable option (one name=value per line, just passed into setenv())
XXX - document environment variables XXX - document environment variables

View File

@ -570,6 +570,50 @@ int spawn_command(void(*exec_fn)(void *user_data), void *exec_data,
} }
} }
static void
install_environment(void)
{
char *s = conf_env;
if (!s) {
return;
}
while (*s) {
char *name, *val;
int name_len, val_len;
name = s;
while (*s && (*s != '=') && (*s != '\r') && (*s != '\n')) {
s++;
}
name_len = s-name;
if (*s == '=') {
s++;
val = s;
while (*s && (*s != '\r') && (*s != '\n')) {
s++;
}
val_len = s-val;
} else {
val = "";
val_len = 0;
}
while ((*s == '\r') || (*s == '\n')) {
s++;
}
if (name_len) {
char *n = m_malloc(name_len+1);
char *v = m_malloc(val_len+1);
memcpy(n, name, name_len);
memcpy(v, val, val_len);
n[name_len] = 0;
v[val_len] = 0;
setenv(n, v, /*overwrite=*/1); /* might fail *shrug* */
}
}
}
/* Runs a command with "sh -c". Will close FDs (except stdin/stdout/stderr) and /* Runs a command with "sh -c". Will close FDs (except stdin/stdout/stderr) and
* re-enabled SIGPIPE. If cmd is NULL, will run a login shell. * re-enabled SIGPIPE. If cmd is NULL, will run a login shell.
*/ */
@ -626,6 +670,8 @@ void run_shell_command(const char* cmd, unsigned int maxfd, char* usershell) {
m_close(i); m_close(i);
} }
install_environment();
execv(usershell, argv); execv(usershell, argv);
} }

View File

@ -41,6 +41,7 @@ extern const char *conf_shell;
extern const char *conf_home; extern const char *conf_home;
const char *conf_path_file(const char *fn); const char *conf_path_file(const char *fn);
extern int conf_rsyncbuffer; extern int conf_rsyncbuffer;
extern const char *conf_env;
#endif /* __CONFIG_H__ */ #endif /* __CONFIG_H__ */

View File

@ -9,7 +9,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <ctype.h> #include <ctype.h>
const char *conf_path = "", *conf_shell = "", *conf_home = ""; const char *conf_path = "", *conf_shell = "", *conf_home = "", *conf_env = "";
int conf_rsyncbuffer = 0; int conf_rsyncbuffer = 0;
/* NB - this will leak memory like crazy if called often.... */ /* NB - this will leak memory like crazy if called often.... */
@ -116,7 +116,7 @@ JNIEXPORT jint JNICALL
Java_org_galexander_sshd_SimpleSSHDService_start_1sshd(JNIEnv *env_, Java_org_galexander_sshd_SimpleSSHDService_start_1sshd(JNIEnv *env_,
jclass cl, jclass cl,
jint port, jobject jpath, jobject jshell, jobject jhome, jobject jextra, jint port, jobject jpath, jobject jshell, jobject jhome, jobject jextra,
jint rsyncbuffer) jint rsyncbuffer, jobject jenv)
{ {
pid_t pid; pid_t pid;
const char *extra; const char *extra;
@ -129,6 +129,7 @@ Java_org_galexander_sshd_SimpleSSHDService_start_1sshd(JNIEnv *env_,
conf_home = from_java_string(jhome); conf_home = from_java_string(jhome);
extra = from_java_string(jextra); extra = from_java_string(jextra);
conf_rsyncbuffer = rsyncbuffer; conf_rsyncbuffer = rsyncbuffer;
conf_env = from_java_string(jenv);
pid = fork(); pid = fork();
if (pid == 0) { if (pid == 0) {

View File

@ -44,4 +44,9 @@
android:title="Extra Commandline" android:title="Extra Commandline"
android:summary="Commandline options for dropbear server" android:summary="Commandline options for dropbear server"
android:defaultValue="" /> android:defaultValue="" />
<EditTextPreference
android:key="env"
android:title="Environment Variables"
android:summary="Environment variables (one &quot;name=value&quot; per line)."
android:defaultValue="" />
</PreferenceScreen> </PreferenceScreen>

View File

@ -46,6 +46,9 @@ public class Prefs {
public static String get_extra() { public static String get_extra() {
return pref.getString("extra", ""); return pref.getString("extra", "");
} }
public static String get_env() {
return pref.getString("env", "");
}
public static SharedPreferences.Editor edit() { public static SharedPreferences.Editor edit() {
return pref.edit(); return pref.edit();

View File

@ -129,7 +129,8 @@ public class SimpleSSHDService extends Service {
final int pid = start_sshd(Prefs.get_port(), final int pid = start_sshd(Prefs.get_port(),
Prefs.get_path(), Prefs.get_shell(), Prefs.get_path(), Prefs.get_shell(),
Prefs.get_home(), Prefs.get_extra(), Prefs.get_home(), Prefs.get_extra(),
(Prefs.get_rsyncbuffer() ? 1 : 0)); (Prefs.get_rsyncbuffer() ? 1 : 0),
Prefs.get_env());
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (pid != 0) { if (pid != 0) {
@ -178,7 +179,7 @@ public class SimpleSSHDService extends Service {
private static native int start_sshd(int port, String path, private static native int start_sshd(int port, String path,
String shell, String home, String extra, String shell, String home, String extra,
int rsyncbuffer); int rsyncbuffer, String env);
private static native void kill(int pid); private static native void kill(int pid);
private static native int waitpid(int pid); private static native int waitpid(int pid);
static { static {