Mitigate JSONP callback vulnerabilities
[aur.git] / git-interface / git-serve.py
blob45c9a01bc592c442eaaaa590fa2e4a86df19dc35
1 #!/usr/bin/python3
3 import configparser
4 import mysql.connector
5 import os
6 import re
7 import shlex
8 import sys
10 config = configparser.RawConfigParser()
11 config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
13 aur_db_host = config.get('database', 'host')
14 aur_db_name = config.get('database', 'name')
15 aur_db_user = config.get('database', 'user')
16 aur_db_pass = config.get('database', 'password')
17 aur_db_socket = config.get('database', 'socket')
19 repo_path = config.get('serve', 'repo-path')
20 repo_regex = config.get('serve', 'repo-regex')
21 git_shell_cmd = config.get('serve', 'git-shell-cmd')
22 git_update_cmd = config.get('serve', 'git-update-cmd')
23 ssh_cmdline = config.get('serve', 'ssh-cmdline')
25 enable_maintenance = config.getboolean('options', 'enable-maintenance')
26 maintenance_exc = config.get('options', 'maintenance-exceptions').split()
28 def pkgbase_exists(pkgbase):
29 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
30 passwd=aur_db_pass, db=aur_db_name,
31 unix_socket=aur_db_socket)
32 cur = db.cursor()
34 cur.execute("SELECT COUNT(*) FROM PackageBases WHERE Name = %s ",
35 [pkgbase])
37 db.close()
38 return (cur.fetchone()[0] > 0)
40 def list_repos(user):
41 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
42 passwd=aur_db_pass, db=aur_db_name,
43 unix_socket=aur_db_socket)
44 cur = db.cursor()
46 cur.execute("SELECT ID FROM Users WHERE Username = %s ", [user])
47 userid = cur.fetchone()[0]
48 if userid == 0:
49 die('{:s}: unknown user: {:s}'.format(action, user))
51 cur.execute("SELECT Name, PackagerUID FROM PackageBases " +
52 "WHERE MaintainerUID = %s ", [userid])
53 for row in cur:
54 print((' ' if row[1] else '*') + row[0])
55 db.close()
57 def create_pkgbase(pkgbase, user):
58 if not re.match(repo_regex, pkgbase):
59 die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
60 if pkgbase_exists(pkgbase):
61 die('{:s}: package base already exists: {:s}'.format(action, pkgbase))
63 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
64 passwd=aur_db_pass, db=aur_db_name,
65 unix_socket=aur_db_socket)
66 cur = db.cursor()
68 cur.execute("SELECT ID FROM Users WHERE Username = %s ", [user])
69 userid = cur.fetchone()[0]
70 if userid == 0:
71 die('{:s}: unknown user: {:s}'.format(action, user))
73 cur.execute("INSERT INTO PackageBases (Name, SubmittedTS, ModifiedTS, " +
74 "SubmitterUID, MaintainerUID) VALUES (%s, UNIX_TIMESTAMP(), " +
75 "UNIX_TIMESTAMP(), %s, %s)", [pkgbase, userid, userid])
76 pkgbase_id = cur.lastrowid
78 cur.execute("INSERT INTO CommentNotify (PackageBaseID, UserID) " +
79 "VALUES (%s, %s)", [pkgbase_id, userid])
81 db.commit()
82 db.close()
84 def check_permissions(pkgbase, user):
85 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
86 passwd=aur_db_pass, db=aur_db_name,
87 unix_socket=aur_db_socket, buffered=True)
88 cur = db.cursor()
90 if os.environ.get('AUR_PRIVILEGED', '0') == '1':
91 return True
93 cur.execute("SELECT COUNT(*) FROM PackageBases " +
94 "LEFT JOIN PackageComaintainers " +
95 "ON PackageComaintainers.PackageBaseID = PackageBases.ID " +
96 "INNER JOIN Users ON Users.ID = PackageBases.MaintainerUID " +
97 "OR PackageBases.MaintainerUID IS NULL " +
98 "OR Users.ID = PackageComaintainers.UsersID " +
99 "WHERE Name = %s AND Username = %s", [pkgbase, user])
100 return cur.fetchone()[0] > 0
102 def die(msg):
103 sys.stderr.write("{:s}\n".format(msg))
104 exit(1)
106 def die_with_help(msg):
107 die(msg + "\nTry `{:s} help` for a list of commands.".format(ssh_cmdline))
109 user = os.environ.get("AUR_USER")
110 cmd = os.environ.get("SSH_ORIGINAL_COMMAND")
111 if not cmd:
112 die_with_help("Interactive shell is disabled.")
113 cmdargv = shlex.split(cmd)
114 action = cmdargv[0]
116 if enable_maintenance:
117 remote_addr = os.environ["SSH_CLIENT"].split(" ")[0]
118 if not remote_addr in maintenance_exc:
119 die("The AUR is down due to maintenance. We will be back soon.")
121 if action == 'git-upload-pack' or action == 'git-receive-pack':
122 if len(cmdargv) < 2:
123 die_with_help("{:s}: missing path".format(action))
125 path = cmdargv[1].rstrip('/')
126 if not path.startswith('/'):
127 path = '/' + path
128 if not path.endswith('.git'):
129 path = path + '.git'
130 pkgbase = path[1:-4]
131 if not re.match(repo_regex, pkgbase):
132 die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
134 if not pkgbase_exists(pkgbase):
135 create_pkgbase(pkgbase, user)
137 if action == 'git-receive-pack':
138 if not check_permissions(pkgbase, user):
139 die('{:s}: permission denied: {:s}'.format(action, user))
141 os.environ["AUR_USER"] = user
142 os.environ["AUR_PKGBASE"] = pkgbase
143 os.environ["GIT_NAMESPACE"] = pkgbase
144 cmd = action + " '" + repo_path + "'"
145 os.execl(git_shell_cmd, git_shell_cmd, '-c', cmd)
146 elif action == 'list-repos':
147 if len(cmdargv) > 1:
148 die_with_help("{:s}: too many arguments".format(action))
149 list_repos(user)
150 elif action == 'setup-repo':
151 if len(cmdargv) < 2:
152 die_with_help("{:s}: missing repository name".format(action))
153 if len(cmdargv) > 2:
154 die_with_help("{:s}: too many arguments".format(action))
155 create_pkgbase(cmdargv[1], user)
156 elif action == 'restore':
157 if len(cmdargv) < 2:
158 die_with_help("{:s}: missing repository name".format(action))
159 if len(cmdargv) > 2:
160 die_with_help("{:s}: too many arguments".format(action))
162 pkgbase = cmdargv[1]
163 if not re.match(repo_regex, pkgbase):
164 die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
166 if pkgbase_exists(pkgbase):
167 die('{:s}: package base exists: {:s}'.format(action, pkgbase))
168 create_pkgbase(pkgbase, user)
170 os.environ["AUR_USER"] = user
171 os.environ["AUR_PKGBASE"] = pkgbase
172 os.execl(git_update_cmd, git_update_cmd, 'restore')
173 elif action == 'help':
174 die("Commands:\n" +
175 " help Show this help message and exit.\n" +
176 " list-repos List all your repositories.\n" +
177 " restore <name> Restore a deleted package base.\n" +
178 " setup-repo <name> Create an empty repository.\n" +
179 " git-receive-pack Internal command used with Git.\n" +
180 " git-upload-pack Internal command used with Git.")
181 else:
182 die_with_help("invalid command: {:s}".format(action))