123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- #include "includes.h"
- #ifndef HAVE_GETUSERSHELL
- static char **curshell, **shells, *strings;
- static char **initshells();
- #endif
- #ifndef HAVE_STRLCPY
- size_t strlcpy(char *dst, const char *src, size_t size) {
- size_t i;
-
- if (size < 1) {
- return 0;
- }
- for (i = 0; i < size-1; i++) {
- if (src[i] == '\0') {
- break;
- } else {
- dst[i] = src[i];
- }
- }
- dst[i] = '\0';
- return strlen(src);
- }
- #endif
- #ifndef HAVE_STRLCAT
- size_t
- strlcat(char *dst, const char *src, size_t siz)
- {
- char *d = dst;
- const char *s = src;
- size_t n = siz;
- size_t dlen;
-
- while (n-- != 0 && *d != '\0')
- d++;
- dlen = d - dst;
- n = siz - dlen;
- if (n == 0)
- return(dlen + strlen(s));
- while (*s != '\0') {
- if (n != 1) {
- *d++ = *s;
- n--;
- }
- s++;
- }
- *d = '\0';
- return(dlen + (s - src));
- }
- #endif
- #ifndef HAVE_DAEMON
- int daemon(int nochdir, int noclose) {
- int fd;
- switch (fork()) {
- case -1:
- return (-1);
- case 0:
- break;
- default:
- _exit(0);
- }
- if (setsid() == -1)
- return -1;
- if (!nochdir)
- (void)chdir("/");
- if (!noclose && (fd = open(DROPBEAR_PATH_DEVNULL, O_RDWR, 0)) != -1) {
- (void)dup2(fd, STDIN_FILENO);
- (void)dup2(fd, STDOUT_FILENO);
- (void)dup2(fd, STDERR_FILENO);
- if (fd > STDERR_FILENO)
- (void)close(fd);
- }
- return 0;
- }
- #endif
- #ifndef HAVE_BASENAME
- char *basename(const char *path) {
- char *foo = strrchr(path, '/');
- if (!foo)
- {
- return path;
- }
- return ++foo;
- }
- #endif
- #ifndef HAVE_GETUSERSHELL
- char * getusershell() {
- char *ret;
- if (curshell == NULL)
- curshell = initshells();
- ret = *curshell;
- if (ret != NULL)
- curshell++;
- return (ret);
- }
- void endusershell() {
- if (shells != NULL)
- free(shells);
- shells = NULL;
- if (strings != NULL)
- free(strings);
- strings = NULL;
- curshell = NULL;
- }
- void setusershell() {
- curshell = initshells();
- }
- static char **initshells() {
-
- static const char *okshells[] = { "/bin/sh", "/bin/csh", NULL };
- register char **sp, *cp;
- register FILE *fp;
- struct stat statb;
- int flen;
- if (shells != NULL)
- free(shells);
- shells = NULL;
- if (strings != NULL)
- free(strings);
- strings = NULL;
- if ((fp = fopen("/etc/shells", "rc")) == NULL)
- return (char **) okshells;
- if (fstat(fileno(fp), &statb) == -1) {
- (void)fclose(fp);
- return (char **) okshells;
- }
- if ((strings = malloc((u_int)statb.st_size + 1)) == NULL) {
- (void)fclose(fp);
- return (char **) okshells;
- }
- shells = calloc((unsigned)statb.st_size / 3, sizeof (char *));
- if (shells == NULL) {
- (void)fclose(fp);
- free(strings);
- strings = NULL;
- return (char **) okshells;
- }
- sp = shells;
- cp = strings;
- flen = statb.st_size;
- while (fgets(cp, flen - (cp - strings), fp) != NULL) {
- while (*cp != '#' && *cp != '/' && *cp != '\0')
- cp++;
- if (*cp == '#' || *cp == '\0')
- continue;
- *sp++ = cp;
- while (!isspace(*cp) && *cp != '#' && *cp != '\0')
- cp++;
- *cp++ = '\0';
- }
- *sp = NULL;
- (void)fclose(fp);
- return (shells);
- }
- #endif
|