api_utils.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. // Copyright (C) 2019 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package httpd
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "io"
  22. "io/fs"
  23. "mime"
  24. "net/http"
  25. "net/url"
  26. "os"
  27. "path"
  28. "slices"
  29. "strconv"
  30. "strings"
  31. "sync"
  32. "time"
  33. "github.com/go-chi/chi/v5"
  34. "github.com/go-chi/chi/v5/middleware"
  35. "github.com/go-chi/render"
  36. "github.com/klauspost/compress/zip"
  37. "github.com/rs/xid"
  38. "github.com/sftpgo/sdk/plugin/notifier"
  39. "github.com/drakkan/sftpgo/v2/internal/common"
  40. "github.com/drakkan/sftpgo/v2/internal/dataprovider"
  41. "github.com/drakkan/sftpgo/v2/internal/logger"
  42. "github.com/drakkan/sftpgo/v2/internal/metric"
  43. "github.com/drakkan/sftpgo/v2/internal/plugin"
  44. "github.com/drakkan/sftpgo/v2/internal/smtp"
  45. "github.com/drakkan/sftpgo/v2/internal/util"
  46. "github.com/drakkan/sftpgo/v2/internal/vfs"
  47. )
  48. type pwdChange struct {
  49. CurrentPassword string `json:"current_password"`
  50. NewPassword string `json:"new_password"`
  51. }
  52. type pwdReset struct {
  53. Code string `json:"code"`
  54. Password string `json:"password"`
  55. }
  56. type baseProfile struct {
  57. Email string `json:"email,omitempty"`
  58. Description string `json:"description,omitempty"`
  59. AllowAPIKeyAuth bool `json:"allow_api_key_auth"`
  60. }
  61. type adminProfile struct {
  62. baseProfile
  63. }
  64. type userProfile struct {
  65. baseProfile
  66. AdditionalEmails []string `json:"additional_emails,omitempty"`
  67. PublicKeys []string `json:"public_keys,omitempty"`
  68. TLSCerts []string `json:"tls_certs,omitempty"`
  69. }
  70. func sendAPIResponse(w http.ResponseWriter, r *http.Request, err error, message string, code int) {
  71. var errorString string
  72. if errors.Is(err, util.ErrNotFound) {
  73. errorString = http.StatusText(http.StatusNotFound)
  74. } else if err != nil {
  75. errorString = err.Error()
  76. }
  77. resp := apiResponse{
  78. Error: errorString,
  79. Message: message,
  80. }
  81. ctx := context.WithValue(r.Context(), render.StatusCtxKey, code)
  82. render.JSON(w, r.WithContext(ctx), resp)
  83. }
  84. func getRespStatus(err error) int {
  85. if errors.Is(err, util.ErrValidation) {
  86. return http.StatusBadRequest
  87. }
  88. if errors.Is(err, util.ErrMethodDisabled) {
  89. return http.StatusForbidden
  90. }
  91. if errors.Is(err, util.ErrNotFound) {
  92. return http.StatusNotFound
  93. }
  94. if errors.Is(err, fs.ErrNotExist) {
  95. return http.StatusBadRequest
  96. }
  97. if errors.Is(err, fs.ErrPermission) || errors.Is(err, dataprovider.ErrLoginNotAllowedFromIP) {
  98. return http.StatusForbidden
  99. }
  100. if errors.Is(err, plugin.ErrNoSearcher) || errors.Is(err, dataprovider.ErrNotImplemented) {
  101. return http.StatusNotImplemented
  102. }
  103. if errors.Is(err, dataprovider.ErrDuplicatedKey) || errors.Is(err, dataprovider.ErrForeignKeyViolated) {
  104. return http.StatusConflict
  105. }
  106. return http.StatusInternalServerError
  107. }
  108. // mappig between fs errors for HTTP protocol and HTTP response status codes
  109. func getMappedStatusCode(err error) int {
  110. var statusCode int
  111. switch {
  112. case errors.Is(err, fs.ErrPermission):
  113. statusCode = http.StatusForbidden
  114. case errors.Is(err, common.ErrReadQuotaExceeded):
  115. statusCode = http.StatusForbidden
  116. case errors.Is(err, fs.ErrNotExist):
  117. statusCode = http.StatusNotFound
  118. case errors.Is(err, common.ErrQuotaExceeded):
  119. statusCode = http.StatusRequestEntityTooLarge
  120. case errors.Is(err, common.ErrOpUnsupported):
  121. statusCode = http.StatusBadRequest
  122. default:
  123. if _, ok := err.(*http.MaxBytesError); ok {
  124. statusCode = http.StatusRequestEntityTooLarge
  125. } else {
  126. statusCode = http.StatusInternalServerError
  127. }
  128. }
  129. return statusCode
  130. }
  131. func getURLParam(r *http.Request, key string) string {
  132. v := chi.URLParam(r, key)
  133. unescaped, err := url.PathUnescape(v)
  134. if err != nil {
  135. return v
  136. }
  137. return unescaped
  138. }
  139. func getURLPath(r *http.Request) string {
  140. rctx := chi.RouteContext(r.Context())
  141. if rctx != nil && rctx.RoutePath != "" {
  142. return rctx.RoutePath
  143. }
  144. return r.URL.Path
  145. }
  146. func getCommaSeparatedQueryParam(r *http.Request, key string) []string {
  147. var result []string
  148. for val := range strings.SplitSeq(r.URL.Query().Get(key), ",") {
  149. val = strings.TrimSpace(val)
  150. if val != "" {
  151. result = append(result, val)
  152. }
  153. }
  154. return util.RemoveDuplicates(result, false)
  155. }
  156. func getBoolQueryParam(r *http.Request, param string) bool {
  157. return r.URL.Query().Get(param) == "true"
  158. }
  159. func getActiveConnections(w http.ResponseWriter, r *http.Request) {
  160. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  161. claims, err := getTokenClaims(r)
  162. if err != nil || claims.Username == "" {
  163. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  164. return
  165. }
  166. stats := common.Connections.GetStats(claims.Role)
  167. if claims.NodeID == "" {
  168. stats = append(stats, getNodesConnections(claims.Username, claims.Role)...)
  169. }
  170. render.JSON(w, r, stats)
  171. }
  172. func handleCloseConnection(w http.ResponseWriter, r *http.Request) {
  173. r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
  174. claims, err := getTokenClaims(r)
  175. if err != nil || claims.Username == "" {
  176. sendAPIResponse(w, r, err, "Invalid token claims", http.StatusBadRequest)
  177. return
  178. }
  179. connectionID := getURLParam(r, "connectionID")
  180. if connectionID == "" {
  181. sendAPIResponse(w, r, nil, "connectionID is mandatory", http.StatusBadRequest)
  182. return
  183. }
  184. node := r.URL.Query().Get("node")
  185. if node == "" || node == dataprovider.GetNodeName() {
  186. if common.Connections.Close(connectionID, claims.Role) {
  187. sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
  188. } else {
  189. sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
  190. }
  191. return
  192. }
  193. n, err := dataprovider.GetNodeByName(node)
  194. if err != nil {
  195. logger.Warn(logSender, "", "unable to get node with name %q: %v", node, err)
  196. status := getRespStatus(err)
  197. sendAPIResponse(w, r, nil, http.StatusText(status), status)
  198. return
  199. }
  200. perms := []string{dataprovider.PermAdminCloseConnections}
  201. uri := fmt.Sprintf("%s/%s", activeConnectionsPath, connectionID)
  202. if err := n.SendDeleteRequest(claims.Username, claims.Role, uri, perms); err != nil {
  203. logger.Warn(logSender, "", "unable to delete connection id %q from node %q: %v", connectionID, n.Name, err)
  204. sendAPIResponse(w, r, nil, "Not Found", http.StatusNotFound)
  205. return
  206. }
  207. sendAPIResponse(w, r, nil, "Connection closed", http.StatusOK)
  208. }
  209. // getNodesConnections returns the active connections from other nodes.
  210. // Errors are silently ignored
  211. func getNodesConnections(admin, role string) []common.ConnectionStatus {
  212. nodes, err := dataprovider.GetNodes()
  213. if err != nil || len(nodes) == 0 {
  214. return nil
  215. }
  216. var results []common.ConnectionStatus
  217. var mu sync.Mutex
  218. var wg sync.WaitGroup
  219. for _, n := range nodes {
  220. wg.Add(1)
  221. go func(node dataprovider.Node) {
  222. defer wg.Done()
  223. var stats []common.ConnectionStatus
  224. perms := []string{dataprovider.PermAdminViewConnections}
  225. if err := node.SendGetRequest(admin, role, activeConnectionsPath, perms, &stats); err != nil {
  226. logger.Warn(logSender, "", "unable to get connections from node %s: %v", node.Name, err)
  227. return
  228. }
  229. mu.Lock()
  230. results = append(results, stats...)
  231. mu.Unlock()
  232. }(n)
  233. }
  234. wg.Wait()
  235. return results
  236. }
  237. func getSearchFilters(w http.ResponseWriter, r *http.Request) (int, int, string, error) {
  238. var err error
  239. limit := 100
  240. offset := 0
  241. order := dataprovider.OrderASC
  242. if _, ok := r.URL.Query()["limit"]; ok {
  243. limit, err = strconv.Atoi(r.URL.Query().Get("limit"))
  244. if err != nil {
  245. err = errors.New("invalid limit")
  246. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  247. return limit, offset, order, err
  248. }
  249. if limit > 500 {
  250. limit = 500
  251. }
  252. }
  253. if _, ok := r.URL.Query()["offset"]; ok {
  254. offset, err = strconv.Atoi(r.URL.Query().Get("offset"))
  255. if err != nil {
  256. err = errors.New("invalid offset")
  257. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  258. return limit, offset, order, err
  259. }
  260. }
  261. if _, ok := r.URL.Query()["order"]; ok {
  262. order = r.URL.Query().Get("order")
  263. if order != dataprovider.OrderASC && order != dataprovider.OrderDESC {
  264. err = errors.New("invalid order")
  265. sendAPIResponse(w, r, err, "", http.StatusBadRequest)
  266. return limit, offset, order, err
  267. }
  268. }
  269. return limit, offset, order, err
  270. }
  271. func renderAPIDirContents(w http.ResponseWriter, lister vfs.DirLister, omitNonRegularFiles bool) {
  272. defer lister.Close()
  273. dataGetter := func(limit, _ int) ([]byte, int, error) {
  274. contents, err := lister.Next(limit)
  275. if errors.Is(err, io.EOF) {
  276. err = nil
  277. }
  278. if err != nil {
  279. return nil, 0, err
  280. }
  281. results := make([]map[string]any, 0, len(contents))
  282. for _, info := range contents {
  283. if omitNonRegularFiles && !info.Mode().IsDir() && !info.Mode().IsRegular() {
  284. continue
  285. }
  286. res := make(map[string]any)
  287. res["name"] = info.Name()
  288. if info.Mode().IsRegular() {
  289. res["size"] = info.Size()
  290. }
  291. res["mode"] = info.Mode()
  292. res["last_modified"] = info.ModTime().UTC().Format(time.RFC3339)
  293. results = append(results, res)
  294. }
  295. data, err := json.Marshal(results)
  296. count := limit
  297. if len(results) == 0 {
  298. count = 0
  299. }
  300. return data, count, err
  301. }
  302. streamJSONArray(w, defaultQueryLimit, dataGetter)
  303. }
  304. func streamData(w io.Writer, data []byte) {
  305. b := bytes.NewBuffer(data)
  306. _, err := io.CopyN(w, b, int64(len(data)))
  307. if err != nil {
  308. panic(http.ErrAbortHandler)
  309. }
  310. }
  311. func streamJSONArray(w http.ResponseWriter, chunkSize int, dataGetter func(limit, offset int) ([]byte, int, error)) {
  312. w.Header().Set("Content-Type", "application/json")
  313. w.Header().Set("Accept-Ranges", "none")
  314. w.WriteHeader(http.StatusOK)
  315. streamData(w, []byte("["))
  316. offset := 0
  317. for {
  318. data, count, err := dataGetter(chunkSize, offset)
  319. if err != nil {
  320. panic(http.ErrAbortHandler)
  321. }
  322. if count == 0 {
  323. break
  324. }
  325. if offset > 0 {
  326. streamData(w, []byte(","))
  327. }
  328. streamData(w, data[1:len(data)-1])
  329. if count < chunkSize {
  330. break
  331. }
  332. offset += count
  333. }
  334. streamData(w, []byte("]"))
  335. }
  336. func renderPNGImage(w http.ResponseWriter, r *http.Request, b []byte) {
  337. if len(b) == 0 {
  338. ctx := context.WithValue(r.Context(), render.StatusCtxKey, http.StatusNotFound)
  339. render.PlainText(w, r.WithContext(ctx), http.StatusText(http.StatusNotFound))
  340. return
  341. }
  342. w.Header().Set("Content-Type", "image/png")
  343. streamData(w, b)
  344. }
  345. func getCompressedFileName(username string, files []string) string {
  346. if len(files) == 1 {
  347. name := path.Base(files[0])
  348. return fmt.Sprintf("%s-%s.zip", username, strings.TrimSuffix(name, path.Ext(name)))
  349. }
  350. return fmt.Sprintf("%s-download.zip", username)
  351. }
  352. func renderCompressedFiles(w http.ResponseWriter, conn *Connection, baseDir string, files []string,
  353. share *dataprovider.Share,
  354. ) {
  355. conn.User.CheckFsRoot(conn.ID) //nolint:errcheck
  356. w.Header().Set("Content-Type", "application/zip")
  357. w.Header().Set("Accept-Ranges", "none")
  358. w.Header().Set("Content-Transfer-Encoding", "binary")
  359. w.WriteHeader(http.StatusOK)
  360. wr := zip.NewWriter(w)
  361. for _, file := range files {
  362. fullPath := util.CleanPath(path.Join(baseDir, file))
  363. if err := addZipEntry(wr, conn, fullPath, baseDir, nil, 0); err != nil {
  364. if share != nil {
  365. dataprovider.UpdateShareLastUse(share, -1) //nolint:errcheck
  366. }
  367. panic(http.ErrAbortHandler)
  368. }
  369. }
  370. if err := wr.Close(); err != nil {
  371. conn.Log(logger.LevelError, "unable to close zip file: %v", err)
  372. if share != nil {
  373. dataprovider.UpdateShareLastUse(share, -1) //nolint:errcheck
  374. }
  375. panic(http.ErrAbortHandler)
  376. }
  377. }
  378. func addZipEntry(wr *zip.Writer, conn *Connection, entryPath, baseDir string, info os.FileInfo, recursion int) error {
  379. if recursion >= util.MaxRecursion {
  380. conn.Log(logger.LevelDebug, "unable to add zip entry %q, recursion too depth: %d", entryPath, recursion)
  381. return util.ErrRecursionTooDeep
  382. }
  383. recursion++
  384. var err error
  385. if info == nil {
  386. info, err = conn.Stat(entryPath, 1)
  387. if err != nil {
  388. conn.Log(logger.LevelDebug, "unable to add zip entry %q, stat error: %v", entryPath, err)
  389. return err
  390. }
  391. }
  392. entryName, err := getZipEntryName(entryPath, baseDir)
  393. if err != nil {
  394. conn.Log(logger.LevelError, "unable to get zip entry name: %v", err)
  395. return err
  396. }
  397. if info.IsDir() {
  398. _, err = wr.CreateHeader(&zip.FileHeader{
  399. Name: entryName + "/",
  400. Method: zip.Deflate,
  401. Modified: info.ModTime(),
  402. })
  403. if err != nil {
  404. conn.Log(logger.LevelError, "unable to create zip entry %q: %v", entryPath, err)
  405. return err
  406. }
  407. lister, err := conn.ReadDir(entryPath)
  408. if err != nil {
  409. conn.Log(logger.LevelDebug, "unable to add zip entry %q, get list dir error: %v", entryPath, err)
  410. return err
  411. }
  412. defer lister.Close()
  413. for {
  414. contents, err := lister.Next(vfs.ListerBatchSize)
  415. finished := errors.Is(err, io.EOF)
  416. if err != nil && !finished {
  417. return err
  418. }
  419. for _, info := range contents {
  420. fullPath := util.CleanPath(path.Join(entryPath, info.Name()))
  421. if err := addZipEntry(wr, conn, fullPath, baseDir, info, recursion); err != nil {
  422. return err
  423. }
  424. }
  425. if finished {
  426. return nil
  427. }
  428. }
  429. }
  430. if !info.Mode().IsRegular() {
  431. // we only allow regular files
  432. conn.Log(logger.LevelInfo, "skipping zip entry for non regular file %q", entryPath)
  433. return nil
  434. }
  435. return addFileToZipEntry(wr, conn, entryPath, entryName, info)
  436. }
  437. func addFileToZipEntry(wr *zip.Writer, conn *Connection, entryPath, entryName string, info os.FileInfo) error {
  438. reader, err := conn.getFileReader(entryPath, 0, http.MethodGet)
  439. if err != nil {
  440. conn.Log(logger.LevelDebug, "unable to add zip entry %q, cannot open file: %v", entryPath, err)
  441. return err
  442. }
  443. defer reader.Close()
  444. f, err := wr.CreateHeader(&zip.FileHeader{
  445. Name: entryName,
  446. Method: zip.Deflate,
  447. Modified: info.ModTime(),
  448. })
  449. if err != nil {
  450. conn.Log(logger.LevelError, "unable to create zip entry %q: %v", entryPath, err)
  451. return err
  452. }
  453. _, err = io.Copy(f, reader)
  454. return err
  455. }
  456. func getZipEntryName(entryPath, baseDir string) (string, error) {
  457. if !strings.HasPrefix(entryPath, baseDir) {
  458. return "", fmt.Errorf("entry path %q is outside base dir %q", entryPath, baseDir)
  459. }
  460. entryPath = strings.TrimPrefix(entryPath, baseDir)
  461. return strings.TrimPrefix(entryPath, "/"), nil
  462. }
  463. func checkDownloadFileFromShare(share *dataprovider.Share, info os.FileInfo) error {
  464. if share != nil && !info.Mode().IsRegular() {
  465. return util.NewValidationError("non regular files are not supported for shares")
  466. }
  467. return nil
  468. }
  469. func downloadFile(w http.ResponseWriter, r *http.Request, connection *Connection, name string,
  470. info os.FileInfo, inline bool, share *dataprovider.Share,
  471. ) (int, error) {
  472. connection.User.CheckFsRoot(connection.ID) //nolint:errcheck
  473. err := checkDownloadFileFromShare(share, info)
  474. if err != nil {
  475. return http.StatusBadRequest, err
  476. }
  477. rangeHeader := r.Header.Get("Range")
  478. if rangeHeader != "" && checkIfRange(r, info.ModTime()) == condFalse {
  479. rangeHeader = ""
  480. }
  481. offset := int64(0)
  482. size := info.Size()
  483. responseStatus := http.StatusOK
  484. if strings.HasPrefix(rangeHeader, "bytes=") {
  485. if strings.Contains(rangeHeader, ",") {
  486. return http.StatusRequestedRangeNotSatisfiable, fmt.Errorf("unsupported range %q", rangeHeader)
  487. }
  488. offset, size, err = parseRangeRequest(rangeHeader[6:], size)
  489. if err != nil {
  490. return http.StatusRequestedRangeNotSatisfiable, err
  491. }
  492. responseStatus = http.StatusPartialContent
  493. }
  494. reader, err := connection.getFileReader(name, offset, r.Method)
  495. if err != nil {
  496. return getMappedStatusCode(err), fmt.Errorf("unable to read file %q: %v", name, err)
  497. }
  498. defer reader.Close()
  499. w.Header().Set("Last-Modified", info.ModTime().UTC().Format(http.TimeFormat))
  500. if checkPreconditions(w, r, info.ModTime()) {
  501. return 0, fmt.Errorf("%v", http.StatusText(http.StatusPreconditionFailed))
  502. }
  503. ctype := mime.TypeByExtension(path.Ext(name))
  504. if ctype == "" {
  505. ctype = "application/octet-stream"
  506. }
  507. if responseStatus == http.StatusPartialContent {
  508. w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", offset, offset+size-1, info.Size()))
  509. }
  510. w.Header().Set("Content-Length", strconv.FormatInt(size, 10))
  511. w.Header().Set("Content-Type", ctype)
  512. if !inline {
  513. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%q", path.Base(name)))
  514. }
  515. w.Header().Set("Accept-Ranges", "bytes")
  516. w.WriteHeader(responseStatus)
  517. if r.Method != http.MethodHead {
  518. _, err = io.CopyN(w, reader, size)
  519. if err != nil {
  520. if share != nil {
  521. dataprovider.UpdateShareLastUse(share, -1) //nolint:errcheck
  522. }
  523. connection.Log(logger.LevelDebug, "error reading file to download: %v", err)
  524. panic(http.ErrAbortHandler)
  525. }
  526. }
  527. return http.StatusOK, nil
  528. }
  529. func checkPreconditions(w http.ResponseWriter, r *http.Request, modtime time.Time) bool {
  530. if checkIfUnmodifiedSince(r, modtime) == condFalse {
  531. w.WriteHeader(http.StatusPreconditionFailed)
  532. return true
  533. }
  534. if checkIfModifiedSince(r, modtime) == condFalse {
  535. w.WriteHeader(http.StatusNotModified)
  536. return true
  537. }
  538. return false
  539. }
  540. func checkIfUnmodifiedSince(r *http.Request, modtime time.Time) condResult {
  541. ius := r.Header.Get("If-Unmodified-Since")
  542. if ius == "" || isZeroTime(modtime) {
  543. return condNone
  544. }
  545. t, err := http.ParseTime(ius)
  546. if err != nil {
  547. return condNone
  548. }
  549. // The Last-Modified header truncates sub-second precision so
  550. // the modtime needs to be truncated too.
  551. modtime = modtime.Truncate(time.Second)
  552. if modtime.Before(t) || modtime.Equal(t) {
  553. return condTrue
  554. }
  555. return condFalse
  556. }
  557. func checkIfModifiedSince(r *http.Request, modtime time.Time) condResult {
  558. if r.Method != http.MethodGet && r.Method != http.MethodHead {
  559. return condNone
  560. }
  561. ims := r.Header.Get("If-Modified-Since")
  562. if ims == "" || isZeroTime(modtime) {
  563. return condNone
  564. }
  565. t, err := http.ParseTime(ims)
  566. if err != nil {
  567. return condNone
  568. }
  569. // The Last-Modified header truncates sub-second precision so
  570. // the modtime needs to be truncated too.
  571. modtime = modtime.Truncate(time.Second)
  572. if modtime.Before(t) || modtime.Equal(t) {
  573. return condFalse
  574. }
  575. return condTrue
  576. }
  577. func checkIfRange(r *http.Request, modtime time.Time) condResult {
  578. if r.Method != http.MethodGet && r.Method != http.MethodHead {
  579. return condNone
  580. }
  581. ir := r.Header.Get("If-Range")
  582. if ir == "" {
  583. return condNone
  584. }
  585. if modtime.IsZero() {
  586. return condFalse
  587. }
  588. t, err := http.ParseTime(ir)
  589. if err != nil {
  590. return condFalse
  591. }
  592. if modtime.Unix() == t.Unix() {
  593. return condTrue
  594. }
  595. return condFalse
  596. }
  597. func parseRangeRequest(bytesRange string, size int64) (int64, int64, error) {
  598. var start, end int64
  599. var err error
  600. values := strings.Split(bytesRange, "-")
  601. if values[0] == "" {
  602. start = -1
  603. } else {
  604. start, err = strconv.ParseInt(values[0], 10, 64)
  605. if err != nil {
  606. return start, size, err
  607. }
  608. }
  609. if len(values) >= 2 {
  610. if values[1] != "" {
  611. end, err = strconv.ParseInt(values[1], 10, 64)
  612. if err != nil {
  613. return start, size, err
  614. }
  615. if end >= size {
  616. end = size - 1
  617. }
  618. }
  619. }
  620. if start == -1 && end == 0 {
  621. return 0, 0, fmt.Errorf("unsupported range %q", bytesRange)
  622. }
  623. if end > 0 {
  624. if start == -1 {
  625. // we have something like -500
  626. start = size - end
  627. size = end
  628. // start cannot be < 0 here, we did end = size -1 above
  629. } else {
  630. // we have something like 500-600
  631. size = end - start + 1
  632. if size < 0 {
  633. return 0, 0, fmt.Errorf("unacceptable range %q", bytesRange)
  634. }
  635. }
  636. return start, size, nil
  637. }
  638. // we have something like 500-
  639. size -= start
  640. if size < 0 {
  641. return 0, 0, fmt.Errorf("unacceptable range %q", bytesRange)
  642. }
  643. return start, size, err
  644. }
  645. func handleDefenderEventLoginFailed(ipAddr string, err error) error {
  646. event := common.HostEventLoginFailed
  647. if errors.Is(err, util.ErrNotFound) {
  648. event = common.HostEventUserNotFound
  649. err = dataprovider.ErrInvalidCredentials
  650. }
  651. common.AddDefenderEvent(ipAddr, common.ProtocolHTTP, event)
  652. common.DelayLogin(err)
  653. return err
  654. }
  655. func updateLoginMetrics(user *dataprovider.User, loginMethod, ip string, err error, r *http.Request) {
  656. metric.AddLoginAttempt(loginMethod)
  657. var protocol string
  658. switch loginMethod {
  659. case dataprovider.LoginMethodIDP:
  660. protocol = common.ProtocolOIDC
  661. default:
  662. protocol = common.ProtocolHTTP
  663. }
  664. if err == nil {
  665. logger.LoginLog(user.Username, ip, loginMethod, protocol, "", r.UserAgent(), r.TLS != nil, "")
  666. plugin.Handler.NotifyLogEvent(notifier.LogEventTypeLoginOK, protocol, user.Username, ip, "", nil)
  667. common.DelayLogin(nil)
  668. } else if err != common.ErrInternalFailure && err != common.ErrNoCredentials {
  669. logger.ConnectionFailedLog(user.Username, ip, loginMethod, protocol, err.Error())
  670. err = handleDefenderEventLoginFailed(ip, err)
  671. logEv := notifier.LogEventTypeLoginFailed
  672. if errors.Is(err, util.ErrNotFound) {
  673. logEv = notifier.LogEventTypeLoginNoUser
  674. }
  675. plugin.Handler.NotifyLogEvent(logEv, protocol, user.Username, ip, "", err)
  676. }
  677. metric.AddLoginResult(loginMethod, err)
  678. dataprovider.ExecutePostLoginHook(user, loginMethod, ip, protocol, err)
  679. }
  680. func checkHTTPClientUser(user *dataprovider.User, r *http.Request, connectionID string, checkSessions, isOIDCLogin bool) error {
  681. if slices.Contains(user.Filters.DeniedProtocols, common.ProtocolHTTP) {
  682. logger.Info(logSender, connectionID, "cannot login user %q, protocol HTTP is not allowed", user.Username)
  683. return util.NewI18nError(
  684. fmt.Errorf("protocol HTTP is not allowed for user %q", user.Username),
  685. util.I18nErrorProtocolForbidden,
  686. )
  687. }
  688. if !isLoggedInWithOIDC(r) && !isOIDCLogin && !user.IsLoginMethodAllowed(dataprovider.LoginMethodPassword, common.ProtocolHTTP) {
  689. logger.Info(logSender, connectionID, "cannot login user %q, password login method is not allowed", user.Username)
  690. return util.NewI18nError(
  691. fmt.Errorf("login method password is not allowed for user %q", user.Username),
  692. util.I18nErrorPwdLoginForbidden,
  693. )
  694. }
  695. if checkSessions && user.MaxSessions > 0 {
  696. activeSessions := common.Connections.GetActiveSessions(user.Username)
  697. if activeSessions >= user.MaxSessions {
  698. logger.Info(logSender, connectionID, "authentication refused for user: %q, too many open sessions: %v/%v", user.Username,
  699. activeSessions, user.MaxSessions)
  700. return util.NewI18nError(fmt.Errorf("too many open sessions: %v", activeSessions), util.I18nError429Message)
  701. }
  702. }
  703. if !user.IsLoginFromAddrAllowed(r.RemoteAddr) {
  704. logger.Info(logSender, connectionID, "cannot login user %q, remote address is not allowed: %v", user.Username, r.RemoteAddr)
  705. return util.NewI18nError(
  706. fmt.Errorf("login for user %q is not allowed from this address: %v", user.Username, r.RemoteAddr),
  707. util.I18nErrorIPForbidden,
  708. )
  709. }
  710. return nil
  711. }
  712. func getActiveAdmin(username, ipAddr string) (dataprovider.Admin, error) {
  713. admin, err := dataprovider.AdminExists(username)
  714. if err != nil {
  715. return admin, err
  716. }
  717. if err := admin.CanLogin(ipAddr); err != nil {
  718. return admin, util.NewRecordNotFoundError(fmt.Sprintf("admin %q cannot login: %v", username, err))
  719. }
  720. return admin, nil
  721. }
  722. func getActiveUser(username string, r *http.Request) (dataprovider.User, error) {
  723. user, err := dataprovider.GetUserWithGroupSettings(username, "")
  724. if err != nil {
  725. return user, err
  726. }
  727. if err := user.CheckLoginConditions(); err != nil {
  728. return user, util.NewRecordNotFoundError(fmt.Sprintf("user %q cannot login: %v", username, err))
  729. }
  730. if err := checkHTTPClientUser(&user, r, xid.New().String(), false, false); err != nil {
  731. return user, util.NewRecordNotFoundError(fmt.Sprintf("user %q cannot login: %v", username, err))
  732. }
  733. return user, nil
  734. }
  735. func handleForgotPassword(r *http.Request, username string, isAdmin bool) error {
  736. var emails []string
  737. var subject string
  738. var err error
  739. var admin dataprovider.Admin
  740. var user dataprovider.User
  741. if username == "" {
  742. return util.NewI18nError(util.NewValidationError("username is mandatory"), util.I18nErrorUsernameRequired)
  743. }
  744. if isAdmin {
  745. admin, err = getActiveAdmin(username, util.GetIPFromRemoteAddress(r.RemoteAddr))
  746. if admin.Email != "" {
  747. emails = []string{admin.Email}
  748. }
  749. subject = fmt.Sprintf("Email Verification Code for admin %q", username)
  750. } else {
  751. user, err = getActiveUser(username, r)
  752. emails = user.GetEmailAddresses()
  753. subject = fmt.Sprintf("Email Verification Code for user %q", username)
  754. if err == nil {
  755. if !isUserAllowedToResetPassword(r, &user) {
  756. return util.NewI18nError(
  757. util.NewValidationError("you are not allowed to reset your password"),
  758. util.I18nErrorPwdResetForbidded,
  759. )
  760. }
  761. }
  762. }
  763. if err != nil {
  764. if errors.Is(err, util.ErrNotFound) {
  765. handleDefenderEventLoginFailed(util.GetIPFromRemoteAddress(r.RemoteAddr), err) //nolint:errcheck
  766. logger.Debug(logSender, middleware.GetReqID(r.Context()),
  767. "username %q does not exists or cannot login, reset password request silently ignored, is admin? %t, err: %v",
  768. username, isAdmin, err)
  769. return nil
  770. }
  771. return util.NewI18nError(util.NewGenericError("Error retrieving your account, please try again later"), util.I18nErrorGetUser)
  772. }
  773. if len(emails) == 0 {
  774. return util.NewI18nError(
  775. util.NewValidationError("Your account does not have an email address, it is not possible to reset your password by sending an email verification code"),
  776. util.I18nErrorPwdResetNoEmail,
  777. )
  778. }
  779. c := newResetCode(username, isAdmin)
  780. body := new(bytes.Buffer)
  781. data := make(map[string]string)
  782. data["Code"] = c.Code
  783. if err := smtp.RenderPasswordResetTemplate(body, data); err != nil {
  784. logger.Warn(logSender, middleware.GetReqID(r.Context()), "unable to render password reset template: %v", err)
  785. return util.NewGenericError("Unable to render password reset template")
  786. }
  787. startTime := time.Now()
  788. if err := smtp.SendEmail(emails, nil, subject, body.String(), smtp.EmailContentTypeTextHTML); err != nil {
  789. logger.Warn(logSender, middleware.GetReqID(r.Context()), "unable to send password reset code via email: %v, elapsed: %v",
  790. err, time.Since(startTime))
  791. return util.NewI18nError(
  792. util.NewGenericError(fmt.Sprintf("Error sending confirmation code via email: %v", err)),
  793. util.I18nErrorPwdResetSendEmail,
  794. )
  795. }
  796. logger.Debug(logSender, middleware.GetReqID(r.Context()), "reset code sent via email to %q, emails: %+v, is admin? %v, elapsed: %v",
  797. username, emails, isAdmin, time.Since(startTime))
  798. return resetCodesMgr.Add(c)
  799. }
  800. func handleResetPassword(r *http.Request, code, newPassword, confirmPassword string, isAdmin bool) (
  801. *dataprovider.Admin, *dataprovider.User, error,
  802. ) {
  803. var admin dataprovider.Admin
  804. var user dataprovider.User
  805. var err error
  806. if newPassword == "" {
  807. return &admin, &user, util.NewValidationError("please set a password")
  808. }
  809. if code == "" {
  810. return &admin, &user, util.NewValidationError("please set a confirmation code")
  811. }
  812. if newPassword != confirmPassword {
  813. return &admin, &user, util.NewI18nError(errors.New("the two password fields do not match"), util.I18nErrorChangePwdNoMatch)
  814. }
  815. ipAddr := util.GetIPFromRemoteAddress(r.RemoteAddr)
  816. resetCode, err := resetCodesMgr.Get(code)
  817. if err != nil {
  818. handleDefenderEventLoginFailed(ipAddr, dataprovider.ErrInvalidCredentials) //nolint:errcheck
  819. return &admin, &user, util.NewValidationError("confirmation code not found")
  820. }
  821. if resetCode.IsAdmin != isAdmin {
  822. return &admin, &user, util.NewValidationError("invalid confirmation code")
  823. }
  824. if isAdmin {
  825. admin, err = getActiveAdmin(resetCode.Username, ipAddr)
  826. if err != nil {
  827. return &admin, &user, util.NewValidationError("unable to associate the confirmation code with an existing admin")
  828. }
  829. admin.Password = newPassword
  830. admin.Filters.RequirePasswordChange = false
  831. err = dataprovider.UpdateAdmin(&admin, dataprovider.ActionExecutorSelf, ipAddr, admin.Role)
  832. if err != nil {
  833. return &admin, &user, util.NewGenericError(fmt.Sprintf("unable to set the new password: %v", err))
  834. }
  835. err = resetCodesMgr.Delete(code)
  836. return &admin, &user, err
  837. }
  838. user, err = getActiveUser(resetCode.Username, r)
  839. if err != nil {
  840. return &admin, &user, util.NewValidationError("Unable to associate the confirmation code with an existing user")
  841. }
  842. if !isUserAllowedToResetPassword(r, &user) {
  843. return &admin, &user, util.NewI18nError(
  844. util.NewValidationError("you are not allowed to reset your password"),
  845. util.I18nErrorPwdResetForbidded,
  846. )
  847. }
  848. err = dataprovider.UpdateUserPassword(user.Username, newPassword, dataprovider.ActionExecutorSelf,
  849. util.GetIPFromRemoteAddress(r.RemoteAddr), user.Role)
  850. if err == nil {
  851. err = resetCodesMgr.Delete(code)
  852. }
  853. user.LastPasswordChange = util.GetTimeAsMsSinceEpoch(time.Now())
  854. user.Filters.RequirePasswordChange = false
  855. return &admin, &user, err
  856. }
  857. func isUserAllowedToResetPassword(r *http.Request, user *dataprovider.User) bool {
  858. if !user.CanResetPassword() {
  859. return false
  860. }
  861. if slices.Contains(user.Filters.DeniedProtocols, common.ProtocolHTTP) {
  862. return false
  863. }
  864. if !user.IsLoginMethodAllowed(dataprovider.LoginMethodPassword, common.ProtocolHTTP) {
  865. return false
  866. }
  867. if !user.IsLoginFromAddrAllowed(r.RemoteAddr) {
  868. return false
  869. }
  870. return true
  871. }
  872. func getProtocolFromRequest(r *http.Request) string {
  873. if isLoggedInWithOIDC(r) {
  874. return common.ProtocolOIDC
  875. }
  876. return common.ProtocolHTTP
  877. }
  878. func hideConfidentialData(claims *jwtTokenClaims, r *http.Request) bool {
  879. if !claims.hasPerm(dataprovider.PermAdminAny) {
  880. return true
  881. }
  882. return r.URL.Query().Get("confidential_data") != "1"
  883. }
  884. func responseControllerDeadlines(rc *http.ResponseController, read, write time.Time) {
  885. if err := rc.SetReadDeadline(read); err != nil {
  886. logger.Error(logSender, "", "unable to set read timeout to %s: %v", read, err)
  887. }
  888. if err := rc.SetWriteDeadline(write); err != nil {
  889. logger.Error(logSender, "", "unable to set write timeout to %s: %v", write, err)
  890. }
  891. }