What are the legal powers of a starport? For example can they ground a ship for being unsafe or not having enough crew? I know they can do custom checks but can they detain crew on suspicion that they are smugglers. Do the starports do routine inspections of ship logs to check that the ship crew do regular maintenance of their ship?
That's kind of a IYTU question, really. If you want your starports to be high security like in Star Wars, have at it.
Something to consider: the Third Imperium DOES NOT have the Posse Comitatus Act of 1876 that separates the power of the military and law enforcement. If duly authorized by both the SPA Director for the port and his own chain of command, an Army private on duty can arrest and detain someone. They'll have to answer for that arrest with their seniors, but they're just as allowed to do so as if they were on guard duty. Aid to The Civil Power is alive and well in the OTU.
IMTU, it depends on the starport..
You can bet that all A, B and some C class starports will have their own armed security. In a high value, high threat port that a portion of that armed security might even be Imperial troops [either Marines or Army], especially if a Navy Base is present insystem [see the consideration comment above].
Now, here's the thing: the average military troop is a *lousy* police officer. They go overboard over minor stuff and oftentimes miss obvious clues pointing to criminal behavior the rest of the time. This is why the Imperial Armed Services have Military Police, who are specially trained for this kind of duty.
So the way I logic this out is that guarding a starport and enforcing the law at the starport are two linked duty profiles. Every A, B, and C starport has law enforcement officers duly appointed to enforce laws, regulations, customs and immigration. That is what they do and they're good at it.
IF a starport has a military presence beyond a recruiter's office, those personnel work with their own chain of command to guard the perimeter, control access across the extrality line, and defend the port if necessary.
And, of course, in an emergency the cops and the troops support each other as necessary.
All this handles the 'who' part of the question.
As to the 'what' [as in 'what do they enforce'] part, under the authority of the Starport Authority [the SPA] AND the local Navy or system squadron commander, both law enforcement and troops are appointed to enforce whatever regulation is deemed necessary by the Port Authority.
[What that really means is, 'whatever the referee needs them to enforce for his game']
Logic says that A starports with a Navy or Scout base on a high tech high pop world are most likely to be more stringent about regulation enforcement than a Class E on the back arse of nowhere. Location and facilities will be a factor.