summaryrefslogtreecommitdiff
path: root/lib/param
diff options
context:
space:
mode:
authorAndreas Schneider <asn@samba.org>2012-12-14 17:54:13 +0100
committerGünther Deschner <gd@samba.org>2012-12-21 13:56:00 +0100
commit5b1d95046c8ea624419d94dd7d9e2785ba86f556 (patch)
tree282aaac8dc0a9275fd26a6691b6b125149b4129d /lib/param
parent426bcdb37c05bd5b6ceae34a03b9dbabbf6e9727 (diff)
downloadsamba-5b1d95046c8ea624419d94dd7d9e2785ba86f556.tar.gz
samba-5b1d95046c8ea624419d94dd7d9e2785ba86f556.tar.bz2
samba-5b1d95046c8ea624419d94dd7d9e2785ba86f556.zip
param: Correctly create directory and create common function.
Found by Coverity. Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Günther Deschner <gd@samba.org>
Diffstat (limited to 'lib/param')
-rw-r--r--lib/param/util.c96
1 files changed, 41 insertions, 55 deletions
diff --git a/lib/param/util.c b/lib/param/util.c
index 98894fc747..2569e7b89b 100644
--- a/lib/param/util.c
+++ b/lib/param/util.c
@@ -71,14 +71,13 @@ bool lpcfg_is_myname(struct loadparm_context *lp_ctx, const char *name)
return false;
}
-
-/**
- A useful function for returning a path in the Samba lock directory.
-**/
-char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
- const char *name)
+static char *lpcfg_common_path(TALLOC_CTX* mem_ctx,
+ struct loadparm_context *lp_ctx,
+ const char *name)
{
char *fname, *dname;
+ bool ok;
+
if (name == NULL) {
return NULL;
}
@@ -87,47 +86,44 @@ char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
}
dname = talloc_strdup(mem_ctx, lpcfg_lockdir(lp_ctx));
+ if (dname == NULL) {
+ return NULL;
+ }
trim_string(dname,"","/");
-
- if (!directory_exist(dname)) {
- if (!mkdir(dname,0755))
- DEBUG(1, ("Unable to create directory %s for file %s. "
- "Error was %s\n", dname, name, strerror(errno)));
+
+ ok = directory_create_or_exist(dname, geteuid(), 0755);
+ if (!ok) {
+ DEBUG(1, ("Unable to create directory %s for file %s. "
+ "Error was %s\n", dname, name, strerror(errno)));
+ return NULL;
}
-
- fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+ fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+ if (fname == NULL) {
+ return dname;
+ }
talloc_free(dname);
return fname;
}
+
+/**
+ A useful function for returning a path in the Samba lock directory.
+**/
+char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+ const char *name)
+{
+ return lpcfg_common_path(mem_ctx, lp_ctx, name);
+}
+
/**
A useful function for returning a path in the Samba state directory.
**/
char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
const char *name)
{
- char *fname, *dname;
- if (name == NULL) {
- return NULL;
- }
- if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
- return talloc_strdup(mem_ctx, name);
- }
-
- dname = talloc_strdup(mem_ctx, lpcfg_statedir(lp_ctx));
- trim_string(dname,"","/");
-
- if (!directory_exist(dname)) {
- mkdir(dname,0755);
- }
-
- fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-
- talloc_free(dname);
-
- return fname;
+ return lpcfg_common_path(mem_ctx, lp_ctx, name);
}
/**
@@ -136,26 +132,7 @@ char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
const char *name)
{
- char *fname, *dname;
- if (name == NULL) {
- return NULL;
- }
- if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
- return talloc_strdup(mem_ctx, name);
- }
-
- dname = talloc_strdup(mem_ctx, lpcfg_cachedir(lp_ctx));
- trim_string(dname,"","/");
-
- if (!directory_exist(dname)) {
- mkdir(dname,0755);
- }
-
- fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
-
- talloc_free(dname);
-
- return fname;
+ return lpcfg_common_path(mem_ctx, lp_ctx, name);
}
/**
@@ -222,10 +199,16 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
const char *name)
{
char *fname, *dname;
+ bool ok;
dname = lpcfg_private_path(mem_ctx, lp_ctx, "smbd.tmp");
- if (!directory_exist(dname)) {
- mkdir(dname,0755);
+ if (dname == NULL) {
+ return NULL;
+ }
+
+ ok = directory_create_or_exist(dname, geteuid(), 0755);
+ if (!ok) {
+ return NULL;
}
if (name == NULL) {
@@ -233,6 +216,9 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
}
fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+ if (fname == NULL) {
+ return dname;
+ }
talloc_free(dname);
return fname;