Density-functional theory for internal magnetic fields