Eric Myhre
2013-06-13 41124cddb6edd82c1630efb99b29c839304ed897
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright 2014 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.transport.ssh;
 
import java.io.IOException;
import java.util.Scanner;
 
import org.apache.sshd.server.Command;
import org.apache.sshd.server.CommandFactory;
import org.apache.sshd.server.Environment;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.PacketLineOut;
import org.eclipse.jgit.transport.ReceivePack;
import org.eclipse.jgit.transport.ServiceMayNotContinueException;
import org.eclipse.jgit.transport.UploadPack;
import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
import org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException;
import org.eclipse.jgit.transport.resolver.ServiceNotEnabledException;
import org.eclipse.jgit.transport.resolver.UploadPackFactory;
 
import com.gitblit.git.RepositoryResolver;
 
/**
 *
 * @author Eric Myhre
 *
 */
public class SshCommandFactory implements CommandFactory {
    public SshCommandFactory(RepositoryResolver<SshDaemonClient> repositoryResolver, UploadPackFactory<SshDaemonClient> uploadPackFactory, ReceivePackFactory<SshDaemonClient> receivePackFactory) {
        this.repositoryResolver = repositoryResolver;
        this.uploadPackFactory = uploadPackFactory;
        this.receivePackFactory = receivePackFactory;
    }
 
    private RepositoryResolver<SshDaemonClient> repositoryResolver;
 
    private UploadPackFactory<SshDaemonClient> uploadPackFactory;
 
    private ReceivePackFactory<SshDaemonClient> receivePackFactory;
 
    @Override
    public Command createCommand(final String commandLine) {
        Scanner commandScanner = new Scanner(commandLine);
        final String command = commandScanner.next();
        final String argument = commandScanner.nextLine();
 
        if ("git-upload-pack".equals(command))
            return new UploadPackCommand(argument);
        if ("git-receive-pack".equals(command))
            return new ReceivePackCommand(argument);
        return new NonCommand();
    }
 
    public abstract class RepositoryCommand extends AbstractSshCommand {
        protected final String repositoryName;
 
        public RepositoryCommand(String repositoryName) {
            this.repositoryName = repositoryName;
        }
 
        @Override
        public void start(Environment env) throws IOException {
            Repository db = null;
            try {
                SshDaemonClient client = session.getAttribute(SshDaemonClient.ATTR_KEY);
                db = selectRepository(client, repositoryName);
                if (db == null) return;
                run(client, db);
                exit.onExit(0);
            } catch (ServiceNotEnabledException e) {
                // Ignored. Client cannot use this repository.
            } catch (ServiceNotAuthorizedException e) {
                // Ignored. Client cannot use this repository.
            } finally {
                if (db != null)
                    db.close();
                exit.onExit(1);
            }
        }
 
        protected Repository selectRepository(SshDaemonClient client, String name) throws IOException {
            try {
                return openRepository(client, name);
            } catch (ServiceMayNotContinueException e) {
                // An error when opening the repo means the client is expecting a ref
                // advertisement, so use that style of error.
                PacketLineOut pktOut = new PacketLineOut(out);
                pktOut.writeString("ERR " + e.getMessage() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
                return null;
            }
        }
 
        protected Repository openRepository(SshDaemonClient client, String name)
                throws ServiceMayNotContinueException {
            // Assume any attempt to use \ was by a Windows client
            // and correct to the more typical / used in Git URIs.
            //
            name = name.replace('\\', '/');
 
            // ssh://git@thishost/path should always be name="/path" here
            //
            if (!name.startsWith("/")) //$NON-NLS-1$
                return null;
 
            try {
                return repositoryResolver.open(client, name.substring(1));
            } catch (RepositoryNotFoundException e) {
                // null signals it "wasn't found", which is all that is suitable
                // for the remote client to know.
                return null;
            } catch (ServiceNotEnabledException e) {
                // null signals it "wasn't found", which is all that is suitable
                // for the remote client to know.
                return null;
            }
        }
 
        protected abstract void run(SshDaemonClient client, Repository db)
            throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException;
    }
 
    public class UploadPackCommand extends RepositoryCommand {
        public UploadPackCommand(String repositoryName) { super(repositoryName); }
 
        @Override
        protected void run(SshDaemonClient client, Repository db)
                throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
            UploadPack up = uploadPackFactory.create(client, db);
            up.upload(in, out, null);
        }
    }
 
    public class ReceivePackCommand extends RepositoryCommand {
        public ReceivePackCommand(String repositoryName) { super(repositoryName); }
 
        @Override
        protected void run(SshDaemonClient client, Repository db)
                throws IOException, ServiceNotEnabledException, ServiceNotAuthorizedException {
            ReceivePack rp = receivePackFactory.create(client, db);
            rp.receive(in, out, null);
        }
    }
 
    public static class NonCommand extends AbstractSshCommand {
        @Override
        public void start(Environment env) {
            exit.onExit(127);
        }
    }
}