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

serv.go 9.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead.
  5. package private
  6. import (
  7. "fmt"
  8. "net/http"
  9. "strings"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/private"
  13. "code.gitea.io/gitea/modules/setting"
  14. macaron "gopkg.in/macaron.v1"
  15. )
  16. // ServNoCommand returns information about the provided keyid
  17. func ServNoCommand(ctx *macaron.Context) {
  18. keyID := ctx.ParamsInt64(":keyid")
  19. if keyID <= 0 {
  20. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  21. "err": fmt.Sprintf("Bad key id: %d", keyID),
  22. })
  23. }
  24. results := private.KeyAndOwner{}
  25. key, err := models.GetPublicKeyByID(keyID)
  26. if err != nil {
  27. if models.IsErrKeyNotExist(err) {
  28. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  29. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  30. })
  31. return
  32. }
  33. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  34. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  35. "err": err.Error(),
  36. })
  37. return
  38. }
  39. results.Key = key
  40. if key.Type == models.KeyTypeUser {
  41. user, err := models.GetUserByID(key.OwnerID)
  42. if err != nil {
  43. if models.IsErrUserNotExist(err) {
  44. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  45. "err": fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID),
  46. })
  47. return
  48. }
  49. log.Error("Unable to get owner with id: %d for public key: %d Error: %v", key.OwnerID, keyID, err)
  50. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  51. "err": err.Error(),
  52. })
  53. return
  54. }
  55. results.Owner = user
  56. }
  57. ctx.JSON(http.StatusOK, &results)
  58. return
  59. }
  60. // ServCommand returns information about the provided keyid
  61. func ServCommand(ctx *macaron.Context) {
  62. // Although we provide the verbs we don't need them at present they're just for logging purposes
  63. keyID := ctx.ParamsInt64(":keyid")
  64. ownerName := ctx.Params(":owner")
  65. repoName := ctx.Params(":repo")
  66. mode := models.AccessMode(ctx.QueryInt("mode"))
  67. // Set the basic parts of the results to return
  68. results := private.ServCommandResults{
  69. RepoName: repoName,
  70. OwnerName: ownerName,
  71. KeyID: keyID,
  72. }
  73. // Now because we're not translating things properly let's just default some Engish strings here
  74. modeString := "read"
  75. if mode > models.AccessModeRead {
  76. modeString = "write to"
  77. }
  78. // The default unit we're trying to look at is code
  79. unitType := models.UnitTypeCode
  80. // Unless we're a wiki...
  81. if strings.HasSuffix(repoName, ".wiki") {
  82. // in which case we need to look at the wiki
  83. unitType = models.UnitTypeWiki
  84. // And we'd better munge the reponame and tell downstream we're looking at a wiki
  85. results.IsWiki = true
  86. results.RepoName = repoName[:len(repoName)-5]
  87. }
  88. // Now get the Repository and set the results section
  89. repo, err := models.GetRepositoryByOwnerAndName(results.OwnerName, results.RepoName)
  90. if err != nil {
  91. if models.IsErrRepoNotExist(err) {
  92. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  93. "results": results,
  94. "type": "ErrRepoNotExist",
  95. "err": fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
  96. })
  97. return
  98. }
  99. log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
  100. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  101. "results": results,
  102. "type": "InternalServerError",
  103. "err": fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
  104. })
  105. return
  106. }
  107. repo.OwnerName = ownerName
  108. results.RepoID = repo.ID
  109. // We can shortcut at this point if the repo is a mirror
  110. if mode > models.AccessModeRead && repo.IsMirror {
  111. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  112. "results": results,
  113. "type": "ErrMirrorReadOnly",
  114. "err": fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
  115. })
  116. return
  117. }
  118. // Get the Public Key represented by the keyID
  119. key, err := models.GetPublicKeyByID(keyID)
  120. if err != nil {
  121. if models.IsErrKeyNotExist(err) {
  122. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  123. "results": results,
  124. "type": "ErrKeyNotExist",
  125. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  126. })
  127. return
  128. }
  129. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  130. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  131. "results": results,
  132. "type": "InternalServerError",
  133. "err": fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
  134. })
  135. return
  136. }
  137. results.KeyName = key.Name
  138. results.KeyID = key.ID
  139. results.UserID = key.OwnerID
  140. // Deploy Keys have ownerID set to 0 therefore we can't use the owner
  141. // So now we need to check if the key is a deploy key
  142. // We'll keep hold of the deploy key here for permissions checking
  143. var deployKey *models.DeployKey
  144. var user *models.User
  145. if key.Type == models.KeyTypeDeploy {
  146. results.IsDeployKey = true
  147. var err error
  148. deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID)
  149. if err != nil {
  150. if models.IsErrDeployKeyNotExist(err) {
  151. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  152. "results": results,
  153. "type": "ErrDeployKeyNotExist",
  154. "err": fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  155. })
  156. return
  157. }
  158. log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err)
  159. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  160. "results": results,
  161. "type": "InternalServerError",
  162. "err": fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
  163. })
  164. return
  165. }
  166. results.KeyName = deployKey.Name
  167. // FIXME: Deploy keys aren't really the owner of the repo pushing changes
  168. // however we don't have good way of representing deploy keys in hook.go
  169. // so for now use the owner of the repository
  170. results.UserName = results.OwnerName
  171. results.UserID = repo.OwnerID
  172. } else {
  173. // Get the user represented by the Key
  174. var err error
  175. user, err = models.GetUserByID(key.OwnerID)
  176. if err != nil {
  177. if models.IsErrUserNotExist(err) {
  178. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  179. "results": results,
  180. "type": "ErrUserNotExist",
  181. "err": fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
  182. })
  183. return
  184. }
  185. log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err)
  186. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  187. "results": results,
  188. "type": "InternalServerError",
  189. "err": fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
  190. })
  191. return
  192. }
  193. results.UserName = user.Name
  194. }
  195. // Don't allow pushing if the repo is archived
  196. if mode > models.AccessModeRead && repo.IsArchived {
  197. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  198. "results": results,
  199. "type": "ErrRepoIsArchived",
  200. "err": fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
  201. })
  202. return
  203. }
  204. // Permissions checking:
  205. if mode > models.AccessModeRead || repo.IsPrivate || setting.Service.RequireSignInView {
  206. if key.Type == models.KeyTypeDeploy {
  207. if deployKey.Mode < mode {
  208. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  209. "results": results,
  210. "type": "ErrUnauthorized",
  211. "err": fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  212. })
  213. return
  214. }
  215. } else {
  216. perm, err := models.GetUserRepoPermission(repo, user)
  217. if err != nil {
  218. log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err)
  219. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  220. "results": results,
  221. "type": "InternalServerError",
  222. "err": fmt.Sprintf("Unable to get permissions for user %d:%s with key %d in %s/%s Error: %v", user.ID, user.Name, key.ID, results.OwnerName, results.RepoName, err),
  223. })
  224. return
  225. }
  226. userMode := perm.UnitAccessMode(unitType)
  227. if userMode < mode {
  228. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  229. "results": results,
  230. "type": "ErrUnauthorized",
  231. "err": fmt.Sprintf("User: %d:%s with Key: %d:%s is not authorized to %s %s/%s.", user.ID, user.Name, key.ID, key.Name, modeString, ownerName, repoName),
  232. })
  233. return
  234. }
  235. }
  236. }
  237. // Finally if we're trying to touch the wiki we should init it
  238. if results.IsWiki {
  239. if err = repo.InitWiki(); err != nil {
  240. log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err)
  241. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  242. "results": results,
  243. "type": "InternalServerError",
  244. "err": fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
  245. })
  246. return
  247. }
  248. }
  249. log.Debug("Serv Results:\nIsWiki: %t\nIsDeployKey: %t\nKeyID: %d\tKeyName: %s\nUserName: %s\nUserID: %d\nOwnerName: %s\nRepoName: %s\nRepoID: %d",
  250. results.IsWiki,
  251. results.IsDeployKey,
  252. results.KeyID,
  253. results.KeyName,
  254. results.UserName,
  255. results.UserID,
  256. results.OwnerName,
  257. results.RepoName,
  258. results.RepoID)
  259. ctx.JSON(http.StatusOK, results)
  260. // We will update the keys in a different call.
  261. return
  262. }