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

serv.go 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. repo_service "code.gitea.io/gitea/services/repository"
  15. "gitea.com/macaron/macaron"
  16. )
  17. // ServNoCommand returns information about the provided keyid
  18. func ServNoCommand(ctx *macaron.Context) {
  19. keyID := ctx.ParamsInt64(":keyid")
  20. if keyID <= 0 {
  21. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  22. "err": fmt.Sprintf("Bad key id: %d", keyID),
  23. })
  24. }
  25. results := private.KeyAndOwner{}
  26. key, err := models.GetPublicKeyByID(keyID)
  27. if err != nil {
  28. if models.IsErrKeyNotExist(err) {
  29. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  30. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  31. })
  32. return
  33. }
  34. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  35. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  36. "err": err.Error(),
  37. })
  38. return
  39. }
  40. results.Key = key
  41. if key.Type == models.KeyTypeUser {
  42. user, err := models.GetUserByID(key.OwnerID)
  43. if err != nil {
  44. if models.IsErrUserNotExist(err) {
  45. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  46. "err": fmt.Sprintf("Cannot find owner with id: %d for key: %d", key.OwnerID, keyID),
  47. })
  48. return
  49. }
  50. log.Error("Unable to get owner with id: %d for public key: %d Error: %v", key.OwnerID, keyID, err)
  51. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  52. "err": err.Error(),
  53. })
  54. return
  55. }
  56. results.Owner = user
  57. }
  58. ctx.JSON(http.StatusOK, &results)
  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. repoExist := true
  90. repo, err := models.GetRepositoryByOwnerAndName(results.OwnerName, results.RepoName)
  91. if err != nil {
  92. if models.IsErrRepoNotExist(err) {
  93. repoExist = false
  94. } else {
  95. log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
  96. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  97. "results": results,
  98. "type": "InternalServerError",
  99. "err": fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
  100. })
  101. return
  102. }
  103. }
  104. if repoExist {
  105. repo.OwnerName = ownerName
  106. results.RepoID = repo.ID
  107. if repo.IsBeingCreated() {
  108. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  109. "results": results,
  110. "type": "InternalServerError",
  111. "err": "Repository is being created, you could retry after it finished",
  112. })
  113. return
  114. }
  115. // We can shortcut at this point if the repo is a mirror
  116. if mode > models.AccessModeRead && repo.IsMirror {
  117. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  118. "results": results,
  119. "type": "ErrMirrorReadOnly",
  120. "err": fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
  121. })
  122. return
  123. }
  124. }
  125. // Get the Public Key represented by the keyID
  126. key, err := models.GetPublicKeyByID(keyID)
  127. if err != nil {
  128. if models.IsErrKeyNotExist(err) {
  129. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  130. "results": results,
  131. "type": "ErrKeyNotExist",
  132. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  133. })
  134. return
  135. }
  136. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  137. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  138. "results": results,
  139. "type": "InternalServerError",
  140. "err": fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
  141. })
  142. return
  143. }
  144. results.KeyName = key.Name
  145. results.KeyID = key.ID
  146. results.UserID = key.OwnerID
  147. // If repo doesn't exist, deploy key doesn't make sense
  148. if !repoExist && key.Type == models.KeyTypeDeploy {
  149. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  150. "results": results,
  151. "type": "ErrRepoNotExist",
  152. "err": fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
  153. })
  154. return
  155. }
  156. // Deploy Keys have ownerID set to 0 therefore we can't use the owner
  157. // So now we need to check if the key is a deploy key
  158. // We'll keep hold of the deploy key here for permissions checking
  159. var deployKey *models.DeployKey
  160. var user *models.User
  161. if key.Type == models.KeyTypeDeploy {
  162. results.IsDeployKey = true
  163. var err error
  164. deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID)
  165. if err != nil {
  166. if models.IsErrDeployKeyNotExist(err) {
  167. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  168. "results": results,
  169. "type": "ErrDeployKeyNotExist",
  170. "err": fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  171. })
  172. return
  173. }
  174. log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err)
  175. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  176. "results": results,
  177. "type": "InternalServerError",
  178. "err": fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
  179. })
  180. return
  181. }
  182. results.KeyName = deployKey.Name
  183. // FIXME: Deploy keys aren't really the owner of the repo pushing changes
  184. // however we don't have good way of representing deploy keys in hook.go
  185. // so for now use the owner of the repository
  186. results.UserName = results.OwnerName
  187. results.UserID = repo.OwnerID
  188. } else {
  189. // Get the user represented by the Key
  190. var err error
  191. user, err = models.GetUserByID(key.OwnerID)
  192. if err != nil {
  193. if models.IsErrUserNotExist(err) {
  194. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  195. "results": results,
  196. "type": "ErrUserNotExist",
  197. "err": fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
  198. })
  199. return
  200. }
  201. log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err)
  202. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  203. "results": results,
  204. "type": "InternalServerError",
  205. "err": fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
  206. })
  207. return
  208. }
  209. results.UserName = user.Name
  210. }
  211. // Don't allow pushing if the repo is archived
  212. if repoExist && mode > models.AccessModeRead && repo.IsArchived {
  213. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  214. "results": results,
  215. "type": "ErrRepoIsArchived",
  216. "err": fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
  217. })
  218. return
  219. }
  220. // Permissions checking:
  221. if repoExist && (mode > models.AccessModeRead || repo.IsPrivate || setting.Service.RequireSignInView) {
  222. if key.Type == models.KeyTypeDeploy {
  223. if deployKey.Mode < mode {
  224. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  225. "results": results,
  226. "type": "ErrUnauthorized",
  227. "err": fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  228. })
  229. return
  230. }
  231. } else {
  232. perm, err := models.GetUserRepoPermission(repo, user)
  233. if err != nil {
  234. log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err)
  235. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  236. "results": results,
  237. "type": "InternalServerError",
  238. "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),
  239. })
  240. return
  241. }
  242. userMode := perm.UnitAccessMode(unitType)
  243. if userMode < mode {
  244. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  245. "results": results,
  246. "type": "ErrUnauthorized",
  247. "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),
  248. })
  249. return
  250. }
  251. }
  252. }
  253. // We already know we aren't using a deploy key
  254. if !repoExist {
  255. owner, err := models.GetUserByName(ownerName)
  256. if err != nil {
  257. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  258. "results": results,
  259. "type": "InternalServerError",
  260. "err": fmt.Sprintf("Unable to get owner: %s %v", results.OwnerName, err),
  261. })
  262. return
  263. }
  264. if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
  265. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  266. "results": results,
  267. "type": "ErrForbidden",
  268. "err": "Push to create is not enabled for organizations.",
  269. })
  270. return
  271. }
  272. if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
  273. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  274. "results": results,
  275. "type": "ErrForbidden",
  276. "err": "Push to create is not enabled for users.",
  277. })
  278. return
  279. }
  280. repo, err = repo_service.PushCreateRepo(user, owner, results.RepoName)
  281. if err != nil {
  282. log.Error("pushCreateRepo: %v", err)
  283. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  284. "results": results,
  285. "type": "ErrRepoNotExist",
  286. "err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  287. })
  288. return
  289. }
  290. results.RepoID = repo.ID
  291. }
  292. // Finally if we're trying to touch the wiki we should init it
  293. if results.IsWiki {
  294. if err = repo.InitWiki(); err != nil {
  295. log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err)
  296. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  297. "results": results,
  298. "type": "InternalServerError",
  299. "err": fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
  300. })
  301. return
  302. }
  303. }
  304. log.Debug("Serv Results:\nIsWiki: %t\nIsDeployKey: %t\nKeyID: %d\tKeyName: %s\nUserName: %s\nUserID: %d\nOwnerName: %s\nRepoName: %s\nRepoID: %d",
  305. results.IsWiki,
  306. results.IsDeployKey,
  307. results.KeyID,
  308. results.KeyName,
  309. results.UserName,
  310. results.UserID,
  311. results.OwnerName,
  312. results.RepoName,
  313. results.RepoID)
  314. ctx.JSON(http.StatusOK, results)
  315. // We will update the keys in a different call.
  316. }