Ejemplo n.º 1
0
static char *get_home_dir(uid_t uid) {
    char *strbuf;
    char *result;
    struct passwd pwbuf;
    struct passwd *pw = NULL;
    long val = sysconf(_SC_GETPW_R_SIZE_MAX);
    size_t strbuflen = val;

    if (val < 0)
        return NULL;

    if (ALLOC_N(strbuf, strbuflen) < 0)
        return NULL;

    if (getpwuid_r(uid, &pwbuf, strbuf, strbuflen, &pw) != 0 || pw == NULL) {
        free(strbuf);
        return NULL;
    }

    result = strdup(pw->pw_dir);

    free(strbuf);

    return result;
}
/* For functions that return STRING or DECIMAL */ 
char* lib_mysqludf_getpwuid(
        UDF_INIT *initid,
        UDF_ARGS *args,
        char* result,
        unsigned long* length,
        char *is_null,
        char *error
        )
{
    struct passwd pwent;
    struct passwd *pwentp;
    long long uid;
    char buf[1024];
    int s;

    strcpy(result, "unknown");
    uid = *((long long*) args->args[0] );
    s = getpwuid_r( uid, &pwent, buf, sizeof buf, &pwentp );
    if ( pwentp == NULL) {
        strcpy(result, "unknown");
    } else {
        strcpy(result, pwent.pw_name);
    }
    *length = strlen(result);
    return result;
}
Ejemplo n.º 3
0
static int elektraResolvePasswdHome (resolverHandle * p, Key * warningsKey)
{
	ssize_t bufSize = sysconf (_SC_GETPW_R_SIZE_MAX);
	if (bufSize == -1) bufSize = 16384; // man 3 getpwuid

	char * buf = elektraMalloc (bufSize);
	if (!buf) return -1;
	struct passwd pwd;
	struct passwd * result;
	int s;

	s = getpwuid_r (getuid (), &pwd, buf, bufSize, &result);
	if (result == NULL)
	{
		elektraFree (buf);
		if (s != 0)
		{
			ELEKTRA_ADD_WARNING (90, warningsKey, strerror (s));
		}
		return -1;
	}

	const char * home = pwd.pw_dir;
	size_t filenameSize = elektraStrLen (home) + elektraStrLen (p->path) - 1;

	p->filename = elektraMalloc (filenameSize);
	snprintf (p->filename, filenameSize, "%s/%s", home, (p->path) + 2);

	elektraFree (buf);
	return 0;
}
Ejemplo n.º 4
0
void
server_lock(void)
{
	struct client	       *c;
	static struct passwd   *pw, pwstore;
	static char		pwbuf[_PW_BUF_LEN];
	u_int			i;

	if (server_locked)
		return;

	if (getpwuid_r(getuid(), &pwstore, pwbuf, sizeof pwbuf, &pw) != 0) {
		server_locked_pw = NULL;
		return;
	}
	server_locked_pw = pw;

	for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
		c = ARRAY_ITEM(&clients, i);
		if (c == NULL || c->session == NULL)
			continue;

		status_prompt_clear(c);
		status_prompt_set(c,
		    "Password:", server_lock_callback, NULL, c, PROMPT_HIDDEN);
  		server_redraw_client(c);
	}

	server_locked = 1;
}
Ejemplo n.º 5
0
QT_BEGIN_NAMESPACE

