As to the question of “are they legitimate popes”, they were elected like all the other legitimate popes, so they are legitimate. It is not for us to decide whether they are in heresy or not (Pope Honorius I, was excommunicated 40+ years AFTER he died, excommunicated by another pope. Excommunicated for not opposing heresy and “seeming” to side with it.), that is left to the proper authorities.
It is our duty to follow ALL the popes, when one pope teaches contrary to, or sets a bad example contrary to ALL the popes before him, then we do not follow. We would not have followed Honorius in his error, but, other than that error, his pontificate was in order with tradition.
The Vatican II popes are another story, they don't act or think like the prior popes. We don't follow them when they go against what has always been taught, tradition, and we follow them when they follow tradition.
That is where we demonstrate to the Protestants and the Orthodox, that the pope is not a personality cult, we don't follow whatever one pope says, we follow what ALL the popes have always taught and passed on to us unchanged. Not unchanged like an oak acorn passed on as an oak acorn for 2000 years, but, as an oak acorn passed on as a naturally developing oak tree, always an oak tree from the acorn phase till the final tree phase. Not passed on as an oak acorn from the beginning turned into a fig tree, or an elephant over time.
Pardon me for using my own analogy, you can look to Vatican I for the legal description.