summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6dc15ce)
raw | patch | inline | side by side (parent: 6dc15ce)
author | Sebastian Harl <sh@tokkee.org> | |
Fri, 30 Jan 2015 10:49:20 +0000 (11:49 +0100) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Fri, 30 Jan 2015 10:49:20 +0000 (11:49 +0100) |
src/include/utils/os.h | patch | blob | history | |
src/tools/sysdb/main.c | patch | blob | history | |
src/utils/os.c | patch | blob | history |
diff --git a/src/include/utils/os.h b/src/include/utils/os.h
index 8ffca4bcd70a5b6ea117ba748cd3b75cb2cf6c47..f4decde0f4b6312692c238d7d8cff2dc87165d36 100644 (file)
--- a/src/include/utils/os.h
+++ b/src/include/utils/os.h
extern "C" {
#endif
+/*
+ * sdb_get_homedir:
+ * Returns the home directory of the current user. The buffer to hold the
+ * return value is allocated dynamically and has to be freed by the caller.
+ *
+ * Returns:
+ * - the current user's home directory on success
+ * - NULL else
+ */
+char *
+sdb_get_homedir(void);
+
/*
* sysdb_mkdir_all:
* Recursively create the directory 'pathname' (similar to 'mkdir -p' on the
diff --git a/src/tools/sysdb/main.c b/src/tools/sysdb/main.c
index 20f37ae34791d2dbebd942066844a60ecdcd395c..b22e11f0f89da462596783a000ffdcb2d70835fa 100644 (file)
--- a/src/tools/sysdb/main.c
+++ b/src/tools/sysdb/main.c
# define DEFAULT_SOCKET "unix:"LOCALSTATEDIR"/run/sysdbd.sock"
#endif
-static const char *
-get_homedir(void)
-{
- char *username = sdb_get_current_user();
-
- struct passwd pw_entry;
- struct passwd *result = NULL;
-
- /* needs to be static because we return a pointer into this buffer
- * to the caller */
- static char buf[1024];
-
- int status;
-
- if (username) {
- memset(&pw_entry, 0, sizeof(pw_entry));
- status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
- }
- else
- status = -1;
-
- if (status || (! result)) {
- char errbuf[1024];
- sdb_log(SDB_LOG_WARNING, "Failed to determine home directory "
- "for user %s: %s", username,
- sdb_strerror(errno, errbuf, sizeof(errbuf)));
- free(username);
- return NULL;
- }
- free(username);
- return result->pw_dir;
-} /* get_homedir */
static void
exit_usage(char *name, int status)
{
const char *host = NULL;
- const char *homedir;
+ char *homedir;
char hist_file[1024] = "";
sdb_input_t input = SDB_INPUT_INIT;
using_history();
- if ((homedir = get_homedir())) {
+ if ((homedir = sdb_get_homedir())) {
snprintf(hist_file, sizeof(hist_file) - 1,
"%s/.sysdb_history", homedir);
hist_file[sizeof(hist_file) - 1] = '\0';
+ free(homedir);
+ homedir = NULL;
errno = 0;
if (read_history(hist_file) && (errno != ENOENT)) {
diff --git a/src/utils/os.c b/src/utils/os.c
index 4afc466683138aed81667a446638698bbe96da91..e975f96e743b6510102426d6450dbe9270690e1c 100644 (file)
--- a/src/utils/os.c
+++ b/src/utils/os.c
* public API
*/
+char *
+sdb_get_homedir(void)
+{
+ char *username = sdb_get_current_user();
+
+ struct passwd pw_entry;
+ struct passwd *result = NULL;
+
+ char buf[4096];
+
+ int status;
+
+ if (username) {
+ memset(&pw_entry, 0, sizeof(pw_entry));
+ status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
+ }
+ else
+ status = -1;
+
+ if (status || (! result)) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory "
+ "for user %s: %s", username,
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ free(username);
+ return NULL;
+ }
+ free(username);
+ return strdup(result->pw_dir);
+} /* sdb_get_homedir */
+
int
sdb_mkdir_all(const char *pathname, mode_t mode)
{
uid_t uid;
- char buf[1024];
+ char buf[4096];
int status;
uid = geteuid();