//static
QString _resolveUserName(uint userId)
{
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
    int size_max = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (size_max == -1)
        size_max = 1024;
    QVarLengthArray<char, 1024> buf(size_max);
#endif

    struct passwd *pw = 0;
#if !defined(Q_OS_INTEGRITY)
#if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS) && !defined(Q_OS_OPENBSD)
    struct passwd entry;
    getpwuid_r(userId, &entry, buf.data(), buf.size(), &pw);
#else
    pw = getpwuid(userId);
#endif
#endif
    if (pw)
        return QFile::decodeName(QByteArray(pw->pw_name));
    return QString();
}
Ejemplo n.º 6
0
void Repo::initCentral() {
  std::vector<std::string> failPaths;

  assert(m_dbc == nullptr);

  // Try Repo.Central.Path (or HHVM_REPO_CENTRAL_PATH).
  if (!RuntimeOption::RepoCentralPath.empty()) {
    if (!openCentral(RuntimeOption::RepoCentralPath.c_str())) {
      return;
    }
    failPaths.push_back(RuntimeOption::RepoCentralPath);
  }

  const char* HHVM_REPO_CENTRAL_PATH = getenv("HHVM_REPO_CENTRAL_PATH");
  if (HHVM_REPO_CENTRAL_PATH != nullptr) {
    if (!openCentral(HHVM_REPO_CENTRAL_PATH)) {
      return;
    }
    failPaths.push_back(HHVM_REPO_CENTRAL_PATH);
  }

  // Try "$HOME/.hhvm.hhbc".
  char* HOME = getenv("HOME");
  if (HOME != nullptr) {
    std::string centralPath = HOME;
    centralPath += "/.hhvm.hhbc";
    if (!openCentral(centralPath.c_str())) {
      return;
    }
    failPaths.push_back(centralPath);
  }

  // Try the equivalent of "$HOME/.hhvm.hhbc", but look up the home directory
  // in the password database.
  {
    struct passwd pwbuf;
    struct passwd* pwbufp;
    long bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (bufsize != -1) {
      char buf[size_t(bufsize)];
      if (!getpwuid_r(getuid(), &pwbuf, buf, size_t(bufsize), &pwbufp)
          && (HOME == nullptr || strcmp(HOME, pwbufp->pw_dir))) {
        std::string centralPath = pwbufp->pw_dir;
        centralPath += "/.hhvm.hhbc";
        if (!openCentral(centralPath.c_str())) {
          return;
        }
        failPaths.push_back(centralPath);
      }
    }
  }

  // Database initialization failed; this is an unrecoverable state.
  for (std::vector<std::string>::const_iterator it = failPaths.begin();
       it != failPaths.end(); ++it) {
    Logger::Error("Failed to initialize central HHBC repository at '%s'",
                  (*it).c_str());
  }
  exit(1);
}
Ejemplo n.º 7
0
static void torture_options_set_user(void **state) {
    ssh_session session = *state;
    int rc;
#ifndef _WIN32
# ifndef NSS_BUFLEN_PASSWD
#  define NSS_BUFLEN_PASSWD 4096
# endif /* NSS_BUFLEN_PASSWD */
    struct passwd pwd;
    struct passwd *pwdbuf;
    char buf[NSS_BUFLEN_PASSWD];

    /* get local username */
    rc = getpwuid_r(getuid(), &pwd, buf, NSS_BUFLEN_PASSWD, &pwdbuf);
    assert_true(rc == 0);
#endif /* _WIN32 */

    rc = ssh_options_set(session, SSH_OPTIONS_USER, "guru");
    assert_true(rc == 0);
    assert_string_equal(session->opts.username, "guru");


    rc = ssh_options_set(session, SSH_OPTIONS_USER, NULL);
    assert_true(rc == 0);

#ifndef _WIN32
    assert_string_equal(session->opts.username, pwd.pw_name);
#endif
}
Ejemplo n.º 8
0
/*
 * SNOOPY DATA SOURCE: username
 *
 * Description:
 *     Returns literal username of current process
 *
 * Params:
 *     result: pointer to string, to write result into
 *     arg:    (ignored)
 *
 * Return:
 *     number of characters in the returned string, or SNOOPY_DATASOURCE_FAILURE
 */
