Friday, 27 February 2015

Windows Impersonation and the Double Hop Issue

I've been aware of the Windows Double Hop Issue since a long while ago. I used to think of it as a limitation, but reading lately about it I've found out that it's indeed a security feature, but when trying to understand how it's implemented I've gone through some confusion that I think I've finally defeated.

The idea is that the double hop issue is related to impersonation. Usually, when a Thread in a Server Process is impersonating the user in the client machine, for security reasons, the impersonated thread won't be allowed to access remote resources. This is built upon something that I'd never heard about, the Impersonation Level of the Impersonation Token. Most times the kind of Impersonation Token being used has an Impersonation Impersonation Level, rather than a Delegation one. These types of Impersonation Levels are explained here

  • Delegation: The server process can impersonate the client's security context on remote systems.
  • Impersonation: The server process can impersonate the client's security context on its local system. The server cannot impersonate the client on remote systems.

This looks good when applied to the typical example of a user connection to an Asp.Net application using impersonation and then failing in the second hop when that page tries to connect to a Shared Folder or SqlServer with Windows Authentication.
But my problem with this comes from something that I've done on several times and works like a charm. Let's say I have a Windows Application running on my PC and need to connect to a remote SqlServer DataBase using Windows Authentication. The Windows user running the application has no permissions on that DataBase, so I will impersonate the current thread to an account with access to the DB and open the SqlConnection from this impersonated thread. It works fine, and I do it using the typical code that you can find for example in this example.

Based on the previous explanation on Delegation/Impersonation, the explanation for this to work would be that that this call: WindowsIdentity.Impersonate would be returning an Impersonation token with ImpersonationLevel set to Delegation rather than to Impersonation, but I've checked it (this value: WindowsIdentity.GetCurrent().ImpersonationLevel.ToString()), and the Impersonation Level is Impersonation.

So, how is it that is it that I'm being allowed to access to a remote resource like a remote SqlServer DB or a Shared Folder? This seems to contradict the definition given above. Well, the thing is that such definition was misleading to say the least. You'll find a much better explanation here

.
  • Impersonate: The service can impersonate the client. If the service is on the same computer as the client process, it can access network resources as the client. If the service is on a remote computer, it can impersonate the client only when accessing resources on the service’s computer.
  • Delegate: The service can impersonate the client not only when it accesses resources on the service’s computer but also when it accesses resources on other computers. This level is supported only in Windows 2000 and later versions of the Windows operating system.

Well, putting aside that client/service nomenclature that I think helps quite little here, this makes more sense, but there's still something more to explain. In the end we'll have a thread running with an impersonation token trying to access resources, so how does that "If the service is on the same computer as the client process" really apply to the information contained in that token?

Well, in the same article we'll find a table with the Physical Structure of an Access Token. We can see there that there is a field called Origin.

Origin: Introduced with Windows Server 2003. If the token resulted from a logon using explicit credentials, then the token will contain the ID of the logon session that created it. If the token resulted from network authentication, then this value will be zero.

So in the end my understanding is that the access to a resource from an impersonated thread is granted depending on the Impersonation Level and the Origin.

Sunday, 15 February 2015

Chapelle des Carmelites

It's really great when you think you know one place up and down and suddenly you come across a nice spot you had not even heard about.

It happened to me a couple of weeks ago here in Toulouse when in one of my long weekend strolls (quite unrelated to me, but did you know that some of the greatest thinkers in history absolutely loved to indulge themselves in very long walks?) I stumpled upon the Carmelites Chapel. It's in one street (rue de Périgord) connecting other 2 (Taur and Réemusat) that I've walked over in countless occasions, but I had never taken this connecting route.

Though the outside is simple and not particularly appealing, I've learnt that as with people it's sometimes good to take the time to look in the inside, and this time it was well worth. I found there an impressively decorated space. Beautiful paintings on the walls, with those in the space between windows depicting the virtues for a female (in the Christian tradition) and above you, some of the most beautiful painted ceilings that I've ever seen. I think the place is quite underrated, as apart from the French Wikipedia article, you'll find very little information about it (for example it does not show up in the main free tourist guides handed out by the Toulouse city council). Inside the building though, you'll find a rather useful paper sheet with information about the place.



