~ubuntu-branches/ubuntu/trusty/jsch/trusty-proposed

« back to all changes in this revision

Viewing changes to src/com/jcraft/jsch/KnownHosts.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2006-12-04 03:11:50 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20061204031150-kg8n1b0utae11phk
Tags: 0.1.28-2
* Upload to unstable.
* Fix typo in copyright. Closes: #391988.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*-mode:java; c-basic-offset:2; -*- */
 
1
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
2
2
/*
3
 
Copyright (c) 2002,2003,2004 ymnk, JCraft,Inc. All rights reserved.
 
3
Copyright (c) 2002,2003,2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
4
4
 
5
5
Redistribution and use in source and binary forms, with or without
6
6
modification, are permitted provided that the following conditions are met:
86
86
          if(j==-1){ break loop;}
87
87
          if(j==0x0d){ continue; }
88
88
          if(j==0x0a){ break; }
 
89
          if(buf.length<=bufl){
 
90
            if(bufl>1024*10) break;   // too long...
 
91
            byte[] newbuf=new byte[buf.length*2];
 
92
            System.arraycopy(buf, 0, newbuf, 0, buf.length);
 
93
            buf=newbuf;
 
94
          }
89
95
          buf[bufl++]=(byte)j;
90
96
        }
91
97
 
177
183
    HostKey hk;
178
184
    int result=NOT_INCLUDED;
179
185
    int type=getType(key);
 
186
 
 
187
    synchronized(pool){
180
188
    for(int i=0; i<pool.size(); i++){
181
189
      hk=(HostKey)(pool.elementAt(i));
182
190
      if(isIncluded(hk.host, host) && hk.type==type){
189
197
        }
190
198
      }
191
199
    }
 
200
    }
192
201
    //System.out.println("fail!!");
193
202
    return result;
194
203
  }
195
204
  public void add(String host, byte[] key, UserInfo userinfo){
196
205
    HostKey hk;
197
206
    int type=getType(key);
198
 
    for(int i=0; i<pool.size(); i++){
199
 
      hk=(HostKey)(pool.elementAt(i));
200
 
      if(isIncluded(hk.host, host) && hk.type==type){
 
207
 
 
208
    synchronized(pool){
 
209
      for(int i=0; i<pool.size(); i++){
 
210
        hk=(HostKey)(pool.elementAt(i));
 
211
        if(isIncluded(hk.host, host) && hk.type==type){
201
212
/*
202
 
        if(Util.array_equals(hk.key, key)){ return; }
203
 
        if(hk.host.equals(host)){
204
 
          hk.key=key;
205
 
          return;
206
 
        }
207
 
        else{
208
 
          hk.host=deleteSubString(hk.host, host);
209
 
          break;
210
 
        }
 
213
          if(Util.array_equals(hk.key, key)){ return; }
 
214
          if(hk.host.equals(host)){
 
215
            hk.key=key;
 
216
            return;
 
217
          }
 
218
          else{
 
219
            hk.host=deleteSubString(hk.host, host);
 
220
            break;
 
221
          }
211
222
*/
 
223
        }
212
224
      }
213
225
    }
 
226
 
214
227
    hk=new HostKey(host, type, key);
215
228
    pool.addElement(hk);
216
229
 
217
230
    String bar=getKnownHostsRepositoryID();
