I'm working on a project that has separate Git repositories for
different components. Repositories are cloneable via
git clone git@xvm.mit.edu:path/to/repo.git,
where the 'git' user's shell is git-shell.
We have been seeking a simple and maintainable way for users to
discover the set of available repositories. E.g. posting the list on
our website would add extra steps for users to find and retrieve the
list as well as require extra effort from our end. Since we already give
users ssh access to git@xvm.mit.edu, we would like to multiplex the
functionality to allow discovery of available repositories.
Our solution is to expose a 'list' command to the end user, invocable
as
ssh git@xvm.mit.edu list,
which displays the available repositories.
We find this mechanism useful in that it requires no extra
infrastructure on either our end or the user's end. Our
implementation is extensible, allowing the system administrator to
place arbitrary commands in ~/git-shell-commands (if the directory is
omitted, no extra functionality is exposed), and also supports an
interactive mode.
What do people think of this approach? I'd love to get this
functionality merged in some form.
Thank you!
Greg Brockman
This provides a mechanism for the server to expose custom
functionality to clients. My particular use case is that I would like
a way of discovering all repositories available for cloning. A
client that clones via
git clone user@example.com
can invoke a command by
ssh user@example.com $command
Signed-off-by: Greg Brockman <redacted>
---
shell.c | 16 ++++++++++++++++
1 files changed, 16 insertions(+), 0 deletions(-)
@@ -33,6 +35,12 @@ static int do_cvs_cmd(const char *me, char *arg)returnexecv_git_cmd(cvsserver_argv);}+staticintis_valid_cmd_name(constchar*cmd)+{+/* Test command contains no . or / characters */+returncmd[strcspn(cmd,"./")]=='\0';+}+staticstructcommands{constchar*name;
@@ -99,5 +107,13 @@ int main(int argc, char **argv)}exit(cmd->exec(cmd->name,arg));}++/* Shell should be spawned with cwd in the git user's home directory */+if(chdir(COMMAND_DIR))+die("unrecognized command '%s'",prog);++if(is_valid_cmd_name(prog))+execl(prog,prog,(char*)NULL);+die("unrecognized command '%s'",prog);}
@@ -81,8 +105,30 @@ int main(int argc, char **argv)*Wedonotacceptanythingbut"-c"followedby"cmd arg",*where"cmd"isaverylimitedsubsetofgitcommands.*/-elseif(argc!=3||strcmp(argv[1],"-c"))-die("What do you think I am? A shell?");+elseif(argc!=3||strcmp(argv[1],"-c")){+if(chdir(COMMAND_DIR))+die("Sorry, the interactive git-shell is not enabled");+for(;;){+printf("git> ");+if(fgets(line,MAX_LINE_LEN,stdin)==NULL){+printf("\n");+exit(0);+}++if(line[strlen(line)-1]=='\n')+line[strlen(line)-1]='\0';++if(!strcmp(line,"quit")||!strcmp(line,"logout")||+!strcmp(line,"exit")){+exit(0);+}elseif(!strcmp(line,"")){+}elseif(is_valid_cmd_name(line)){+run(line);+}else{+fprintf(stderr,"invalid command format '%s'\n",line);+}+};+}prog=argv[2];if(!strncmp(prog,"git",3)&&isspace(prog[3]))
From: Johannes Sixt <hidden> Date: 2016-06-15 22:49:07
I don't have an immediate need for features implemented by this series,
but I think they can be useful occasionally.
Am 7/14/2010 5:01, schrieb Greg Brockman:
quoted hunk
--- a/shell.c+++ b/shell.c
@@ -1,8 +1,11 @@+#include<stdio.h>
Is it really needed? Doesn't cache.h pull it in already?
Is there a reason that you duplicate functionality offered by run_command()?
quoted hunk
@@ -81,8 +105,30 @@ int main(int argc, char **argv) * We do not accept anything but "-c" followed by "cmd arg", * where "cmd" is a very limited subset of git commands. */- else if (argc != 3 || strcmp(argv[1], "-c"))- die("What do you think I am? A shell?");+ else if (argc != 3 || strcmp(argv[1], "-c")) {+ if (chdir(COMMAND_DIR))+ die("Sorry, the interactive git-shell is not enabled");+ for (;;) {+ printf("git> ");+ if (fgets(line, MAX_LINE_LEN, stdin) == NULL) {+ printf("\n");+ exit(0);+ }++ if (line[strlen(line) - 1] == '\n')+ line[strlen(line) - 1] = '\0';++ if (!strcmp(line, "quit") || !strcmp(line, "logout") ||+ !strcmp(line, "exit")) {+ exit(0);+ } else if (!strcmp(line, "")) {+ } else if (is_valid_cmd_name(line)) {+ run(line);+ } else {+ fprintf(stderr, "invalid command format '%s'\n", line);+ }+ };+ }
I can imagine that this loop grows in the future, so I suggest to move it
to a separate function right from the beginning.
I think it would make sense to print a help message before the first prompt.
-- Hannes
From: Kevin P. Fleming <hidden> Date: 2016-06-15 22:49:07
On 07/14/2010 04:04 AM, Ævar Arnfjörð Bjarmason wrote:
On Wed, Jul 14, 2010 at 03:01, Greg Brockman [off-list ref] wrote:
quoted
+ execl(prog, prog, (char *) NULL);
Why the casting of NULL? It's not done in the builtin/help.c code.
Anyway, if it was cast it should be to (const char *), shouldn't it?
When a NULL sentinel is passed to a varargs function that only
understands 'char *' arguments, the NULL must be cast specifically,
otherwise it will appear in the varargs array as an int or a long.
execl() is an example of a varargs function that only uses varargs
functionality to accept a variable *number* of arguments, it does not
allow for arguments of differing types, so it does not check the types
of its arguments at all. On any platform where an int and a pointer are
not the same size, this can cause a serious problem. When we came across
this problem in Asterisk, we added a macro called SENTINEL (that just
expands to the proper type for the target platform) that is used in
these cases, so that it is clear to the reader of the code what is going on.
--
Kevin P. Fleming
Digium, Inc. | Director of Software Technologies
445 Jan Davis Drive NW - Huntsville, AL 35806 - USA
skype: kpfleming | jabber: kfleming@digium.com
Check us out at www.digium.com & www.asterisk.org
From: Bernhard R. Link <hidden> Date: 2016-06-15 22:49:07
* Kevin P. Fleming [off-list ref] [100714 15:59]:
On 07/14/2010 04:04 AM, Ævar Arnfjörð Bjarmason wrote:
quoted
On Wed, Jul 14, 2010 at 03:01, Greg Brockman [off-list ref] wrote:
quoted
+ execl(prog, prog, (char *) NULL);
Why the casting of NULL? It's not done in the builtin/help.c code.
Anyway, if it was cast it should be to (const char *), shouldn't it?
When a NULL sentinel is passed to a varargs function that only
understands 'char *' arguments, the NULL must be cast specifically,
otherwise it will appear in the varargs array as an int or a long.
To be more specific: If NULL is (void *)0 then it does not need to be
cast. Sadly the standard allows to define it as 0, and so it is on
some systems. So to be portable it needs to be cast to be a pointer,
otherwise the varargs argument is assumed to be an int.
Bernhard R. Link
From: Thomas Rast <hidden> Date: 2016-06-15 22:49:08
[Please don't trim the Cc list without good reason.]
Bernhard R. Link wrote:
* Kevin P. Fleming [off-list ref] [100714 15:59]:
quoted
On 07/14/2010 04:04 AM, Ævar Arnfjörð Bjarmason wrote:
quoted
On Wed, Jul 14, 2010 at 03:01, Greg Brockman [off-list ref] wrote:
quoted
+ execl(prog, prog, (char *) NULL);
Why the casting of NULL? It's not done in the builtin/help.c code.
Anyway, if it was cast it should be to (const char *), shouldn't it?
When a NULL sentinel is passed to a varargs function that only
understands 'char *' arguments, the NULL must be cast specifically,
otherwise it will appear in the varargs array as an int or a long.
To be more specific: If NULL is (void *)0 then it does not need to be
cast. Sadly the standard allows to define it as 0, and so it is on
some systems. So to be portable it needs to be cast to be a pointer,
otherwise the varargs argument is assumed to be an int.
Worse, the pointer representations need not be the same between types,
even though that is a fairly exotic idea:
http://c-faq.com/null/machexamp.html
So it seems execl() must always have an explicitly-cast (char*)NULL
sentinel.
--
Thomas Rast
trast@{inf,student}.ethz.ch
From: Junio C Hamano <hidden> Date: 2016-06-15 22:49:08
Greg Brockman [off-list ref] writes:
We find this mechanism useful in that it requires no extra
infrastructure on either our end or the user's end. Our
implementation is extensible, allowing the system administrator to
place arbitrary commands in ~/git-shell-commands (if the directory is
omitted, no extra functionality is exposed), and also supports an
interactive mode.
What do people think of this approach? I'd love to get this
functionality merged in some form.
It seems to me that any time you need to add a new helper command, the
administrator needs to make sure that appears in ~$user/git-shell-commands
of all the users who need it. When adding a new user, a similar
management action needs to happen. Perhaps that is done by making a
symlink from all the users' home directories to one shared place. Is that
the general idea?
In any case, I'd prefer that the sample command implementations like list
and help to live in contrib/ somewhere. They are not part of what the
main Makefile needs to know about, right?
We find this mechanism useful in that it requires no extra
infrastructure on either our end or the user's end. Our
implementation is extensible, allowing the system administrator to
place arbitrary commands in ~/git-shell-commands (if the directory is
omitted, no extra functionality is exposed), and also supports an
interactive mode.
What do people think of this approach? I'd love to get this
functionality merged in some form.
It seems to me that any time you need to add a new helper command, the
administrator needs to make sure that appears in ~$user/git-shell-commands
of all the users who need it. When adding a new user, a similar
management action needs to happen. Perhaps that is done by making a
symlink from all the users' home directories to one shared place. Is that
the general idea?
That's correct. Our particular environment only has a single git
user, but if we were to add more we would probably make
git-shell-commands a symlink as you suggest.
In any case, I'd prefer that the sample command implementations like list
and help to live in contrib/ somewhere. They are not part of what the
main Makefile needs to know about, right?
Also correct. I'll look for a reasonable place within contrib/ to put them.
From: Thomas Rast <hidden> Date: 2016-06-15 22:49:10
The NULL sentinel argument to the execl*() family of calls must be
cast to (char *), as otherwise:
- platforms where NULL is just 0 (not (void *)) would pass an int
- (admittedly esoteric) platforms where NULL is (void *)0 and (void *)
and (char *) have different memory layouts would pass the wrong kind
of pointer
Signed-off-by: Thomas Rast <redacted>
---
Let's not forget about this.
builtin/help.c | 12 ++++++------
1 files changed, 6 insertions(+), 6 deletions(-)
On Sat, Jul 24, 2010 at 15:20, Thomas Rast [off-list ref] wrote:
The NULL sentinel argument to the execl*() family of calls must be
cast to (char *), as otherwise:
- platforms where NULL is just 0 (not (void *)) would pass an int
- (admittedly esoteric) platforms where NULL is (void *)0 and (void *)
and (char *) have different memory layouts would pass the wrong kind
of pointer
Signed-off-by: Thomas Rast <redacted>
Nice that you got around to this after I inadvertently pointed it out
in another thread.
Acked.