diff --git a/main.go b/main.go index 3c4fa17..b5027cd 100644 --- a/main.go +++ b/main.go @@ -220,6 +220,38 @@ func initRouter(r *gin.Engine) { }) // All following routes need authentication + /* panic: ':ou' in new path '/:ou/:cn/:class' conflicts with existing wildcard ':dn' in existing prefix '/:dn' + r.GET("/:dn", ldapBasicAuth, func(c *gin.Context) { + dn := c.Param("dn") + + // Get user authenticated LDAP connection from context + ldapCon, err := getLdapConFromContext(c) + if err != nil { + log.Error(err) + c.AbortWithError(http.StatusInternalServerError, nil) + return + } + + // json format is the default + format := c.DefaultQuery("format", "json") + + res, err := searchByDn(ldapCon, dn, "ALL") + + // If DN does not exist, we'll get err='LDAP Result Code 32 "No Such Object"' + if err != nil { + if strings.Contains(err.Error(), "LDAP Result Code 32") { + c.AbortWithError(http.StatusNotFound, err) + return + } else { + log.Errorf("Error searching %s: %v", dn, err) + c.AbortWithError(http.StatusInternalServerError, err) + return + } + } + sendResponse(c, res, format) + return + })*/ + r.GET("/:ou/:cn/:class", ldapBasicAuth, func(c *gin.Context) { ou := c.Param("ou") cn := c.Param("cn") @@ -240,9 +272,14 @@ func initRouter(r *gin.Engine) { // If OU does not exist, we'll get err='LDAP Result Code 32 "No Such Object"' if err != nil { - log.Errorf("Error searching %s in %s : %v", cn, ou, err) - c.AbortWithError(http.StatusInternalServerError, err) - return + if strings.Contains(err.Error(), "LDAP Result Code 32") { + c.AbortWithError(http.StatusNotFound, err) + return + } else { + log.Errorf("Error searching %s in %s : %v", cn, ou, err) + c.AbortWithError(http.StatusInternalServerError, err) + return + } } sendResponse(c, res, format) return @@ -301,9 +338,14 @@ func initRouter(r *gin.Engine) { res, err := searchByCn(ldapCon, ou, cn, class, attr) if err != nil { - log.Errorf("Error searching %s in %s : %v", cn, ou, err) - c.AbortWithError(http.StatusInternalServerError, err) - return + if strings.Contains(err.Error(), "LDAP Result Code 32") { + c.AbortWithError(http.StatusNotFound, err) + return + } else { + log.Errorf("Error searching %s in %s : %v", cn, ou, err) + c.AbortWithError(http.StatusInternalServerError, err) + return + } } sendResponse(c, res, format) return @@ -412,6 +454,11 @@ func initRouter(r *gin.Engine) { * curl -u "admin:admin" --header "Content-Type: application/json" -X PUT * --data '{"objectClass":["person","top"],"cn":"newuser","sn":"New","description":"Test account"}' \ * https://localhost:8443/cn=newuser,ou=users,dc=example,dc=org + * or + * curl -u "admin:admin" -H "Content-Type: application/json" -X PUT + * -d '{"dn":"cn=newuser,ou=users,dc=example,dc=org", \ + * "objectClass":["person","top"],"cn":"newuser","sn":"New","description":"Test account"}' \ + * https://localhost:8443/modify */ r.PUT("/:dn", ldapBasicAuth, func(c *gin.Context) { dn := c.Param("dn") @@ -432,6 +479,15 @@ func initRouter(r *gin.Engine) { return } + // Get dn in body if called with "http://1.2.3.4/modify" + if strings.EqualFold(dn, "modify") { + dn = attributes["dn"].(string) + } + if len(dn) == 0 { + c.AbortWithError(http.StatusBadRequest, err) + return + } + err = updateEntry(ldapCon, dn, attributes) if err != nil { if strings.Contains(err.Error(), "LDAP Result Code 50") {