int snoopy_datasource_username (char * const result, char const * const arg)
{
    struct passwd  pwd;
    struct passwd *pwd_uid = NULL;
    char          *buffpwd_uid = NULL;
    long           buffpwdsize_uid = 0;
    int            messageLength   = 0;

    /* Allocate memory */
    buffpwdsize_uid = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (-1 == buffpwdsize_uid) {
        buffpwdsize_uid = 16384;
    }
    buffpwd_uid = malloc(buffpwdsize_uid);
    if (NULL == buffpwd_uid) {
        return snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(malloc)");
    }

    /* Try to get data */
    if (0 != getpwuid_r(getuid(), &pwd, buffpwd_uid, buffpwdsize_uid, &pwd_uid)) {
        messageLength  = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "ERROR(getpwuid_r)");
    } else {
        if (NULL == pwd_uid) {
            messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "(undefined)");
        } else {
            messageLength = snprintf(result, SNOOPY_DATASOURCE_MESSAGE_MAX_SIZE, "%s", pwd_uid->pw_name);
        }
    }

    /* Cleanup and return */
    free(buffpwd_uid);
    return messageLength;
}
// Returns path to user home directory. Checks both environment and password
// database; password database value takes precendence when it differs from
// value returned by |getenv|. Path returned always ends with '/'.
std::string ReadHomeDirectoryPath() {
  // Read HOME value from environment.
  const char* ptr_env_home_dir = getenv("HOME");
  std::string home_dir = ptr_env_home_dir;

  // Check with password database for home directory. No guarantee that HOME
  // environment variable is set or valid.

  // Obtain max size for |passwd| struct from |sysconf|.
  struct passwd passwd_entry = {0};
  const size_t passwd_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (passwd_buf_size > 0) {
    char* const passwd_buf = new (std::nothrow) char[passwd_buf_size];
    if (passwd_buf) {
      struct passwd* ptr_passwd_entry = NULL;
      const int result = getpwuid_r(getuid(),
                                    &passwd_entry,
                                    passwd_buf,
                                    passwd_buf_size,
                                    &ptr_passwd_entry);
      if (!result && ptr_passwd_entry == &passwd_entry) {
        const std::string passwd_home_dir = passwd_entry.pw_dir;
        if (home_dir != passwd_home_dir) {
          home_dir = passwd_home_dir;
        }
      }
      delete[] passwd_buf;
    }
  }
  if (!home_dir.empty() && home_dir[home_dir.size()] != '/') {
    home_dir.append("/");
  }
  return home_dir;
}
Ejemplo n.º 10
0
Archivo: auth.c Proyecto: finid/cockpit
static struct passwd *
getpwuid_a (uid_t uid,
            int *errp)
{
  int err;
  long bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
  struct passwd *ret = NULL;
  struct passwd *buf;

  g_return_val_if_fail (bufsize >= 0, NULL);

  buf = malloc (sizeof(struct passwd) + bufsize);
  if (buf == NULL)
    err = ENOMEM;
  else
    err = getpwuid_r (uid, buf, (char *)(buf + 1), bufsize, &ret);

  if (ret == NULL)
    {
      free (buf);
      if (err == 0)
        err = ENOENT;
    }

  if (errp)
    *errp = err;
  return ret;
}
Ejemplo n.º 11
0
PHPAPI char *php_get_current_user(void)
{
	struct stat *pstat;
	TSRMLS_FETCH();

	if (SG(request_info).current_user) {
		return SG(request_info).current_user;
	}

	/* FIXME: I need to have this somehow handled if
	USE_SAPI is defined, because cgi will also be
	interfaced in USE_SAPI */

	pstat = sapi_get_stat(TSRMLS_C);

	if (!pstat) {
		return "";
	} else {
#ifdef PHP_WIN32
		char name[256];
		DWORD len = sizeof(name)-1;

		if (!GetUserName(name, &len)) {
			return "";
		}
		name[len] = '\0';
		SG(request_info).current_user_length = len;
		SG(request_info).current_user = estrndup(name, len);
		return SG(request_info).current_user;		
#else
		struct passwd *pwd;
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
		struct passwd _pw;
		struct passwd *retpwptr = NULL;
		int pwbuflen = sysconf(_SC_GETPW_R_SIZE_MAX);
		char *pwbuf;

		if (pwbuflen < 1) {
			return "";
		}
		pwbuf = emalloc(pwbuflen);
		if (getpwuid_r(pstat->st_uid, &_pw, pwbuf, pwbuflen, &retpwptr) != 0) {
			efree(pwbuf);
			return "";
		}
		pwd = &_pw;
#else
		if ((pwd=getpwuid(pstat->st_uid))==NULL) {
			return "";
		}
#endif
		SG(request_info).current_user_length = strlen(pwd->pw_name);
		SG(request_info).current_user = estrndup(pwd->pw_name, SG(request_info).current_user_length);
#if defined(ZTS) && defined(HAVE_GETPWUID_R) && defined(_SC_GETPW_R_SIZE_MAX)
		efree(pwbuf);
#endif
		return SG(request_info).current_user;		
#endif
	}	
}	
/**
 * Worker for RTPathUserHome that looks up the home directory
 * using the getpwuid_r api.
 *
 * @returns IPRT status code.
 * @param   pszPath     The path buffer.
 * @param   cchPath     The size of the buffer.
 * @param   uid         The User ID to query the home directory of.
 */
