user105: Is castling possible if any of the squares involved in the castling are under attack, or is this a problem only if the squares moved through by the king are under attack and not the ones moved through by the rook?

user18832: There is more on the condition to consider
The King and Rook have to be on the same rank, otherwise Pam-Krabbe castling would be possible.
Pam-Krabbe castling is basically a vertical castling, where the king would castle with a promoted pawn. This is not allowed anymore. 

user2001: You can castle when all of these conditions hold true:

The King and the Rook did not make any moves so far.
The King is not in check.
The King will not pass a threatened square during castling.
The King will not land on a threatened square.

Condition 3 might need clarification. For example, you have the white pieces and you want to castle kingside. Then, you are using your Ke1 and Rh1 for castling. Let's say that conditions 1 and 2 are fulfilled. Next, the g1 square should not be threatened in order to fulfill condition 4. Finally, the King will pass the square f1 on its way to g1. Thus, the square f1 should not be threatened for condition 3 to hold true!

user17: The rules are that the king can't castle into check, through check, or when in check. This applies to the king's square, plus the two squares to the right or left.
Castling is permitted when the rook is under attack (on the rook's square). On the queen side, that would also include the knight's square. But not on the kingside, because the king would be castling into check. He also can't castle if the bishop's or queen's square is under attack. 

user396: One nice example of this is in this game, where a player named Feuer takes advantage of the ability to castle queenside while b1 is attacked to play a beautiful combination.
[FEN ""]
[Event "Belgian Championship"]
[Site "Liege BEL"]
[Date "1934"]
[EventDate "?"]
[Round "?"]
[Result "1-0"]
[White "Otto Feuer"]
[Black "Alberic O'Kelly de Galway"]
[ECO "C73"]
[WhiteElo "?"]
[BlackElo "?"]
[PlyCount "25"]

1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 d6 5.Bxc6+ bxc6 6.d4 f6 7.Nc3
Rb8 8.Qd3 Ne7 9.Be3 Rxb2 10.dxe5 fxe5 11.Nxe5 dxe5 12.Qxd8+
Kxd8 13.O-O-O+ 1-0


user29: Yes, if the rook is threatened, you may still castle. The threatened squares rule only applies to squares where the king passes (starting and final position included).
For example, in the case of white castling queenside, for instance, a threat to a1 or b1 does not prevent the castle from taking place.

user99: Yes, you can, as long as the king doesn't pass through or end up on an attacked square.
From FIDE Laws of Chess:

3.8.2.1   The right to castle has been lost:
           
3.8.2.1.1   if the king has already moved, or
           
3.8.2.1.2   with a rook that has already moved.
3.8.2.2   Castling is prevented temporarily:
           
3.8.2.2.1   if the square on which the king stands, or the square which it must cross, or the square which it is to occupy, is attacked by one
or more of the opponent's pieces, or
           
3.8.2.2.2   if there is any piece between the king and the rook with which castling is to be effected.


105: <p>Is castling possible if any of the squares involved in the castling are under attack, or is this a problem only if the squares moved through by the king are under attack and not the ones moved through by the rook?</p>
 18832: <p>There is more on the condition to consider
The King and Rook have to be on the same rank, otherwise Pam-Krabbe castling would be possible.
Pam-Krabbe castling is basically a vertical castling, where the king would castle with a promoted pawn. This is not allowed anymore. </p>
 2001: <p>You can castle when <strong>all</strong> of these conditions hold true:</p>

<ol>
<li><p>The King and the Rook did not make any moves so far.</p></li>
<li><p>The King is not in check.</p></li>
<li><p>The King will not pass a threatened square during castling.</p></li>
<li><p>The King will not land on a threatened square.</p></li>
</ol>

<p>Condition 3 might need clarification. For example, you have the white pieces and you want to castle kingside. Then, you are using your Ke1 and Rh1 for castling. Let's say that conditions 1 and 2 are fulfilled. Next, the g1 square should not be threatened in order to fulfill condition 4. Finally, the King will pass the square f1 on its way to g1. Thus, the square f1 should not be threatened for condition 3 to hold true!</p>
 17: <p>The rules are that the king can't castle <em>into</em> check, <em>through</em> check, or when <em>in</em> check. This applies to the king's square, plus the two squares to the right or left.</p>

<p>Castling is permitted when the <em>rook</em> is under attack (on the rook's square). On the queen side, that would also include the knight's square. But not on the kingside, because the king would be castling <em>into</em> check. He also can't castle if the bishop's or queen's square is under attack. </p>
 396: <p>One nice example of this is in <a href="//www.chessgames.com/perl/chessgame?gid=1104131" rel="noreferrer">this game</a>, where a player named Feuer takes advantage of the ability to castle queenside while b1 is attacked to play a beautiful combination.</p>

<pre><code>[FEN ""]
[Event "Belgian Championship"]
[Site "Liege BEL"]
[Date "1934"]
[EventDate "?"]
[Round "?"]
[Result "1-0"]
[White "Otto Feuer"]
[Black "Alberic O'Kelly de Galway"]
[ECO "C73"]
[WhiteElo "?"]
[BlackElo "?"]
[PlyCount "25"]

1.e4 e5 2.Nf3 Nc6 3.Bb5 a6 4.Ba4 d6 5.Bxc6+ bxc6 6.d4 f6 7.Nc3
Rb8 8.Qd3 Ne7 9.Be3 Rxb2 10.dxe5 fxe5 11.Nxe5 dxe5 12.Qxd8+
Kxd8 13.O-O-O+ 1-0
</code></pre>
 29: <p>Yes, if the rook is threatened, you may still castle. The threatened squares rule only applies to squares where the king passes (starting and final position included).</p>

<p>For example, in the case of white castling queenside, for instance, a threat to <code>a1</code> or <code>b1</code> does not prevent the castle from taking place.</p>
 99: <p>Yes, you can, as long as the king doesn't pass through or end up on an attacked square.</p>
<p>From <a href="//handbook.fide.com/chapter/E012018" rel="nofollow noreferrer">FIDE Laws of Chess</a>:</p>
<blockquote>
<p><strong>3.8.2.1</strong>   The right to castle has been lost:<br />
           
<em>3.8.2.1.1</em>   if the king has already moved, or<br />
           
<em>3.8.2.1.2</em>   with a rook that has already moved.</p>
<p><strong>3.8.2.2</strong>   Castling is prevented temporarily:<br />
           
<em>3.8.2.2.1</em>   if the square on which the king stands, or the square which it must cross, or the square which it is to occupy, is attacked by one
or more of the opponent's pieces, or<br />
           
<em>3.8.2.2.2</em>   if there is any piece between the king and the rook with which castling is to be effected.</p>
</blockquote>
