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
)
33 cur
.execute("SELECT COUNT(*) FROM PackageBases WHERE Name = %s ",
37 return (cur
.fetchone()[0] > 0)
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
)
45 cur
.execute("SELECT ID FROM Users WHERE Username = %s ", [user
])
46 userid
= cur
.fetchone()[0]
48 die('{:s}: unknown user: {:s}'.format(action
, user
))
50 cur
.execute("SELECT Name, PackagerUID FROM PackageBases " +
51 "WHERE MaintainerUID = %s ", [userid
])
53 print((' ' if row
[1] else '*') + row
[0])
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
)
67 cur
.execute("SELECT ID FROM Users WHERE Username = %s ", [user
])
68 userid
= cur
.fetchone()[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
])
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)
89 if os
.environ
.get('AUR_PRIVILEGED', '0') == '1':
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
102 sys
.stderr
.write("{:s}\n".format(msg
))
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")
111 die_with_help("Interactive shell is disabled.")
112 cmdargv
= shlex
.split(cmd
)
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':
122 die_with_help("{:s}: missing path".format(action
))
124 path
= cmdargv
[1].rstrip('/')
125 if not path
.startswith('/'):
127 if not path
.endswith('.git'):
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':
147 die_with_help("{:s}: too many arguments".format(action
))
149 elif action
== 'setup-repo':
151 die_with_help("{:s}: missing repository name".format(action
))
153 die_with_help("{:s}: too many arguments".format(action
))
154 create_pkgbase(cmdargv
[1], user
)
155 elif action
== 'help':
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.")
163 die_with_help("invalid command: {:s}".format(action
))