static int rtPathUserHomeByPasswd(char *pszPath, size_t cchPath, uid_t uid)
{
    /*
     * The getpwuid_r function uses the passed in buffer to "allocate" any
     * extra memory it needs. On some systems we should probably use the
     * sysconf function to find the appropriate buffer size, but since it won't
     * work everywhere we'll settle with a 5KB buffer and ASSUME that it'll
     * suffice for even the lengthiest user descriptions...
     */
    char            achBuffer[5120];
    struct passwd   Passwd;
    struct passwd  *pPasswd;
    memset(&Passwd, 0, sizeof(Passwd));
    int rc = getpwuid_r(uid, &Passwd, &achBuffer[0], sizeof(achBuffer), &pPasswd);
    if (rc != 0)
        return RTErrConvertFromErrno(rc);
    if (!pPasswd) /* uid not found in /etc/passwd */
        return VERR_PATH_NOT_FOUND;

    /*
     * Check that it isn't empty and that it exists.
     */
    struct stat st;
    if (    !pPasswd->pw_dir
        ||  !*pPasswd->pw_dir
        ||  stat(pPasswd->pw_dir, &st)
        ||  !S_ISDIR(st.st_mode))
        return VERR_PATH_NOT_FOUND;

    /*
     * Convert it to UTF-8 and copy it to the return buffer.
     */
    return rtPathFromNativeCopy(pszPath, cchPath, pPasswd->pw_dir, NULL);
}
Ejemplo n.º 13
0
enum perf_target_errno perf_target__parse_uid(struct perf_target *target)
{
	struct passwd pwd, *result;
	char buf[1024];
	const char *str = target->uid_str;

	target->uid = UINT_MAX;
	if (str == NULL)
		return PERF_ERRNO_TARGET__SUCCESS;

	/* Try user name first */
	getpwnam_r(str, &pwd, buf, sizeof(buf), &result);

	if (result == NULL) {
		/*
		 * The user name not found. Maybe it's a UID number.
		 */
		char *endptr;
		int uid = strtol(str, &endptr, 10);

		if (*endptr != '\0')
			return PERF_ERRNO_TARGET__INVALID_UID;

		getpwuid_r(uid, &pwd, buf, sizeof(buf), &result);

		if (result == NULL)
			return PERF_ERRNO_TARGET__USER_NOT_FOUND;
	}

