Step 1: Breaking it into two reported questions:
This is a double question joined by 'and' — each part is reported separately then joined.
Step 2: Part 1:
"Who are you?" → who he was.
Step 3: Part 2:
"Why have you come here?" → why he had come there (here → there, present perfect → past perfect).
Final Answer:
The guard asked the visitor who he was and why he had come there.