In this same street you'll find a nice public library, Bibliothèque d’étude et du patrimoine that is well worth a visit. I quite like the female sculptures outside representing literature, and the main door, and inside you'll find a nice stained glass piece, a beautiful dome and a "place of knowledge" feeling.

Last week I bought this nice book with panoramique pictures of Toulouse, you can find them here.

Sunday, 8 February 2015

Guts Pie Earshot

Guts Pie Earshot has been around for more than 20 years now, and I'm pretty sure that some of my friends have been listening to them since their beginnings, but at that time I was only into fast, screaming and aggressive music, and I never paid them any attention. Last May I was lucky to see them live with one of their other projects, Outsourced Underground, where the excellent German rapper Lena Stoehrfaktor(along with Tapete) sing over their melodies. It's a beautiful and necessary project of Political Rap/Something (they define it as RapCore), and their demo is one of the sounds that has been more present in my players for the last months. However, 2014 was a really busy year for me (so much work combined with trying to leverage the many possibilities that living in a city like Toulouse gives you on a personal level), so never found the time to dig into Guts Pie Earshot.

This week I've finally done it and well, their sound is fascinating, mesmerizing and unique. Their music has evolved from album to album and changes from song to song, but some of them are so melancholic, powerful and beautiful that is difficult to find words to make them justice. Basically we could say that right now these are 2 punks playing celo and drums, but there's so much more to such poor description... Different distortions are applied to the cello, drums move from conventional to electronic ones, and in the past they had additional members playing bass, keyboards/samples and singing (with such a beautiful voice). Don't worry, the lack of those members has not done the ensuing releases less powerful or authentic, they keep the same masterful signature that blends sadness and joy, light and darkness, beauty and beauty.

When the band turned 20 they decided to make available all their releases for download. You can also find 2 of them in bandcamp (which I pretty like it, I feel like paying for their work, more than a payment is like a "many thanks and now let me pay you a coffee or a beer for these moments").

Apart from Outsourced Underground, they've got some some other interesting projects like Dubvasion or Scheng, and no doubt this song by Scheng honoring one deceased Kurdish YPG Freedom Fighter really shocked me.

I'll end up this post with a list of some of my favorite songs:

and I'll copy here the emotive text that they wrote on the occasion of their 20 years:


since 2013 we exist now for 20 years…

What does it mean to us?


1000 concerts...
10 releases….

it is a long long time with many
ups and downs,
we made so many experiences,
we met so many people
we saw so many countries

we met so many
idealistic people becoming boring,
boring people becoming idealistic

we saw so many cool projects built,
we saw so many projects destroyed.

we are really really grateful
to all of you
who keep us alive …

Saturday, 31 January 2015

A Mix of Thoughts and News