	target->uid = result->pw_uid;
	return PERF_ERRNO_TARGET__SUCCESS;
}
Ejemplo n.º 14
0
int sigar_user_name_get(sigar_t *sigar, int uid, char *buf, int buflen)
{
    struct passwd *pw = NULL;
    /* XXX cache lookup */

# ifdef HAVE_GETPWUID_R
    struct passwd pwbuf;
    char buffer[R_SIZE_MAX];

    if (getpwuid_r(uid, &pwbuf, buffer, sizeof(buffer), &pw) != 0) {
        return errno;
    }
    if (!pw) {
        return ENOENT;
    }
# else
    if ((pw = getpwuid(uid)) == NULL) {
        return errno;
    }
# endif

    strncpy(buf, pw->pw_name, buflen);
    buf[buflen-1] = '\0';

    return SIGAR_OK;
}
Ejemplo n.º 15
0
int _9p_tools_get_fsal_op_context_by_uid( u32 uid, _9p_fid_t * pfid ) 
{
  char buff[1024];
  struct passwd p;
  struct passwd *pp;
  gid_t gid ;
  fsal_status_t fsal_status ;

  if((getpwuid_r( uid, &p, buff, MAXPATHLEN, &pp) != 0) || (pp == NULL))
    {
      LogFullDebug(COMPONENT_IDMAPPER, "getpwuid_r %d failed", uid ) ;
      return -ENOENT;
    }
  else
   gid = p.pw_gid ;
  

  fsal_status = FSAL_GetClientContext( &pfid->fsal_op_context,
                                       &pfid->pexport->FS_export_context,
                                       uid, gid, NULL, 0 ) ;
  if( FSAL_IS_ERROR( fsal_status ) )
   return -fsal_status.major ; 

  return 0 ;
} /* _9p_tools_get_fsal_cred */
Ejemplo n.º 16
0
ssize_t homedir(char *buf, size_t n)
{
    assert(buf != NULL);

    ssize_t result = -1;
    char *pwbuf = NULL;

    const char *hdir = getenv("HOME");
    if (hdir == NULL)
    {
        ssize_t pwbufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
        if (pwbufsize < 0)
        {
            return -1;
        }

        pwbuf = malloc(pwbufsize);
        if (pwbuf == NULL)
        {
            return -1;
        }

        struct passwd pwd;
        struct passwd *result = NULL;
        int err = getpwuid_r(geteuid(), &pwd, pwbuf, pwbufsize, &result);
        if (err != 0)
        {
            errno = err;
            goto cleanup;
        }
        if (result == NULL)
        {
            errno = EIDRM;
            goto cleanup;
        }
        hdir = pwd.pw_dir;
    }

    size_t hlen = strlen(hdir);
    for (; hlen > 0 && hdir[hlen-1] == '/'; --hlen)
        ;
    if ((hlen + 1) > n)
    {
        errno = ERANGE;
        return -1;
    }
    memcpy(buf, hdir, hlen + 1);

    result = hlen;

    int errsv;
cleanup:
    errsv = errno;
    if (pwbuf != NULL)
    {
        free(pwbuf);
    }
    errno = errsv;
    return result;
}
Ejemplo n.º 17
0
int getlogin_r(char *name, size_t namesize)
{
	struct passwd			pwd_buffer, *pwd;
	char					buffer[BUFSIZ+1];
	int						ret;
	char					*logname;

	if(!(logname = getenv("LOGNAME"))) {
		if (namesize <= 1) {
			return ERANGE;
		}
		if((ret = getpwuid_r(getuid(), &pwd_buffer, buffer, sizeof buffer, &pwd)) != EOK) {
			return ret;
		}

		if(!pwd) {
			*name = '\0';
			return 0;
		}
		logname = pwd->pw_name;
	}
	if(strlen(logname) + 1 > namesize) {
		return ERANGE;
	}
	strcpy(name, logname);
	return 0;
}
Ejemplo n.º 18
0
int
main(int argc, char *argv[])
{
    int idx;
    char prompt[64];
    struct passwd pwdbuf, *pwdbufptr;
    char pwdstrbuf[1024];
    int rc;

    if (argc == 1) printf("Usage: %s UID ...\n", argv[0]);
    for (idx = 1; idx < argc; ++idx) {
	if (strspn(argv[idx], "0123456789") == strlen(argv[idx])) {
	    snprintf(prompt, sizeof(prompt),
		     "getpwuid(%u)", (unsigned)atoi(argv[idx]));
	    rc = getpwuid_r((uid_t)atoi(argv[idx]), &pwdbuf,
			    pwdstrbuf, sizeof(pwdstrbuf), &pwdbufptr);
	} else {
	    snprintf(prompt, sizeof(prompt), "getpwnam(%s)", argv[idx]);
	    rc = getpwnam_r(argv[idx], &pwdbuf,
			    pwdstrbuf, sizeof(pwdstrbuf), &pwdbufptr);
	}
	showpwd(prompt, pwdbufptr, rc);
    }
    return 0;
}
Ejemplo n.º 19
0
std::string
ArchFileUnix::getUserDirectory()
{
	char* buffer = NULL;
	std::string dir;
#if HAVE_GETPWUID_R
	struct passwd pwent;
	struct passwd* pwentp;
#if defined(_SC_GETPW_R_SIZE_MAX)
	long size = sysconf(_SC_GETPW_R_SIZE_MAX);
	if (size == -1) {
		size = BUFSIZ;
	}
#else
	long size = BUFSIZ;
#endif
	buffer = new char[size];
	getpwuid_r(getuid(), &pwent, buffer, size, &pwentp);
#else
	struct passwd* pwentp = getpwuid(getuid());
#endif
	if (pwentp != NULL && pwentp->pw_dir != NULL) {
		dir = pwentp->pw_dir;
	}
	delete[] buffer;
	return dir;
}
Ejemplo n.º 20
0
static const char *getUserName(uid_t uid) {
  struct passwd pwbuf, *pw;
  char *buf;
  #ifdef _SC_GETPW_R_SIZE_MAX
  int len = sysconf(_SC_GETPW_R_SIZE_MAX);
  if (len <= 0) {
    len = 4096;
  }
  #else
  int len = 4096;
  #endif
  buf = malloc(len);
  char *user;
  if (getpwuid_r(uid, &pwbuf, buf, len, &pw) || !pw) {
    user = malloc(32);
    snprintf(user, 32, "%d", uid);
  } else {
    user = strdup(pw->pw_name);
    if (!user) {
      perror("malloc()");
      _exit(1);
    }
  }
  free(buf);
  return user;
}
Ejemplo n.º 21
0
static char *
get_username_from_passwd_file (void *ctx)
{
    long pw_buf_size;
    char *pw_buf;
    struct passwd passwd, *ignored;
    char *name;
    int e;

    pw_buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
    if (pw_buf_size == -1) pw_buf_size = 64;
    pw_buf = talloc_zero_size (ctx, pw_buf_size);

    while ((e = getpwuid_r (getuid (), &passwd, pw_buf,
                            pw_buf_size, &ignored)) == ERANGE) {
        pw_buf_size = pw_buf_size * 2;
        pw_buf = talloc_zero_size(ctx, pw_buf_size);
    }

    if (e == 0)
	name = talloc_strdup (ctx, passwd.pw_name);
    else
	name = talloc_strdup (ctx, "");

    talloc_free (pw_buf);

    return name;
}
Ejemplo n.º 22
0
    static
    inline
    void
    prime_cache(const uid_t   uid,
                const gid_t   gid,
                gid_t_vector &gidlist)
    {
      int rv;
      char buf[4096];
      struct passwd pwd;
      struct passwd *pwdrv;

      rv = getpwuid_r(uid,&pwd,buf,sizeof(buf),&pwdrv);
      if(pwdrv != NULL && rv == 0)
        {
          int count;

          count = 0;
          rv = ::getgrouplist(pwd.pw_name,gid,NULL,&count);
          gidlist.resize(count);
          rv = ::getgrouplist(pwd.pw_name,gid,&gidlist[0],&count);
          if(rv == -1)
            gidlist.resize(1,gid);
        }
    }
