Jump to latest comments after adding a comment
[aur.git] / git-interface / git-serve.py
blob8316cf7d16d3f730b261164c2d3239ade2c3ba35
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 ssh_cmdline = config.get('serve', 'ssh-cmdline')
24 enable_maintenance = config.getboolean('options', 'enable-maintenance')
25 maintenance_exc = config.get('options', 'maintenance-exceptions').split()
27 def pkgbase_exists(pkgbase):
28 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
29 passwd=aur_db_pass, db=aur_db_name,
30 unix_socket=aur_db_socket)
31 cur = db.cursor()
33 cur.execute("SELECT COUNT(*) FROM PackageBases WHERE Name = %s ",
34 [pkgbase])
36 db.close()
37 return (cur.fetchone()[0] > 0)
39 def list_repos(user):
40 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
41 passwd=aur_db_pass, db=aur_db_name,
42 unix_socket=aur_db_socket)
43 cur = db.cursor()
45 cur.execute("SELECT ID FROM Users WHERE Username = %s ", [user])
46 userid = cur.fetchone()[0]
47 if userid == 0:
48 die('{:s}: unknown user: {:s}'.format(action, user))
50 cur.execute("SELECT Name, PackagerUID FROM PackageBases " +
51 "WHERE MaintainerUID = %s ", [userid])
52 for row in cur:
53 print((' ' if row[1] else '*') + row[0])
54 db.close()
56 def create_pkgbase(pkgbase, user):
57 if not re.match(repo_regex, pkgbase):
58 die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
59 if pkgbase_exists(pkgbase):
60 die('{:s}: package base already exists: {:s}'.format(action, pkgbase))
62 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
63 passwd=aur_db_pass, db=aur_db_name,
64 unix_socket=aur_db_socket)
65 cur = db.cursor()
67 cur.execute("SELECT ID FROM Users WHERE Username = %s ", [user])
68 userid = cur.fetchone()[0]
69 if userid == 0:
70 die('{:s}: unknown user: {:s}'.format(action, user))
72 cur.execute("INSERT INTO PackageBases (Name, SubmittedTS, ModifiedTS, " +
73 "SubmitterUID, MaintainerUID) VALUES (%s, UNIX_TIMESTAMP(), " +
74 "UNIX_TIMESTAMP(), %s, %s)", [pkgbase, userid, userid])
75 pkgbase_id = cur.lastrowid
77 cur.execute("INSERT INTO CommentNotify (PackageBaseID, UserID) " +
78 "VALUES (%s, %s)", [pkgbase_id, userid])
80 db.commit()
81 db.close()
83 def check_permissions(pkgbase, user):
84 db = mysql.connector.connect(host=aur_db_host, user=aur_db_user,
85 passwd=aur_db_pass, db=aur_db_name,
86 unix_socket=aur_db_socket, buffered=True)
87 cur = db.cursor()
89 if os.environ.get('AUR_PRIVILEGED', '0') == '1':
90 return True
92 cur.execute("SELECT COUNT(*) FROM PackageBases " +
93 "LEFT JOIN PackageComaintainers " +
94 "ON PackageComaintainers.PackageBaseID = PackageBases.ID " +
95 "INNER JOIN Users ON Users.ID = PackageBases.MaintainerUID " +
96 "OR PackageBases.MaintainerUID IS NULL " +
97 "OR Users.ID = PackageComaintainers.UsersID " +
98 "WHERE Name = %s AND Username = %s", [pkgbase, user])
99 return cur.fetchone()[0] > 0
101 def die(msg):
102 sys.stderr.write("{:s}\n".format(msg))
103 exit(1)
105 def die_with_help(msg):
106 die(msg + "\nTry `{:s} help` for a list of commands.".format(ssh_cmdline))
108 user = os.environ.get("AUR_USER")
109 cmd = os.environ.get("SSH_ORIGINAL_COMMAND")
110 if not cmd:
111 die_with_help("Interactive shell is disabled.")
112 cmdargv = shlex.split(cmd)
113 action = cmdargv[0]
115 if enable_maintenance:
116 remote_addr = os.environ["SSH_CLIENT"].split(" ")[0]
117 if not remote_addr in maintenance_exc:
118 die("The AUR is down due to maintenance. We will be back soon.")
120 if action == 'git-upload-pack' or action == 'git-receive-pack':
121 if len(cmdargv) < 2:
122 die_with_help("{:s}: missing path".format(action))
124 path = cmdargv[1].rstrip('/')
125 if not path.startswith('/'):
126 path = '/' + path
127 if not path.endswith('.git'):
128 path = path + '.git'
129 pkgbase = path[1:-4]
130 if not re.match(repo_regex, pkgbase):
131 die('{:s}: invalid repository name: {:s}'.format(action, pkgbase))
133 if not pkgbase_exists(pkgbase):
134 create_pkgbase(pkgbase, user)
136 if action == 'git-receive-pack':
137 if not check_permissions(pkgbase, user):
138 die('{:s}: permission denied: {:s}'.format(action, user))
140 os.environ["AUR_USER"] = user
141 os.environ["AUR_PKGBASE"] = pkgbase
142 os.environ["GIT_NAMESPACE"] = pkgbase
143 cmd = action + " '" + repo_path + "'"
144 os.execl(git_shell_cmd, git_shell_cmd, '-c', cmd)
145 elif action == 'list-repos':
146 if len(cmdargv) > 1:
147 die_with_help("{:s}: too many arguments".format(action))
148 list_repos(user)
149 elif action == 'setup-repo':
150 if len(cmdargv) < 2:
151 die_with_help("{:s}: missing repository name".format(action))
152 if len(cmdargv) > 2:
153 die_with_help("{:s}: too many arguments".format(action))
154 create_pkgbase(cmdargv[1], user)
155 elif action == 'help':
156 die("Commands:\n" +
157 " help Show this help message and exit.\n" +
158 " list-repos List all your repositories.\n" +
159 " setup-repo <name> Create an empty repository.\n" +
160 " git-receive-pack Internal command used with Git.\n" +
161 " git-upload-pack Internal command used with Git.")
162 else:
163 die_with_help("invalid command: {:s}".format(action))