<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>

<channel>
	<title>Unwound Stack</title>
	<atom:link href="http://www.unwoundstack.com/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.unwoundstack.com</link>
	<description>Recreational hacking</description>
	<pubDate>Tue, 14 Oct 2008 03:50:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>VisEmacs 3.1.1 Released</title>
		<link>http://www.unwoundstack.com/?p=21</link>
		<comments>http://www.unwoundstack.com/?p=21#comments</comments>
		<pubDate>Tue, 14 Oct 2008 03:50:00 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Projects]]></category>

		<category><![CDATA[coding]]></category>

		<category><![CDATA[addins]]></category>

		<category><![CDATA[VisualStudio]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=21</guid>
		<description><![CDATA[Well, I&#8217;m feeling overwhelmed these days, but at least I&#8217;ve knocked one item off my todo list&#8211; I&#8217;ve released a new version of VisEmacs.  You can get the installer here.
Release 3.1 included support for Emacsclient, but I think introduced a bug&#8211; the new configuration settings weren&#8217;t being saved correctly (or maybe they never were&#8211; I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I&#8217;m feeling overwhelmed these days, but at least I&#8217;ve knocked one item off my todo list&#8211; I&#8217;ve released a new version of VisEmacs.  You can get the installer <a href="dl/tools/VisEmacs.3.1.1.exe">here</a>.</p>
<p>Release 3.1 included support for Emacsclient, but I think introduced a bug&#8211; the new configuration settings weren&#8217;t being saved correctly (or maybe they never were&#8211; I&#8217;m too tired to check).  Many thankgs to Andrew Ng <anng@ptc.com> for first, catching it, and second fixing it.</p>
<p>Also, Christoph Conrad <conrad@surgitaix.com> tells me that the <a href="http://tulrich.com/geekstuff#dot_net_sanity">DotEnvCommand</a> tool makes a really handy companion to VisEmacs.  I haven&#8217;t tried it yet, but it looks pretty cool.  If anyone&#8217;s using it, do let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=21</wfw:commentRss>
		</item>
		<item>
		<title>I&#8217;m Alive&#8230;</title>
		<link>http://www.unwoundstack.com/?p=20</link>
		<comments>http://www.unwoundstack.com/?p=20#comments</comments>
		<pubDate>Wed, 24 Sep 2008 14:33:43 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<category><![CDATA[Site]]></category>

		<category><![CDATA[blogging]]></category>

		<category><![CDATA[house]]></category>

		<category><![CDATA[photo]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=20</guid>
		<description><![CDATA[Sheesh&#8211; I see I haven&#8217;t posted since May.  Well, I haven&#8217;t died.
Its just that I bought a house!
Yes, news reports to the contrary, the housing market hasn&#8217;t
collapsed completely.  In fact, those of us who&#8217;ve been sitting
on the sidelines during the bubble are now jumping in  
Anyway, between hanging sheetrock, clearing brush, &#38; [...]]]></description>
			<content:encoded><![CDATA[<p>Sheesh&#8211; I see I haven&#8217;t posted since May.  Well, I haven&#8217;t died.<br />
Its just that I bought a house!</p>
<p>Yes, news reports to the contrary, the housing market hasn&#8217;t<br />
collapsed completely.  In fact, those of us who&#8217;ve been sitting<br />
on the sidelines during the bubble are now jumping in <img src='http://www.unwoundstack.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Anyway, between hanging sheetrock, clearing brush, &amp; painting, I<br />
just haven&#8217;t had time to post as much as I&#8217;d like.</p>
<p>This is a Redwood grove on the property:</p>
<p><img src="http://www.unwoundstack.com/images/photos/house/0605080900_480x640.jpg" alt="" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=20</wfw:commentRss>
		</item>
		<item>
		<title>Socked in the past few mornings&#8230;</title>
		<link>http://www.unwoundstack.com/?p=19</link>
		<comments>http://www.unwoundstack.com/?p=19#comments</comments>
		<pubDate>Sat, 10 May 2008 00:06:07 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<category><![CDATA[commute]]></category>

		<category><![CDATA[photo]]></category>

		<category><![CDATA[scmountains]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=19</guid>
		<description><![CDATA[The San Lorenzo Valley&#8217;s been socked in with fog &#38; even a little drizzle the past few mornings.  It was getting really irritating for me (I like it warm &#38; sunny), but at least I drive out of it on the way into work.  I took this shot from Saratoga Gap yesterday:

]]></description>
			<content:encoded><![CDATA[<p>The San Lorenzo Valley&#8217;s been socked in with fog &amp; even a little drizzle the past few mornings.  It was getting <em>really</em> irritating for me (I like it warm &amp; sunny), but at least I drive out of it on the way into work.  I took this shot from Saratoga Gap yesterday:</p>
<p><img style="none" src="http://www.unwoundstack.com/images/photos/nature/0508080832_small.jpg" alt="Looking south-east from Saratoga Gap" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=19</wfw:commentRss>
		</item>
		<item>
		<title>Article on writing Add-Ins restored&#8230;</title>
		<link>http://www.unwoundstack.com/?p=17</link>
		<comments>http://www.unwoundstack.com/?p=17#comments</comments>
		<pubDate>Sun, 27 Apr 2008 01:57:24 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[coding]]></category>

		<category><![CDATA[addins]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[VisualStudio]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=17</guid>
		<description><![CDATA[About eighteen months ago, I wrote an article about how to build an Add-In that would load itself into DeStudio 6.0, Visual Studio (2003 or 2005), and Office (2003). Like some other things I&#8217;ve written up, it found a small but appreciative audience (here, for instance).
I&#8217;d hosted the article, along with source code, on the [...]]]></description>
			<content:encoded><![CDATA[<p>About eighteen months ago, I wrote an article about how to build an Add-In that would load itself into DeStudio 6.0, Visual Studio (2003 or 2005), <em>and</em> Office (2003). Like some other things I&#8217;ve written up, it found a small but appreciative audience (<a href="http://www.codeproject.com/KB/macros/samplecai.aspx">here</a>, for instance).</p>
<p>I&#8217;d hosted the article, along with source code, on the old site. Over the past week or so, I updated it for Visual Studio 2008 &#038; re-posted it <a href="http://www.unwoundstack.com/?page_id=16">here</a>.  To the half-dozen or so people who might interested in such a thing, enjoy <img src='http://www.unwoundstack.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=17</wfw:commentRss>
		</item>
		<item>
		<title>Rainy nights</title>
		<link>http://www.unwoundstack.com/?p=14</link>
		<comments>http://www.unwoundstack.com/?p=14#comments</comments>
		<pubDate>Fri, 25 Apr 2008 03:39:56 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<category><![CDATA[commute]]></category>

		<category><![CDATA[photo]]></category>

		<category><![CDATA[scmountains]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=14</guid>
		<description><![CDATA[I love rainy nights; they&#8217;re good for sleeping, and they make for great views on the drive into work the next morning:

]]></description>
			<content:encoded><![CDATA[<p>I love rainy nights; they&#8217;re good for sleeping, and they make for great views on the drive into work the next morning:</p>
<p><img style="none" src="http://www.unwoundstack.com/images/photos/nature/0423080804a_small.jpg" alt="Looking east from the overlook on 9" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=14</wfw:commentRss>
		</item>
		<item>
		<title>Figuring out dependencies introduced by static libraries</title>
		<link>http://www.unwoundstack.com/?p=13</link>
		<comments>http://www.unwoundstack.com/?p=13#comments</comments>
		<pubDate>Wed, 23 Apr 2008 18:15:41 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[coding]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[windows]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=13</guid>
		<description><![CDATA[I don&#8217;t know if anyone else has this problem, but I sometimes want to know what dependencies will be introduced into my program by linking against a static library (on Windows).  If I&#8217;m linking against a DLL, I can just run depends, which will tell me what other DLLs that DLL needs to load [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t know if anyone else has this problem, but I sometimes want to know what dependencies will be introduced into my program by linking against a static library (on Windows).  If I&#8217;m linking against a DLL, I can just run <a href="http://msdn2.microsoft.com/en-us/library/ms235265.aspx">depends</a>, which will tell me what other DLLs that DLL needs to load (and even which exports it&#8217;s pulling in), but in the case of a static library, I can&#8217;t find an analagous tool.</p>
<p>This came up for me recently at work: I&#8217;ve been asked to port a static library which I wrote some time ago to another platform.  To get a sense of what kind of dependencies this library will want to drag along, I wanted to get a list of all it&#8217;s unresolved externals.</p>
<p>This has turned out to be harder than you might think.  A few minutes with Google didn&#8217;t really turn up anything, so I took a look at <a href="http://msdn2.microsoft.com/en-us/library/c1h23y6c(VS.71).aspx">dumpbin</a>.  <code>dumpbin /symbols</code> prints out a nice report of the library&#8217;s symbol table, complete with unresolved symbols clearly marked as <code>UNDEF</code>.  Great, I thought: I&#8217;ll  just type:</p>
<pre>
dumpbin /symbols foo.lib | grep -e '^[0-9a-fA-F]\+ [0-9a-fA-F]\{8\} UNDEF'
</pre>
<p>(this would be in a <a href="http://www.cygwin.com">Cygwin</a> shell, obviously) and have my list (which I&#8217;d pretty up later).</p>
<p>Not so fast. Persusing the list, I started seeing symbols listed as undefined which I <em>knew</em> to be defined in this library&#8230; hmmmm.  A few more minutes spent persuing the original <a href="http://msdn2.microsoft.com/en-us/library/c1h23y6c(VS.71).aspx">dumpbin</a> output showed that they <em>were</em>, in fact, defined in this library!  The symbols would show up once as undefined, and a second time as defined.</p>
<p>I can only guess that dumpbin is just concatenating the output I would get if I ran it against each .obj separately.  That is, if symbol <code>_XYZ</code> is defined in module a.obj, and referenced in mobule<br />
b.obj, we get two records (one for each module):</p>
<pre>67F 00000000 SECT183 notype ()    External    | _XYX
....
107 00000000 UNDEF  notype ()    External     | _XYZ</pre>
<p>Damnit. Ok, so I&#8217;m going to have to write a little code, here.  What I want to do is walk <a href="http://msdn2.microsoft.com/en-us/library/c1h23y6c(VS.71).aspx">dumpbin</a>&#8217;s output, parsing each record containing a symbol definition, that symbol&#8217;s undecorated name (if present), and whether or not it&#8217;s defined.  The trick is that it may show up more than once.</p>
<p>IOW, a &#8220;mark &amp; sweep&#8221; approach: as I parse each record, I need to check to see if it&#8217;s already been recorded and only mark it as undefined <em>if</em> the current record says it is <em>and</em> if it hasn&#8217;t already been marked down as present.  Else, I want to mark it as defined.  Once I&#8217;m done, I&#8217;ll sweep the datastructure of any records corresponding to symbols defined inside<br />
my library.</p>
<p>I fired up a <a href="http://www.python.org">Python</a> shell, even tho this kind of little reporting problem &#8220;feels&#8221; like Perl to me, so that I could horse around with these ideas interactively:</p>
<pre>&gt;&gt;&gt; import os, re
&gt;&gt;&gt; f = os.popen("dumpbin /symbols foo.lib", "r")
&gt;&gt;&gt; x = f.readline()
&gt;&gt;&gt; print x</pre>
<p>Now, the records we want generally look like this:</p>
<pre>023 00000000 SECT9  notype       External     | ?FRAG_ACK@WscMsg@ani8021x@@2EB (public: static unsigned char const ani8021x::WscMsg::FRAG_ACK)</pre>
<p>but we get lots of stuff we dont&#8217; care about like:</p>
<pre>Section length    1, #relocs    0, #linenums    0, checksum E963A535, selection    2 (pick any)</pre>
<p>and some stuff that&#8217;s not un-decorated:</p>
<pre>357 00000000 UNDEF  notype ()    External     | _memset</pre>
<p>I guessed at a regexp,</p>
<pre>^[0-9a-f]{3} [0-9a-f]{8} (SECT[0-9a-f]+|UNDEF) [^|]+\| ([^(]+) ?(?:\((.*)\))?</pre>
<p>but how to tell?  I tried it a few times in the interpreter:</p>
<pre>&gt;&gt;&gt; for i in range(1, 25):
...     x = f.readline()
...     m = re.search("^[0-9a-f]{3} [0-9a-f]{8} (SECT[0-9a-f]+|UNDEF) [^|]+\| ([^(]+) ?(?:\((.*)\))?", x, re.I)
...     print x
...     if m: print m.groups()
...     else: print None
...</pre>
<p>Cool.  This let me watch my regex in action over enough lines to get some confidence in my approach: it was discarding the stuff about which I didn&#8217;t care, and parsing what I wanted.</p>
<p>So, let&#8217;s do this:</p>
<pre>&gt;&gt;&gt; program = re.compile("^[0-9a-f]{3} [0-9a-f]{8} (SECT[0-9a-f]+|UNDEF) [^|]+\| ([^(]+) ?(?:\((.*)\))?", re.I)
&gt;&gt;&gt; print program
&lt;_sre.SRE_Pattern object at 0x00A507B8&gt;</pre>
<p>With the regex now compiled, we&#8217;re ready to rock:</p>
<pre>&gt;&gt;&gt; f.close()
&gt;&gt;&gt; data={}
&gt;&gt;&gt; f = os.popen("dumpbin /symbols foo.lib", "r")
&gt;&gt;&gt; x = f.readline()
&gt;&gt;&gt; while x:
...     m = program.search(x)
...     if m:
...         sym = m.group(2).strip()
...         if sym[0] != '.' and sym[0] != '$':
...             undefd = m.group(1) == "UNDEF"
...             und = m.group(3)
...             if not data.has_key(sym):
...                 data[sym] = [ undefd, und ]
...             elif not undefd:
...                 data[sym][0] = False
...     x = f.readline()
...</pre>
<p>So at this point, we&#8217;ve traversed all the symbols in our library, and marked those that are undefined.  Cleanup,</p>
<pre>&gt;&gt;&gt; f.close()</pre>
<p>&amp; sweep:</p>
<pre>&gt;&gt;&gt; for k in data.keys():
...     if data[k][0]:
...         undefined_symbols.append([k, data[k][1]])
...</pre>
<p>That&#8217;s it&#8211; <code>undefined_symbols</code> is now a list of lists, each sub-list containing two elements: the symbol name and the undecorated version (which may be <code>None</code>).</p>
<p>We can just as quickly pretty-print our results to file:</p>
<pre>&gt;&gt;&gt; f = file("C:\\tmp\\report.txt", "w")
&gt;&gt;&gt; for x in undefined_symbols:
...     und = ""
...     if x[1]: und = x[1]
...     f.write("%s | %s\n" % (x[0], und))
...
&gt;&gt;&gt; f.close()</pre>
<p>Of course, I <em>still</em> have to figure out how to enumerate template instantiations made in my library, but whose <em>definitions</em> were pulled in from external code&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=13</wfw:commentRss>
		</item>
		<item>
		<title>How does WTL connect HWNDs to C++ objects?</title>
		<link>http://www.unwoundstack.com/?p=12</link>
		<comments>http://www.unwoundstack.com/?p=12#comments</comments>
		<pubDate>Sun, 20 Apr 2008 01:44:08 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[coding]]></category>

		<category><![CDATA[atl]]></category>

		<category><![CDATA[c++]]></category>

		<category><![CDATA[subclassing]]></category>

		<category><![CDATA[windows]]></category>

		<category><![CDATA[wtl]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=12</guid>
		<description><![CDATA[This is another recycled post from the old blog.
In any windowing library, the question is always how to connect instances of whatever C++ class is representing a Window, and the HWNDs that the OS actually uses. I was curious as to how ATL &#38; WTL do that, so I did a little digging.
I started with [...]]]></description>
			<content:encoded><![CDATA[<p>This is another recycled post from the old blog.</p>
<p>In any windowing library, the question is always how to connect instances of whatever C++ class is representing a Window, and the HWNDs that the OS actually uses. I was curious as to how ATL &amp; WTL do that, so I did a little digging.</p>
<p>I started with ATL&#8217;s support for Property Sheets. Looking at the implementation of CPropertySheetImpl, we see:</p>
<pre>ATL::_AtlWinModule.AddCreateWndData(&amp;pT-&gt;m_thunk.cd, pT); // 1
INT_PTR nRet = ::PropertySheet(&amp;m_psh);                   // 2</pre>
<p>Line number 1 obviously looks interesting. So, what the heck is AddCreateWndData? Like so many ATL methods, it ends up in a global:</p>
<pre>AtlWinModuleAddCreateWndData(_ATL_WIN_MODULE* pWinModule,
                             _AtlCreateWndData* pData,
                             void* pObject)
{
    pData-&gt;m_pThis = pObject;
    pData-&gt;m_dwThreadID = ::GetCurrentThreadId();
    pData-&gt;m_pNext = pWinModule-&gt;m_pCreateWndList;
    pWinModule-&gt;m_pCreateWndList = pData;
...</pre>
<p>Here&#8217;s what&#8217;s happening; ATL maintains a per-thread, singly linked list of <code>AtlCreateWndData</code> structures.  These structures record the current thread and the current class instance.  Here&#8217;s the structure definition:</p>
<p><code><br />
struct _AtlCreateWndData<br />
{<br />
void* m_pThis;<br />
DWORD m_dwThreadID;<br />
_AtlCreateWndData* m_pNext;<br />
};<br />
</code></p>
<p>for every kind of window (plain-jane Windows, Dialogs, Property Pages, &amp;c), ATL finds some kind of &#8220;hook&#8221; that will be called <em>after</em> the window is actually created, but <em>before</em> it begins receiveing messages. There, (when it has an <code>HWND</code> laying around) it pops the head of the current list to find the C++ object corresponding to the window being created.</p>
<p>The current head of the list (for the current thread) is retrieved by calling <code>AtlWinModuleExtractCreateWndData</code>.  This functon  is called by <code>CAtlWinModule::ExtractCreateWndData</code>, which is in turn called by:</p>
<ul>
<li><code>CWindowImplBaseT&lt; TBase, TWinTraits &gt;::StartWindowProc</code></li>
<li><code>CDialogImplBaseT&lt; TBase &gt;::StartDialogProc</code></li>
<li><code>CCommonDialogImplBase::HookProc</code></li>
<li><code>CColorDialogImpl::HookProc</code></li>
<li><code>CPropertySheetImpl::PropSheetCallback</code></li>
</ul>
<p>Let&#8217;s dig into <code>StartWindowProc</code>. This is the <code>WNDPROC</code> that gets registered with the Window class <code>WNDCLASS</code>. What does it do?</p>
<p>It pops the head off the current thread&#8217;s list of <code>AtlCreateWndData</code>, sets <code>pThis</code> to the corresponding member, and then:</p>
<p><code><br />
pThis-&gt;m_thunk.Init(pThis-&gt;GetWindowProc(), pThis);<br />
</code></p>
<p>Alright, so what&#8217;s <code>m_thunk</code>?  This is a member variable of type <code>CWndProcThunk</code>.  The actual implementation is hidden behind a typedef or two, but the upshot is this: it&#8217;s a little structure whose member variables actually make up executable code!  Specifically:</p>
<p><code><br />
mov dword ptr [esp+0x4], pThis<br />
jmp relwndproc<br />
</code></p>
<p>The <code>Init</code> member sets up the <code>pThis</code> &amp; the address of the actual <code>WNDPROC</code> we&#8217;re going to call.  Here&#8217;s the trick: if this code is executed at the beginning of a function in which the first parameter on the stack is an HWND (like, say, a window procedure), this code will write the address of the C++ class instance representing the window that&#8217;s just been created over top of the <code>HWND</code> and then jump to the beginning of the window procedure returned from the <code>GetWindowProc</code> function.</p>
<p>Remember that while we get back to <code>StartWindowProc</code>.  <code>StartWindowProc</code> next gets the address at which this new code resides by calling:</p>
<pre>WNDPROC pProc = pThis-&gt;m_thunk.GetWNDPROC();
WNDPROC pOldProc = (WNDPROC)::SetWindowLongPtr(hWnd, GWLP_WNDPROC,
                                               (LONG_PTR)pProc);</pre>
<p>What we&#8217;ve just done is substituted our little thunk for the new window&#8217;s window procedure.  With the thunk in place, we can now do this first off in the real <code>WNDPROC</code>:</p>
<pre>CWindowImplBaseT&lt; TBase, TWinTraits &gt;* pThis = (CWindowImplBaseT&lt; TBase, TWinTraits &gt;*)hWnd;</pre>
<p>which is actually kind of slick (although a maintenance headache, I&#8217;d guess&#8230;).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=12</wfw:commentRss>
		</item>
		<item>
		<title>ssh, Cygwin, and Samba</title>
		<link>http://www.unwoundstack.com/?p=11</link>
		<comments>http://www.unwoundstack.com/?p=11#comments</comments>
		<pubDate>Sun, 20 Apr 2008 00:04:10 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[sysadmin]]></category>

		<category><![CDATA[cygwin]]></category>

		<category><![CDATA[samba]]></category>

		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=11</guid>
		<description><![CDATA[This was a post from my old blog, which I&#8217;m just now getting around to posting here.
I run Cygwin on my Windows boxes. I use OpenSSH a lot. I&#8217;d wanted to run OpenSSH daemons on my Windows boxes for a long time, but I&#8217;ve found it, ahem, challenging. One weekend, I finally decided to dig [...]]]></description>
			<content:encoded><![CDATA[<p>This was a post from my old blog, which I&#8217;m just now getting around to posting here.</p>
<p>I run <a href="http://www.cygwin.com">Cygwin</a> on my Windows boxes. I use <a href="http://www.openssh.org">OpenSSH</a> a <em>lot</em>. I&#8217;d wanted to run <a href="http://www.openssh.org">OpenSSH</a> daemons on my Windows boxes for a long time, but I&#8217;ve found it, ahem, challenging. One weekend, I finally decided to dig into the problem &amp; get get to the bottom of the matter. Since others have had problems (for instance, see <a href="http://pigtail.net/LRP/printsrv/cygwin-sshd.html">here</a>, <a href="http://ncyoung.com/entry/389">here</a>, &amp; <a href="http://erdelynet.com/2004/08/30/cygwin/see-cygwincom-for-cygwin-ssh-docs/">here</a>), I thought I&#8217;d post what I learned &amp; what I did.</p>
<p>Initially, I installed according to the stock instructions. That is, I installed Cygwin as <a href="http://www.cygwin.com/setup.exe">usual</a>, then ran <code>ssh-host-config</code>. However, I could never log in and have access to my home directory. I poked at the problem a bit with <a href="http://www.microsoft.com/technet/sysinternals/FileAndDisk/Filemon.mspx">FileMon</a>, and saw that the sshd process, running as <code>SYSTEM</code>, was trying to access my home directory, which is mounted via Samba from a Linux box &#8212; no go.</p>
<p>So, I dug in &amp; did a little reading. The first thing I came across was the notion of<br />
<a href="http://www.citi.umich.edu/u/provos/ssh/privsep.html">Privilege Separation</a>:</p>
<blockquote><p>Privilege separation, or privsep, is method in OpenSSH by which operations that require root privilege are performed by a separate privileged monitor process. Its purpose is to prevent privilege escalation by containing corruption to an unprivileged process&#8230;When privsep is enabled, during the pre-authentication phase sshd will chroot(2) to &#8220;/var/empty&#8221; and change its privileges to the &#8220;sshd&#8221; user and its primary group. sshd is a pseudo-account that should not be used by other daemons, and must be locked and should contain a &#8220;nologin&#8221; or invalid shell.</p></blockquote>
<p><img src="http://www.unwoundstack.com/images/misc/privs.jpg" alt="" /><br />
(diagram courtesy of <a href="http://www.citi.umich.edu/u/provos/ssh/privsep.html">Niels Provos</a>).</p>
<p>Now, I noticed that on Windows Server 2003, due to some privilege issues with the <code>SYSTEM</code> account, the install script would create a <em>separate</em> user under which the service would be run:</p>
<blockquote><p>&#8220;Since Cygwin release 1.3.3, applications that are members of the Administrators group and have the Create a token object, Replace a process level token and Increase Quota user rights can<br />
switch user context without giving a password by just calling the usual setuid, seteuid, setgid and setegid functions.</p></blockquote>
<blockquote><p>On NT and Windows 2000 the SYSTEM user has these privileges and can run services such as sshd. However, on Windows 2003 SYSTEM lacks the Create a token object right, so it is necessary to create a special user with all the necessary rights, as well as Logon as a service, to run such services. For security reasons this user should be denied the rights to logon interactively or over the<br />
network. All this is done by configuration scripts such as ssh-host-config.&#8221;</p></blockquote>
<p>If you look at <code>ssh-host-config</code>, you can see the logic for setting this up.</p>
<p>That was the trick&#8211; I just needed to run the service under an account that had sufficient privileges to access the Samba-mounted drives. I went to my Linux box &amp; added a new user, <code>nw</code> (for network). I didn&#8217;t give him a login shell, but I <em>did</em> make him part of my <code>users</code> group. I added him to my Samba database via:</p>
<pre>/usr/local/samba/bin/smbpasswd -a nw</pre>
<p>Then, I went to each Cygwin machine &amp; ran <code>mkgroup -d</code> to get the relevant <code>passwd</code> entry for the new user. Once that was done, I added the new user to the local administrators group on each machine, &amp; granted it the required privileges:</p>
<pre>editrights -a SeAssignPrimaryTokenPrivilege -u BYWATER\nw
editrights -a SeCreateTokenPrivilege -u BYWATER\nw
editrights -a SeTcbPrivilege -u BYWATER\nw
editrights -a SeDenyInteractiveLogonRight -u BYWATER\nw
editrights -a SeDenyNetworkLogonRight -u BYWATER\nw
editrights -a SeDenyRemoteInteractiveLogonRight -u BYWATER\nw
editrights -a SeIncreaseQuotaPrivilege -u BYWATER\nw
editrights -a SeServiceLogonRight -u BYWATER\nw</pre>
<p>Note that <code>Bywater</code> is my Windows domain name.</p>
<p>Ok&#8211; then, I ran <code>ssh-host-config</code> as usual, but declined to install as a Service. I did that myself like so:</p>
<pre>cygrunsrv -I sshd -d "Cygwin sshd" -p /usr/sbin/sshd -a -D -u BYWATER\nw -w "xxx" -e "CYGWIN=binmode winsymlinks ntsec" -y tcpip</pre>
<p>I started the service, and, naturally, it died. As a means of debugging it, I granted <code>Bywater\nw</code> logon privileges, and did the following:</p>
<pre>runas /user:BYWATER\nw cmd bash -i /usr/sbin/sshd -D</pre>
<p>Running in the <em>foreground</em>, the thing happily told me that it was having permissions problems with /var/empty, /var/log/sshd.log, and /etc/ssh_host*. Easily remedied:</p>
<pre>chown nw.Administrators /var/empty /var/log/sshd.log /etc/ssh_host*</pre>
<p>Presto! All was well. I could even forward X11 over an ssh session from my Linux box! <img src='http://www.unwoundstack.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=11</wfw:commentRss>
		</item>
		<item>
		<title>Sun &#038; fog on the way into work</title>
		<link>http://www.unwoundstack.com/?p=9</link>
		<comments>http://www.unwoundstack.com/?p=9#comments</comments>
		<pubDate>Tue, 15 Apr 2008 20:46:55 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Life]]></category>

		<category><![CDATA[commute]]></category>

		<category><![CDATA[photography]]></category>

		<category><![CDATA[scmountains]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=9</guid>
		<description><![CDATA[Monday was one of those days where the weather changed four or five times on the way into work. It was overcast at the house when I left, but I broke out of the clouds in the valley into sunshine partway up Highway 9. This is a shot looking back over the San Lorenzo Valley. [...]]]></description>
			<content:encoded><![CDATA[<div style="height:648px;"><img style="padding:4px;float:left;" title="SLV from 9 04.14.2008" src="http://www.unwoundstack.com/images/photos/nature/0414080741a_small.jpg" alt="" width="480" height="640" />Monday was one of those days where the weather changed four or five times on the way into work. It was overcast at the house when I left, but I broke out of the clouds in the valley into sunshine partway up Highway 9. This is a shot looking back over the San Lorenzo Valley. It&#8217;s not great, but you can see the cloud cover below me.</p>
</div>
<div style="height:648px;"><img style="padding:4px;float:right;" title="Trees on Skyline 04.14.2008" src="http://www.unwoundstack.com/images/photos/nature/0414080748b_small.jpg" alt="" width="480" height="640" />Heading north along Skyline, I drove <em>back</em> into fog. Here&#8217;s a shot at one of the trailheads. You can just see blue sky peeking through the trees, <em>below</em> the clouds&#8230;</p>
</div>
<div style="height:488px;">
<p>&#8230; until around Skyline OSP where the sun came out again:</p>
<p><img style="padding:4px;float:center;" title="Skyline Ridge 04.14.2008" src="http://www.unwoundstack.com/images/photos/nature/0414080753a_small.jpg" alt="" width="640" height="480" /></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=9</wfw:commentRss>
		</item>
		<item>
		<title>VisEmacs</title>
		<link>http://www.unwoundstack.com/?p=8</link>
		<comments>http://www.unwoundstack.com/?p=8#comments</comments>
		<pubDate>Sun, 13 Apr 2008 16:46:21 +0000</pubDate>
		<dc:creator>michael</dc:creator>
		
		<category><![CDATA[Projects]]></category>

		<category><![CDATA[Add-Ins]]></category>

		<category><![CDATA[Emacs]]></category>

		<category><![CDATA[hacking]]></category>

		<category><![CDATA[VisualStudio]]></category>

		<guid isPermaLink="false">http://www.unwoundstack.com/?p=8</guid>
		<description><![CDATA[I also uploaded VisEmacs 3.  VisEmacs is a Visual Studio Add-In that allows Emacs to be used as your text editor.
It was originally authored by Jeff Paquette, targeting DevStudio 6.0, and it saw a lot of use.  Time went on, and Visual Studio 2002, 2003, &#38; 2005 came along (and changed the Add-In [...]]]></description>
			<content:encoded><![CDATA[<p>I also uploaded <a href="http://www.unwoundstack.com/dl/tools/VisEmacs.3.0.2.exe">VisEmacs 3</a>.  <a href="http://sourceforge.net/projects/visemacs/">VisEmacs</a> is a Visual Studio Add-In that allows <a href="http://www.gnu.org/software/emacs/">Emacs</a> to be used as your text editor.</p>
<p>It was originally authored by <a href="http://blogs.snowmoonsoftware.com/">Jeff Paquette</a>, targeting DevStudio 6.0, and it saw a lot of use.  Time went on, and Visual Studio 2002, 2003, &amp; 2005 came along (and changed the Add-In model completely, meaning the Add-In was no longer compatible).  There would periodically be requests for ports on the VisEmacs <a href="http://groups.yahoo.com/group/visemacs/">mailing list</a>, but since Jeff was still using DevStudio 6, he demurred.</p>
<p>Instead, he would invite <em>us</em>, the users, to do so, and in 2006, I finally did.  Here&#8217;s the original announcement:</p>
<blockquote><p>Hi all,</p>
<p>Over the past few years folks on the list have asked about<br />
getting VisEmacs ported to Visual Studio 2002, 2003, and so<br />
forth.  Jeff invited us to dig in to the source code &amp; go for<br />
it.  Well, I finally did.</p>
<p>At first, I was just trying to get something up &amp; running for<br />
my own use.  Later on, I thought it might be useful to other<br />
folks, as well.</p>
<p>So, here goes:</p>
<p>1. This is a complete re-write.  The code needed to be<br />
heavily re-structured anyway, and I&#8217;m not a big MFC fan.  I<br />
developed it using Visual Studio 2005 &amp; it uses STLport 5,<br />
ATL 8, and WTL 7.5.</p>
<p>2. One DLL (&#8217;visemacs.dll&#8217;) will load in DevStudio 6, Visual<br />
Studio 2003, &amp; Visual Studio 2005.  These are the three IDEs<br />
I use &amp; have access to.  If someone wants to support another<br />
environment, let me know &amp; maybe we can collaborate on something.</p>
<p>3. I only implemented the functionality present in VisEmacs<br />
2.1.  I wasn&#8217;t even aware of the 2.9 release before I<br />
started!  Again, if folks find this useful, and want the new<br />
functionality, I could add it to the re-write.</p></blockquote>
<p>Afterwards, Jeff got in touch, and we agreed to host the new code on it&#8217;s project <a href="http://sourceforge.net/projects/visemacs/">page</a> at <a href="http://www.unwoundstack.com/wp-admin/&lt;a href=">SourceForge</a> (you can download it there, too).</p>
<p>All that said, I saw while perusing Jeff&#8217;s <a href="http://www.unwoundstack.com/dl/tools/VisEmacs.3.0.2.exe">blog</a> the other day that <em>he&#8217;s</em> working on a <a href="http://sourceforge.net/projects/visemacs/">VisEmacs</a> re-write which is compatible with the new Add-In model (i.e. <em>not</em> DevStudio 6), and has even made a preliminary version available <a href="http://blogs.snowmoonsoftware.com/news/visemacsnet-pretest/">here</a>. So, if you&#8217;re not interested in backward compatibility with DevStudio 6, you might want to try that one, too.</p>
<p>Finally, I&#8217;m working on adding Visual Studio 2008 compatibilty to <a href="http://sourceforge.net/projects/visemacs/">VisEmacs 3</a> and hope to have a new build ready in the next week or two.  In the meantime, you can grab the most recent version (3.0.2) <a href="http://www.unwoundstack.com/dl/tools/VisEmacs.3.0.2.exe">here</a> (debug build <a href="http://www.unwoundstack.com/dl/tools/VisEmacsD.3.0.2.exe">here</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.unwoundstack.com/?feed=rss2&amp;p=8</wfw:commentRss>
		</item>
	</channel>
</rss>