218
 
    if(userinfo!=null && 
219
 
       bar!=null){
 
231
    if(bar!=null){
220
232
      boolean foo=true;
221
233
      File goo=new File(bar);
222
234
      if(!goo.exists()){
223
 
        foo=false;
224
 
        if(userinfo!=null){
225
 
          foo=userinfo.promptYesNo(
226
 
bar+" does not exist.\n"+
227
 
"Are you sure you want to create it?"
228
 
                                    );
229
 
          goo=goo.getParentFile();
230
 
          if(foo && goo!=null && !goo.exists()){
231
 
            foo=userinfo.promptYesNo(
232
 
"The parent directory "+goo+" does not exist.\n"+
233
 
"Are you sure you want to create it?"
234
 
);
235
 
            if(foo){
236
 
              if(!goo.mkdirs()){
237
 
                userinfo.showMessage(goo+" has not been created.");
238
 
                foo=false;
239
 
              }
240
 
              else{
241
 
                userinfo.showMessage(goo+" has been succesfully created.\nPlease check its access permission.");
242
 
              }
243
 
            }
244
 
          }
245
 
          if(goo==null)foo=false;
246
 
        }
 
235
        foo=false;
 
236
        if(userinfo!=null){
 
237
          foo=userinfo.promptYesNo(bar+" does not exist.\n"+
 
238
                                   "Are you sure you want to create it?"
 
239
                                   );
 
240
          goo=goo.getParentFile();
 
241
          if(foo && goo!=null && !goo.exists()){
 
242
            foo=userinfo.promptYesNo("The parent directory "+goo+" does not exist.\n"+
 
243
                                     "Are you sure you want to create it?"
 
244
                                     );
 
245
            if(foo){
 
246
              if(!goo.mkdirs()){
 
247
                userinfo.showMessage(goo+" has not been created.");
 
248
                foo=false;
 
249
              }
 
250
              else{
 
251
                userinfo.showMessage(goo+" has been succesfully created.\nPlease check its access permission.");
 
252
              }
 
253
            }
 
254
          }
 
255
          if(goo==null)foo=false;
 
256
        }
247
257
      }
248
258
      if(foo){
249
 
        try{ 
250
 
          sync(bar); 
251
 
        }
252
 
        catch(Exception e){ System.out.println("sync known_hosts: "+e); }
 
259
        try{ 
 
260
          sync(bar); 
 
261
        }
 
262
        catch(Exception e){ System.out.println("sync known_hosts: "+e); }
253
263
      }
254
264
    }
255
265
  }
289
299
  }
290
300
  public void remove(String host, String type, byte[] key){
291
301
    boolean sync=false;
 
302
    synchronized(pool){
292
303
    for(int i=0; i<pool.size(); i++){
293
304
      HostKey hk=(HostKey)(pool.elementAt(i));
 
305
      String hosts=hk.getHost();
294
306
      if(host==null ||
295
 
         (hk.getHost().equals(host) && 
 
307
         (isIncluded(hosts, host) && 
296
308
          (type==null || (hk.getType().equals(type) &&
297
309
                          (key==null || Util.array_equals(key, hk.key)))))){
298
 
        pool.removeElement(hk);
 
310
        if(hosts.equals(host)){
 
311
          pool.removeElement(hk);
 
312
        }
 
313
        else{
 
314
          hk.host=deleteSubString(hosts, host);
 
315
        }
299
316
        sync=true;
300
317
      }
301
318
    }
 
319
    }
302
320
    if(sync){
303
321
      try{sync();}catch(Exception e){};
304
322
    }
305
323
  }
306
324
 
307
 
  private void sync() throws IOException { 
 
325
  protected void sync() throws IOException { 
308
326
    if(known_hosts!=null)
309
327
      sync(known_hosts); 
310
328
  }
311
 
  private void sync(String foo) throws IOException {
 
329
  protected synchronized void sync(String foo) throws IOException {
312
330
    if(foo==null) return;
313
331
    FileOutputStream fos=new FileOutputStream(foo);
314
332
    dump(fos);
320
338
  void dump(OutputStream out) throws IOException {
321
339
    try{
322
340
      HostKey hk;
 
341
      synchronized(pool){
323
342
      for(int i=0; i<pool.size(); i++){
324
343
        hk=(HostKey)(pool.elementAt(i));
325
344
        //hk.dump(out);
337
356
        out.write(hk.getKey().getBytes());
338
357
        out.write(cr);
339
358
      }
 
359
      }
340
360
    }
341
361
    catch(Exception e){
342
362
      System.out.println(e);