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 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. 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. for _, verb := range ctx.QueryStrings("verb") {
  95. if "git-upload-pack" == verb {
  96. // User is fetching/cloning a non-existent repository
  97. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  98. "results": results,
  99. "type": "ErrRepoNotExist",
  100. "err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  101. })
  102. return
  103. }
  104. }
  105. } else {
  106. log.Error("Unable to get repository: %s/%s Error: %v", results.OwnerName, results.RepoName, err)
  107. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  108. "results": results,
  109. "type": "InternalServerError",
  110. "err": fmt.Sprintf("Unable to get repository: %s/%s %v", results.OwnerName, results.RepoName, err),
  111. })
  112. return
  113. }
  114. }
  115. for _, verb := range ctx.QueryStrings("verb") { // clone_cnt
  116. if verb == "git-upload-pack" {
  117. go repo.IncreaseCloneCnt()
  118. }
  119. }
  120. if repoExist {
  121. repo.OwnerName = ownerName
  122. results.RepoID = repo.ID
  123. if repo.IsBeingCreated() {
  124. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  125. "results": results,
  126. "type": "InternalServerError",
  127. "err": "Repository is being created, you could retry after it finished",
  128. })
  129. return
  130. }
  131. // We can shortcut at this point if the repo is a mirror
  132. if mode > models.AccessModeRead && repo.IsMirror {
  133. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  134. "results": results,
  135. "type": "ErrMirrorReadOnly",
  136. "err": fmt.Sprintf("Mirror Repository %s/%s is read-only", results.OwnerName, results.RepoName),
  137. })
  138. return
  139. }
  140. }
  141. // Get the Public Key represented by the keyID
  142. key, err := models.GetPublicKeyByID(keyID)
  143. if err != nil {
  144. if models.IsErrKeyNotExist(err) {
  145. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  146. "results": results,
  147. "type": "ErrKeyNotExist",
  148. "err": fmt.Sprintf("Cannot find key: %d", keyID),
  149. })
  150. return
  151. }
  152. log.Error("Unable to get public key: %d Error: %v", keyID, err)
  153. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  154. "results": results,
  155. "type": "InternalServerError",
  156. "err": fmt.Sprintf("Unable to get key: %d Error: %v", keyID, err),
  157. })
  158. return
  159. }
  160. results.KeyName = key.Name
  161. results.KeyID = key.ID
  162. results.UserID = key.OwnerID
  163. // If repo doesn't exist, deploy key doesn't make sense
  164. if !repoExist && key.Type == models.KeyTypeDeploy {
  165. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  166. "results": results,
  167. "type": "ErrRepoNotExist",
  168. "err": fmt.Sprintf("Cannot find repository %s/%s", results.OwnerName, results.RepoName),
  169. })
  170. return
  171. }
  172. // Deploy Keys have ownerID set to 0 therefore we can't use the owner
  173. // So now we need to check if the key is a deploy key
  174. // We'll keep hold of the deploy key here for permissions checking
  175. var deployKey *models.DeployKey
  176. var user *models.User
  177. if key.Type == models.KeyTypeDeploy {
  178. results.IsDeployKey = true
  179. var err error
  180. deployKey, err = models.GetDeployKeyByRepo(key.ID, repo.ID)
  181. if err != nil {
  182. if models.IsErrDeployKeyNotExist(err) {
  183. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  184. "results": results,
  185. "type": "ErrDeployKeyNotExist",
  186. "err": fmt.Sprintf("Public (Deploy) Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  187. })
  188. return
  189. }
  190. log.Error("Unable to get deploy for public (deploy) key: %d in %-v Error: %v", key.ID, repo, err)
  191. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  192. "results": results,
  193. "type": "InternalServerError",
  194. "err": fmt.Sprintf("Unable to get Deploy Key for Public Key: %d:%s in %s/%s.", key.ID, key.Name, results.OwnerName, results.RepoName),
  195. })
  196. return
  197. }
  198. results.KeyName = deployKey.Name
  199. // FIXME: Deploy keys aren't really the owner of the repo pushing changes
  200. // however we don't have good way of representing deploy keys in hook.go
  201. // so for now use the owner of the repository
  202. results.UserName = results.OwnerName
  203. results.UserID = repo.OwnerID
  204. } else {
  205. // Get the user represented by the Key
  206. var err error
  207. user, err = models.GetUserByID(key.OwnerID)
  208. if err != nil {
  209. if models.IsErrUserNotExist(err) {
  210. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  211. "results": results,
  212. "type": "ErrUserNotExist",
  213. "err": fmt.Sprintf("Public Key: %d:%s owner %d does not exist.", key.ID, key.Name, key.OwnerID),
  214. })
  215. return
  216. }
  217. log.Error("Unable to get owner: %d for public key: %d:%s Error: %v", key.OwnerID, key.ID, key.Name, err)
  218. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  219. "results": results,
  220. "type": "InternalServerError",
  221. "err": fmt.Sprintf("Unable to get Owner: %d for Deploy Key: %d:%s in %s/%s.", key.OwnerID, key.ID, key.Name, ownerName, repoName),
  222. })
  223. return
  224. }
  225. results.UserName = user.Name
  226. }
  227. // Don't allow pushing if the repo is archived
  228. if repoExist && mode > models.AccessModeRead && repo.IsArchived {
  229. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  230. "results": results,
  231. "type": "ErrRepoIsArchived",
  232. "err": fmt.Sprintf("Repo: %s/%s is archived.", results.OwnerName, results.RepoName),
  233. })
  234. return
  235. }
  236. // Permissions checking:
  237. if repoExist && (mode > models.AccessModeRead || repo.IsPrivate || setting.Service.RequireSignInView) {
  238. if key.Type == models.KeyTypeDeploy {
  239. if deployKey.Mode < mode {
  240. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  241. "results": results,
  242. "type": "ErrUnauthorized",
  243. "err": fmt.Sprintf("Deploy Key: %d:%s is not authorized to %s %s/%s.", key.ID, key.Name, modeString, results.OwnerName, results.RepoName),
  244. })
  245. return
  246. }
  247. } else {
  248. perm, err := models.GetUserRepoPermission(repo, user)
  249. if err != nil {
  250. log.Error("Unable to get permissions for %-v with key %d in %-v Error: %v", user, key.ID, repo, err)
  251. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  252. "results": results,
  253. "type": "InternalServerError",
  254. "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),
  255. })
  256. return
  257. }
  258. userMode := perm.UnitAccessMode(unitType)
  259. if userMode < mode {
  260. ctx.JSON(http.StatusUnauthorized, map[string]interface{}{
  261. "results": results,
  262. "type": "ErrUnauthorized",
  263. "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),
  264. })
  265. return
  266. }
  267. }
  268. }
  269. // We already know we aren't using a deploy key
  270. if !repoExist {
  271. owner, err := models.GetUserByName(ownerName)
  272. if err != nil {
  273. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  274. "results": results,
  275. "type": "InternalServerError",
  276. "err": fmt.Sprintf("Unable to get owner: %s %v", results.OwnerName, err),
  277. })
  278. return
  279. }
  280. if owner.IsOrganization() && !setting.Repository.EnablePushCreateOrg {
  281. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  282. "results": results,
  283. "type": "ErrForbidden",
  284. "err": "Push to create is not enabled for organizations.",
  285. })
  286. return
  287. }
  288. if !owner.IsOrganization() && !setting.Repository.EnablePushCreateUser {
  289. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  290. "results": results,
  291. "type": "ErrForbidden",
  292. "err": "Push to create is not enabled for users.",
  293. })
  294. return
  295. }
  296. repo, err = repo_service.PushCreateRepo(user, owner, results.RepoName)
  297. if err != nil {
  298. log.Error("pushCreateRepo: %v", err)
  299. ctx.JSON(http.StatusNotFound, map[string]interface{}{
  300. "results": results,
  301. "type": "ErrRepoNotExist",
  302. "err": fmt.Sprintf("Cannot find repository: %s/%s", results.OwnerName, results.RepoName),
  303. })
  304. return
  305. }
  306. results.RepoID = repo.ID
  307. }
  308. if results.IsWiki {
  309. // Ensure the wiki is enabled before we allow access to it
  310. if _, err := repo.GetUnit(models.UnitTypeWiki); err != nil {
  311. if models.IsErrUnitTypeNotExist(err) {
  312. ctx.JSON(http.StatusForbidden, map[string]interface{}{
  313. "results": results,
  314. "type": "ErrForbidden",
  315. "err": "repository wiki is disabled",
  316. })
  317. return
  318. }
  319. log.Error("Failed to get the wiki unit in %-v Error: %v", repo, err)
  320. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  321. "results": results,
  322. "type": "InternalServerError",
  323. "err": fmt.Sprintf("Failed to get the wiki unit in %s/%s Error: %v", ownerName, repoName, err),
  324. })
  325. return
  326. }
  327. // Finally if we're trying to touch the wiki we should init it
  328. if err = wiki_service.InitWiki(repo); err != nil {
  329. log.Error("Failed to initialize the wiki in %-v Error: %v", repo, err)
  330. ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  331. "results": results,
  332. "type": "InternalServerError",
  333. "err": fmt.Sprintf("Failed to initialize the wiki in %s/%s Error: %v", ownerName, repoName, err),
  334. })
  335. return
  336. }
  337. }
  338. log.Debug("Serv Results:\nIsWiki: %t\nIsDeployKey: %t\nKeyID: %d\tKeyName: %s\nUserName: %s\nUserID: %d\nOwnerName: %s\nRepoName: %s\nRepoID: %d",
  339. results.IsWiki,
  340. results.IsDeployKey,
  341. results.KeyID,
  342. results.KeyName,
  343. results.UserName,
  344. results.UserID,
  345. results.OwnerName,
  346. results.RepoName,
  347. results.RepoID)
  348. ctx.JSON(http.StatusOK, results)
  349. // We will update the keys in a different call.
  350. }