e98d4479d62034e518fd6c7dfa85c08ed67d5e0f
[gssweb.git] / json_gssapi / src / datamodel / GSSName.cpp
1 /*
2  * Copyright (c) 2014 <copyright holder> <email>
3  * 
4  * For license details, see the LICENSE file in the root of this project.
5  * 
6  */
7
8 #include "GSSName.h"
9 #include "../GSSException.h" 
10
11 void GSSName::init(const GSSBuffer namestr, GSSOID name_type, bool skipRelease, gss_imp_name_type fn)
12 {
13   /* Variables */
14   /* Error checking */
15   /* Setup */
16   /* Main */ 
17   this->major_status = fn(&(this->minor_status), namestr.toGss(), name_type.toGss(), &name);
18   if ( GSS_ERROR(this->major_status) )
19   {
20     std::string errMsg;
21     errMsg += "Cannot import name: ";
22     errMsg += namestr.toString();
23     throw GSSException(errMsg.c_str(), this->major_status, this->minor_status, name_type.toGss());
24   }
25   this->skipRelease = skipRelease;
26   
27   /* Cleanup */
28   /* Return */
29 }
30
31
32 GSSName::GSSName ( gss_name_t gss_name, bool skipRelease )
33
34   name = gss_name; 
35   this->skipRelease = skipRelease; 
36   
37 }
38  
39
40 void GSSName::release()
41 {
42   /* Variables */
43   OM_uint32 major, minor;
44   
45   /* Error checking */
46   if (GSS_C_NO_NAME == name || skipRelease)
47     return;
48   
49   /* Setup */
50   /* Main */
51   //  if (hashKey.length() > 0)
52   //    std::cout << std::endl << "About to release name for key " << hashKey << std::endl;
53   
54   major = gss_release_name(&minor, &name);
55   name = GSS_C_NO_NAME;
56   hashKey = "";
57   if ( GSS_ERROR(major) && !( major & GSS_S_BAD_NAME ) )
58   {
59     throw GSSException("Cannot free memory for a GSS name.", major, minor);
60   }
61
62   /* Cleanup */
63   /* Return */
64 }
65
66 GSSName::GSSName ( const GSSName& n )
67 {
68   this->hashKey = n.hashKey;
69   if (GSS_C_NO_NAME == n.name)
70   {
71     major_status = 0;
72     minor_status = 0;
73     name = GSS_C_NO_NAME;
74   }
75   else
76   {
77     this->major_status = gss_duplicate_name(&minor_status, n.name, &name);
78     if ( GSS_ERROR(major_status) )
79     {
80       throw GSSException("Cannot copy a GSS name.", major_status, minor_status);
81     }
82   }
83   this->skipRelease = false;
84 }
85
86 GSSName::~GSSName()
87 {
88   this->release();
89 }  
90
91 GSSName& GSSName::operator= ( const GSSName& rhs )
92 {
93   if (rhs.toGss() != this->toGss())
94   {
95     this->function = rhs.function;
96     if (GSS_C_NO_NAME == rhs.name)
97     {
98       major_status = 0;
99       minor_status = 0;
100       name = GSS_C_NO_NAME;
101     }
102     else
103     {
104       this->major_status = gss_duplicate_name(&minor_status, rhs.name, &name);
105       if ( GSS_ERROR(major_status) )
106       {
107         throw GSSException("Cannot copy a GSS name.", major_status, minor_status);
108       }
109     }
110     this->hashKey = "";
111     this->skipRelease = rhs.skipRelease;
112   }
113   return *this;
114 }
115
116
117 std::string GSSName::toString() const
118 {
119   /* Variables */
120   OM_uint32 major, minor;
121   gss_OID oid;
122   GSSBuffer buf;
123   
124   /* Error checking */
125   /* Setup */
126   /* Main */ 
127   major = gss_display_name(&minor, (this->name), buf.toGss(), &oid);
128   if ( GSS_ERROR(major) )
129   {
130     throw GSSException("Cannot convert a GSS name to a string.", major, minor);
131   }
132   
133   /* Cleanup */
134   /* Return */
135   return buf.toString();
136 }
137
138 bool GSSName::setValue ( gss_name_t newName, bool skipRelease )
139 {
140   this->release();
141   this->name = newName;
142   this->skipRelease = skipRelease;
143   return(true);
144 }