server_modify.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package ldap
  2. import (
  3. "log"
  4. "net"
  5. "github.com/nmcclain/asn1-ber"
  6. )
  7. func HandleAddRequest(req *ber.Packet, boundDN string, fns map[string]Adder, conn net.Conn) (resultCode LDAPResultCode) {
  8. if len(req.Children) != 2 {
  9. return LDAPResultProtocolError
  10. }
  11. var ok bool
  12. addReq := AddRequest{}
  13. addReq.dn, ok = req.Children[0].Value.(string)
  14. if !ok {
  15. return LDAPResultProtocolError
  16. }
  17. addReq.attributes = []Attribute{}
  18. for _, attr := range req.Children[1].Children {
  19. if len(attr.Children) != 2 {
  20. return LDAPResultProtocolError
  21. }
  22. a := Attribute{}
  23. a.attrType, ok = attr.Children[0].Value.(string)
  24. if !ok {
  25. return LDAPResultProtocolError
  26. }
  27. a.attrVals = []string{}
  28. for _, val := range attr.Children[1].Children {
  29. v, ok := val.Value.(string)
  30. if !ok {
  31. return LDAPResultProtocolError
  32. }
  33. a.attrVals = append(a.attrVals, v)
  34. }
  35. addReq.attributes = append(addReq.attributes, a)
  36. }
  37. fnNames := []string{}
  38. for k := range fns {
  39. fnNames = append(fnNames, k)
  40. }
  41. fn := routeFunc(boundDN, fnNames)
  42. resultCode, err := fns[fn].Add(boundDN, addReq, conn)
  43. if err != nil {
  44. log.Printf("AddFn Error %s", err.Error())
  45. return LDAPResultOperationsError
  46. }
  47. return resultCode
  48. }
  49. func HandleDeleteRequest(req *ber.Packet, boundDN string, fns map[string]Deleter, conn net.Conn) (resultCode LDAPResultCode) {
  50. deleteDN := ber.DecodeString(req.Data.Bytes())
  51. fnNames := []string{}
  52. for k := range fns {
  53. fnNames = append(fnNames, k)
  54. }
  55. fn := routeFunc(boundDN, fnNames)
  56. resultCode, err := fns[fn].Delete(boundDN, deleteDN, conn)
  57. if err != nil {
  58. log.Printf("DeleteFn Error %s", err.Error())
  59. return LDAPResultOperationsError
  60. }
  61. return resultCode
  62. }
  63. func HandleModifyRequest(req *ber.Packet, boundDN string, fns map[string]Modifier, conn net.Conn) (resultCode LDAPResultCode) {
  64. if len(req.Children) != 2 {
  65. return LDAPResultProtocolError
  66. }
  67. var ok bool
  68. modReq := ModifyRequest{}
  69. modReq.Dn, ok = req.Children[0].Value.(string)
  70. if !ok {
  71. return LDAPResultProtocolError
  72. }
  73. for _, change := range req.Children[1].Children {
  74. if len(change.Children) != 2 {
  75. return LDAPResultProtocolError
  76. }
  77. attr := PartialAttribute{}
  78. attrs := change.Children[1].Children
  79. if len(attrs) != 2 {
  80. return LDAPResultProtocolError
  81. }
  82. attr.AttrType, ok = attrs[0].Value.(string)
  83. if !ok {
  84. return LDAPResultProtocolError
  85. }
  86. for _, val := range attrs[1].Children {
  87. v, ok := val.Value.(string)
  88. if !ok {
  89. return LDAPResultProtocolError
  90. }
  91. attr.AttrVals = append(attr.AttrVals, v)
  92. }
  93. op, ok := change.Children[0].Value.(uint64)
  94. if !ok {
  95. return LDAPResultProtocolError
  96. }
  97. switch op {
  98. default:
  99. log.Printf("Unrecognized Modify attribute %d", op)
  100. return LDAPResultProtocolError
  101. case AddAttribute:
  102. modReq.Add(attr.AttrType, attr.AttrVals)
  103. case DeleteAttribute:
  104. modReq.Delete(attr.AttrType, attr.AttrVals)
  105. case ReplaceAttribute:
  106. modReq.Replace(attr.AttrType, attr.AttrVals)
  107. }
  108. }
  109. fnNames := []string{}
  110. for k := range fns {
  111. fnNames = append(fnNames, k)
  112. }
  113. fn := routeFunc(boundDN, fnNames)
  114. resultCode, err := fns[fn].Modify(boundDN, modReq, conn)
  115. if err != nil {
  116. log.Printf("ModifyFn Error %s", err.Error())
  117. return LDAPResultOperationsError
  118. }
  119. return resultCode
  120. }
  121. func HandleCompareRequest(req *ber.Packet, boundDN string, fns map[string]Comparer, conn net.Conn) (resultCode LDAPResultCode) {
  122. if len(req.Children) != 2 {
  123. return LDAPResultProtocolError
  124. }
  125. var ok bool
  126. compReq := CompareRequest{}
  127. compReq.dn, ok = req.Children[0].Value.(string)
  128. if !ok {
  129. return LDAPResultProtocolError
  130. }
  131. ava := req.Children[1]
  132. if len(ava.Children) != 2 {
  133. return LDAPResultProtocolError
  134. }
  135. attr, ok := ava.Children[0].Value.(string)
  136. if !ok {
  137. return LDAPResultProtocolError
  138. }
  139. val, ok := ava.Children[1].Value.(string)
  140. if !ok {
  141. return LDAPResultProtocolError
  142. }
  143. compReq.ava = []AttributeValueAssertion{AttributeValueAssertion{attr, val}}
  144. fnNames := []string{}
  145. for k := range fns {
  146. fnNames = append(fnNames, k)
  147. }
  148. fn := routeFunc(boundDN, fnNames)
  149. resultCode, err := fns[fn].Compare(boundDN, compReq, conn)
  150. if err != nil {
  151. log.Printf("CompareFn Error %s", err.Error())
  152. return LDAPResultOperationsError
  153. }
  154. return resultCode
  155. }
  156. func HandleExtendedRequest(req *ber.Packet, boundDN string, fns map[string]Extender, conn net.Conn) (resultCode LDAPResultCode) {
  157. if len(req.Children) != 1 && len(req.Children) != 2 {
  158. return LDAPResultProtocolError
  159. }
  160. name := ber.DecodeString(req.Children[0].Data.Bytes())
  161. var val string
  162. if len(req.Children) == 2 {
  163. val = ber.DecodeString(req.Children[1].Data.Bytes())
  164. }
  165. extReq := ExtendedRequest{name, val}
  166. fnNames := []string{}
  167. for k := range fns {
  168. fnNames = append(fnNames, k)
  169. }
  170. fn := routeFunc(boundDN, fnNames)
  171. resultCode, err := fns[fn].Extended(boundDN, extReq, conn)
  172. if err != nil {
  173. log.Printf("ExtendedFn Error %s", err.Error())
  174. return LDAPResultOperationsError
  175. }
  176. return resultCode
  177. }
  178. func HandleAbandonRequest(req *ber.Packet, boundDN string, fns map[string]Abandoner, conn net.Conn) error {
  179. fnNames := []string{}
  180. for k := range fns {
  181. fnNames = append(fnNames, k)
  182. }
  183. fn := routeFunc(boundDN, fnNames)
  184. err := fns[fn].Abandon(boundDN, conn)
  185. return err
  186. }
  187. func HandleModifyDNRequest(req *ber.Packet, boundDN string, fns map[string]ModifyDNr, conn net.Conn) (resultCode LDAPResultCode) {
  188. if len(req.Children) != 3 && len(req.Children) != 4 {
  189. return LDAPResultProtocolError
  190. }
  191. var ok bool
  192. mdnReq := ModifyDNRequest{}
  193. mdnReq.dn, ok = req.Children[0].Value.(string)
  194. if !ok {
  195. return LDAPResultProtocolError
  196. }
  197. mdnReq.newrdn, ok = req.Children[1].Value.(string)
  198. if !ok {
  199. return LDAPResultProtocolError
  200. }
  201. mdnReq.deleteoldrdn, ok = req.Children[2].Value.(bool)
  202. if !ok {
  203. return LDAPResultProtocolError
  204. }
  205. if len(req.Children) == 4 {
  206. mdnReq.newSuperior, ok = req.Children[3].Value.(string)
  207. if !ok {
  208. return LDAPResultProtocolError
  209. }
  210. }
  211. fnNames := []string{}
  212. for k := range fns {
  213. fnNames = append(fnNames, k)
  214. }
  215. fn := routeFunc(boundDN, fnNames)
  216. resultCode, err := fns[fn].ModifyDN(boundDN, mdnReq, conn)
  217. if err != nil {
  218. log.Printf("ModifyDN Error %s", err.Error())
  219. return LDAPResultOperationsError
  220. }
  221. return resultCode
  222. }