etc/services - sync with NetBSD-8
[minix.git] / external / bsd / bind / dist / contrib / zkt-1.1.3 / zkt-soaserial.c
blob26a695c831b1c7c3b9e6187cac0546890a9f944b
1 /* $NetBSD: zkt-soaserial.c,v 1.1.1.1 2015/07/08 15:37:48 christos Exp $ */
3 /*****************************************************************
4 **
5 ** @(#) zkt-soaserial.c (c) Oct 2007 Holger Zuleger hznet.de
6 **
7 ** A small utility to print out the (unixtime) soa serial
8 ** number in a human readable form
9 **
10 ** Copyright (c) Oct 2007, Holger Zuleger HZnet. All rights reserved.
12 ** This software is open source.
14 ** Redistribution and use in source and binary forms, with or without
15 ** modification, are permitted provided that the following conditions
16 ** are met:
18 ** Redistributions of source code must retain the above copyright notice,
19 ** this list of conditions and the following disclaimer.
21 ** Redistributions in binary form must reproduce the above copyright notice,
22 ** this list of conditions and the following disclaimer in the documentation
23 ** and/or other materials provided with the distribution.
25 ** Neither the name of Holger Zuleger HZnet nor the names of its contributors may
26 ** be used to endorse or promote products derived from this software without
27 ** specific prior written permission.
29 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
31 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
32 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
33 ** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
34 ** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
35 ** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
36 ** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
37 ** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 ** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 ** POSSIBILITY OF SUCH DAMAGE.
41 *****************************************************************/
42 # include <stdio.h>
43 # include <string.h>
44 # include <sys/types.h>
45 # include <time.h>
46 # include <utime.h>
47 # include <assert.h>
48 # include <stdlib.h>
49 # include <ctype.h>
50 #ifdef HAVE_CONFIG_H
51 # include <config.h>
52 #endif
53 # include "config_zkt.h"
55 static const char *progname;
57 static char *timestr (time_t sec);
58 static int read_serial_fromfile (const char *fname, unsigned long *serial);
59 static void printserial (const char *fname, unsigned long serial);
60 static void usage (const char *msg);
62 /*****************************************************************
63 ** timestr (sec)
64 *****************************************************************/
65 static char *timestr (time_t sec)
67 struct tm *t;
68 static char timestr[31+1]; /* 27+1 should be enough */
70 #if defined(HAVE_STRFTIME) && HAVE_STRFTIME
71 t = localtime (&sec);
72 strftime (timestr, sizeof (timestr), "%b %d %Y %T %z", t);
73 #else
74 static char *mstr[] = {
75 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
76 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
78 int h, s;
80 t = localtime (&sec);
81 s = abs (t->tm_gmtoff);
82 h = t->tm_gmtoff / 3600;
83 s = t->tm_gmtoff % 3600;
84 snprintf (timestr, sizeof (timestr), "%s %2d %4d %02d:%02d:%02d %c%02d%02d",
85 mstr[t->tm_mon], t->tm_mday, t->tm_year + 1900,
86 t->tm_hour, t->tm_min, t->tm_sec,
87 t->tm_gmtoff < 0 ? '-': '+',
88 h, s);
89 #endif
91 return timestr;
95 /****************************************************************
97 ** int read_serial_fromfile (filename)
99 ** This function depends on a special syntax formating the
100 ** SOA record in the zone file!!
102 ** To match the SOA record, the SOA RR must be formatted
103 ** like this:
104 ** @ IN SOA <master.fq.dn.> <hostmaster.fq.dn.> (
105 ** <SPACEes or TABs> 1234567890; serial number
106 ** <SPACEes or TABs> 86400 ; other values
107 ** ...
109 ****************************************************************/
110 static int read_serial_fromfile (const char *fname, unsigned long *serial)
112 FILE *fp;
113 char buf[4095+1];
114 char master[254+1];
115 int c;
116 int soafound;
118 if ( (fp = fopen (fname, "r")) == NULL )
119 return -1; /* file not found */
121 /* read until the line matches the beginning of a soa record ... */
122 soafound = 0;
123 while ( !soafound && fgets (buf, sizeof buf, fp) )
125 if ( sscanf (buf, "%*s %*d IN SOA %255s %*s (\n", master) == 1 )
126 soafound = 1;
127 else if ( sscanf (buf, "%*s IN SOA %255s %*s (\n", master) == 1 )
128 soafound = 1;
131 if ( !soafound )
132 return -2; /* no zone file (soa not found) */
134 /* move forward until any non ws is reached */
135 while ( (c = getc (fp)) != EOF && isspace (c) )
137 ungetc (c, fp); /* pushback the non ws */
139 *serial = 0L; /* read in the current serial number */
140 if ( fscanf (fp, "%lu", serial) != 1 ) /* try to get serial no */
141 return -3; /* no serial number found */
143 fclose (fp);
145 return 0; /* ok! */
148 /*****************************************************************
149 ** printserial()
150 *****************************************************************/
151 static void printserial (const char *fname, unsigned long serial)
153 if ( fname && *fname )
154 printf ("%-30s\t", fname);
156 printf ("%10lu", serial);
158 /* try to guess the soa serial format */
159 if ( serial < 1136070000L ) /* plain integer (this is 2006-1-1 00:00 in unixtime format) */
161 else if ( serial > 2006010100L ) /* date format */
163 int y, m, d, v;
165 v = serial % 100;
166 serial /= 100;
167 d = serial % 100;
168 serial /= 100;
169 m = serial % 100;
170 serial /= 100;
171 y = serial;
173 printf ("\t%d-%02d-%02d Version %02d", y, m, d, v);
175 else /* unixtime */
176 printf ("\t%s\n", timestr (serial) );
178 printf ("\n");
181 /*****************************************************************
182 ** usage (msg)
183 *****************************************************************/
184 static void usage (const char *msg)
186 if ( msg && *msg )
187 fprintf (stderr, "%s\n", msg);
188 fprintf (stderr, "usage: %s {-s serial | signed_zonefile [...]}\n", progname);
190 exit (1);
193 /*****************************************************************
194 ** main()
195 *****************************************************************/
196 int main (int argc, char *argv[])
198 unsigned long serial;
200 progname = *argv;
202 if ( --argc == 0 )
203 usage ("");
205 if ( argv[1][0] == '-' )
207 if ( argv[1][1] != 's' )
208 usage ("illegal option");
210 if ( argc != 2 )
211 usage ("Option -s requires an argument");
213 serial = atol (argv[2]);
214 printserial ("", serial);
216 else
217 while ( argc-- > 0 )
218 if ( (read_serial_fromfile (*++argv, &serial)) != 0 )
219 fprintf (stderr, "couldn't read serial number from file %s\n", *argv);
220 else
221 printserial (*argv, serial);
223 return 0;