The ionization of the circumstellar medium of SN 1987A by hard radiation following the supernova outburst is analyzed. Particular emphasis is placed on circumstellar UV and optical emission lines from the supernova. The observations are well modeled by a circumstellar shell, ionized by the strong soft X-ray burst during the first hours after the explosion. After the initial nearly complete ionization, the shell recombines into the observed ionization stages. At the breakout, the shell is heated to approximately 10(5) K, subsequently cooling to approximately 5 x 10(4) K in approximately 300 days, in agreement with optical observations. Evolution of the UV line fluxes fixes the peak radiation temperature at the supernova breakout to the range (3-6) x 10(5) K. The shell density determines the recombination and cooling rates, and most of the gas is constrained to have a density in the range (1-4) x 10(4) cm-3. Relative CNO abundances calculated from the models are consistent with those earlier derived from simplified models, N/C approximately 5 and N/O approximately 2. The helium to hydrogen ratio is 0.06-0.20, and total metal abundance 0.2-0.4 times solar. The profiles of the optical lines are discussed, and it is shown how these can provide supplementary information about dynamics and density structure. The mass of the emitting gas is 0.03-0.05 M.. The total mass lost by the progenitor is not constrained by these observations, nor by observations in the radio. The derived expansion rate of the UV shell, approximately 15 km s-1, is difficult to reconcile with a simple interaction between a fast and a slow wind. If the mass-loss rate in the supergiant phase is normal, the remnant of its wind is expected to give rise to a steady contribution in N v lambda-1240 for several decades. We also find that most of the [O III] emission at late time requires a component with density less than 10(4) cm-3. The temperature of the blue supergiant wind close to the supernova is sensitive to whether it was ionized by the progenitor or not. For a preionized wind the temperature at the time of the radio observations was approximately (3.5-7.5) x 10(4) K, implying a mass-loss rate of approximately (3.5-6) x 10(-6) M. yr-1 for a progenitor wind speed of 550 km s-1.