1
0
mirror of http://galexander.org/git/simplesshd.git synced 2024-11-27 09:48:08 +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 - add environment variable option (one name=value per line, just passed into setenv())
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
* 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);
}
install_environment();
execv(usershell, argv);
}

View File

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

View File

@ -9,7 +9,7 @@
#include <fcntl.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;
/* 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_,
jclass cl,
jint port, jobject jpath, jobject jshell, jobject jhome, jobject jextra,
jint rsyncbuffer)
jint rsyncbuffer, jobject jenv)
{
pid_t pid;
const char *extra;
@ -129,6 +129,7 @@ Java_org_galexander_sshd_SimpleSSHDService_start_1sshd(JNIEnv *env_,
conf_home = from_java_string(jhome);
extra = from_java_string(jextra);
conf_rsyncbuffer = rsyncbuffer;
conf_env = from_java_string(jenv);
pid = fork();
if (pid == 0) {

View File

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

View File

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

View File

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