You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin_auth_ldap.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models/auth"
  9. "code.gitea.io/gitea/services/auth/source/ldap"
  10. "github.com/urfave/cli/v2"
  11. )
  12. type (
  13. authService struct {
  14. initDB func(ctx context.Context) error
  15. createAuthSource func(*auth.Source) error
  16. updateAuthSource func(*auth.Source) error
  17. getAuthSourceByID func(id int64) (*auth.Source, error)
  18. }
  19. )
  20. var (
  21. commonLdapCLIFlags = []cli.Flag{
  22. &cli.StringFlag{
  23. Name: "name",
  24. Usage: "Authentication name.",
  25. },
  26. &cli.BoolFlag{
  27. Name: "not-active",
  28. Usage: "Deactivate the authentication source.",
  29. },
  30. &cli.BoolFlag{
  31. Name: "active",
  32. Usage: "Activate the authentication source.",
  33. },
  34. &cli.StringFlag{
  35. Name: "security-protocol",
  36. Usage: "Security protocol name.",
  37. },
  38. &cli.BoolFlag{
  39. Name: "skip-tls-verify",
  40. Usage: "Disable TLS verification.",
  41. },
  42. &cli.StringFlag{
  43. Name: "host",
  44. Usage: "The address where the LDAP server can be reached.",
  45. },
  46. &cli.IntFlag{
  47. Name: "port",
  48. Usage: "The port to use when connecting to the LDAP server.",
  49. },
  50. &cli.StringFlag{
  51. Name: "user-search-base",
  52. Usage: "The LDAP base at which user accounts will be searched for.",
  53. },
  54. &cli.StringFlag{
  55. Name: "user-filter",
  56. Usage: "An LDAP filter declaring how to find the user record that is attempting to authenticate.",
  57. },
  58. &cli.StringFlag{
  59. Name: "admin-filter",
  60. Usage: "An LDAP filter specifying if a user should be given administrator privileges.",
  61. },
  62. &cli.StringFlag{
  63. Name: "restricted-filter",
  64. Usage: "An LDAP filter specifying if a user should be given restricted status.",
  65. },
  66. &cli.BoolFlag{
  67. Name: "allow-deactivate-all",
  68. Usage: "Allow empty search results to deactivate all users.",
  69. },
  70. &cli.StringFlag{
  71. Name: "username-attribute",
  72. Usage: "The attribute of the user’s LDAP record containing the user name.",
  73. },
  74. &cli.StringFlag{
  75. Name: "firstname-attribute",
  76. Usage: "The attribute of the user’s LDAP record containing the user’s first name.",
  77. },
  78. &cli.StringFlag{
  79. Name: "surname-attribute",
  80. Usage: "The attribute of the user’s LDAP record containing the user’s surname.",
  81. },
  82. &cli.StringFlag{
  83. Name: "email-attribute",
  84. Usage: "The attribute of the user’s LDAP record containing the user’s email address.",
  85. },
  86. &cli.StringFlag{
  87. Name: "public-ssh-key-attribute",
  88. Usage: "The attribute of the user’s LDAP record containing the user’s public ssh key.",
  89. },
  90. &cli.BoolFlag{
  91. Name: "skip-local-2fa",
  92. Usage: "Set to true to skip local 2fa for users authenticated by this source",
  93. },
  94. &cli.StringFlag{
  95. Name: "avatar-attribute",
  96. Usage: "The attribute of the user’s LDAP record containing the user’s avatar.",
  97. },
  98. }
  99. ldapBindDnCLIFlags = append(commonLdapCLIFlags,
  100. &cli.StringFlag{
  101. Name: "bind-dn",
  102. Usage: "The DN to bind to the LDAP server with when searching for the user.",
  103. },
  104. &cli.StringFlag{
  105. Name: "bind-password",
  106. Usage: "The password for the Bind DN, if any.",
  107. },
  108. &cli.BoolFlag{
  109. Name: "attributes-in-bind",
  110. Usage: "Fetch attributes in bind DN context.",
  111. },
  112. &cli.BoolFlag{
  113. Name: "synchronize-users",
  114. Usage: "Enable user synchronization.",
  115. },
  116. &cli.BoolFlag{
  117. Name: "disable-synchronize-users",
  118. Usage: "Disable user synchronization.",
  119. },
  120. &cli.UintFlag{
  121. Name: "page-size",
  122. Usage: "Search page size.",
  123. })
  124. ldapSimpleAuthCLIFlags = append(commonLdapCLIFlags,
  125. &cli.StringFlag{
  126. Name: "user-dn",
  127. Usage: "The user’s DN.",
  128. })
  129. cmdAuthAddLdapBindDn = &cli.Command{
  130. Name: "add-ldap",
  131. Usage: "Add new LDAP (via Bind DN) authentication source",
  132. Action: func(c *cli.Context) error {
  133. return newAuthService().addLdapBindDn(c)
  134. },
  135. Flags: ldapBindDnCLIFlags,
  136. }
  137. cmdAuthUpdateLdapBindDn = &cli.Command{
  138. Name: "update-ldap",
  139. Usage: "Update existing LDAP (via Bind DN) authentication source",
  140. Action: func(c *cli.Context) error {
  141. return newAuthService().updateLdapBindDn(c)
  142. },
  143. Flags: append([]cli.Flag{idFlag}, ldapBindDnCLIFlags...),
  144. }
  145. cmdAuthAddLdapSimpleAuth = &cli.Command{
  146. Name: "add-ldap-simple",
  147. Usage: "Add new LDAP (simple auth) authentication source",
  148. Action: func(c *cli.Context) error {
  149. return newAuthService().addLdapSimpleAuth(c)
  150. },
  151. Flags: ldapSimpleAuthCLIFlags,
  152. }
  153. cmdAuthUpdateLdapSimpleAuth = &cli.Command{
  154. Name: "update-ldap-simple",
  155. Usage: "Update existing LDAP (simple auth) authentication source",
  156. Action: func(c *cli.Context) error {
  157. return newAuthService().updateLdapSimpleAuth(c)
  158. },
  159. Flags: append([]cli.Flag{idFlag}, ldapSimpleAuthCLIFlags...),
  160. }
  161. )
  162. // newAuthService creates a service with default functions.
  163. func newAuthService() *authService {
  164. return &authService{
  165. initDB: initDB,
  166. createAuthSource: auth.CreateSource,
  167. updateAuthSource: auth.UpdateSource,
  168. getAuthSourceByID: auth.GetSourceByID,
  169. }
  170. }
  171. // parseAuthSource assigns values on authSource according to command line flags.
  172. func parseAuthSource(c *cli.Context, authSource *auth.Source) {
  173. if c.IsSet("name") {
  174. authSource.Name = c.String("name")
  175. }
  176. if c.IsSet("not-active") {
  177. authSource.IsActive = !c.Bool("not-active")
  178. }
  179. if c.IsSet("active") {
  180. authSource.IsActive = c.Bool("active")
  181. }
  182. if c.IsSet("synchronize-users") {
  183. authSource.IsSyncEnabled = c.Bool("synchronize-users")
  184. }
  185. if c.IsSet("disable-synchronize-users") {
  186. authSource.IsSyncEnabled = !c.Bool("disable-synchronize-users")
  187. }
  188. }
  189. // parseLdapConfig assigns values on config according to command line flags.
  190. func parseLdapConfig(c *cli.Context, config *ldap.Source) error {
  191. if c.IsSet("name") {
  192. config.Name = c.String("name")
  193. }
  194. if c.IsSet("host") {
  195. config.Host = c.String("host")
  196. }
  197. if c.IsSet("port") {
  198. config.Port = c.Int("port")
  199. }
  200. if c.IsSet("security-protocol") {
  201. p, ok := findLdapSecurityProtocolByName(c.String("security-protocol"))
  202. if !ok {
  203. return fmt.Errorf("Unknown security protocol name: %s", c.String("security-protocol"))
  204. }
  205. config.SecurityProtocol = p
  206. }
  207. if c.IsSet("skip-tls-verify") {
  208. config.SkipVerify = c.Bool("skip-tls-verify")
  209. }
  210. if c.IsSet("bind-dn") {
  211. config.BindDN = c.String("bind-dn")
  212. }
  213. if c.IsSet("user-dn") {
  214. config.UserDN = c.String("user-dn")
  215. }
  216. if c.IsSet("bind-password") {
  217. config.BindPassword = c.String("bind-password")
  218. }
  219. if c.IsSet("user-search-base") {
  220. config.UserBase = c.String("user-search-base")
  221. }
  222. if c.IsSet("username-attribute") {
  223. config.AttributeUsername = c.String("username-attribute")
  224. }
  225. if c.IsSet("firstname-attribute") {
  226. config.AttributeName = c.String("firstname-attribute")
  227. }
  228. if c.IsSet("surname-attribute") {
  229. config.AttributeSurname = c.String("surname-attribute")
  230. }
  231. if c.IsSet("email-attribute") {
  232. config.AttributeMail = c.String("email-attribute")
  233. }
  234. if c.IsSet("attributes-in-bind") {
  235. config.AttributesInBind = c.Bool("attributes-in-bind")
  236. }
  237. if c.IsSet("public-ssh-key-attribute") {
  238. config.AttributeSSHPublicKey = c.String("public-ssh-key-attribute")
  239. }
  240. if c.IsSet("avatar-attribute") {
  241. config.AttributeAvatar = c.String("avatar-attribute")
  242. }
  243. if c.IsSet("page-size") {
  244. config.SearchPageSize = uint32(c.Uint("page-size"))
  245. }
  246. if c.IsSet("user-filter") {
  247. config.Filter = c.String("user-filter")
  248. }
  249. if c.IsSet("admin-filter") {
  250. config.AdminFilter = c.String("admin-filter")
  251. }
  252. if c.IsSet("restricted-filter") {
  253. config.RestrictedFilter = c.String("restricted-filter")
  254. }
  255. if c.IsSet("allow-deactivate-all") {
  256. config.AllowDeactivateAll = c.Bool("allow-deactivate-all")
  257. }
  258. if c.IsSet("skip-local-2fa") {
  259. config.SkipLocalTwoFA = c.Bool("skip-local-2fa")
  260. }
  261. return nil
  262. }
  263. // findLdapSecurityProtocolByName finds security protocol by its name ignoring case.
  264. // It returns the value of the security protocol and if it was found.
  265. func findLdapSecurityProtocolByName(name string) (ldap.SecurityProtocol, bool) {
  266. for i, n := range ldap.SecurityProtocolNames {
  267. if strings.EqualFold(name, n) {
  268. return i, true
  269. }
  270. }
  271. return 0, false
  272. }
  273. // getAuthSource gets the login source by its id defined in the command line flags.
  274. // It returns an error if the id is not set, does not match any source or if the source is not of expected type.
  275. func (a *authService) getAuthSource(c *cli.Context, authType auth.Type) (*auth.Source, error) {
  276. if err := argsSet(c, "id"); err != nil {
  277. return nil, err
  278. }
  279. authSource, err := a.getAuthSourceByID(c.Int64("id"))
  280. if err != nil {
  281. return nil, err
  282. }
  283. if authSource.Type != authType {
  284. return nil, fmt.Errorf("Invalid authentication type. expected: %s, actual: %s", authType.String(), authSource.Type.String())
  285. }
  286. return authSource, nil
  287. }
  288. // addLdapBindDn adds a new LDAP via Bind DN authentication source.
  289. func (a *authService) addLdapBindDn(c *cli.Context) error {
  290. if err := argsSet(c, "name", "security-protocol", "host", "port", "user-search-base", "user-filter", "email-attribute"); err != nil {
  291. return err
  292. }
  293. ctx, cancel := installSignals()
  294. defer cancel()
  295. if err := a.initDB(ctx); err != nil {
  296. return err
  297. }
  298. authSource := &auth.Source{
  299. Type: auth.LDAP,
  300. IsActive: true, // active by default
  301. Cfg: &ldap.Source{
  302. Enabled: true, // always true
  303. },
  304. }
  305. parseAuthSource(c, authSource)
  306. if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
  307. return err
  308. }
  309. return a.createAuthSource(authSource)
  310. }
  311. // updateLdapBindDn updates a new LDAP via Bind DN authentication source.
  312. func (a *authService) updateLdapBindDn(c *cli.Context) error {
  313. ctx, cancel := installSignals()
  314. defer cancel()
  315. if err := a.initDB(ctx); err != nil {
  316. return err
  317. }
  318. authSource, err := a.getAuthSource(c, auth.LDAP)
  319. if err != nil {
  320. return err
  321. }
  322. parseAuthSource(c, authSource)
  323. if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
  324. return err
  325. }
  326. return a.updateAuthSource(authSource)
  327. }
  328. // addLdapSimpleAuth adds a new LDAP (simple auth) authentication source.
  329. func (a *authService) addLdapSimpleAuth(c *cli.Context) error {
  330. if err := argsSet(c, "name", "security-protocol", "host", "port", "user-dn", "user-filter", "email-attribute"); err != nil {
  331. return err
  332. }
  333. ctx, cancel := installSignals()
  334. defer cancel()
  335. if err := a.initDB(ctx); err != nil {
  336. return err
  337. }
  338. authSource := &auth.Source{
  339. Type: auth.DLDAP,
  340. IsActive: true, // active by default
  341. Cfg: &ldap.Source{
  342. Enabled: true, // always true
  343. },
  344. }
  345. parseAuthSource(c, authSource)
  346. if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
  347. return err
  348. }
  349. return a.createAuthSource(authSource)
  350. }
  351. // updateLdapBindDn updates a new LDAP (simple auth) authentication source.
  352. func (a *authService) updateLdapSimpleAuth(c *cli.Context) error {
  353. ctx, cancel := installSignals()
  354. defer cancel()
  355. if err := a.initDB(ctx); err != nil {
  356. return err
  357. }
  358. authSource, err := a.getAuthSource(c, auth.DLDAP)
  359. if err != nil {
  360. return err
  361. }
  362. parseAuthSource(c, authSource)
  363. if err := parseLdapConfig(c, authSource.Cfg.(*ldap.Source)); err != nil {
  364. return err
  365. }
  366. return a.updateAuthSource(authSource)
  367. }