Bugtraq mailing list archives

environment


From: hobbit () bronze lcs mit edu (*Hobbit*)
Date: Tue, 6 Dec 1994 00:55:39 -0500


Just about any source for "login" contains a good example of smashing the
environment.  The portable way seems to be pointing the global "environ"
at a newly zeroed block, filling it in with what you want, and then
execvp'ing with that as your envp.

An example pulled from that same tired old BSD code we know and love is
attached [i actually yanked this out of my s/key stuff].  This seems to
work everywhere I've ported it, bsd or sv flavors.

_H*

==============

char    *cleanenv[] = { userbuf, homebuf, shellbuf, pathbuf, 0, 0 };
extern char     **environ;

/* ... */
        if (fulllogin) {
                cleanenv[4] = getenv("TERM");
                environ = cleanenv;
        }
        if (strcmp(user, "root"))
                setenv("USER", pwd->pw_name, userbuf);
        setenv("SHELL", shell, shellbuf);
        setenv("HOME", pwd->pw_dir, homebuf);
/* ... */

setenv(ename, eval, buf)
        char *ename, *eval, *buf;
{
        register char *cp, *dp;
        register char **ep = environ;

        /*
         * this assumes an environment variable "ename" already exists
         */
        while (dp = *ep++) {
                for (cp = ename; *cp == *dp && *cp; cp++, dp++)
                        continue;
                if (*cp == 0 && (*dp == '=' || *dp == 0)) {
                        strcat(buf, eval);
                        *--ep = buf;
                        return;
                }
        }
}

char *
getenv(ename)
        char *ename;
{
        register char *cp, *dp;
        register char **ep = environ;

        while (dp = *ep++) {
                for (cp = ename; *cp == *dp && *cp; cp++, dp++)
                        continue;
                if (*cp == 0 && (*dp == '=' || *dp == 0))
                        return (*--ep);
        }
        return ((char *)0);
}



Current thread: