diff options
author | Simo Sorce <idra@samba.org> | 2002-07-14 22:21:40 +0000 |
---|---|---|
committer | Simo Sorce <idra@samba.org> | 2002-07-14 22:21:40 +0000 |
commit | 050126e6844519c0587776932063e54b5f2c527c (patch) | |
tree | b05ea046219fa9f3f0889cc82328d7cb8e44e436 /source3 | |
parent | 13157d1494b99bf85632120c27286ba4d877a68f (diff) | |
download | samba-050126e6844519c0587776932063e54b5f2c527c.tar.gz samba-050126e6844519c0587776932063e54b5f2c527c.tar.bz2 samba-050126e6844519c0587776932063e54b5f2c527c.zip |
addedd new (t)alloc_sub_* functions
they will get a const string and return a (t)alloced epanded one.
also modified passdb/* stuff to use this one.
(This used to be commit d378ac1e2efb0efc9a0f983d69cf678ca6255fd5)
Diffstat (limited to 'source3')
-rw-r--r-- | source3/lib/substitute.c | 330 | ||||
-rw-r--r-- | source3/lib/util_str.c | 6 | ||||
-rw-r--r-- | source3/passdb/passdb.c | 8 | ||||
-rw-r--r-- | source3/passdb/pdb_ldap.c | 8 | ||||
-rw-r--r-- | source3/passdb/pdb_tdb.c | 10 |
5 files changed, 318 insertions, 44 deletions
diff --git a/source3/lib/substitute.c b/source3/lib/substitute.c index dbd382a942..1e8d8c2555 100644 --- a/source3/lib/substitute.c +++ b/source3/lib/substitute.c @@ -84,6 +84,66 @@ static size_t expand_env_var(char *p, int len) } /******************************************************************* + Given a pointer to a %$(NAME) in p and the whole string in str + expand it as an environment variable. + Return a new allocated and expanded string. + Based on code by Branko Cibej <branko.cibej@hermes.si> + When this is called p points at the '%' character. + May substitute multiple occurrencies of the same env var. +********************************************************************/ + + +static char * realloc_expand_env_var(char *str, char *p) +{ + char *envname; + char *envval; + char *q, *r; + int copylen; + + if (p[0] != '%' || p[1] != '$' || p[2] != '(') + return str; + + /* + * Look for the terminating ')'. + */ + + if ((q = strchr_m(p,')')) == NULL) { + DEBUG(0,("expand_env_var: Unterminated environment variable [%s]\n", p)); + return str; + } + + /* + * Extract the name from within the %$(NAME) string. + */ + + r = p + 3; + copylen = q - r; + envname = (char *)malloc(copylen + 1 + 4); // reserve space for use later add %$() chars + if (envname == NULL) return NULL; + strncpy(envname,r,copylen); + envname[copylen] = '\0'; + + if ((envval = getenv(envname)) == NULL) { + DEBUG(0,("expand_env_var: Environment variable [%s] not set\n", envname)); + SAFE_FREE(envname); + return str; + } + + /* + * Copy the full %$(NAME) into envname so it + * can be replaced. + */ + + copylen = q + 1 - p; + strncpy(envname,p,copylen); + envname[copylen] = '\0'; + r = realloc_string_sub(str, envname, envval); + SAFE_FREE(envname); + if (r == NULL) return NULL; + return r; +} + +/******************************************************************* Patch from jkf@soton.ac.uk Added this to implement %p (NIS auto-map version of %H) *******************************************************************/ @@ -244,10 +304,6 @@ void standard_sub_basic(const char *smb_name, char *str,size_t len) } } -/**************************************************************************** - Do some standard substitutions in a string. -****************************************************************************/ - static void standard_sub_advanced(int snum, const char *user, const char *connectpath, gid_t gid, const char *smb_name, char *str, size_t len) @@ -305,55 +361,273 @@ static void standard_sub_advanced(int snum, const char *user, standard_sub_basic(smb_name, str, len); } -const char *standard_sub_specified(TALLOC_CTX *mem_ctx, const char *input_string, - const char *username, - const char *domain, - uid_t uid, - gid_t gid) +/**************************************************************************** + Do some standard substitutions in a string. + This function will return an allocated string that have to be freed. +****************************************************************************/ + +char *talloc_sub_basic(TALLOC_CTX *mem_ctx, const char *smb_name, const char *str) { - pstring input_pstring; - char *p, *s; + char *a, *t; + a = alloc_sub_basic(smb_name, str); + if (!a) return NULL; + t = talloc_strdup(mem_ctx, a); + SAFE_FREE(a); + return t; +} - pstrcpy(input_pstring, input_string); +char *alloc_sub_basic(const char *smb_name, const char *str) +{ + char *b, *p, *s, *t, *r, *a_string; + fstring pidstr; + struct passwd *pass; + + a_string = strdup(str); + if (a_string == NULL) { + DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); + return NULL; + } - for (s=input_pstring; (p=strchr_m(s, '%')); s=p) { + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { + + r = NULL; + b = t = a_string; + + switch (*(p+1)) { + case 'U' : + r = strdup_lower(smb_name); + if (r == NULL) goto error; + t = realloc_string_sub(t, "%U", r); + break; + case 'G' : + r = strdup(smb_name); + if (r == NULL) goto error; + if ((pass = Get_Pwnam(r))!=NULL) { + t = realloc_string_sub(t, "%G", gidtoname(pass->pw_gid)); + } + break; + case 'D' : + r = strdup_upper(current_user_info.domain); + if (r == NULL) goto error; + t = realloc_string_sub(t, "%D", r); + break; + case 'I' : + t = realloc_string_sub(t, "%I", client_addr()); + break; + case 'L' : + if (*local_machine) + t = realloc_string_sub(t, "%L", local_machine); + else + t = realloc_string_sub(t, "%L", global_myname); + break; + case 'M' : + t = realloc_string_sub(t, "%M", client_name()); + break; + case 'R' : + t = realloc_string_sub(t, "%R", remote_proto); + break; + case 'T' : + t = realloc_string_sub(t, "%T", timestring(False)); + break; + case 'a' : + t = realloc_string_sub(t, "%a", remote_arch); + break; + case 'd' : + slprintf(pidstr,sizeof(pidstr)-1, "%d",(int)sys_getpid()); + t = realloc_string_sub(t, "%d", pidstr); + break; + case 'h' : + t = realloc_string_sub(t, "%h", myhostname()); + break; + case 'm' : + t = realloc_string_sub(t, "%m", remote_machine); + break; + case 'v' : + t = realloc_string_sub(t, "%v", VERSION); + break; + case '$' : + t = realloc_expand_env_var(t, p); /* Expand environment variables */ + break; + + default: + break; + } + + p++; + SAFE_FREE(r); + if (t == NULL) goto error; + a_string = t; + } + + return a_string; +error: + SAFE_FREE(a_string); + return NULL; +} + +/**************************************************************************** + Do some specific substitutions in a string. + This function will return an allocated string that have to be freed. +****************************************************************************/ - int l = sizeof(pstring) - (int)(p-input_pstring); +char *talloc_sub_specified(TALLOC_CTX *mem_ctx, + const char *input_string, + const char *username, + const char *domain, + uid_t uid, + gid_t gid) +{ + char *a, *t; + a = alloc_sub_specified(input_string, username, domain, uid, gid); + if (!a) return NULL; + t = talloc_strdup(mem_ctx, a); + SAFE_FREE(a); + return t; +} + +char *alloc_sub_specified(const char *input_string, + const char *username, + const char *domain, + uid_t uid, + gid_t gid) +{ + char *a_string, *ret_string; + char *b, *p, *s, *t; + + a_string = strdup(input_string); + if (a_string == NULL) { + DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); + return NULL; + } + + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { + + b = t = a_string; switch (*(p+1)) { case 'U' : - string_sub(p,"%U",username,l); + t = realloc_string_sub(t, "%U", username); break; case 'u' : - string_sub(p,"%u",username,l); + t = realloc_string_sub(t, "%u", username); break; case 'G' : + if (gid != -1) { + t = realloc_string_sub(t, "%G", gidtoname(gid)); + } else { + t = realloc_string_sub(t, "%G", "NO_GROUP"); + } + break; case 'g' : if (gid != -1) { - string_sub(p,"%G",gidtoname(gid),l); - string_sub(p,"%g",gidtoname(gid),l); + t = realloc_string_sub(t, "%g", gidtoname(gid)); } else { - string_sub(p,"%G","NO_GROUP",l); - string_sub(p,"%g","NO_GROUP",l); + t = realloc_string_sub(t, "%g", "NO_GROUP"); } break; case 'D' : - string_sub(p,"%D", domain,l); + t = realloc_string_sub(t, "%D", domain); break; case 'N' : - string_sub(p,"%N", automount_server(username),l); + t = realloc_string_sub(t, "%N", automount_server(username)); + break; + default: + break; + } + + p++; + if (t == NULL) { + SAFE_FREE(a_string); + return NULL; + } + a_string = t; + } + + ret_string = alloc_sub_basic(username, a_string); + SAFE_FREE(a_string); + return ret_string; +} + +char *talloc_sub_advanced(TALLOC_CTX *mem_ctx, + int snum, + const char *user, + const char *connectpath, + gid_t gid, + const char *smb_name, + char *str) +{ + char *a, *t; + a = alloc_sub_advanced(snum, user, connectpath, gid, smb_name, str); + if (!a) return NULL; + t = talloc_strdup(mem_ctx, a); + SAFE_FREE(a); + return t; +} + +char *alloc_sub_advanced(int snum, const char *user, + const char *connectpath, gid_t gid, + const char *smb_name, char *str) +{ + char *a_string, *ret_string; + char *b, *p, *s, *t, *h; + + a_string = strdup(str); + if (a_string == NULL) { + DEBUG(0, ("alloc_sub_specified: Out of memory!\n")); + return NULL; + } + + for (b = s = a_string; (p = strchr_m(s, '%')); s = a_string + (p - b)) { + + b = t = a_string; + + switch (*(p+1)) { + case 'N' : + t = realloc_string_sub(t, "%N", automount_server(user)); + break; + case 'H': + if ((h = get_user_home_dir(user))) + t = realloc_string_sub(t, "%H", h); + break; + case 'P': + t = realloc_string_sub(t, "%P", connectpath); + break; + case 'S': + t = realloc_string_sub(t, "%S", lp_servicename(snum)); + break; + case 'g': + t = realloc_string_sub(t, "%g", gidtoname(gid)); + break; + case 'u': + t = realloc_string_sub(t, "%u", user); break; - case '\0': - p++; - break; /* don't run off the end of the string */ - default: p+=2; + /* Patch from jkf@soton.ac.uk Left the %N (NIS + * server name) in standard_sub_basic as it is + * a feature for logon servers, hence uses the + * username. The %p (NIS server path) code is + * here as it is used instead of the default + * "path =" string in [homes] and so needs the + * service name, not the username. */ + case 'p': + t = realloc_string_sub(t, "%p", automount_path(lp_servicename(snum))); break; + + default: + break; + } + + p++; + if (t == NULL) { + SAFE_FREE(a_string); + return NULL; } + a_string = t; } - standard_sub_basic(username, input_pstring, sizeof(pstring)); - return talloc_strdup(mem_ctx, input_pstring); + ret_string = alloc_sub_basic(smb_name, a_string); + SAFE_FREE(a_string); + return ret_string; } /**************************************************************************** diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c index 4c8d5d8bf7..88a72f1536 100644 --- a/source3/lib/util_str.c +++ b/source3/lib/util_str.c @@ -762,7 +762,7 @@ char *realloc_string_sub(char *string, const char *pattern, const char *insert) return NULL; } string = t; - s = t + (p - s); + p = t + (p - s); } if (li != lp) { memmove(p+li,p+lp,strlen(p+lp)+1); @@ -992,7 +992,7 @@ void strlower_m(char *s) /******************************************************************* duplicate convert a string to lower case ********************************************************************/ -char *strdup_lower(char *s) +char *strdup_lower(const char *s) { char *t = strdup(s); if (t == NULL) { @@ -1026,7 +1026,7 @@ void strupper_m(char *s) /******************************************************************* convert a string to upper case ********************************************************************/ -char *strdup_upper(char *s) +char *strdup_upper(const char *s) { char *t = strdup(s); if (t == NULL) { diff --git a/source3/passdb/passdb.c b/source3/passdb/passdb.c index 2bf3eccfb7..4e3d558e98 100644 --- a/source3/passdb/passdb.c +++ b/source3/passdb/passdb.c @@ -207,28 +207,28 @@ NTSTATUS pdb_fill_sam_pw(SAM_ACCOUNT *sam_account, const struct passwd *pwd) if (pwd->pw_name[strlen(pwd->pw_name)-1] != '$') { pdb_set_profile_path(sam_account, - standard_sub_specified((sam_account)->mem_ctx, + talloc_sub_specified((sam_account)->mem_ctx, lp_logon_path(), pwd->pw_name, global_myname, pwd->pw_uid, pwd->pw_gid), False); pdb_set_homedir(sam_account, - standard_sub_specified((sam_account)->mem_ctx, + talloc_sub_specified((sam_account)->mem_ctx, lp_logon_home(), pwd->pw_name, global_myname, pwd->pw_uid, pwd->pw_gid), False); pdb_set_dir_drive(sam_account, - standard_sub_specified((sam_account)->mem_ctx, + talloc_sub_specified((sam_account)->mem_ctx, lp_logon_drive(), pwd->pw_name, global_myname, pwd->pw_uid, pwd->pw_gid), False); pdb_set_logon_script(sam_account, - standard_sub_specified((sam_account)->mem_ctx, + talloc_sub_specified((sam_account)->mem_ctx, lp_logon_script(), pwd->pw_name, global_myname, pwd->pw_uid, pwd->pw_gid), diff --git a/source3/passdb/pdb_ldap.c b/source3/passdb/pdb_ldap.c index fd5ad7ee12..24eb7b9dc1 100644 --- a/source3/passdb/pdb_ldap.c +++ b/source3/passdb/pdb_ldap.c @@ -695,7 +695,7 @@ static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state, } if (!get_single_attribute(ldap_struct, entry, "homeDrive", dir_drive)) { - pdb_set_dir_drive(sampass, standard_sub_specified(sampass->mem_ctx, + pdb_set_dir_drive(sampass, talloc_sub_specified(sampass->mem_ctx, lp_logon_drive(), username, domain, uid, gid), @@ -705,7 +705,7 @@ static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state, } if (!get_single_attribute(ldap_struct, entry, "smbHome", homedir)) { - pdb_set_homedir(sampass, standard_sub_specified(sampass->mem_ctx, + pdb_set_homedir(sampass, talloc_sub_specified(sampass->mem_ctx, lp_logon_home(), username, domain, uid, gid), @@ -715,7 +715,7 @@ static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state, } if (!get_single_attribute(ldap_struct, entry, "scriptPath", logon_script)) { - pdb_set_logon_script(sampass, standard_sub_specified(sampass->mem_ctx, + pdb_set_logon_script(sampass, talloc_sub_specified(sampass->mem_ctx, lp_logon_script(), username, domain, uid, gid), @@ -725,7 +725,7 @@ static BOOL init_sam_from_ldap (struct ldapsam_privates *ldap_state, } if (!get_single_attribute(ldap_struct, entry, "profilePath", profile_path)) { - pdb_set_profile_path(sampass, standard_sub_specified(sampass->mem_ctx, + pdb_set_profile_path(sampass, talloc_sub_specified(sampass->mem_ctx, lp_logon_path(), username, domain, uid, gid), diff --git a/source3/passdb/pdb_tdb.c b/source3/passdb/pdb_tdb.c index b309f675b3..6279318969 100644 --- a/source3/passdb/pdb_tdb.c +++ b/source3/passdb/pdb_tdb.c @@ -188,7 +188,7 @@ static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state, } else { pdb_set_homedir(sampass, - standard_sub_specified(sampass->mem_ctx, + talloc_sub_specified(sampass->mem_ctx, lp_logon_home(), username, domain, uid, gid), @@ -199,7 +199,7 @@ static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state, pdb_set_dir_drive(sampass, dir_drive, True); else { pdb_set_dir_drive(sampass, - standard_sub_specified(sampass->mem_ctx, + talloc_sub_specified(sampass->mem_ctx, lp_logon_drive(), username, domain, uid, gid), @@ -210,18 +210,18 @@ static BOOL init_sam_from_buffer (struct tdbsam_privates *tdb_state, pdb_set_logon_script(sampass, logon_script, True); else { pdb_set_logon_script(sampass, - standard_sub_specified(sampass->mem_ctx, + talloc_sub_specified(sampass->mem_ctx, lp_logon_script(), username, domain, uid, gid), False); } - + if (profile_path) { pdb_set_profile_path(sampass, profile_path, True); } else { pdb_set_profile_path(sampass, - standard_sub_specified(sampass->mem_ctx, + talloc_sub_specified(sampass->mem_ctx, lp_logon_path(), username, domain, uid, gid), |