I can't get away without a 'length' arg. :-).
[samba.git] / source3 / lib / substitute.c
index 25a6a2c4c82f7ff0aab1c1c01228ef5c177cfc83..d4c7cd6467e0d7f4e6e071fd6922096e5b345648 100644 (file)
@@ -6,7 +6,7 @@
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -15,8 +15,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 
@@ -38,9 +37,9 @@ static fstring smb_user_name;
  * @param if this is the 'final' name for us, not be be changed again
  */
 
-void set_local_machine_name(const char* local_name, BOOL perm)
+void set_local_machine_name(const char* local_name, bool perm)
 {
-       static BOOL already_perm = False;
+       static bool already_perm = False;
        fstring tmp_local_machine;
 
        fstrcpy(tmp_local_machine,local_name);
@@ -71,9 +70,9 @@ void set_local_machine_name(const char* local_name, BOOL perm)
  * @param if this is the 'final' name for them, not be be changed again
  */
 
-void set_remote_machine_name(const char* remote_name, BOOL perm)
+void set_remote_machine_name(const char* remote_name, bool perm)
 {
-       static BOOL already_perm = False;
+       static bool already_perm = False;
        fstring tmp_remote_machine;
 
        if (already_perm)
@@ -109,7 +108,7 @@ void sub_set_smb_name(const char *name)
 {
        fstring tmp;
        int len;
-       BOOL is_machine_account = False;
+       bool is_machine_account = False;
 
        /* don't let anonymous logins override the name */
        if (! *name)
@@ -141,11 +140,6 @@ void sub_set_smb_name(const char *name)
        }
 }
 
-char* sub_get_smb_name( void )
-{
-       return smb_user_name;
-}
-
 /*******************************************************************
  Setup the strings used by substitutions. Called per packet. Ensure
  %U name is set correctly also.
@@ -155,7 +149,7 @@ void set_current_user_info(const userdom_struct *pcui)
 {
        current_user_info = *pcui;
        /* The following is safe as current_user_info.smb_name
-        * has already been sanitised in register_vuid. */
+        * has already been sanitised in register_existing_vuid. */
        fstrcpy(smb_user_name, current_user_info.smb_name);
 }
 
@@ -264,7 +258,7 @@ struct api_longvar {
        char* (*fn)( void );
 };
 
-struct api_longvar longvar_table[] = {
+static struct api_longvar longvar_table[] = {
        { "DomainSID",          longvar_domainsid },
        { NULL,                 NULL }
 };
@@ -340,7 +334,7 @@ static char *realloc_expand_longvar(char *str, char *p)
 
 static char *automount_path(const char *user_name)
 {
-       static pstring server_path;
+       pstring server_path;
 
        /* use the passwd entry as the default */
        /* this will be the default if WITH_AUTOMOUNT is not used or fails */
@@ -369,7 +363,7 @@ static char *automount_path(const char *user_name)
 
        DEBUG(4,("Home server path: %s\n", server_path));
 
-       return server_path;
+       return talloc_strdup(talloc_tos(), server_path);
 }
 
 /*******************************************************************
@@ -380,7 +374,7 @@ static char *automount_path(const char *user_name)
 
 static const char *automount_server(const char *user_name)
 {
-       static pstring server_name;
+       pstring server_name;
        const char *local_machine_name = get_local_machine_name(); 
 
        /* use the local machine name as the default */
@@ -406,7 +400,7 @@ static const char *automount_server(const char *user_name)
 
        DEBUG(4,("Home server: %s\n", server_name));
 
-       return server_name;
+       return talloc_strdup(talloc_tos(), server_name);
 }
 
 /****************************************************************************
@@ -453,8 +447,9 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                      const char *str)
 {
        char *b, *p, *s, *r, *a_string;
-       fstring pidstr;
+       fstring pidstr, vnnstr;
        struct passwd *pass;
+       char addr[INET6_ADDRSTRLEN];
        const char *local_machine_name = get_local_machine_name();
 
        /* workaround to prevent a crash while looking at bug #687 */
@@ -500,7 +495,8 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                        a_string = realloc_string_sub(a_string, "%D", r);
                        break;
                case 'I' :
-                       a_string = realloc_string_sub(a_string, "%I", client_addr());
+                       a_string = realloc_string_sub(a_string, "%I",
+                                               client_addr(addr, sizeof(addr)));
                        break;
                case 'i': 
                        a_string = realloc_string_sub( a_string, "%i", client_socket_addr() );
@@ -552,6 +548,10 @@ char *alloc_sub_basic(const char *smb_name, const char *domain_name,
                case '(':
                        a_string = realloc_expand_longvar( a_string, p );
                        break;
+               case 'V' :
+                       slprintf(vnnstr,sizeof(vnnstr)-1, "%u", get_my_vnn());
+                       a_string = realloc_string_sub(a_string, "%V", vnnstr);
+                       break;
                default: 
                        break;
                }
@@ -769,3 +769,21 @@ void standard_sub_advanced(const char *servicename, const char *user,
                SAFE_FREE( s );
        }
 }
+
+/****************************************************************************
+ *  Do some standard substitutions in a string.
+ *  ****************************************************************************/
+
+void standard_sub_conn(connection_struct *conn, char *str, size_t len)
+{
+       char *s;
+
+       s = alloc_sub_advanced(lp_servicename(SNUM(conn)), conn->user, conn->connectpath,
+                              conn->gid, smb_user_name, "", str);
+
+       if ( s ) {
+               strncpy( str, s, len );
+               SAFE_FREE( s );
+       }
+}
+