const char * ROKEN_LIB_FUNCTION get_default_username (void) { const char *user; user = getenv ("USER"); if (user == NULL) user = getenv ("LOGNAME"); if (user == NULL) user = getenv ("USERNAME"); #if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN) if (user == NULL) { user = (const char *)getlogin (); if (user != NULL) return user; } #endif #ifdef HAVE_PWD_H { uid_t uid = getuid (); struct passwd *pwd; if (user != NULL) { pwd = k_getpwnam (user); if (pwd != NULL && pwd->pw_uid == uid) return user; } pwd = k_getpwuid (uid); if (pwd != NULL) return pwd->pw_name; } #endif return user; }
/* * expand tilde from the passwd file. */ static const Char * globtilde(const Char *pattern, Char *patbuf, glob_t *pglob) { struct passwd *pwd; char *h; const Char *p; Char *b; if (*pattern != CHAR_TILDE || !(pglob->gl_flags & GLOB_TILDE)) return pattern; /* Copy up to the end of the string or / */ for (p = pattern + 1, h = (char *) patbuf; *p && *p != CHAR_SLASH; *h++ = *p++) continue; *h = CHAR_EOS; if (((char *) patbuf)[0] == CHAR_EOS) { /* * handle a plain ~ or ~/ by expanding $HOME * first and then trying the password file */ if ((h = getenv("HOME")) == NULL) { if ((pwd = k_getpwuid(getuid())) == NULL) return pattern; else h = pwd->pw_dir; } } else { /* * Expand a ~user */ if ((pwd = k_getpwnam((char*) patbuf)) == NULL) return pattern; else h = pwd->pw_dir; } /* Copy the home directory */ for (b = patbuf; *h; *b++ = *h++) continue; /* Append the rest of the pattern */ while ((*b++ = *p++) != CHAR_EOS) continue; return patbuf; }
ROKEN_LIB_FUNCTION const char * ROKEN_LIB_CALL get_default_username (void) { const char *user; user = getenv ("USER"); if (user == NULL) user = getenv ("LOGNAME"); if (user == NULL) user = getenv ("USERNAME"); #if defined(HAVE_GETLOGIN) && !defined(POSIX_GETLOGIN) if (user == NULL) { user = (const char *)getlogin (); if (user != NULL) return user; } #endif #ifdef HAVE_PWD_H { uid_t uid = getuid (); struct passwd *pwd; if (user != NULL) { pwd = k_getpwnam (user); if (pwd != NULL && pwd->pw_uid == uid) return user; } pwd = k_getpwuid (uid); if (pwd != NULL) return pwd->pw_name; } #endif #ifdef _WIN32 /* TODO: We can call GetUserNameEx() and figure out a username. However, callers do not free the return value of this function. */ #endif return user; }
int main(int argc, char **argv) { int f; char tf[1024]; char *p; char *path; char **args; int i; int optind = 0; setprogname(argv[0]); if(getarg(getargs, num_args, argc, argv, &optind)) usage(1); if(help_flag) usage(0); if(version_flag) { print_version(NULL); exit(0); } argc -= optind; argv += optind; #ifdef KRB5 { const krb5_cc_ops *type; krb5_error_code ret; krb5_context context; krb5_ccache id; const char *name; ret = krb5_init_context(&context); if (ret) /* XXX should this really call exit ? */ errx(1, "no kerberos 5 support"); if (typename_arg == NULL) { char *s; name = krb5_cc_default_name(context); if (name == NULL) krb5_errx(context, 1, "Failed getting default " "credential cache type"); typename_arg = strdup(name); if (typename_arg == NULL) errx(1, "strdup"); s = strchr(typename_arg, ':'); if (s) *s = '\0'; } type = krb5_cc_get_prefix_ops(context, typename_arg); if (type == NULL) krb5_err(context, 1, ret, "Failed getting ops for %s " "credential cache", typename_arg); ret = krb5_cc_gen_new(context, type, &id); if (ret) krb5_err(context, 1, ret, "Failed generating credential cache"); name = krb5_cc_get_name(context, id); if (name == NULL) krb5_errx(context, 1, "Generated credential cache have no name"); snprintf(tf, sizeof(tf), "%s:%s", typename_arg, name); ret = krb5_cc_close(context, id); if (ret) krb5_err(context, 1, ret, "Failed closing credential cache"); krb5_free_context(context); esetenv("KRB5CCNAME", tf, 1); } #endif snprintf (tf, sizeof(tf), "%s_XXXXXX", TKT_ROOT); f = mkstemp (tf); if (f < 0) err(1, "mkstemp failed"); close (f); unlink (tf); esetenv("KRBTKFILE", tf, 1); i = 0; args = (char **) malloc((argc + 10)*sizeof(char *)); if (args == NULL) errx (1, "Out of memory allocating %lu bytes", (unsigned long)((argc + 10)*sizeof(char *))); if(*argv == NULL) { path = getenv("SHELL"); if(path == NULL){ struct passwd *pw = k_getpwuid(geteuid()); path = strdup(pw->pw_shell); } } else { path = strdup(*argv++); } if (path == NULL) errx (1, "Out of memory copying path"); p=strrchr(path, '/'); if(p) args[i] = strdup(p+1); else args[i] = strdup(path); if (args[i++] == NULL) errx (1, "Out of memory copying arguments"); while(*argv) args[i++] = *argv++; args[i++] = NULL; if(k_hasafs()) k_setpag(); unsetenv("PAGPID"); execvp(path, args); if (errno == ENOENT || c_flag) { char **sh_args = malloc ((i + 2) * sizeof(char *)); int j; if (sh_args == NULL) errx (1, "Out of memory copying sh arguments"); for (j = 1; j < i; ++j) sh_args[j + 2] = args[j]; sh_args[0] = "sh"; sh_args[1] = "-c"; sh_args[2] = path; execv ("/bin/sh", sh_args); } err (1, "execvp"); }