This can usually be implemented within the desktop environment (eg KDE and Gnome both have graphical utilities for doing this).
You can also achieve the desired button swapping using xinput (which can be automated on login via autostart script). The basic idea described here...
or by creating a custom Xorg input config file. For example
The physical button numbering can be discovered using xev, and a button mapping assignment made accordingly.