1 /*
2  * Copyright (C) 2006-2021 Registro.br. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  * 1. Redistribution of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY REGISTRO.BR ``AS IS AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIE OF FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16  * EVENT SHALL REGISTRO.BR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
19  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
21  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
22  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23  * DAMAGE.
24  */
25 /* $Id$ */
26 /** @file CommonData.H
27  *  @brief EPP CommonData Class
28  */
29 
30 #ifndef __COMMON_DATA_H__
31 #define __COMMON_DATA_H__
32 
33 #include <set>
34 #include <string>
35 #include <vector>
36 
37 #include "libepp_nicbr.H"
38 
39 using std::set;
40 using std::string;
41 using std::vector;
42 
43 LIBEPP_NICBR_NS_BEGIN
44 
45 /// Action Types
46 enum ActionType
47 {
48 	UNSET_ACTION = -1,
49 	LOGIN = 0,
50 	LOGOUT,
51 	HELLO,
52 	POLL,
53 	GREETING,
54 	CONTACT_CHECK = 10,
55 	CONTACT_CREATE,
56 	CONTACT_INFO,
57 	CONTACT_TRANSFER,
58 	CONTACT_UPDATE,
59 	CONTACT_PANDATA,
60 	CONTACT_DELETE,
61 	DOMAIN_CHECK = 20,
62 	DOMAIN_CREATE,
63 	DOMAIN_INFO,
64 	DOMAIN_TRANSFER,
65 	DOMAIN_RENEW,
66 	DOMAIN_UPDATE,
67 	DOMAIN_PANDATA,
68 	DOMAIN_DELETE,
69 	BR_ORG_CHECK = 30,
70 	BR_ORG_INFO,
71 	BR_ORG_CREATE,
72 	BR_ORG_UPDATE,
73 	BR_ORG_PANDATA,
74 	BR_ORG_DELETE,
75 	BR_DOMAIN_CHECK = 40,
76 	BR_DOMAIN_INFO,
77 	BR_DOMAIN_CREATE,
78 	BR_DOMAIN_RENEW,
79 	BR_DOMAIN_UPDATE,
80 	BR_DOMAIN_PANDATA,
81 	IP_NETWORK_CREATE = 50,
82 	IP_NETWORK_CHECK,
83 	IP_NETWORK_DELETE,
84 	IP_NETWORK_RENEW,
85 	IP_NETWORK_TRANSFER,
86 	IP_NETWORK_INFO,
87 	IP_NETWORK_UPDATE,
88 	ASN_CHECK = 60,
89 	ASN_CREATE,
90 	ASN_DELETE,
91 	ASN_RENEW,
92 	ASN_TRANSFER,
93 	ASN_INFO,
94 	ASN_UPDATE,
95 	DEF_REG_CHECK = 70,
96 	DEF_REG_INFO,
97 	DEF_REG_TRANSFER,
98 	DEF_REG_CREATE,
99 	DEF_REG_DELETE,
100 	DEF_REG_RENEW,
101 	DEF_REG_UPDATE,
102 	ASN_RESERVE_CREATE = 80,
103 	ASN_RESERVE_DELETE,
104 	CONTACT_SWAP_DOMAIN,
105 	CONTACT_SWAP_ORG,
106 };
107 
108 /// AuthInfo Class
109 class AuthInfo
110 {
111 public:
112 	AuthInfo &operator=(const AuthInfo &authInfo)
113 	{
114 		_roid = authInfo._roid;
115 		_roid_f = authInfo._roid_f;
116 		_pw = authInfo._pw;
117 		_pw_f = authInfo._pw_f;
118 		return *this;
119 	}
120 
AuthInfo(const AuthInfo & authInfo)121 	AuthInfo(const AuthInfo &authInfo)
122 	{
123 		_roid = authInfo._roid;
124 		_roid_f = authInfo._roid_f;
125 		_pw = authInfo._pw;
126 		_pw_f = authInfo._pw_f;
127 	}
128 
129 	/// Default constructor
AuthInfo()130 	AuthInfo() { this->reset(); }
131 
132 	/// Sets repository object ID
133 	/**
134 	   @param roid   repository object id
135 	*/
set_roid(const string & roid)136 	void set_roid(const string &roid)
137 	{
138 		_roid = roid;
139 		if (roid != "") {
140 			_roid_f = true;
141 		}
142 	}
143 
144 	/// Returns repository object ID
145 	/**
146 	   @return repository object ID
147 	*/
get_roid()148 	string get_roid() const { return _roid; }
149 
150 	/// Returns the roid change flag
151 	/**
152 	   @return roid change flag
153 	*/
get_roid_f()154 	bool get_roid_f() const { return _roid_f; }
155 
156 	/// Sets password
157 	/**
158 	   @param pw   password
159 	*/
set_pw(const string & pw)160 	void set_pw(const string &pw)
161 	{
162 		_pw = pw;
163 		_pw_f = true;
164 	}
165 
166 	/// Returns the password
167 	/**
168 	   @return password
169 	*/
get_pw()170 	string get_pw() const { return _pw; }
171 
172 	/// Returns the password change flag
173 	/**
174 	   @return password change flag
175 	*/
get_pw_f()176 	bool get_pw_f() const { return _pw_f; }
177 
178 	/// Reset all object attributes
reset()179 	void reset()
180 	{
181 		_roid = "";
182 		_roid_f = false;
183 		_pw = "";
184 		_pw_f = false;
185 	}
186 
187 protected:
188 	/// repository object ID
189 	string _roid;
190 
191 	/// roid change flag
192 	bool _roid_f;
193 
194 	/// password
195 	string _pw;
196 
197 	/// password change flag
198 	bool _pw_f;
199 };
200 
201 struct NSIPAddr
202 {
203 	string version;
204 	string addr;
205 	bool operator<(const NSIPAddr &ip) const { return addr < ip.addr; }
206 };
207 
208 struct NameServer
209 {
210 	string name;
211 	set<NSIPAddr> ips;
212 	bool operator<(const NameServer &n) const { return name < n.name; }
213 };
214 
215 /// Describes IpRange structure
216 class IpRange
217 {
218 public:
IpRange()219 	IpRange() { this->reset(); }
220 
221 	bool operator<(const IpRange &ipRange) const
222 	{
223 		if (_ipBegin.compare(ipRange._ipBegin) < 0) {
224 			return true;
225 		}
226 
227 		if (_ipBegin.compare(ipRange._ipBegin) == 0) {
228 			if (_ipEnd.compare(ipRange._ipEnd) < 0) {
229 				return true;
230 			}
231 
232 			return false;
233 		}
234 
235 		return false;
236 	}
237 
reset()238 	void reset()
239 	{
240 		_version = "";
241 		_ipBegin = "";
242 		_ipEnd = "";
243 		_available = false;
244 	}
245 
set_version(const string & version)246 	void set_version(const string &version) { _version = version; }
247 
get_version()248 	string get_version() const { return _version; }
249 
set_ipBegin(const string & ipBegin)250 	void set_ipBegin(const string &ipBegin) { _ipBegin = ipBegin; }
251 
get_ipBegin()252 	string get_ipBegin() const { return _ipBegin; }
253 
set_ipEnd(const string & ipEnd)254 	void set_ipEnd(const string &ipEnd) { _ipEnd = ipEnd; }
255 
get_ipEnd()256 	string get_ipEnd() const { return _ipEnd; }
257 
set_available(const bool & available)258 	void set_available(const bool &available) { _available = available; }
259 
get_available()260 	bool get_available() const { return _available; }
261 
262 private:
263 	string _version;
264 	string _ipBegin;
265 	string _ipEnd;
266 
267 	// Used in ip range check command
268 	bool _available;
269 };
270 
271 #if USE_IP_MANAGEMENT
272 struct ReverseDns
273 {
274 	IpRange ipRange;
275 	vector<string> nameservers;
276 };
277 #endif // USE_IP_MANAGEMENT
278 
279 /// PostalInfo class
280 class PostalInfo
281 {
282 public:
PostalInfo()283 	PostalInfo() { this->reset(); }
284 
reset()285 	void reset()
286 	{
287 		_type_f = false;
288 		_name_f = false;
289 		_org_f = false;
290 		_str1_f = false;
291 		_str2_f = false;
292 		_str3_f = false;
293 		_city_f = false;
294 		_sp_f = false;
295 		_pc_f = false;
296 		_cc_f = false;
297 	}
298 
get_type()299 	string get_type() const { return _type; }
300 
set_type(const string & type)301 	void set_type(const string &type)
302 	{
303 		_type = type;
304 		_type_f = true;
305 	}
306 
get_name()307 	string get_name() const { return _name; }
308 
set_name(const string & name)309 	void set_name(const string &name)
310 	{
311 		_name = name;
312 		_name_f = true;
313 	}
314 
get_org()315 	string get_org() const { return _org; }
316 
set_org(const string & org)317 	void set_org(const string &org)
318 	{
319 		_org = org;
320 		_org_f = true;
321 	}
322 
get_str1()323 	string get_str1() const { return _str1; }
324 
set_str1(const string & str1)325 	void set_str1(const string &str1)
326 	{
327 		_str1 = str1;
328 		_str1_f = true;
329 	}
330 
get_str2()331 	string get_str2() const { return _str2; }
332 
set_str2(const string & str2)333 	void set_str2(const string &str2)
334 	{
335 		_str2 = str2;
336 		_str2_f = true;
337 	}
338 
get_str3()339 	string get_str3() const { return _str3; }
340 
set_str3(const string & str3)341 	void set_str3(const string &str3)
342 	{
343 		_str3 = str3;
344 		_str3_f = true;
345 	}
346 
get_city()347 	string get_city() const { return _city; }
348 
set_city(const string & city)349 	void set_city(const string &city)
350 	{
351 		_city = city;
352 		_city_f = true;
353 	}
354 
get_sp()355 	string get_sp() const { return _sp; }
356 
set_sp(const string & sp)357 	void set_sp(const string &sp)
358 	{
359 		_sp = sp;
360 		_sp_f = true;
361 	}
362 
get_pc()363 	string get_pc() const { return _pc; }
364 
set_pc(const string & pc)365 	void set_pc(const string &pc)
366 	{
367 		_pc = pc;
368 		_pc_f = true;
369 	}
370 
get_cc()371 	string get_cc() const { return _cc; }
372 
set_cc(const string & cc)373 	void set_cc(const string &cc)
374 	{
375 		_cc = cc;
376 		_cc_f = true;
377 	}
378 
get_type_f()379 	bool get_type_f() const { return _type_f; }
380 
get_name_f()381 	bool get_name_f() const { return _name_f; }
382 
get_org_f()383 	bool get_org_f() const { return _org_f; }
384 
get_str1_f()385 	bool get_str1_f() const { return _str1_f; }
386 
get_str2_f()387 	bool get_str2_f() const { return _str2_f; }
388 
get_str3_f()389 	bool get_str3_f() const { return _str3_f; }
390 
get_city_f()391 	bool get_city_f() const { return _city_f; }
392 
get_sp_f()393 	bool get_sp_f() const { return _sp_f; }
394 
get_pc_f()395 	bool get_pc_f() const { return _pc_f; }
396 
get_cc_f()397 	bool get_cc_f() const { return _cc_f; }
398 
399 private:
400 	// Flags for PostalInfo attributes
401 	bool _type_f;
402 	bool _name_f;
403 	bool _org_f;
404 	bool _str1_f;
405 	bool _str2_f;
406 	bool _str3_f;
407 	bool _city_f;
408 	bool _sp_f;
409 	bool _pc_f;
410 	bool _cc_f;
411 
412 	// PostalInfo attributes
413 	string _type;
414 	string _name;
415 	string _org;
416 	string _str1;
417 	string _str2;
418 	string _str3;
419 	string _city;
420 	string _sp;
421 	string _pc;
422 	string _cc;
423 };
424 
425 /// EPP CommonData Class
426 class CommonData
427 {
428 public:
429 	struct Disclose
430 	{
431 		int flag;
432 
433 		bool name_int;
434 		bool name_loc;
435 		bool org_int;
436 		bool org_loc;
437 		bool addr_int;
438 		bool addr_loc;
439 		bool voice;
440 		bool fax;
441 		bool email;
442 
DiscloseDisclose443 		Disclose()
444 		{
445 			flag = -1;
446 			name_int = false;
447 			name_loc = false;
448 			org_int = false;
449 			org_loc = false;
450 			addr_int = false;
451 			addr_loc = false;
452 			voice = false;
453 			fax = false;
454 			email = false;
455 		}
456 
is_setDisclose457 		bool is_set()
458 		{
459 			return ((flag == 0 || flag == 1)
460 			        && (name_int || name_loc || org_int || org_loc || addr_int || addr_loc || voice
461 			            || fax || email));
462 		}
463 	};
464 
465 	struct Phone
466 	{
467 		string ext;
468 		string number;
469 	};
470 
471 	/// Constructor
CommonData()472 	CommonData() { this->reset(); }
473 
474 	/// Sets the object's id
475 	/**
476 	   @param id contact id
477 	*/
478 	void set_id(const string &id);
479 
480 	/// Inserts postal information into the object
481 	/**
482 	   @param postal_info postal information
483 	*/
484 	void insert_postal_info(const PostalInfo &postal_info);
485 
486 	/// Sets the object's voice telephone number
487 	/**
488 	   @param voice contact voice telephone
489 	*/
490 	void set_voice(const Phone &voice);
491 
492 	/// Sets the object's fax number
493 	/**
494 	   @param  fax contact fax
495 	*/
496 	void set_fax(const Phone &fax);
497 
498 	/// Sets the object's email
499 	/**
500 	   @param email contact email
501 	*/
502 	void set_email(const string &email);
503 
504 	/// Sets the object's disclosure policy
505 	/**
506 	   @param disclose disclose information
507 	*/
508 	void set_disclose(const Disclose &disclose);
509 
510 	/// Returns the object's id
511 	/**
512 	   @return Contact id
513 	*/
514 	string get_id() const;
515 
516 	/// Returns the object's postal information
517 	/**
518 	   @return Postal info list
519 	*/
520 	vector<PostalInfo> get_postal_info() const;
521 
522 	/// Returns the object's voice telephone number
523 	/**
524 	   @return voice contact voice telephone number
525 	*/
526 	Phone get_voice() const;
527 
528 	/// Returns the object's voice change flag
529 	/**
530 	   @return contact voice change flag
531 	*/
get_voice_f()532 	bool get_voice_f() const { return _voice_f; }
533 
534 	/// Returns the object's fax number
535 	/**
536 	   @return fax contact fax number
537 	*/
538 	Phone get_fax() const;
539 
540 	/// Returns the object's fax change flag
541 	/**
542 	   @return contact fax change flag
543 	*/
get_fax_f()544 	bool get_fax_f() const { return _fax_f; }
545 
546 	/// Returns the object's email
547 	/**
548 	   @return contact email
549 	*/
550 	string get_email() const;
551 
552 	/// Returns the object's email change flag
553 	/**
554 	   @return contact email change flag
555 	*/
get_email_f()556 	bool get_email_f() const { return _email_f; }
557 
558 	/// Returns the object's disclosure policy
559 	/**
560 	   @return disclose disclose information
561 	*/
562 	Disclose get_disclose() const;
563 
564 	/// reset attributes
565 	void reset();
566 
567 protected:
568 	/// Contact id
569 	string _id;
570 
571 	/// Postal Info list (just 1 or 2 elements)
572 	vector<PostalInfo> _postal_info;
573 
574 	/// Contact's voice telephone number (optional)
575 	Phone _voice;
576 
577 	/// Voice change flag
578 	bool _voice_f;
579 
580 	/// Contact's fax (optional)
581 	Phone _fax;
582 
583 	/// Fax change flag
584 	bool _fax_f;
585 
586 	/// Contact's email
587 	string _email;
588 
589 	/// E-mail change flag
590 	bool _email_f;
591 
592 	/// Disclose information (optional)
593 	Disclose _disclose;
594 };
595 
596 class SuspendedStatus
597 {
598 public:
599 	enum Value
600 	{
601 		UNDEFINED,
602 		NORMAL,
603 		SUSPENDED
604 	};
605 };
606 
607 /// EPP RGP Status Class
608 class RGPStatus
609 {
610 public:
611 	/// List of RGP status acording to RFC 3915
612 	enum Value
613 	{
614 		NONE,
615 		ADD_PERIOD,
616 		AUTO_RENEW_PERIOD,
617 		RENEW_PERIOD,
618 		TRANSFER_PERIOD,
619 		PENDING_DELETE,
620 		PENDING_RESTORE,
621 		REDEMPTION_PERIOD,
622 	};
623 
624 	/// Convert a text based status into a enum status
625 	/**
626 	   @param value text based status acording to RFC3915
627 	   @return enum correspondent status
628 	 */
stringToStatus(const string & value)629 	static Value stringToStatus(const string &value)
630 	{
631 		if (value == "addPeriod") {
632 			return ADD_PERIOD;
633 		} else if (value == "autoRenewPeriod") {
634 			return AUTO_RENEW_PERIOD;
635 		} else if (value == "renewPeriod") {
636 			return RENEW_PERIOD;
637 		} else if (value == "transferPeriod") {
638 			return TRANSFER_PERIOD;
639 		} else if (value == "pendingDelete") {
640 			return PENDING_DELETE;
641 		} else if (value == "pendingRestore") {
642 			return PENDING_RESTORE;
643 		} else if (value == "redemptionPeriod") {
644 			return REDEMPTION_PERIOD;
645 		}
646 
647 		return NONE;
648 	}
649 
650 	/// Convert a enum based status into a text status
651 	/**
652 	   @param value enum based status
653 	   @return text correspondent status acording to RFC3915
654 	 */
statusToString(const Value value)655 	static string statusToString(const Value value)
656 	{
657 		switch (value) {
658 		case NONE:
659 			break;
660 		case ADD_PERIOD:
661 			return "addPeriod";
662 		case AUTO_RENEW_PERIOD:
663 			return "autoRenewPeriod";
664 		case RENEW_PERIOD:
665 			return "renewPeriod";
666 		case TRANSFER_PERIOD:
667 			return "transferPeriod";
668 		case PENDING_DELETE:
669 			return "pendingDelete";
670 		case PENDING_RESTORE:
671 			return "pendingRestore";
672 		case REDEMPTION_PERIOD:
673 			return "redemptionPeriod";
674 		}
675 
676 		return "";
677 	}
678 };
679 
680 /// EPP defensive registration Level Class
681 class DefRegLevel
682 {
683 public:
684 	/// Possible name's values
685 	enum Value
686 	{
687 		NONE,
688 		PREMIUM,
689 		STANDARD
690 	};
691 
692 	/// Convert name's level into text conforming
693 	/// defensive-registration-mapping from Verisign
694 	/*
695 	  @param level Enum name's level
696 	  @return text representation of the level
697 	*/
toStr(const Value level)698 	static string toStr(const Value level)
699 	{
700 		switch (level) {
701 		case NONE:
702 			break;
703 		case PREMIUM:
704 			return "premium";
705 		case STANDARD:
706 			return "standard";
707 		}
708 
709 		return "";
710 	}
711 
712 	/// Convert a text into enum conforming
713 	/// defensive-registration-mapping from Verisign
714 	/*
715 	  @param level text based level
716 	  @return enum representation of the level
717 	*/
fromStr(const string & level)718 	static Value fromStr(const string &level)
719 	{
720 		if (level == "premium") {
721 			return PREMIUM;
722 		} else if (level == "standard") {
723 			return STANDARD;
724 		}
725 
726 		return NONE;
727 	}
728 };
729 
730 /// EPP defensive registration Name Class
731 class DefRegName
732 {
733 public:
734 	/// Default constructor
DefRegName()735 	DefRegName() : _name(""), _level(DefRegLevel::NONE) {}
736 
737 	/// Constructor
738 	/*
739 	  @param name Defensive registration name
740 	  @param level Name's level
741 	*/
DefRegName(const string & name,const DefRegLevel::Value level)742 	DefRegName(const string &name, const DefRegLevel::Value level) : _name(name), _level(level) {}
743 
744 	/// Less operator to allow using this object in a set
745 	/// container. Cannot exist more than one Name object with the
746 	/// same name in a list
747 	/*
748 	  @param other other Name object
749 	  @return true if one name is less than the other or false otherwise
750 	*/
751 	bool operator<(const DefRegName &other) const { return _name < other._name; }
752 
753 	/// Sets the name
754 	/*
755 	  @param name name
756 	*/
set_name(const string & name)757 	void set_name(const string &name) { _name = name; }
758 
759 	/// Returns the name
760 	/*
761 	  @return name
762 	*/
get_name()763 	string get_name() const { return _name; }
764 
765 	/// Sets the name's level
766 	/*
767 	  @param level name's level
768 	*/
set_level(const DefRegLevel::Value level)769 	void set_level(const DefRegLevel::Value level) { _level = level; }
770 
771 	/// Return name's level
772 	/*
773 	  @return name's level
774 	*/
get_level()775 	DefRegLevel::Value get_level() const { return _level; }
776 
777 	/// Resets object attributes
reset()778 	void reset()
779 	{
780 		_name = "";
781 		_level = DefRegLevel::NONE;
782 	}
783 
784 private:
785 	/// Defensive registration name
786 	string _name;
787 	/// Name's level
788 	DefRegLevel::Value _level;
789 };
790 
791 /// EPP Transfer Operation Class
792 class TransferOperation
793 {
794 public:
795 	/// Possible operation values
796 	enum Value
797 	{
798 		NONE,
799 		QUERY,
800 		REQUEST,
801 		CANCEL,
802 		APPROVE,
803 		REJECT
804 	};
805 
806 	/// Convert operation into text. Operations are listed in RFC 5730
807 	/// (section 2.9.3.4). Syntax defined in RFC 5730 (transferOpType)
808 	/*
809 	  @param operation transfer operation
810 	  @return text representation of the operation
811 	*/
toStr(const Value operation)812 	static string toStr(const Value operation)
813 	{
814 		switch (operation) {
815 		case NONE:
816 			break;
817 		case QUERY:
818 			return "query";
819 		case REQUEST:
820 			return "request";
821 		case CANCEL:
822 			return "cancel";
823 		case APPROVE:
824 			return "approve";
825 		case REJECT:
826 			return "reject";
827 		}
828 
829 		return "";
830 	}
831 
832 	/// Convert a text into enum. Operations are listed in RFC 5730
833 	/// (section 2.9.3.4). Syntax defined in RFC 5730 (transferOpType)
834 	/*
835 	  @param operationStr text based operation
836 	  @return enum representation of the operation
837 	*/
fromStr(const string operationStr)838 	static Value fromStr(const string operationStr)
839 	{
840 		if (operationStr == "query") {
841 			return QUERY;
842 		} else if (operationStr == "request") {
843 			return REQUEST;
844 		} else if (operationStr == "cancel") {
845 			return CANCEL;
846 		} else if (operationStr == "approve") {
847 			return APPROVE;
848 		} else if (operationStr == "reject") {
849 			return REJECT;
850 		}
851 
852 		return NONE;
853 	}
854 };
855 
856 LIBEPP_NICBR_NS_END
857 #endif //__COMMON_DATA_H__
858