web_admin.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package ldap
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "net/http"
  6. "regexp"
  7. "strconv"
  8. "strings"
  9. "imuslab.com/arozos/mod/auth/ldap/ldapreader"
  10. "imuslab.com/arozos/mod/utils"
  11. )
  12. func (ldap *ldapHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
  13. //basic components
  14. enabled, err := strconv.ParseBool(ldap.readSingleConfig("enabled"))
  15. if err != nil {
  16. utils.SendTextResponse(w, "Invalid config value [key=enabled].")
  17. return
  18. }
  19. //get the LDAP config from db
  20. BindUsername := ldap.readSingleConfig("BindUsername")
  21. BindPassword := ldap.readSingleConfig("BindPassword")
  22. FQDN := ldap.readSingleConfig("FQDN")
  23. BaseDN := ldap.readSingleConfig("BaseDN")
  24. //marshall it and return
  25. config, err := json.Marshal(Config{
  26. Enabled: enabled,
  27. BindUsername: BindUsername,
  28. BindPassword: BindPassword,
  29. FQDN: FQDN,
  30. BaseDN: BaseDN,
  31. })
  32. if err != nil {
  33. empty, err := json.Marshal(Config{})
  34. if err != nil {
  35. utils.SendErrorResponse(w, "Error while marshalling config")
  36. }
  37. utils.SendJSONResponse(w, string(empty))
  38. }
  39. utils.SendJSONResponse(w, string(config))
  40. }
  41. func (ldap *ldapHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
  42. //receive the parameter
  43. enabled, err := utils.Mv(r, "enabled", true)
  44. if err != nil {
  45. utils.SendErrorResponse(w, "enabled field can't be empty")
  46. return
  47. }
  48. //allow empty fields if enabled is false
  49. showError := true
  50. if enabled != "true" {
  51. showError = false
  52. }
  53. //four fields to store the LDAP authentication information
  54. BindUsername, err := utils.Mv(r, "bind_username", true)
  55. if err != nil {
  56. if showError {
  57. utils.SendErrorResponse(w, "bind_username field can't be empty")
  58. return
  59. }
  60. }
  61. BindPassword, err := utils.Mv(r, "bind_password", true)
  62. if err != nil {
  63. if showError {
  64. utils.SendErrorResponse(w, "bind_password field can't be empty")
  65. return
  66. }
  67. }
  68. FQDN, err := utils.Mv(r, "fqdn", true)
  69. if err != nil {
  70. if showError {
  71. utils.SendErrorResponse(w, "fqdn field can't be empty")
  72. return
  73. }
  74. }
  75. BaseDN, err := utils.Mv(r, "base_dn", true)
  76. if err != nil {
  77. if showError {
  78. utils.SendErrorResponse(w, "base_dn field can't be empty")
  79. return
  80. }
  81. }
  82. //write the data back to db
  83. ldap.coredb.Write("ldap", "enabled", enabled)
  84. ldap.coredb.Write("ldap", "BindUsername", BindUsername)
  85. ldap.coredb.Write("ldap", "BindPassword", BindPassword)
  86. ldap.coredb.Write("ldap", "FQDN", FQDN)
  87. ldap.coredb.Write("ldap", "BaseDN", BaseDN)
  88. //update the new authencation infromation
  89. ldap.ldapreader = ldapreader.NewLDAPReader(BindUsername, BindPassword, FQDN, BaseDN)
  90. //return ok
  91. utils.SendOK(w)
  92. }
  93. func (ldap *ldapHandler) TestConnection(w http.ResponseWriter, r *http.Request) {
  94. //marshall it and return the connection status
  95. userList, totalLength, err := ldap.getAllUser(10)
  96. if err != nil {
  97. errMessage, err := json.Marshal(syncorizeUserReturnInterface{Error: err.Error()})
  98. if err != nil {
  99. utils.SendErrorResponse(w, "{\"error\":\"Error while marshalling information\"}")
  100. return
  101. }
  102. utils.SendJSONResponse(w, string(errMessage))
  103. return
  104. }
  105. returnJSON := syncorizeUserReturnInterface{Userinfo: userList, Length: len(userList), TotalLength: totalLength, Error: ""}
  106. accountJSON, err := json.Marshal(returnJSON)
  107. if err != nil {
  108. errMessage, err := json.Marshal(syncorizeUserReturnInterface{Error: err.Error()})
  109. if err != nil {
  110. utils.SendErrorResponse(w, "{\"error\":\"Error while marshalling information\"}")
  111. return
  112. }
  113. utils.SendJSONResponse(w, string(errMessage))
  114. return
  115. }
  116. utils.SendJSONResponse(w, string(accountJSON))
  117. }
  118. func (ldap *ldapHandler) checkCurrUserAdmin(w http.ResponseWriter, r *http.Request) (bool, error) {
  119. //check current user is admin and new update will remove it or not
  120. currentLoggedInUser, err := ldap.userHandler.GetUserInfoFromRequest(w, r)
  121. if err != nil {
  122. return false, err
  123. }
  124. ldapCurrUserInfo, err := ldap.ldapreader.GetUser(currentLoggedInUser.Username)
  125. if err != nil {
  126. return false, errors.New(err.Error() + ", probably due to your account is not in the LDAP server")
  127. }
  128. isAdmin := false
  129. //get the croups out from LDAP group list
  130. regexSyntax := regexp.MustCompile("cn=([^,]+),")
  131. for _, v := range ldapCurrUserInfo.GetAttributeValues("memberOf") {
  132. //loop through all memberOf's array
  133. groups := regexSyntax.FindStringSubmatch(v)
  134. //if after regex there is still groups exists
  135. if len(groups) > 0 {
  136. //check if the LDAP group is already exists in ArOZOS system
  137. if ldap.permissionHandler.GroupExists(groups[1]) {
  138. if ldap.permissionHandler.GetPermissionGroupByName(groups[1]).IsAdmin {
  139. isAdmin = true
  140. }
  141. }
  142. }
  143. }
  144. return isAdmin, nil
  145. }
  146. func (ldap *ldapHandler) SynchronizeUser(w http.ResponseWriter, r *http.Request) {
  147. //check if suer is admin before executing the command
  148. //if user is admin then check if user will lost him/her's admin access
  149. consistencyCheck, err := ldap.checkCurrUserAdmin(w, r)
  150. if err != nil {
  151. // escape " symbol manually
  152. errorMsg := strings.ReplaceAll(err.Error(), "\"", "\\\"")
  153. utils.SendErrorResponse(w, errorMsg)
  154. return
  155. }
  156. if !consistencyCheck {
  157. utils.SendErrorResponse(w, "You will no longer become the admin after synchronizing, synchronize terminated")
  158. return
  159. }
  160. err = ldap.SynchronizeUserFromLDAP()
  161. if err != nil {
  162. utils.SendErrorResponse(w, err.Error())
  163. return
  164. }
  165. utils.SendOK(w)
  166. }