If the sunroof lid is in "tilt" mode then the sliding headliner will not close completely. If the sunroof is closed but the headliner still will not go all the way forward then it is most likely that the sliders on the headliner have broken and are causing it to jam. This is common on e46 sunroofs.
Here is a link to a DIY writeup I found doing a google search.
http://www.jumboprawn.net/jesse/proj...oof/index.html
Also if you look at the writeup in the second picture down the gray drive which is labeled (A) is outdated and often times bends or breaks. If you have that style then it is recommended that you upgrade to the newer style design but this would be a much bigger job. If you dont want to upgrade then at least be sure that the piece is not bent and contacts the sliders well and does not allow the sliders to go under it, otherwise this will cause them to break again soon.
Good Luck,