modules/vfs_expand_msdfs.c

説明を見る。
00001 /* 
00002  * Expand msdfs targets based on client IP
00003  *
00004  * Copyright (C) Volker Lendecke, 2004
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *  
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *  
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #include "includes.h"
00022 
00023 #undef DBGC_CLASS
00024 #define DBGC_CLASS DBGC_VFS
00025 
00026 extern userdom_struct current_user_info;
00027 
00028 /**********************************************************
00029   Under mapfile we expect a table of the following format:
00030 
00031   IP-Prefix whitespace expansion
00032 
00033   For example:
00034   192.168.234 local.samba.org
00035   192.168     remote.samba.org
00036               default.samba.org
00037 
00038   This is to redirect a DFS client to a host close to it.
00039 ***********************************************************/
00040 
00041 static BOOL read_target_host(const char *mapfile, pstring targethost)
00042 {
00043         XFILE *f;
00044         pstring buf;
00045         char *space = buf;
00046         BOOL found = False;
00047         
00048         f = x_fopen(mapfile, O_RDONLY, 0);
00049 
00050         if (f == NULL) {
00051                 DEBUG(0,("can't open IP map %s. Error %s\n",
00052                          mapfile, strerror(errno) ));
00053                 return False;
00054         }
00055 
00056         DEBUG(10, ("Scanning mapfile [%s]\n", mapfile));
00057 
00058         while (x_fgets(buf, sizeof(buf), f) != NULL) {
00059 
00060                 if ((strlen(buf) > 0) && (buf[strlen(buf)-1] == '\n'))
00061                         buf[strlen(buf)-1] = '\0';
00062 
00063                 DEBUG(10, ("Scanning line [%s]\n", buf));
00064 
00065                 space = strchr_m(buf, ' ');
00066 
00067                 if (space == NULL) {
00068                         DEBUG(0, ("Ignoring invalid line %s\n", buf));
00069                         continue;
00070                 }
00071 
00072                 *space = '\0';
00073 
00074                 if (strncmp(client_addr(), buf, strlen(buf)) == 0) {
00075                         found = True;
00076                         break;
00077                 }
00078         }
00079 
00080         x_fclose(f);
00081 
00082         if (!found)
00083                 return False;
00084 
00085         space += 1;
00086 
00087         while (isspace(*space))
00088                 space += 1;
00089 
00090         pstrcpy(targethost, space);
00091         return True;
00092 }
00093 
00094 /**********************************************************
00095 
00096   Expand the msdfs target host using read_target_host
00097   explained above. The syntax used in the msdfs link is
00098 
00099   msdfs:@table-filename@/share
00100 
00101   Everything between and including the two @-signs is
00102   replaced by the substitution string found in the table
00103   described above.
00104 
00105 ***********************************************************/
00106 
00107 static BOOL expand_msdfs_target(connection_struct* conn, pstring target)
00108 {
00109         pstring mapfilename;
00110         char *filename_start = strchr_m(target, '@');
00111         char *filename_end;
00112         int filename_len;
00113         pstring targethost;
00114         pstring new_target;
00115 
00116         if (filename_start == NULL) {
00117                 DEBUG(10, ("No filename start in %s\n", target));
00118                 return False;
00119         }
00120 
00121         filename_end = strchr_m(filename_start+1, '@');
00122 
00123         if (filename_end == NULL) {
00124                 DEBUG(10, ("No filename end in %s\n", target));
00125                 return False;
00126         }
00127 
00128         filename_len = PTR_DIFF(filename_end, filename_start+1);
00129         pstrcpy(mapfilename, filename_start+1);
00130         mapfilename[filename_len] = '\0';
00131 
00132         DEBUG(10, ("Expanding from table [%s]\n", mapfilename));
00133 
00134         if (!read_target_host(mapfilename, targethost)) {
00135                 DEBUG(1, ("Could not expand target host from file %s\n",
00136                           mapfilename));
00137                 return False;
00138         }
00139 
00140         standard_sub_advanced(lp_servicename(SNUM(conn)), conn->user,
00141                               conn->connectpath, conn->gid,
00142                               get_current_username(),
00143                               current_user_info.domain,
00144                               mapfilename, sizeof(mapfilename));
00145 
00146         DEBUG(10, ("Expanded targethost to %s\n", targethost));
00147 
00148         *filename_start = '\0';
00149         pstrcpy(new_target, target);
00150         pstrcat(new_target, targethost);
00151         pstrcat(new_target, filename_end+1);
00152 
00153         DEBUG(10, ("New DFS target: %s\n", new_target));
00154         pstrcpy(target, new_target);
00155         return True;
00156 }
00157 
00158 static int expand_msdfs_readlink(struct vfs_handle_struct *handle,
00159                                  const char *path, char *buf, size_t bufsiz)
00160 {
00161         pstring target;
00162         int result;
00163 
00164         result = SMB_VFS_NEXT_READLINK(handle, path, target,
00165                                        sizeof(target));
00166 
00167         if (result < 0)
00168                 return result;
00169 
00170         target[result] = '\0';
00171 
00172         if ((strncmp(target, "msdfs:", strlen("msdfs:")) == 0) &&
00173             (strchr_m(target, '@') != NULL)) {
00174                 if (!expand_msdfs_target(handle->conn, target)) {
00175                         errno = ENOENT;
00176                         return -1;
00177                 }
00178         }
00179 
00180         safe_strcpy(buf, target, bufsiz-1);
00181         return strlen(buf);
00182 }
00183 
00184 /* VFS operations structure */
00185 
00186 static vfs_op_tuple expand_msdfs_ops[] = {      
00187         {SMB_VFS_OP(expand_msdfs_readlink), SMB_VFS_OP_READLINK,
00188          SMB_VFS_LAYER_TRANSPARENT},
00189         {SMB_VFS_OP(NULL), SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
00190 };
00191 
00192 NTSTATUS vfs_expand_msdfs_init(void);
00193 NTSTATUS vfs_expand_msdfs_init(void)
00194 {
00195         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "expand_msdfs",
00196                                 expand_msdfs_ops);
00197 }

Sambaに対してSat Aug 29 21:23:07 2009に生成されました。  doxygen 1.4.7