We study the optimal financing and dividend distribution problem with restricted dividend rates in a diffusion type surplus model where the drift and volatility coefficients are general functions of the level of surplus and the external environment regime. The environment regime is modeled by a Markov process. Both capital injections and dividend payments incur expenses. The objective is to maximize the expectation of the total discounted dividends minus the total cost of capital injections. We prove that it is optimal to inject capitals only when the surplus tends to fall below zero and to pay out dividends at the maximal rate when the surplus is at or above the threshold dependent on the environment regime.