| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | package file | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2017-06-21 23:46:20 -07:00
										 |  |  | 	"net" | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/rcode" | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request" | 
					
						
							| 
									
										
										
										
											2016-04-03 07:44:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	"github.com/miekg/dns" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | // isNotify checks if state is a notify message and if so, will *also* check if it | 
					
						
							|  |  |  | // is from one of the configured masters. If not it will not be a valid notify | 
					
						
							|  |  |  | // message. If the zone z is not a secondary zone the message will also be ignored. | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | func (z *Zone) isNotify(state request.Request) bool { | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 	if state.Req.Opcode != dns.OpcodeNotify { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if len(z.TransferFrom) == 0 { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-21 23:46:20 -07:00
										 |  |  | 	// If remote IP matches we accept. | 
					
						
							|  |  |  | 	remote := state.IP() | 
					
						
							|  |  |  | 	for _, f := range z.TransferFrom { | 
					
						
							|  |  |  | 		from, _, err := net.SplitHostPort(f) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		if from == remote { | 
					
						
							|  |  |  | 			return true | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return false | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Notify will send notifies to all configured TransferTo IP addresses. | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | func (z *Zone) Notify() { | 
					
						
							| 
									
										
										
										
											2016-04-15 14:26:27 +01:00
										 |  |  | 	go notify(z.origin, z.TransferTo) | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | // notify sends notifies to the configured remote servers. It will try up to three times | 
					
						
							|  |  |  | // before giving up on a specific remote. We will sequentially loop through "to" | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | // until they all have replied (or have 3 failed attempts). | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | func notify(zone string, to []string) error { | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	m := new(dns.Msg) | 
					
						
							|  |  |  | 	m.SetNotify(zone) | 
					
						
							|  |  |  | 	c := new(dns.Client) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-03 09:02:34 +01:00
										 |  |  | 	for _, t := range to { | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		if t == "*" { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 		if err := notifyAddr(c, m, t); err != nil { | 
					
						
							| 
									
										
										
										
											2018-04-19 07:41:56 +01:00
										 |  |  | 			log.Error(err.Error()) | 
					
						
							| 
									
										
										
										
											2016-04-05 07:37:05 +01:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-27 13:54:23 +00:00
										 |  |  | 	log.Infof("Sent notifies for zone %q to %v", zone, to) | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 	return nil | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-06 05:54:24 -07:00
										 |  |  | func notifyAddr(c *dns.Client, m *dns.Msg, s string) error { | 
					
						
							|  |  |  | 	var err error | 
					
						
							| 
									
										
										
										
											2016-11-08 21:45:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	code := dns.RcodeServerFailure | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 	for i := 0; i < 3; i++ { | 
					
						
							| 
									
										
										
										
											2017-08-06 05:54:24 -07:00
										 |  |  | 		ret, _, err := c.Exchange(m, s) | 
					
						
							| 
									
										
										
										
											2016-04-05 10:53:23 +01:00
										 |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-11-07 19:15:21 +00:00
										 |  |  | 		code = ret.Rcode | 
					
						
							|  |  |  | 		if code == dns.RcodeSuccess { | 
					
						
							| 
									
										
										
										
											2016-04-03 07:37:41 +01:00
										 |  |  | 			return nil | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-11-08 21:45:27 +00:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 		return fmt.Errorf("notify for zone %q was not accepted by %q: %q", m.Question[0].Name, s, err) | 
					
						
							| 
									
										
										
										
											2016-11-08 21:45:27 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-06-14 09:37:10 -07:00
										 |  |  | 	return fmt.Errorf("notify for zone %q was not accepted by %q: rcode was %q", m.Question[0].Name, s, rcode.ToString(code)) | 
					
						
							| 
									
										
										
										
											2016-03-28 12:08:05 +01:00
										 |  |  | } |