001: // yacyNetwork.java
002: // ----------------
003: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
004: // first published 04.07.2007 on http://yacy.net
005: //
006: // This is a part of YaCy, a peer-to-peer based web search engine
007: //
008: // $LastChangedDate: 2007-07-03 22:55:47 +0000 (Di, 03 Jul 2007) $
009: // $LastChangedRevision: 3950 $
010: // $LastChangedBy: orbiter $
011: //
012: // LICENSE
013: //
014: // This program is free software; you can redistribute it and/or modify
015: // it under the terms of the GNU General Public License as published by
016: // the Free Software Foundation; either version 2 of the License, or
017: // (at your option) any later version.
018: //
019: // This program is distributed in the hope that it will be useful,
020: // but WITHOUT ANY WARRANTY; without even the implied warranty of
021: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022: // GNU General Public License for more details.
023: //
024: // You should have received a copy of the GNU General Public License
025: // along with this program; if not, write to the Free Software
026: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
027:
028: package de.anomic.yacy;
029:
030: import java.util.Date;
031:
032: import de.anomic.plasma.plasmaSwitchboard;
033: import de.anomic.server.serverCodings;
034: import de.anomic.server.serverDate;
035: import de.anomic.server.serverObjects;
036: import de.anomic.server.serverSwitch;
037: import de.anomic.tools.crypt;
038:
039: public class yacyNetwork {
040:
041: public static final boolean authentifyRequest(serverObjects post,
042: serverSwitch env) {
043: if ((post == null) || (env == null))
044: return false;
045:
046: // identify network
047: String unitName = post.get("network.unit.name",
048: yacySeed.DFLT_NETWORK_UNIT); // the network unit
049: if (!unitName.equals(env.getConfig("network.unit.name",
050: yacySeed.DFLT_NETWORK_UNIT))) {
051: return false;
052: }
053:
054: // check authentification method
055: String authentificationControl = env.getConfig(
056: "network.unit.protocol.control", "uncontrolled");
057: if (authentificationControl.equals("uncontrolled"))
058: return true;
059: String authentificationMethod = env
060: .getConfig(
061: "network.unit.protocol.request.authentification.method",
062: "");
063: if (authentificationMethod.length() == 0) {
064: return false;
065: }
066: if (authentificationMethod.equals("salted-magic-sim")) {
067: // authentify the peer using the md5-magic
068: String salt = post.get("key", "");
069: String iam = post.get("iam", "");
070: String magic = env
071: .getConfig(
072: "network.unit.protocol.request.authentification.essentials",
073: "");
074: String md5 = serverCodings.encodeMD5Hex(salt + iam + magic);
075: return post.get("magicmd5", "").equals(md5);
076: }
077:
078: // unknown authentification method
079: return false;
080: }
081:
082: public static final serverObjects basicRequestPost(
083: serverSwitch env, String targetHash) {
084: // put in all the essentials for routing and network authentification
085: // generate a session key
086: serverObjects post = new serverObjects();
087: final String salt = crypt.randomSalt();
088: post.put("key", salt);
089:
090: // just standard identification essentials
091: post.put("iam", yacyCore.seedDB.mySeed().hash);
092: if (targetHash != null)
093: post.put("youare", targetHash);
094:
095: // time information for synchronization
096: post.put("mytime", serverDate.formatShortSecond(new Date()));
097: post.put("myUTC", System.currentTimeMillis());
098:
099: // network identification
100: post.put("network.unit.name", plasmaSwitchboard
101: .getSwitchboard().getConfig("network.unit.name",
102: yacySeed.DFLT_NETWORK_UNIT));
103:
104: // authentification essentials
105: String authentificationControl = env.getConfig(
106: "network.unit.protocol.control", "uncontrolled");
107: String authentificationMethod = env
108: .getConfig(
109: "network.unit.protocol.request.authentification.method",
110: "");
111: if ((authentificationControl.equals("controlled"))
112: && (authentificationMethod.length() > 0)) {
113: if (authentificationMethod.equals("salted-magic-sim")) {
114: // generate an authentification essential using the salt, the iam-hash and the network magic
115: String magic = env
116: .getConfig(
117: "network.unit.protocol.request.authentification.essentials",
118: "");
119: String md5 = serverCodings.encodeMD5Hex(salt
120: + yacyCore.seedDB.mySeed().hash + magic);
121: post.put("magicmd5", md5);
122: }
123: }
124:
125: return post;
126: }
127:
128: }
|