Ejemplo n.º 23
0
int excecute_l(){ /* function for excecuting ls -l */
int count,i;
struct direct **files;
struct stat statbuf;
char datestring[256];
struct passwd pwent;
struct passwd *pwentp;
struct group grp;
struct group *grpt;
struct tm time;
char buf[1024];
count = scandir(pathname, &files, file_selecto, alphasort);
if(count > 0){
	printf("total %d\n",count);
	printf("pathname name is = %s\n" , pathname);
	for (i=0; i<count; ++i){
		if (!getpwuid_r(statbuf.st_uid, &pwent, buf, sizeof(buf), &pwentp))
			printf(" %s", pwent.pw_name);
		else
			printf(" %d", statbuf.st_uid); /* User ID of the file's owner */
		if (!getgrgid_r (statbuf.st_gid, &grp, buf, sizeof(buf), &grpt))
			printf(" %s", grp.gr_name);
		else
			printf(" %d", statbuf.st_gid); /* Group ID of the file's group */
		printf(" %5d", (int)statbuf.st_size); /* Print size of file. */
		localtime_r(&statbuf.st_mtime, &time); /* Time of last data modification */
		strftime(datestring, sizeof(datestring), "%F %T", &time); /* Get localized date string. */
		printf(" %s %s\n", datestring, files[i]->d_name);
		free (files[i]);
	}
	free(files);
}
exit(0);
}
Ejemplo n.º 24
0
static size_t
get_user_home_path(char* buffer, size_t bufferSize)
{
	const char* home = NULL;
#ifndef _KERNEL_MODE
#ifdef USE_PWENTS
	uid_t user = geteuid();
	if (user == 0) {
		// TODO: this is a work-around as the launch_daemon, and the registrar
		// must not call getpwuid_r().
		return strlcpy(buffer, kUserDirectory, bufferSize);
	}

	struct passwd pwBuffer;
	char pwStringBuffer[MAX_PASSWD_BUFFER_SIZE];
	struct passwd* pw;

	if (getpwuid_r(user, &pwBuffer, pwStringBuffer,
			sizeof(pwStringBuffer), &pw) == 0
		&& pw != NULL) {
		home = pw->pw_dir;
	}
#endif	// USE_PWENTS
	if (home == NULL) {
		/* use env var */
		ssize_t result = __getenv_reentrant("HOME", buffer, bufferSize);
		if (result >= 0)
			return result;
	}
#endif	// !_KERNEL_MODE
	if (home == NULL)
		home = kUserDirectory;

	return strlcpy(buffer, home, bufferSize);
}
Ejemplo n.º 25
0
Archivo: util.c Proyecto: thewb/mokoiax
char *pa_get_user_name(char *s, size_t l) {
    const char *p;
    char buf[1024];

#ifdef HAVE_PWD_H
    struct passwd pw, *r;
#endif

    pa_assert(s);
    pa_assert(l > 0);

    if (!(p = (getuid() == 0 ? "root" : NULL)) &&
            !(p = getenv("USER")) &&
            !(p = getenv("LOGNAME")) &&
            !(p = getenv("USERNAME"))) {
#ifdef HAVE_PWD_H

#ifdef HAVE_GETPWUID_R
        if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
#else
        /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
            * that do not support getpwuid_r. */
        if ((r = getpwuid(getuid())) == NULL) {
#endif
            pa_snprintf(s, l, "%lu", (unsigned long) getuid());
            return s;
        }

        p = r->pw_name;

#elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
        DWORD size = sizeof(buf);

        if (!GetUserName(buf, &size))
            return NULL;

        p = buf;

#else /* HAVE_PWD_H */
        return NULL;
#endif /* HAVE_PWD_H */
    }

    return pa_strlcpy(s, p, l);
}

