OpenGL - mask with multiple textures

I have implemented masking in OpenGL according to the following concept:
The mask is composed of black and white colors.
A foreground texture should only be visible in the white parts of the mask.
A background texture should only be visible in the black parts of the mask.

I can make the white part or the black part work as supposed by using glBlendFunc(), but not the two at the same time, because the foreground layer not only blends onto the mask, but also onto the background layer.

Is there anyone who knows how to accomplish this in the best way? I have been searching the net and read something about fragment shaders. Is this the way to go?

You can use a fragment shader or you can use multi-texturing (glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE)).

Using a shader requires more work in the short term, as you need to learn GLSL as well as a fair number of new functions for compiling, linking and controlling shader programs. But you’ll probably want to learn that eventually, as multi-texturing is rather limited (and there isn’t a great deal of information on using it). The only significant reason to prefer multi-texturing is if you need to maximum compatibility with ancient hardware (multi-texturing was added in OpenGL 1.3, shaders in 2.0).