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

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