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?
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
b1 does not prevent the castle from taking place.
Yes, you can, as long as the king doesn't pass through or end up on an attacked square.
From FIDE Laws of Chess:
- The right to castle has been lost:
- if the king has already moved, or
- with a rook that has already moved.
- Castling is prevented temporarily:
- 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
- if there is any piece between the king and the rook with which castling is to be effected.
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
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.
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!
Here is a problem I've made that demonstrates how the rook can move through squares that are attacked. The squares a1 and b1 are maximally attcked. If it were on a board, it would defintely frustate a few people, which is the exact point of this little excercise!
[Title "Mate In Two Moves"] [FEN "8/8/8/8/pppbbp1r/nr2pppB/q2n2Pp/R3K2k w Q - 0 1"]