The priest offers the sacrament in alter christus. The priest is but a vessel. It is Jesus Christ that confers the grace of each sacrament. The holiness of the priest that offers the sacrament is irrelevant to the validity of the sacrament, if offered correctly.
See the following from the CCC:
1127 Celebrated worthily in faith, the sacraments confer the grace that they signify. They are efficacious because in them Christ himself is at work: it is he who baptizes, he who acts in his sacraments in order to communicate the grace that each sacrament signifies. the Father always hears the prayer of his Son’s Church which, in the epiclesis of each sacrament, expresses her faith in the power of the Spirit. As fire transforms into itself everything it touches, so the Holy Spirit transforms into the divine life whatever is subjected to his power.
1128 This is the meaning of the Church’s affirmation that the sacraments act ex opere operato (literally: “by the very fact of the action’s being performed”), i.e., by virtue of the saving work of Christ, accomplished once for all. It follows that "the sacrament is not wrought by the righteousness of either the celebrant or the recipient, but by the power of God. "From the moment that a sacrament is celebrated in accordance with the intention of the Church, the power of Christ and his Spirit acts in and through it, independently of the personal holiness of the minister. Nevertheless, the fruits of the sacraments also depend on the disposition of the one who receives them.