“For there is no righteous person in the land who does [only] good and does not sin” (Kohelet 7:20). Sinning is part and parcel of being human. Sinning allows us to grow as people as the greater we fall the greater we can rise. Our Sages teach that “great is teshuva for sins can be turned to mitzvoth” (Yoma 86b). By utilizing the lessons learned from our mistakes, those same mistakes become the first steps to doing what is right. Yet as important as sin can be we may not sin intentionally so that we have an opportunity for growth. We must try to avoid sin and when we do not succeed use that as a growth opportunity.
However there are times when we must and should sin, “Rav Nachman the son of Yitzchak said: greater is a sin done lishma, for proper motives, than a mitzva done not with proper motives” (Nazir 23b). The Gemara derives this rather startling principle from the actions of Yael, who according to the Gemara slept with Sisera seven times in one evening - something that under normal circumstances might be a capital offence - weakening him so that she would be able to kill him (see Shoftim chapters 4-5).
The Gemara brings a proof text -“Blessed above women shall Yael be, the wife of Heber the Kenite, above women in the tent shall she be blessed” (Shoftim 5:24) - to support the greatness of an aveirah lishma. The women in the tent whom Yael surpasses refer to our matriarchs Sarah ,“and behold she is in the tent” (Breisheet 18:9); Rivka, “and Yitzchak brought her to the tent of Sarah his mother” (Breisheet 24:67); and Rachel and Leah, “and he [Lavan] went out of Leah's tent, and entered into Rachel's tent” (Breisheet 31:33).
As Rashi explains Sarah, Rachel and Leah asked their husbands to have children with their maidservants but “they did not have intent for the mitzva, but as they were jealous this one to that one, Rachel to her sister and Sarah and Leah to their maidservants”. Being purely motivated is extremely difficult - something we cannot always expect even from our patriarchs and matriarchs. To have secondary, or ulterior (or at times even nefarious) motives is just part and parcel of being a human being.
And in fact the Gemara modifies this teaching noting that “one should leolam, always, be involved in Torah and Mitzvot shelo lishma, not for their own sake, shimetoch shelo lishma bah lishmah, as from the wrong motives one comes to the proper motives”. This most comforting teaching recognizes that actions count at least as much - perhaps more - than intent and the Gemara concludes that an aveirah lishma is in fact “only” equal to a mitzva shelo lishma.
Rav Chaim of Volozhin, the founder or the Yeshivat Eitz Chaim in Volozhin, which championed the idea of Torah lishma, notes that the introductory phrase “leolam” teaches that it is impossible to always act lishma. Leolam, forever, we will always have some shelo lishma in our actions.
On very rare occasions one can momentarily act with 100% pure motives. Yael’s seduction of Sisera was one such occasion. The Gemara explains that she received no pleasure as “all the favours of the wicked are evil to the righteous”. It might be likened (though not equated) to a case of rape where the violent sexual act gives the victim pain.
Another potential example of an aveirah lishma was Yaakov’s stealing the blessing due to Eisav. As the Netziv explains (see “Eisav’s Revenge" here) while the hurt caused to his father could be excused as a case of aveirah lishma the same cannot be said about the pain caused to Eisav. Yaakov derived some pleasure from this - and for this he was to be punished.
Yael’s actions have a most important role in our Rosh Hashanah tefiilot. Our custom to blow 100 sounds of the shofar is based on the 100 tears Sisera’s mother shed as she waited in vain for her son to return from war (Tosafot, Rosh Hashanah 33b s.v. shiur). Apparently these were tears of great sincerity, even if shed for a most evil person. They were tears shed lishma even if for one who sinned greatly, and we are to be reminded of them on Rosh Hashanah, the Day of Judgment for all of mankind. Even Sisera’s mother can serve as an inspiration for us. May we merit to shed tears of joy as our prayers, prayers uttered to benefit mankind are answered for good.
 In discussing Esther’s living with Achasverosh Tosafot (Sanhedrin 74b s.v. veha) claims that one can technically violate the prohibition of adultery and hence be required to give up one’s life only if the adulterous relationship is with another Jew.
 What Rashi does not explain is what if anything Rivka may have done with improper intent. I would even argue that by initiating the plan to deceive Yitzchak she too was involved in an aveirah lishma.
 The exact definition of Torah lishma is a much debated topic. While in Volozhin it meant learning for the sake of learning and nothing else - surely not to become a Rabbi - others took a much more “practical” approach seeing lishma being expressed in observance of mitzvoth, in teaching, in closeness to G-d. For a detailed discussion of Torah lishma see Rabbi Dr. Norman Lamm's Torah Lishmah: Torah for Torah’s Sake in the Works of Rabbi Hayyim of Volozhin and his Contemporaries (New York, 1989).