char *pa_get_host_name(char *s, size_t l) {

    pa_assert(s);
    pa_assert(l > 0);

    if (gethostname(s, l) < 0) {
        pa_log("gethostname(): %s", pa_cstrerror(errno));
        return NULL;
    }

    s[l-1] = 0;
    return s;
}
Ejemplo n.º 26
0
// Get home directory for a given user
FXString FXSystem::getUserDirectory(const FXString& user){
#ifndef WIN32
#if defined(FOX_THREAD_SAFE) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
  struct passwd pwdresult,*pwd;
  char buffer[1024];
  if(user.empty()){
    register const FXchar* str;
    if((str=getenv("HOME"))!=NULL) return str;
    if((str=getenv("USER"))!=NULL || (str=getenv("LOGNAME"))!=NULL){
      if(getpwnam_r(str,&pwdresult,buffer,sizeof(buffer),&pwd)==0 && pwd) return pwd->pw_dir;
      }
    if(getpwuid_r(getuid(),&pwdresult,buffer,sizeof(buffer),&pwd)==0 && pwd) return pwd->pw_dir;
    return PATHSEPSTRING;
    }
  if(getpwnam_r(user.text(),&pwdresult,buffer,sizeof(buffer),&pwd)==0 && pwd) return pwd->pw_dir;
  return PATHSEPSTRING;
#else
  register struct passwd *pwd;
  if(user.empty()){
    register const FXchar* str;
    if((str=getenv("HOME"))!=NULL) return str;
    if((str=getenv("USER"))!=NULL || (str=getenv("LOGNAME"))!=NULL){
      if((pwd=getpwnam(str))!=NULL) return pwd->pw_dir;
      }
    if((pwd=getpwuid(getuid()))!=NULL) return pwd->pw_dir;
    return PATHSEPSTRING;
    }
  if((pwd=getpwnam(user.text()))!=NULL) return pwd->pw_dir;
  return PATHSEPSTRING;
#endif
#else
  if(user.empty()){
    register const FXchar *str1,*str2;
    FXchar home[MAXPATHLEN];
    DWORD size=MAXPATHLEN;
    HKEY hKey;
    LONG result;
    if((str1=getenv("USERPROFILE"))!=NULL) return str1; // Daniël Hörchner <*****@*****.**>
    if((str1=getenv("HOME"))!=NULL) return str1;
    if((str2=getenv("HOMEPATH"))!=NULL){      // This should be good for WinNT, Win2K according to MSDN
      if((str1=getenv("HOMEDRIVE"))==NULL) str1="c:";
      strncpy(home,str1,MAXPATHLEN);
      strncat(home,str2,MAXPATHLEN);
      return home;
      }
//  FXchar buffer[MAX_PATH]
//  if(SHGetFolderPath(NULL,CSIDL_PERSONAL|CSIDL_FLAG_CREATE,NULL,O,buffer)==S_OK){
//    return buffer;
//    }
    if(RegOpenKeyExA(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",0,KEY_READ,&hKey)==ERROR_SUCCESS){
      result=RegQueryValueExA(hKey,"Personal",NULL,NULL,(LPBYTE)home,&size);  // Change "Personal" to "Desktop" if you want...
      RegCloseKey(hKey);
      if(result==ERROR_SUCCESS) return home;
      }
    return "c:" PATHSEPSTRING;
    }
  return "c:" PATHSEPSTRING;
#endif
  }
Ejemplo n.º 27
0
char *t3_config_xdg_get_path(t3_config_xdg_dirs_t xdg_dir, const char *program_dir, size_t file_name_len) {
	const char *env = getenv(xdg_dirs[xdg_dir].env_name);
	char *pathname, *tmp;
	size_t extra_size;

	if (xdg_dir > sizeof(xdg_dirs) / sizeof(xdg_dirs[0]) || xdg_dir < 0) {
		errno = EINVAL;
		return NULL;
	}

	if (env != NULL && strlen(env) > 0) {
		if ((pathname = _t3_config_strdup(env)) == NULL)
			return NULL;
	} else if (xdg_dirs[xdg_dir].homedir_relative) {
		struct passwd pw_entry;
		struct passwd *result;
		char buffer[4096];
		int error;

		const char *dir;

		if ((error = getpwuid_r(getuid(), &pw_entry, buffer, sizeof(buffer), &result)) != 0 || result != &pw_entry) {
			env = getenv("HOME");
			if (env == NULL || strlen(env) == 0) {
				errno = ENOENT;
				return NULL;
			}
			dir = env;
		} else {
			dir = pw_entry.pw_dir;
		}

		if ((pathname = malloc(strlen(dir) + 1 + strlen(xdg_dirs[xdg_dir].homedir_relative) + 1)) == NULL)
			return NULL;
		strcpy(pathname, dir);
		strcat(pathname, "/");
		strcat(pathname, xdg_dirs[xdg_dir].homedir_relative);
	} else {
		errno = ENOENT;
		return NULL;
	}

	extra_size = file_name_len + 1;
	if (program_dir != NULL)
		extra_size += 1 + strlen(program_dir);

	if ((tmp = realloc(pathname, strlen(pathname) + extra_size + 1)) == NULL) {
		free(pathname);
		return NULL;
	}
	pathname = tmp;

	if (program_dir != NULL) {
		strcat(pathname, "/");
		strcat(pathname, program_dir);
	}

	return pathname;
}
Ejemplo n.º 28
0
struct passwd *
getpwuid(uid_t uid)
{
	nss_XbyY_buf_t *b = get_pwbuf();

	return (b == NULL ? NULL :
	    getpwuid_r(uid, b->result, b->buffer, b->buflen));
}
Ejemplo n.º 29
0
static void *thread(void *arg) {
    struct passwd pwd;
    char buf[1024];
    struct passwd *result;
    pthread_mutex_lock(&mutex);
    getpwuid_r(0, &pwd, buf, sizeof buf, &result);
    return NULL;
}
Ejemplo n.º 30
0
char* cuserid(char* buf) {
  struct passwd pw, *ppw;
  long pwb[256];
  if (getpwuid_r(geteuid(), &pw, (void*)pwb, sizeof pwb, &ppw))
    return 0;
  snprintf(buf, L_cuserid, "%s", pw.pw_name);
  return buf;
}