How are CDMA codes distributed? I think this question is so trivial that it's nowhere explained (I would not find it) and the answer is probably trivial too. But I still wonder how codes in a typical CDMA system are distributed? They act like symmetric keys in a crypto system: both parties need to know them and there is a complexity of O(N) to share them (N being all possible users).

For example, in a WCDMA cellphone system, there is an "infinite" number N of possible nodes (e.g. a roaming user coming to the US). The codes can't be statically assigned. And the wireless base station can't just try all possible codes.

So I assume that the first communication of a handset and base station does not go over CDMA but the base station somehow detects that there is a new handset, creates a new code and sends this code to the handset. Afterwards the communication goes over CDMA.
For handover between different stations, the base station could distribute the code among its neighbor stations.

How does it work in practice?
And am I correct that the seed to an LSFR generating the chipping sequence is distributed as opposed to the chipping sequence itself?
I don't know how WCDMA does it, but CDMA2000 encodes the data twice or three times (it's been a while): once with a "long code" (42 or 43 bits, so it takes days to cycle through even at chip rates in the MHz), once with a "short code" (16 bits, with zero-stuffing so that the cycle is exactly 65536 chips long, and the spectrum is flat), and IIRC once with a 64-bit long Walsh code.  But There's a channel that's only encoded with the long code; that one is used by the base station to assign a specific set of codes and timings to the phone.

So, basically, it's done dynamically.

The CDMA2000 specs are out there.  When I looked (over ten years ago, which is why this answer is a bit fuzzy) the "real" spec was behind a paywall, but there were draft specifications that were pretty close.