Time for a mix of thoughts and news.

  • I have to/need to learn French. There are many reasons for it, but no doubt that this paragraph in this Wikitravel article joins that list of reasons in one of the first positions :-D

    It has long been the language of international diplomacy and communication, and although largely supplanted by English since World War II, it remains de rigueur (of obligatory requirement) for educated people in many societies around the world to have some level of basic French ability. It is also an official language of the UN and the EU.

  • This article about the Lava Layer Anti-Pattern is one of the best things published in InfoQ in a long while. It immediately came to my mind a different scenario for this Anti-Pattern. A Web Application started around 15 years ago with classic ASP. Around 2003/2004 it was extended with some ASP.NET WebForms. In 2007 some Ajax panels where added to these WebForms. In 2009 some clever guy tried to do some real Ajax development by hacking these WebForms to return clean html pages and pieces of html and JSON requested via jQuery Ajax. Then in 2011 some ASP.NET MVC came to the rescue, and then in 2013 some Web API magic was added. All of it living together in a huge IIS Site. Seems unrealistic? Well, think that still 3 or 4 years ago the EasyJet site contained some classic ASP pages...
  • Talking about Asp.Net, much has been said in the last months about .NET going partially OpenSouce, about .NET Core and so on... the most interesting of all this (bearing in mind that the multiplatform thing is nothing new, as Mono has been around since the early beginnings) is ASP.NET 5 (aka ASP vnext). First and foremost we get a unified programming model for MVC and Web API (for example no more similar but different classes in charge of routing for each subsystem), as a consequence, you'll be able to self-host MVC! As you'll be able to run it the Core CLR, you can deploy your complete self-hosted application in just a few MBs, without depending on any installed component, and you'll be able to do it on Windows or Linux (and yes, Mac, but I don't give a shit about rotten apples). On the other side, as OWIN continues to gain momentum, hosting on a separate web Server will be much easier.
  • Well, this is quite old news, but I hadn't found it until recently. Gimp has since v2.8 a Single-Window Mode! This is for me the more needed feature in this nice software, those fucking multiple windows floating around have been a pain for many users (me included of course) and kept many of them away, so this is definitely a great step forward.

Monday, 26 January 2015

Place de la Concorde

Probably the title of this post will make you think of Paris, but not, I'm talking about Toulouse. After more than 1 year living in this gorgeous city I have a series of places that I really love but that probably are not in most tourist guides. La Place de la Concorde is one of them.

I didn't come across it until a few months ago, and when I did I was delighted by the amazing piece of Art Nouveau Sculpture located there. It's quite unique in Toulouse and I'd never heard about it, but it's a rather complete example of Art Nouveau sensitivity: a delicate representation of femininity combined with flowers and animals.

I can not add anything to what you can already read in that French Wikipedia article, so I'll just post some pictures of my own. By the way, the quartier were it's located, "Les Chalets" has some very beautiful houses (for example the one hosting the Instituto Cervantes), the kind that make you wish you lived there

Sunday, 25 January 2015

A War to be Won

Well, I want to write a long post about the tragedy of January 7, but I've not managed to take the time for it, so for the moment I'll just mention here some interesting material concerning the war against madness that our society is waging on, this war between civilization and Wahhabism/Islamofascism or whatever you want to call that scum.

Continuing with my obsession with the fight of the Kurdish Socialists against ISIS, this short Documentary about Syria's Stalingrad depressed me, enraged me and inspired me in equal parts.

This documentary about the threat of ISIS to Lebanon is scary and depressive. That fucking scum seems willing to destroy any place in the Middle East where different religions have managed to coexist for centuries. Why don't they invade Quatar, Saudi Arabia or any other of those Medieval Kingdoms? Ah, yes, because that scum has been funding them with their petrodollars for years... The position of Hezbollah is quite interesting, they don't want to act against ISIS in areas populated mainly by Sunnis in order to prevent those actions from igniting internal tensions. Such a demonized "terrorist group" is now the main defender of Christians and Coexistence in the Region...

While in Montpellier this weekend I came across this nice poster:

Finally this ducumentary about al-Qaeda in the Islamic Maghreb is a real must (unfortunately it's in Spanish) for anyone interested in the history of madness. It tells the story of Islamofascism in Algeria, that I think is a topic that is quite unknown. In my case it was not so unknown, I already knew about the atrocities conducted by the Islamic Salvation Front and related groups, and along with Jomeini's dictatorship they helped to make me develop a deep fear and hate for fundamentalist Islam many years ago. Well before September 11 these beasts were planning to crash a plane against the Eiffel Tower and were committing attacks in French soil, and well before ISIS they were massacring villages, slaughtering pregnant women and kidnapping young ones.

Sunday, 11 January 2015

AsQueryable

The discussions as to whether Repositories should return IEnumerable or IQueryable has been hot and present for a long while. There are pros and cons, and I don't think I can add anything particularly useful on what path to follow. However, if you decide to go the IQueryable path, I've found something of some interest related to it.

One problem with returning IQueryable could seem to be, "and what do I do with my Mocks?" The typical case is having some mock repositories returning List<Entity>, so what can you do to comply with that contract that expects IQueryable<Entity>? Well, the BCL itself provides all you need, so no effort required, all you have to do is invoke AsQueryable in your collections and your normal IEnumerable will be turned into an IQueryable!

Seems interesting to take a fast look at how this thing works, but first let's do quick recap of how the different Linq flavours work.

  • Linq to Objects is implemented as just a set of extension methods in the System.Linq.Enumerable class.
  • As discussed in this previous post, the Extension Methods for Parallel Linq are located in the System.Linq.ParallelEnumerable class.
  • The Extension Methods for IQueryable are located in the System.Linq.Queryable class (that along with the other classes is located in the System.Core assembly)
  • As explained in this old post (time flies...) an IQueryable Linq Provider is based on implementing Sytem.Linq.IQueryProviderT> and having in your Sytem.Linq.IQueryable classes a Provider property pointing to it and an Expression property referencing to an Expression Tree that represents the Query (this means that when you chain Linq calls on an IQueryable, this Expression property will be the composition of those queries).
  • The IQueryable<T> interface implements IEnumerable<T>. If you have a class implementing IQueryable but want to use the Enumerable methods on it you can just call the Enumerable.AsEnumerable extension method (or just do a casting, that is just what that method does, as we know Extension Methods resolution is purely done at compile time). When you do this with your DataBase related IQueryable collection you'll be losing all the Sql construction magic that you LinqProvider probably implements under the covers, and you'll end up with a "select all" query and the filtering will be done in that huge in memory collection. Sometimes you need this cause you have a complex query and can't write a Expression Tree for it or your Provider can't convert it into SQL statements.

So, what happens when you invoke AsQueryable (that is an Extension Method in the System.Linq.Queryable class expecting an IEnumerable parameter)?

Well, it can not be too complex. The IQueryProvider provided by the new IQueryable object will receive an expression Tree representing the query, so it occurs to me that if it keeps a reference to the IEnumerable object, it can compile the Expression Tree into a normal delegate and use it with any of the System.Linq.Enumerable extension methods.

Let's go a bit through the Framework code (out of habit I still feel more comfortable going through the decompiled classes shown by ILSpy that using the Reference Source code...) to see how it is implemented. AsQueryable returns a System.Linq.EnumerableQuery object. Same as some objects implement both IEnumerable and IEnumerator (they know how to iterate themselves), this class implements both IQueryable and IQueryProvider (it knows how to query itself). As MSDN says:

Represents an IEnumerable collection as an IQueryable data source.

This EnumerableQuery object contains a reference (the Enumerable property) to the original IEnumerable object. When we invoke "Linq queries" on it (the System.Linq.Queryable extension methods), they'll retrieve the provider (that in this case we've seen that is the EnumerableQuery object itself) and will invoke the CreateQuery method on it (source.Provider.CreateQuery), that in turn returns another EnumerableQuery object. As we do so, the Expression tree for the current Query is composed to the Expression Tree representing previous queries, this way (extract from System.Linq.Enumerable.Where):
return source.Provider.CreateQuery(Expression.Call(null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[]
	{
		typeof(TSource)
	}), new Expression[]
	{
		source.Expression,
		Expression.Quote(predicate)
	}));

Finally, when the data represented by this query is really needed, the GetEnumerator method in EnumerableQuery will get invoked:

private IEnumerator GetEnumerator()
{
	if (this.enumerable == null)
	{
		EnumerableRewriter enumerableRewriter = new EnumerableRewriter();
		Expression body = enumerableRewriter.Visit(this.expression);
		Expression>> expression = Expression.Lambda>>(body, null);
		this.enumerable = expression.Compile()();
	}
	return this.enumerable.GetEnumerator();
}

which will finally compile and execute that Expression that has been composed all over the chain of queries.