| 1 | /******************************************************************** |
| 2 | * Copyright (c) 2013 - 2014, Pivotal Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Author: Zhanwei Wang |
| 6 | ********************************************************************/ |
| 7 | /******************************************************************** |
| 8 | * 2014 - |
| 9 | * open source under Apache License Version 2.0 |
| 10 | ********************************************************************/ |
| 11 | /** |
| 12 | * Licensed to the Apache Software Foundation (ASF) under one |
| 13 | * or more contributor license agreements. See the NOTICE file |
| 14 | * distributed with this work for additional information |
| 15 | * regarding copyright ownership. The ASF licenses this file |
| 16 | * to you under the Apache License, Version 2.0 (the |
| 17 | * "License"); you may not use this file except in compliance |
| 18 | * with the License. You may obtain a copy of the License at |
| 19 | * |
| 20 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 21 | * |
| 22 | * Unless required by applicable law or agreed to in writing, software |
| 23 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 24 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 25 | * See the License for the specific language governing permissions and |
| 26 | * limitations under the License. |
| 27 | */ |
| 28 | #include "KerberosName.h" |
| 29 | |
| 30 | #include "Exception.h" |
| 31 | #include "ExceptionInternal.h" |
| 32 | |
| 33 | #include <regex.h> |
| 34 | #include <string.h> |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace Hdfs { |
| 38 | namespace Internal { |
| 39 | |
| 40 | static void HandleRegError(int rc, regex_t * comp) { |
| 41 | std::vector<char> buffer; |
| 42 | size_t size = regerror(rc, comp, NULL, 0); |
| 43 | buffer.resize(size + 1); |
| 44 | regerror(rc, comp, &buffer[0], buffer.size()); |
| 45 | THROW(HdfsIOException, "KerberosName: Failed to parse Kerberos principal." ); |
| 46 | } |
| 47 | |
| 48 | KerberosName::KerberosName() { |
| 49 | } |
| 50 | |
| 51 | KerberosName::KerberosName(const std::string & principal) { |
| 52 | parse(principal); |
| 53 | } |
| 54 | |
| 55 | void KerberosName::parse(const std::string & principal) { |
| 56 | int rc; |
| 57 | static const char * pattern = "([^/@]*)(/([^/@]*))?@([^/@]*)" ; |
| 58 | regex_t comp; |
| 59 | regmatch_t pmatch[5]; |
| 60 | |
| 61 | if (principal.empty()) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | memset(&comp, 0, sizeof(regex_t)); |
| 66 | rc = regcomp(&comp, pattern, REG_EXTENDED); |
| 67 | |
| 68 | if (rc) { |
| 69 | HandleRegError(rc, &comp); |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | memset(pmatch, 0, sizeof(pmatch)); |
| 74 | rc = regexec(&comp, principal.c_str(), |
| 75 | sizeof(pmatch) / sizeof(pmatch[1]), pmatch, 0); |
| 76 | |
| 77 | if (rc && rc != REG_NOMATCH) { |
| 78 | HandleRegError(rc, &comp); |
| 79 | } |
| 80 | |
| 81 | if (rc == REG_NOMATCH) { |
| 82 | if (principal.find('@') != principal.npos) { |
| 83 | THROW(HdfsIOException, |
| 84 | "KerberosName: Malformed Kerberos name: %s" , |
| 85 | principal.c_str()); |
| 86 | } else { |
| 87 | name = principal; |
| 88 | } |
| 89 | } else { |
| 90 | if (pmatch[1].rm_so != -1) { |
| 91 | name = principal.substr(pmatch[1].rm_so, |
| 92 | pmatch[1].rm_eo - pmatch[1].rm_so); |
| 93 | } |
| 94 | |
| 95 | if (pmatch[3].rm_so != -1) { |
| 96 | host = principal.substr(pmatch[3].rm_so, |
| 97 | pmatch[3].rm_eo - pmatch[3].rm_so); |
| 98 | } |
| 99 | |
| 100 | if (pmatch[4].rm_so != -1) { |
| 101 | realm = principal.substr(pmatch[4].rm_so, |
| 102 | pmatch[4].rm_eo - pmatch[4].rm_so); |
| 103 | } |
| 104 | } |
| 105 | } catch (...) { |
| 106 | regfree(&comp); |
| 107 | throw; |
| 108 | } |
| 109 | |
| 110 | regfree(&comp); |
| 111 | } |
| 112 | |
| 113 | size_t KerberosName::hash_value() const { |
| 114 | size_t values[] = { StringHasher(name), StringHasher(host), StringHasher( |
| 115 | realm) |
| 116 | }; |
| 117 | return CombineHasher(values, sizeof(values) / sizeof(values[0])); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 | } |
| 122 | |