Index: wp-admin/includes/misc.php
===================================================================
--- wp-admin/includes/misc.php	(revision 11177)
+++ wp-admin/includes/misc.php	(working copy)
@@ -136,6 +136,33 @@
 }
 
 /**
+ * Updates the IIS web.config file with the current rules if it is writable.
+ * If the permalinks do not require rewrite rules then the rules are deleted from the web.config file.
+ * 
+ * 
+ * @return bool True if web.config was updated successfully
+ */
+function iis7_save_url_rewrite_rules(){
+	global $wp_rewrite;
+	
+	$home_path = get_home_path();
+	$web_config_file = $home_path.'web.config';
+
+	// Using is__writable() instead of is_writable() because of a bug in Windows PHP	
+	if ((!file_exists($web_config_file) && is__writable($home_path) && $wp_rewrite->using_mod_rewrite_permalinks()) || is__writable($web_config_file)) {
+		if ( iis7_supports_permalinks() ) {
+			$rule = $wp_rewrite -> iis7_url_rewrite_rules();
+			if ( !empty($rule) ) // 
+				return iis7_add_rewrite_rule($web_config_file, $rule);
+			else{
+				return iis7_delete_rewrite_rule($web_config_file);
+			}
+		}
+	}
+	return false;
+}
+
+/**
  * {@internal Missing Short Description}}
  *
  * @since unknown
@@ -370,4 +397,203 @@
 	 	exit;
 	}
 }
-?>
\ No newline at end of file
+
+// IIS 7 specific functions
+/**
+ * Check if IIS 7 supports pretty permalinks
+ * 
+ * 
+ * @return bool
+ */
+function iis7_supports_permalinks(){
+	global $is_iis7;
+
+	$supports_permalinks = false;	
+	if ($is_iis7){
+		// First we check if the DOMDocument class exists. If it does not exist,
+		// which is the case for PHP 4.X, then we cannot easily update the xml configuration file,
+		// hence we just bail out and tell user that pretty permalinks cannot be used.
+		// This is not a big issue because PHP 4.X is going to be depricated and for IIS it
+		// is recommended to use PHP 5.X NTS.
+		// Next we check if the URL Rewrite Module 1.1 is loaded and enabled for the web site. When
+		// URL Rewrite 1.1 is loaded it always sets a server variable called 'IIS_UrlRewriteModule'.
+		// Lastly we make sure that PHP is running via FastCGI. This is important because if it runs 
+		// via ISAPI then pretty permalinks will not work.
+		$supports_permalinks = class_exists('DOMDocument') && isset($_SERVER['IIS_UrlRewriteModule']) && (php_sapi_name() == 'cgi-fcgi');
+	}
+		
+	return apply_filters('iis7_supports_permalinks', $supports_permalinks);
+}
+
+/**
+ * Check if rewrite rule for WordPress already exists in the IIS 7 configuration file
+ * 
+ * 
+ * @return bool
+ * @param string $filename The file path to the configuration file
+ */
+function iis7_rewrite_rule_exists($filename)
+{	
+	if (!file_exists($filename)) return false;	
+	if (!class_exists('DOMDocument')) return false;
+	
+	$doc = new DOMDocument();
+	if ($doc -> load($filename) === false) return false;
+	$xpath = new DOMXPath($doc);
+	$rules = $xpath -> query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
+	if ($rules -> length == 0) 
+		return false;
+	else
+		return true;	
+}
+
+/**
+ * Delete WordPress rewrite rule from web.config file if it exists there
+ * @return 
+ * @param string $filename Name of the configuration file
+ */
+function iis7_delete_rewrite_rule($filename)
+{	
+	// If configuration file does not exist then rules also do not exist so there is nothing to delete
+	if (!file_exists($filename)) return true;
+	
+	if (!class_exists('DOMDocument')) return false;
+	
+	$doc = new DOMDocument();
+	$doc -> preserveWhiteSpace = false;
+
+	if ($doc -> load($filename) === false) return false;
+	$xpath = new DOMXPath($doc);
+	$rules = $xpath -> query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
+	if ($rules -> length > 0) {
+		$child = $rules -> item(0);
+		$parent = $child -> parentNode;
+		$parent -> removeChild($child);
+		$doc -> formatOutput = true;
+		saveDomDocument($doc, $filename);
+	}
+	return true;
+}
+
+/**
+ * Add WordPress rewrite rule to the IIS 7 configuration file.
+ * 
+ * 
+ * @return bool
+ * @param string $filename The file path to the configuration file
+ * @param string $rewrite_rule The XML fragment with URL Rewrite rule
+ */
+function iis7_add_rewrite_rule($filename, $rewrite_rule)
+{	
+	if (!class_exists('DOMDocument')) return false;
+	
+	// If configuration file does not exist then we create one.
+	if (!file_exists($filename)) {
+		$fp = fopen( $filename, 'w');
+		fwrite($fp, '<configuration/>');
+		fclose($fp);
+	}
+	
+	$doc = new DOMDocument();
+	$doc -> preserveWhiteSpace = false;
+
+	if ($doc -> load($filename) === false) return false;
+	
+	$xpath = new DOMXPath($doc);
+
+	// First check if the rule already exists as in that case there is no need to re-add it
+	$wordpress_rules = $xpath -> query('/configuration/system.webServer/rewrite/rules/rule[@name=\'wordpress\']');
+	if ($wordpress_rules -> length > 0) return true;
+
+	// Check the XPath to the rewrite rule and create XML nodes if they do not exist
+	$xmlnodes = $xpath -> query('/configuration/system.webServer/rewrite/rules');
+	if ($xmlnodes -> length > 0){
+		$rules_node = $xmlnodes -> item(0);
+	}
+	else{
+		$rules_node = $doc -> createElement('rules');
+		
+		$xmlnodes = $xpath -> query('/configuration/system.webServer/rewrite');
+		if ($xmlnodes -> length > 0){
+			$rewrite_node = $xmlnodes -> item(0);
+			$rewrite_node -> appendChild($rules_node);
+		}
+		else{
+			$rewrite_node = $doc -> createElement('rewrite');
+			$rewrite_node -> appendChild($rules_node);
+
+			$xmlnodes = $xpath -> query('/configuration/system.webServer');
+			if ($xmlnodes -> length > 0){
+				$system_webServer_node = $xmlnodes -> item(0);
+				$system_webServer_node -> appendChild($rewrite_node);
+			}
+			else{
+				$system_webServer_node = $doc -> createElement('system.webServer');
+				$system_webServer_node -> appendChild($rewrite_node);
+		
+				$xmlnodes = $xpath -> query('/configuration');
+				if ($xmlnodes -> length > 0){
+					$config_node = $xmlnodes -> item(0);
+					$config_node -> appendChild($system_webServer_node);
+				}
+				else{
+					$config_node = $doc -> createElement('configuration');
+					$doc -> appendChild($config_node);
+					$config_node -> appendChild($system_webServer_node);
+				} 
+			}		
+		}
+	}
+	
+	$rule_fragment = $doc -> createDocumentFragment();
+	$rule_fragment -> appendXML($rewrite_rule);
+	$rules_node -> appendChild($rule_fragment);
+
+	$doc -> formatOutput = true;	
+	saveDomDocument($doc, $filename);
+
+	return true;	
+}
+
+/**
+ * Saves the XML document into a file
+ * 
+ * @param DOMDocument $doc
+ * @param string $filename
+ */
+function saveDomDocument($doc, $filename)
+{
+	$config = $doc -> saveXML();	
+	$config = preg_replace("/([^\r])\n/", "$1\r\n", $config);
+	$fp = fopen($filename, 'w');
+	fwrite($fp, $config);
+	fclose($fp);
+}
+
+/**
+ * Workaround for Windows bug in is_writable() function
+ * 
+ * @return 
+ * @param object $path
+ */
+function is__writable($path) {
+//will work in despite of Windows ACLs bug
+//NOTE: use a trailing slash for folders!!!
+//see http://bugs.php.net/bug.php?id=27609
+//see http://bugs.php.net/bug.php?id=30931
+
+    if ($path{strlen($path)-1}=='/') // recursively return a temporary file path
+        return is__writable($path.uniqid(mt_rand()).'.tmp');
+    else if (is_dir($path))
+        return is__writable($path.'/'.uniqid(mt_rand()).'.tmp');
+    // check tmp file for read/write capabilities
+    $rm = file_exists($path);
+    $f = @fopen($path, 'a');
+    if ($f===false)
+        return false;
+    fclose($f);
+    if (!$rm)
+        unlink($path);
+    return true;
+}
+?>
Index: wp-admin/options-permalink.php
===================================================================
--- wp-admin/options-permalink.php	(revision 11177)
+++ wp-admin/options-permalink.php	(working copy)
@@ -70,6 +70,7 @@
 include('admin-header.php');
 
 $home_path = get_home_path();
