Section 1668 is the outer boundary of what a contract may excuse. All contracts which have for their object, directly or indirectly, to exempt anyone from responsibility for their own fraud, or willful injury to the person or property of another, or violation of law, whether willful or negligent, are against the policy of the law. It applies to the object of the clause rather than to its wording, and "directly or indirectly" is there to catch the drafting that achieves the exemption by another route — a limitation of liability to a nominal sum, a waiver of remedies, a conclusive-evidence clause.
The four protected categories are not the same in scope. Fraud and willful injury to person or property are firmly within it. So is a violation of law — and the section says "whether willful or negligent", which is what makes clauses excusing statutory violations vulnerable in a way that clauses about ordinary carelessness may not be. The section does not, on its face, void every exculpatory clause for negligence; California law treats a release of ordinary negligence between private parties as capable of being valid, subject to the public interest limits developed by the courts and to specific statutes such as § 1953, which voids a residential tenant's waiver of the landlord's duty of care.
What the section provides is that the clause is against the policy of the law, which means it is not enforced; it does not follow that the whole contract falls. Section 1670.5 provides a separate route for unconscionable terms, with its own remedies. Whether a particular exclusion clause is within § 1668, is unconscionable, or is simply an allocation of risk the law permits, is the question to take to a lawyer with the contract.