How to programatically create an FTP7 site

Few people asked me how to programatically create FTP 7 site. FTP 7 doesn't use the IIS metabase to store the site information. Instead, it uses the new configuration store that comes with IIS7.The following jscript function uses "Microsoft.ApplicationHost.WritableAdminManager" to create anonymous FTP 7 site that allows everyone a read access. Please run it with the cscript.exe engine to avoid annoying pop-ups.

Note: Unlike the previous versions of FTP servers released by Microsoft, FTP7 shares it's site configuration with the Web Server (IIS7). Standalone FTP server is now nothing more than a web site with ftp binding. Sharing of the configuration is really nice because it allows FTP publishing to be set up on top of the existing web site. Such site would contain both "http" and "ftp" binding.
Administrator has to be aware of this new logic. It means that site names and site IDs are now shared between the Web Server and FTP server. So if there is site with Id="1" already used by web server, then different site ID would need to be used for the standalone FTP server. Please keep it in mind when using the code snippet. Also, make sure you have FTP 7 installed.

function AddFtp7Site(  siteName, siteId, siteRoot )
{
    var configPath;
    var configSectionName;
    var fNewSite = false;
    var fNewApplication = false;
    var fNewVDir = false;
    //
    // First setup the sites section
    //
    configPath = "MACHINE/WEBROOT/APPHOST";
    configSectionName = "system.applicationHost/sites";

    var adminManager = WScript.CreateObject( "Microsoft.ApplicationHost.WritableAdminManager" );
    adminManager.CommitPath = configPath;

    try
    {
        var sitesElement = adminManager.GetAdminSection( configSectionName, configPath );
        var newSiteElement;
      
        //
        // check if site already exists
        //
        for ( var i = 0; i < sitesElement .Collection.Count; i++ )
        {
            var siteElement = sitesElement.Collection.Item( i );
   
            if ( siteElement.Properties.Item( "name" ).Value == siteName &&
                 siteElement.Properties.Item( "id" ).Value == siteId )
            {
                newSiteElement = siteElement;
                break;
            }
        }

        if ( newSiteElement == null )
        {
            //
            // Site doesn't exist yet. Add new site node
            //

            newSiteElement = sitesElement.collection.CreateNewElement();
            newSiteElement.Properties.Item( "id" ).Value = siteId;
            newSiteElement.Properties.Item( "name" ).Value = siteName;
            fNewSite = true;
        }

        // setup bindings for the new site

        var ftpBindingString = "*:21:";

        var Bindings  = newSiteElement.GetElementByName("bindings");
        var BindingElement = Bindings.collection.CreateNewElement();
       
       
        BindingElement.Properties.Item( "protocol" ).Value = "ftp";
        BindingElement.Properties.Item( "bindingInformation").Value = ftpBindingString;
       
        try
        {
            Bindings.Collection.AddElement( BindingElement );
        }
        catch( e )
        {
            if ( e.number != ERROR_ALREADY_EXISTS )
            {
                throw e;
            }
        }

        var newApplication;
        //
        // check if root application already exists
        //
        for ( var i = 0; i < newSiteElement.Collection.Count; i++ )
        {
            var applicationElement = newSiteElement.Collection.Item( i );
   
            if ( applicationElement.Properties.Item( "path" ).Value == "/" )
            {
                newApplication = applicationElement;
                break;
            }
        }
       
        if ( newApplication == null )
        {
            newApplication = newSiteElement.collection.CreateNewElement("application");
            newApplication.Properties.Item( "path" ).Value = "/";
            fNewApplication = true;
        }
       
        var newVirtualDirectory;
        //
        // search for the root vdir
        //

        for ( var i = 0; i < newApplication.Collection.Count; i++ )
        {
            var vdirElement = newApplication.Collection.Item( i );
   
            if ( vdirElement.Properties.Item( "path" ).Value == "/" )
            {
                newVirtualDirectory = vdirElement;
                break;
            }
        }
        if ( newVirtualDirectory == null )
        {
            newVirtualDirectory = newApplication.collection.CreateNewElement();
            newVirtualDirectory.Properties.Item( "path" ).Value = "/";
            fNewVDir = true;
        }
       
        newVirtualDirectory.Properties.Item( "physicalPath" ).Value = siteRoot;

        if ( fNewVDir )
        {
            newApplication.Collection.AddElement( newVirtualDirectory );
        }
       
        if ( fNewApplication )
        {
            newSiteElement.Collection.AddElement( newApplication );
        }

       
        var ftpSiteSettings  = newSiteElement.GetElementByName("ftpServer").GetElementByName("security").GetElementByName("authentication");

        WScript.Echo( "Enable anonymous authentication" );
       
        var anonAuthSettings = ftpSiteSettings.GetElementByName("anonymousAuthentication");
        anonAuthSettings.Properties.Item( "enabled" ).Value = "true";

        WScript.Echo( "Disable basic authentication" );       
        var basicAuthSettings = ftpSiteSettings.GetElementByName("basicAuthentication");       
        basicAuthSettings.Properties.Item( "enabled" ).Value = "false";
       

        BindingElement.Properties.Item( "bindingInformation").Value = "*:21:";

        //
        // Time to add new site element and commit changes
        //

        if ( fNewSite )
        {             
            sitesElement.Collection.AddElement( newSiteElement );
        }

        adminManager.CommitChanges();
    }
    catch( e )
    {
        WScript.Echo( "Error occured in AddDefaultFtpSite: " +e.description + " ErrorCode="+e.number );
    }

    //
    // Add <authorization> section to allow everyone Read
    //

    WScript.Echo( "Enable everyone Read access" );
    
    try
    {  
        configPath = "MACHINE/WEBROOT/APPHOST/"+ siteName;
        configSectionName = "system.ftpServer/security/authorization";
         
        var azSection = adminManager.GetAdminSection( configSectionName, configPath );

        azSection.Collection.Clear();

        var newAzElement = azSection.collection.CreateNewElement();
        newAzElement.Properties.Item( "accessType" ).Value = "Allow";
        newAzElement.Properties.Item( "users" ).Value = "*";
        newAzElement.Properties.Item( "permissions" ).Value = "Read";
       
        azSection.Collection.AddElement( newAzElement );
        adminManager.CommitChanges();
    }
    catch( e )
    {
        WScript.Echo( "Error occured while adding authorization section: " +e.description + " ErrorCode="+e.number );
    }
}

 

 

 

 

5 Comments

Comments have been disabled for this content.