+$iis7_permalinks = iis7_supports_permalinks();
 
 if ( isset($_POST['permalink_structure']) || isset($_POST['category_base']) ) {
 	check_admin_referer('update-permalink');
@@ -100,10 +101,18 @@
 $category_base = get_option('category_base');
 $tag_base = get_option( 'tag_base' );
 
-if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') )
-	$writable = true;
-else
-	$writable = false;
+if ($iis7_permalinks){
+	if ((!file_exists($home_path.'web.config') && is__writable($home_path)) || is__writable($home_path.'web.config') )
+		$writable = true;
+	else
+		$writable = false;
+}
+else{
+	if ( (!file_exists($home_path.'.htaccess') && is_writable($home_path)) || is_writable($home_path.'.htaccess') )
+		$writable = true;
+	else
+		$writable = false;
+}
 
 if ($wp_rewrite->using_index_permalinks())
 	$usingpi = true;
@@ -115,10 +124,20 @@
 
 <?php if (isset($_POST['submit'])) : ?>
 <div id="message" class="updated fade"><p><?php
-if ( $permalink_structure && !$usingpi && !$writable )
-	_e('You should update your .htaccess now.');
-else
-	_e('Permalink structure updated.');
+if ($iis7_permalinks){
+	if ($permalink_structure && !$usingpi && !$writable)
+		_e('You should update your web.config now');
+	else if ($permalink_structure && !$usingpi && $writable)
+		_e('Permalink structure updated. Remove write access on web.config file now!');
+	else
+		_e('Permalink structure updated');
+}
+else{
+	if ( $permalink_structure && !$usingpi && !$writable )
+		_e('You should update your .htaccess now.');
+	else
+		_e('Permalink structure updated.');
+}
 ?></p></div>
 <?php endif; ?>
 
@@ -134,7 +153,7 @@
 <?php
 $prefix = '';
 
-if ( ! got_mod_rewrite() )
+if ( !got_mod_rewrite() && !$iis7_permalinks )
 	$prefix = '/index.php';
 
 $structures = array(
@@ -179,7 +198,7 @@
 </table>
 
 <h3><?php _e('Optional'); ?></h3>
-<?php if ($is_apache) : ?>
+<?php if ($is_apache || $iis7_permalinks) : ?>
 	<p><?php _e('If you like, you may enter custom structures for your category and tag <abbr title="Universal Resource Locator">URL</abbr>s here. For example, using <kbd>topics</kbd> as your category base would make your category links like <code>http://example.org/topics/uncategorized/</code>. If you leave these blank the defaults will be used.') ?></p>
 <?php else : ?>
 	<p><?php _e('If you like, you may enter custom structures for your category and tag <abbr title="Universal Resource Locator">URL</abbr>s here. For example, using <code>topics</code> as your category base would make your category links like <code>http://example.org/index.php/topics/uncategorized/</code>. If you leave these blank the defaults will be used.') ?></p>
@@ -203,14 +222,25 @@
 	<input type="submit" name="submit" class="button-primary" value="<?php _ea('Save Changes') ?>" />
 </p>
   </form>
-<?php if ( $permalink_structure && !$usingpi && !$writable ) : ?>
-  <p><?php _e('If your <code>.htaccess</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.') ?></p>
+<?php if ($iis7_permalinks) :
+	if ( isset($_POST['submit']) && $permalink_structure && !$usingpi && !$writable ) : ?>
+<p><?php _e('If your <code>web.config</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so this is the url rewrite rule you should have in your <code>web.config</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all. Then insert this rule inside of the <code>/&lt;configuration&gt;/&lt;system.webServer&gt;/&lt;rewrite&gt;/&lt;rules&gt;</code> element in <code>web.config</code> file.') ?></p>
 <form action="options-permalink.php" method="post">
 <?php wp_nonce_field('update-permalink') ?>
+	<p><textarea rows="10" class="large-text readonly" name="rules" id="rules" readonly="readonly"><?php echo wp_specialchars($wp_rewrite->iis7_url_rewrite_rules()); ?></textarea></p>
+</form>
+<p><?php _e('If you temporarily make your <code>web.config</code> file writable for us to generate rewrite rules automatically, do not forget to revert the permissions after rule has been saved.')  ?></p>  
+	<?php endif; ?>
+<?php else : 
+	if ( $permalink_structure && !$usingpi && !writable ) :?>	
+<p><?php _e('If your <code>.htaccess</code> file were <a href="http://codex.wordpress.org/Changing_File_Permissions">writable</a>, we could do this automatically, but it isn&#8217;t so these are the mod_rewrite rules you should have in your <code>.htaccess</code> file. Click in the field and press <kbd>CTRL + a</kbd> to select all.') ?></p>
+<form action="options-permalink.php" method="post">
+<?php wp_nonce_field('update-permalink') ?>
 	<p><textarea rows="6" class="large-text readonly" name="rules" id="rules" readonly="readonly"><?php echo wp_specialchars($wp_rewrite->mod_rewrite_rules()); ?></textarea></p>
 </form>
+	<?php endif; ?>
 <?php endif; ?>
 
 </div>
 
-<?php require('./admin-footer.php'); ?>
+<?php require('./admin-footer.php'); ?>
\ No newline at end of file
Index: wp-includes/rewrite.php
===================================================================
--- wp-includes/rewrite.php	(revision 11177)
+++ wp-includes/rewrite.php	(working copy)
@@ -1695,6 +1695,35 @@
 
 		return $rules;
 	}
+	
+	/**
+	 * Retrieve IIS7 URL Rewrite formatted rewrite rules to write to web.config file.
+	 *
+	 * Does not actually write to the web.config file, but creates the rules for
+	 * the process that will.
+	 * 
+	 * @access public
+	 *
+	 * @return string
+	 */
+	function iis7_url_rewrite_rules(){
+		
+		if ( ! $this->using_permalinks()) {
+			return '';
+		}
+		$rules =  "<rule name=\"wordpress\" patternSyntax=\"Wildcard\">\n";
+		$rules .= "	<match url=\"*\" />\n";
+		$rules .= "	<conditions>\n";
+		$rules .= "		<add input=\"{REQUEST_FILENAME}\" matchType=\"IsFile\" negate=\"true\" />\n";
+		$rules .= "		<add input=\"{REQUEST_FILENAME}\" matchType=\"IsDirectory\" negate=\"true\" />\n";
+		$rules .= "	</conditions>\n";
+		$rules .= "	<action type=\"Rewrite\" url=\"index.php\" />\n";
+		$rules .= "</rule>";
+				
+		$rules = apply_filters('iis7_url_rewrite_rules', $rules);
+		
+		return $rules;
+	}
 
 	/**
 	 * Add a straight rewrite rule.
@@ -1790,6 +1819,8 @@
 		$this->wp_rewrite_rules();
 		if ( function_exists('save_mod_rewrite_rules') )
 			save_mod_rewrite_rules();
+		if ( function_exists('iis7_save_url_rewrite_rules'))
+			iis7_save_url_rewrite_rules();
 	}
 
 	/**
Index: wp-includes/vars.php
===================================================================
--- wp-includes/vars.php	(revision 11177)
+++ wp-includes/vars.php	(working copy)
@@ -67,7 +67,14 @@
  */
 $is_apache = ((strpos($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) || (strpos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false)) ? true : false;
 
+
 /**
+ * Whether the server software is IIS 7.X
+ * @global bool $is_IIS7
+ */
+$is_iis7 = (strpos($_SERVER['SERVER_SOFTWARE'], 'Microsoft-IIS/7.') !== false) ? true : false;
+
+/**
  * Whether the server software is IIS or something else
  * @global bool $is_IIS
  */
