summaryrefslogtreecommitdiff
path: root/usr/src/lib/libnsl/ipsec/algs.c
blob: a20473a6dac978b62ef42506d8b36adc3dd01901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include "mt.h"
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <ipsec_util.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <synch.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>

/* Globals... */
static rwlock_t proto_rw = DEFAULTRWLOCK; /* Protects cached algorithm list. */
static time_t proto_last_update;
static ipsec_proto_t *protos;
static int num_protos;

void
_clean_trash(ipsec_proto_t *proto, int num)
{
	int alg_offset;

	if (proto == NULL)
		return;

	while (num-- != 0) {
		free(proto[num].proto_name);
		free(proto[num].proto_pkg);
		for (alg_offset = 0; alg_offset < proto[num].proto_numalgs;
		    alg_offset++)
			freeipsecalgent(proto[num].proto_algs[alg_offset]);
		free(proto[num].proto_algs);
		for (alg_offset = 0; alg_offset < proto[num].proto_algs_npkgs;
		    alg_offset++)
			free(proto[num].proto_algs_pkgs[alg_offset].pkg_name);
		free(proto[num].proto_algs_pkgs);
	}

	free(proto);
}

static const char *pipechar = "|";
static const char *comma = ",";
static const char *dash = "-";
static const char *slash = "/";

/*
 * Returns >= 0 if success (and > 0 means "increment").
 * Returns -1 if failure.
 */
static int
build_keysizes(int **sizep, char *input_string)
{
	char *lasts, *token;
	int *key_sizes = NULL, num_sizes, key_low, key_high, key_default;
	int key_increment = 0;

	/*
	 * Okay, let's check the format of the key string.  It'll be either:
	 *
	 * enumeration: size1,size2...,sizeN
	 * range: defaultSize/sizeLow-sizeHi,increment
	 *
	 * In the case of an enumeration, the default key size is the
	 * first one in the list.
	 */

	if (strchr(input_string, '/') != NULL) {
		/* key sizes specified by range */

		/* default */
		token = strtok_r(input_string, slash, &lasts);
		if (token == NULL || (key_default = atoi(token)) == 0)
			return (-1);

		/* low */
		token = strtok_r(NULL, dash, &lasts);
		if (token == NULL || (key_low = atoi(token)) == 0)
			return (-1);

		/* high */
		token = strtok_r(NULL, comma, &lasts);
		if (token == NULL || (key_high = atoi(token)) == 0 ||
		    key_high <= key_low)
			return (-1);

		/* increment */
		token = strtok_r(NULL, "", &lasts);
		if (token == NULL || (key_increment = atoi(token)) == 0)
			return (-1);

		key_sizes = (int *)malloc(LIBIPSEC_ALGS_KEY_NUM_VAL *
		    sizeof (int));
		if (key_sizes == NULL)
			return (-1);

		key_sizes[LIBIPSEC_ALGS_KEY_DEF_IDX] = key_default;
		key_sizes[LIBIPSEC_ALGS_KEY_MIN_IDX] = key_low;
		key_sizes[LIBIPSEC_ALGS_KEY_MAX_IDX] = key_high;
		key_sizes[LIBIPSEC_ALGS_KEY_MAX_IDX + 1] = 0;
	} else {
		/* key sizes specified by enumeration */

		key_sizes = (int *)malloc(sizeof (int));
		if (key_sizes == NULL)
			return (-1);
		num_sizes = 0;

		token = strtok_r(input_string, comma, &lasts);
		if (token == NULL) {
			free(key_sizes);
			return (-1);
		}
		*key_sizes = 0;
		do {
			int *nks;

			nks = (int *)realloc(key_sizes,
			    sizeof (int) * ((++num_sizes) + 1));
			if (nks == NULL) {
				free(key_sizes);
				return (-1);
			}
			key_sizes = nks;
			/* Can't check for atoi() == 0 here... */
			key_sizes[num_sizes - 1] = atoi(token);
			key_sizes[num_sizes] = 0;
		} while ((token = strtok_r(NULL, comma, &lasts)) != NULL);
	}
	*sizep = key_sizes;

	return (key_increment);
}

/*
 * Find the execution mode corresponding to the given string.
 * Returns 0 on success, -1 on failure.
 */
int
_str_to_ipsec_exec_mode(char *str, ipsecalgs_exec_mode_t *exec_mode)
{
	if (strcmp(str, "sync") == 0) {
		*exec_mode = LIBIPSEC_ALGS_EXEC_SYNC;
		return (0);
	} else if (strcmp(str, "async") == 0) {
		*exec_mode = LIBIPSEC_ALGS_EXEC_ASYNC;
		return (0);
	}

	return (-1);
}

/*
 * Given a file pointer, read all the text from the file and convert it into
 * a bunch of ipsec_proto_t's, each with an array of struct ipsecalgent
 * pointers - one for each algorithm.
 */
static ipsec_proto_t *
build_list(FILE *f, int *num)
{
	char line[1024];
	char *token, *lasts, *alg_names, *ef_name, *key_string, *block_string;
	char *proto_name, *params_string;
	ipsec_proto_t *rc = NULL, *new_proto = NULL;
	int *block_sizes = NULL, *key_sizes = NULL, *mech_params = NULL;
	int rc_num = 0, key_increment;
	int new_num, alg_num, num_sizes, flags = 0;
	struct ipsecalgent *curalg, **newalglist;
	char cur_pkg[1024];
	boolean_t doing_pkg = B_FALSE;
	ipsecalgs_exec_mode_t exec_mode;
	char diag_buf[128];

	diag_buf[0] = '\0';

	while (fgets(line, sizeof (line), f) != NULL) {
		if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO,
		    sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) != 0 &&
		    strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG,
		    sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) != 0 &&
		    strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART,
		    sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) != 0 &&
		    strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGEND,
		    sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1) != 0) {
			if ((token = strtok_r(line, " \t\n", &lasts)) == NULL ||
			    token[0] == '#') {
				continue;
			} else {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "non-recognized start of line");
				goto bail;
			}
		}

		if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PROTO,
		    sizeof (LIBIPSEC_ALGS_LINE_PROTO) - 1) == 0) {
			/* current line defines a new protocol */

			/* skip the protocol token */
			token = strtok_r(line, pipechar, &lasts);

			/* protocol number */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL || (new_num = atoi(token)) == 0) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "invalid protocol number");
				goto bail;
			}

			/* protocol name */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read protocol name");
				goto bail;
			}
			proto_name = token;

			/* execution mode */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read execution mode");
				goto bail;
			}
			/* remove trailing '\n' */
			token[strlen(token) - 1] = '\0';
			if (_str_to_ipsec_exec_mode(token, &exec_mode) != 0) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "invalid execution mode: \"%s\"", token);
				goto bail;
			}

			/* initialize protocol structure */
			rc_num++;
			new_proto = (ipsec_proto_t *)realloc(rc,
			    sizeof (ipsec_proto_t) * rc_num);
			rc = new_proto;
			if (new_proto == NULL)
				goto bail;
			new_proto += (rc_num - 1);
			new_proto->proto_num = new_num;
			new_proto->proto_algs = NULL;
			new_proto->proto_numalgs = 0;
			new_proto->proto_name = strdup(proto_name);
			if (new_proto->proto_name == NULL)
				goto bail;
			new_proto->proto_exec_mode = exec_mode;

			if (doing_pkg) {
				/* record proto as being part of current pkg */
				new_proto->proto_pkg = strdup(cur_pkg);
				if (new_proto->proto_pkg == NULL)
					goto bail;
			} else {
				new_proto->proto_pkg = NULL;
			}

			new_proto->proto_algs_pkgs = NULL;
			new_proto->proto_algs_npkgs = 0;

		} else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_ALG,
		    sizeof (LIBIPSEC_ALGS_LINE_ALG) - 1) == 0) {
			/* current line defines a new algorithm */

			/* skip the algorithm token */
			token = strtok_r(line, pipechar, &lasts);

			/* protocol number */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL || (new_num = atoi(token)) == 0) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "invalid algorithm number");
				goto bail;
			}

			/* We can be O(N) for now.  There aren't that many. */
			for (new_proto = rc; new_proto < (rc + new_num);
			    new_proto++)
				if (new_proto->proto_num == new_num)
					break;
			if (new_proto == (rc + new_num)) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "invalid protocol number %d for algorithm",
				    new_num);
				goto bail;
			}

			/* algorithm number */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read algorithm number");
				goto bail;
			}
			/* Can't check for 0 here. */
			alg_num = atoi(token);

			/* algorithm names */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read algorithm number");
				goto bail;
			}
			alg_names = token;

			/* mechanism name */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read mechanism name for alg %d "
				    "(proto %d)", alg_num,
				    new_proto->proto_num);
				goto bail;
			}
			ef_name = token;

			/* key sizes */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read key sizes for alg %d "
				    "(proto %d)", alg_num,
				    new_proto->proto_num);
				goto bail;
			}
			key_string = token;

			/* block sizes */
			token = strtok_r(NULL, pipechar, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "cannot read block sizes for alg %d "
				    "(proto %d)", alg_num,
				    new_proto->proto_num);
				goto bail;
			}
			block_string = token;

			/*
			 * Check for mechanism params and flags. As these
			 * are optional, we won't bail if they don't exist.
			 */
			token = strtok_r(NULL, pipechar, &lasts);
			params_string = token;

			token = strtok_r(NULL, pipechar, &lasts);
			if (token != NULL)
				flags = atoi(token);

			/* extract key sizes */
			key_increment = build_keysizes(&key_sizes, key_string);
			if (key_increment == -1) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "invalid key sizes for alg %d (proto %d)",
				    alg_num, new_proto->proto_num);
				goto bail;
			}

			/* extract block sizes */
			block_sizes = (int *)malloc(sizeof (int));
			if (block_sizes == NULL) {
				goto bail;
			}
			num_sizes = 0;
			token = strtok_r(block_string, comma, &lasts);
			if (token == NULL) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "invalid block sizes for alg %d (proto %d)",
				    alg_num, new_proto->proto_num);
				goto bail;
			}
			*block_sizes = 0;
			do {
				int *nbk;

				nbk = (int *)realloc(block_sizes,
				    sizeof (int) * ((++num_sizes) + 1));
				if (nbk == NULL) {
					goto bail;
				}
				block_sizes = nbk;
				/* Can't check for 0 here... */
				block_sizes[num_sizes - 1] = atoi(token);
				block_sizes[num_sizes] = 0;
			} while ((token = strtok_r(NULL, comma, &lasts)) !=
			    NULL);

			/* extract mech params */
			mech_params = (int *)malloc(sizeof (int));
			if (mech_params == NULL) {
				goto bail;
			}
			*mech_params = 0;
			num_sizes = 0;
			if (params_string != NULL) {
				token = strtok_r(params_string, comma, &lasts);
				if (token == NULL) {
					(void) snprintf(diag_buf,
					    sizeof (diag_buf), "invalid mech "
					    "params for alg %d (proto %d)",
					    alg_num, new_proto->proto_num);
					goto bail;
				}
				do {
					int *nbk;

					nbk = (int *)realloc(mech_params,
					    sizeof (int) * ((++num_sizes) + 1));
					if (nbk == NULL) {
						goto bail;
					}
					mech_params = nbk;
					/* Can't check for 0 here... */
					mech_params[num_sizes - 1] =
					    atoi(token);
					mech_params[num_sizes] = 0;
				} while ((token = strtok_r(NULL, comma, &lasts))
				    != NULL);
			}
			/* Allocate a new struct ipsecalgent. */
			curalg = (struct ipsecalgent *)calloc(
			    sizeof (struct ipsecalgent), 1);
			if (curalg == NULL) {
				goto bail;
			}
			curalg->a_proto_num = new_num;
			curalg->a_alg_num = alg_num;
			curalg->a_block_sizes = block_sizes;
			curalg->a_alg_flags = flags;
			curalg->a_mech_params = mech_params;
			curalg->a_key_sizes = key_sizes;
			curalg->a_key_increment = key_increment;
			if ((curalg->a_mech_name = strdup(ef_name)) == NULL) {
				freeipsecalgent(curalg);
				goto bail;
			}
			/* Set names. */
			curalg->a_names = (char **)malloc(sizeof (char *));
			num_sizes = 0;	/* Recycle "sizes" */
			token = strtok_r(alg_names, comma, &lasts);
			if (curalg->a_names == NULL || token == NULL) {
				freeipsecalgent(curalg);
				goto bail;
			}
			do {
				char **nnames;

				nnames = (char **)realloc(curalg->a_names,
				    sizeof (char *) * ((++num_sizes) + 1));
				if (nnames == NULL) {
					freeipsecalgent(curalg);
					goto bail;
				}
				curalg->a_names = nnames;
				curalg->a_names[num_sizes] = NULL;
				curalg->a_names[num_sizes - 1] =
				    strdup(token);
				if (curalg->a_names[num_sizes - 1] == NULL) {
					freeipsecalgent(curalg);
					goto bail;
				}
			} while ((token = strtok_r(NULL, comma, &lasts)) !=
			    NULL);

			if (doing_pkg) {
				/* record alg as being part of current pkg */
				int npkgs = new_proto->proto_algs_npkgs;

				new_proto->proto_algs_pkgs = realloc(
				    new_proto->proto_algs_pkgs,
				    (npkgs + 1) * sizeof (ipsecalgs_pkg_t));
				if (new_proto->proto_algs_pkgs == NULL)
					goto bail;

				new_proto->proto_algs_pkgs[npkgs].alg_num =
				    curalg->a_alg_num;
				new_proto->proto_algs_pkgs[npkgs].pkg_name =
				    strdup(cur_pkg);
				if (new_proto->proto_algs_pkgs[npkgs].pkg_name
				    == NULL)
					goto bail;

				new_proto->proto_algs_npkgs = npkgs + 1;
			}

			/* add new alg to protocol */
			newalglist = realloc(new_proto->proto_algs,
			    (new_proto->proto_numalgs + 1) *
			    sizeof (struct ipsecalgent *));
			if (newalglist == NULL) {
				freeipsecalgent(curalg);
				goto bail;
			}
			newalglist[new_proto->proto_numalgs] = curalg;
			new_proto->proto_numalgs++;
			new_proto->proto_algs = newalglist;

		} else if (strncasecmp(line, LIBIPSEC_ALGS_LINE_PKGSTART,
		    sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1) == 0) {
			/* start of package delimiter */
			if (doing_pkg) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "duplicate package start delimiters");
				goto bail;
			}
			(void) strncpy(cur_pkg, line +
			    (sizeof (LIBIPSEC_ALGS_LINE_PKGSTART) - 1),
			    sizeof (cur_pkg));
			/* remove trailing '\n' */
			cur_pkg[strlen(cur_pkg) - 1] = '\0';
			doing_pkg = B_TRUE;

		} else {
			/* end of package delimiter */
			char tmp_pkg[1024];

			if (!doing_pkg) {
				(void) snprintf(diag_buf, sizeof (diag_buf),
				    "end package delimiter without start");
				goto bail;
			}
			/*
			 * Get specified pkg name, fail if it doesn't match
			 * the package specified by the last # Begin.
			 */
			(void) strncpy(tmp_pkg, line +
			    (sizeof (LIBIPSEC_ALGS_LINE_PKGEND) - 1),
			    sizeof (tmp_pkg));
			/* remove trailing '\n' */
			tmp_pkg[strlen(tmp_pkg) - 1] = '\0';
			if (strncmp(cur_pkg, tmp_pkg, sizeof (cur_pkg)) != 0)
				goto bail;
			doing_pkg = B_FALSE;
		}
	}

	*num = rc_num;
	return (rc);

bail:
	if (strlen(diag_buf) > 0) {
		syslog(LOG_ERR, "possibly corrupt %s file: %s\n",
		    INET_IPSECALGSFILE, diag_buf);
	}
	free(key_sizes);
	free(block_sizes);
	free(mech_params);
	_clean_trash(rc, rc_num);
	return (NULL);
}

/*
 * If alg_context is NULL, update the library's cached copy of
 * INET_IPSECALGSFILE.  If alg_context is non-NULL, hang a
 * library-internal representation of a cached copy.  The latter is useful
 * for routines in libipsecutil that _write_ the contents out.
 */
void
_build_internal_algs(ipsec_proto_t **alg_context, int *alg_nums)
{
	FILE *f;
	int rc, trash_num = 0;
	ipsec_proto_t *new_protos = NULL, *trash;
	time_t filetime;
	struct stat statbuf;

	/*
	 * Construct new_protos from the file.
	 */
	if (alg_context == NULL) {
		/*
		 * Check the time w/o holding the lock.  This is just a
		 * cache reality check.  We'll do it again for real if this
		 * surface check fails.
		 */
		if (stat(INET_IPSECALGSFILE, &statbuf) == -1 ||
		    (statbuf.st_mtime < proto_last_update && protos != NULL))
			return;
		(void) rw_wrlock(&proto_rw);
	}

	f = fopen(INET_IPSECALGSFILE, "rF");
	if (f != NULL) {
		rc = fstat(fileno(f), &statbuf);
		if (rc != -1) {
			/*
			 * Update if the file is newer than our
			 * last cached copy.
			 */
			filetime = statbuf.st_mtime;
			if (alg_context != NULL ||
			    filetime > proto_last_update)
				new_protos = build_list(f, &rc);
		}
		/* Since f is read-only, can avoid all of the failures... */
		(void) fclose(f);
	}

	if (alg_context == NULL) {
		/*
		 * If we have failed anywhere above, new_protoss will be NULL.
		 * This way, the previous cached protos will still be intact.
		 */
		if (new_protos != NULL) {
			proto_last_update = filetime;
			trash = protos;
			trash_num = num_protos;
			protos = new_protos;
			num_protos = rc;
		} else {
			/*
			 * Else the original protocols and algorithms lists
			 * remains the same.
			 */
			trash = NULL;
		}
		(void) rw_unlock(&proto_rw);
		_clean_trash(trash, trash_num);
	} else {
		/*
		 * Assume caller has done the appropriate locking,
		 * cleanup, etc.  And if new_protos is NULL, it's the caller's
		 * problem.
		 */
		*alg_context = new_protos;
		*alg_nums = rc;
	}

}

/*
 * Assume input is 0-terminated.
 */
static int *
duplicate_intarr(int *orig)
{
	size_t allocsize = sizeof (int);
	int *iwalker = orig;

	if (orig == NULL)
		return (NULL);

	while (*iwalker != 0) {
		allocsize += sizeof (int);
		iwalker++;
	}

	iwalker = malloc(allocsize);
	if (iwalker != NULL)
		(void) memcpy(iwalker, orig, allocsize);

	return (iwalker);
}

/*
 * Assume input is NULL terminated.
 */
static char **
duplicate_strarr(char **orig)
{
	int i;
	char **swalker;
	char **newbie;

	if (orig == NULL)
		return (NULL);

	/* count number of elements in source array */
	for (swalker = orig; *swalker != NULL; swalker++)
		;

	/* use calloc() to get NULL-initialization */
	newbie = calloc(swalker - orig + 1, sizeof (char *));

	if (newbie != NULL) {
		/* do the copy */
		for (i = 0; orig[i] != NULL; i++) {
			newbie[i] = strdup(orig[i]);
			if (newbie[i] == NULL) {
				for (swalker = newbie; *swalker != NULL;
				    swalker++)
					free(*swalker);
				free(newbie);
				return (NULL);
			}
		}
	}

	return (newbie);
}

struct ipsecalgent *
_duplicate_alg(struct ipsecalgent *orig)
{
	struct ipsecalgent *rc;

	/* use calloc() to get NULL-initialization. */
	rc = calloc(1, sizeof (struct ipsecalgent));
	if (rc == NULL)
		return (NULL);

	rc->a_proto_num = orig->a_proto_num;
	rc->a_alg_num = orig->a_alg_num;
	rc->a_key_increment = orig->a_key_increment;
	rc->a_mech_name = strdup(orig->a_mech_name);
	rc->a_alg_flags = orig->a_alg_flags;
	rc->a_block_sizes = duplicate_intarr(orig->a_block_sizes);
	rc->a_mech_params = duplicate_intarr(orig->a_mech_params);
	rc->a_key_sizes = duplicate_intarr(orig->a_key_sizes);
	rc->a_names = duplicate_strarr(orig->a_names);

	if (rc->a_mech_name == NULL || rc->a_block_sizes == NULL ||
	    rc->a_key_sizes == NULL || rc->a_names == NULL ||
	    rc->a_mech_params == NULL) {
		freeipsecalgent(rc);
		return (NULL);
	}

	return (rc);
}

/*
 * Assume the rwlock is held for reading.
 */
static ipsec_proto_t *
findprotobynum(int proto_num)
{
	int i;

	for (i = 0; i < num_protos; i++) {
		if (protos[i].proto_num == proto_num)
			return (protos + i);
	}

	return (NULL);
}

static ipsec_proto_t *
findprotobyname(const char *name)
{
	int i;

	if (name == NULL)
		return (NULL);

	for (i = 0; i < num_protos; i++) {
		/* Can use strcasecmp because our proto_name is bounded. */
		if (strcasecmp(protos[i].proto_name, name) == 0)
			return (protos + i);
	}

	return (NULL);
}

int *
_real_getipsecprotos(int *nentries)
{
	int *rc, i;

	if (nentries == NULL)
		return (NULL);

	_build_internal_algs(NULL, NULL);

	(void) rw_rdlock(&proto_rw);
	*nentries = num_protos;
	/*
	 * Allocate 1 byte if there are no protocols so a non-NULL return
	 * happens.
	 */
	rc = malloc((num_protos == 0) ? 1 : num_protos * sizeof (int));
	if (rc != NULL) {
		for (i = 0; i < num_protos; i++)
			rc[i] = protos[i].proto_num;
	}
	(void) rw_unlock(&proto_rw);
	return (rc);
}

int *
_real_getipsecalgs(int *nentries, int proto_num)
{
	int *rc = NULL, i;
	ipsec_proto_t *proto;

	if (nentries == NULL)
		return (NULL);

	_build_internal_algs(NULL, NULL);

	(void) rw_rdlock(&proto_rw);
	proto = findprotobynum(proto_num);
	if (proto != NULL) {
		*nentries = proto->proto_numalgs;
		/*
		 * Allocate 1 byte if there are no algorithms so a non-NULL
		 * return happens.
		 */
		rc = malloc((proto->proto_numalgs == 0) ? 1 :
		    proto->proto_numalgs * sizeof (int));
		if (rc != NULL) {
			for (i = 0; i < proto->proto_numalgs; i++)
				rc[i] = proto->proto_algs[i]->a_alg_num;
		}
	}
	(void) rw_unlock(&proto_rw);
	return (rc);
}

struct ipsecalgent *
getipsecalgbyname(const char *name, int proto_num, int *errnop)
{
	ipsec_proto_t *proto;
	struct ipsecalgent *rc = NULL;
	int i, my_errno = ENOENT;
	char **name_check;

	_build_internal_algs(NULL, NULL);
	if (name == NULL) {
		my_errno = EFAULT;
		goto bail;
	}

	(void) rw_rdlock(&proto_rw);
	proto = findprotobynum(proto_num);
	if (proto != NULL) {
		for (i = 0; i < proto->proto_numalgs; i++) {
			for (name_check = proto->proto_algs[i]->a_names;
			    *name_check != NULL; name_check++) {
				/*
				 * Can use strcasecmp because our name_check
				 * is bounded.
				 */
				if (strcasecmp(*name_check, name) == 0) {
					/* found match */
					rc = _duplicate_alg(
					    proto->proto_algs[i]);
					my_errno = (rc == NULL) ? ENOMEM : 0;
					(void) rw_unlock(&proto_rw);
					goto bail;
				}
			}
		}
	} else {
		my_errno = EINVAL;
	}

	(void) rw_unlock(&proto_rw);
bail:
	if (errnop != NULL)
		*errnop = my_errno;
	return (rc);
}

struct ipsecalgent *
getipsecalgbynum(int alg_num, int proto_num, int *errnop)
{
	ipsec_proto_t *proto;
	struct ipsecalgent *rc = NULL;
	int i, my_errno = ENOENT;

	_build_internal_algs(NULL, NULL);

	(void) rw_rdlock(&proto_rw);

	proto = findprotobynum(proto_num);
	if (proto != NULL) {
		for (i = 0; i < proto->proto_numalgs; i++) {
			if (proto->proto_algs[i]->a_alg_num == alg_num) {
				rc = _duplicate_alg(proto->proto_algs[i]);
				my_errno = (rc == NULL) ? ENOMEM : 0;
				break;
			}
		}
	} else {
		my_errno = EINVAL;
	}

	(void) rw_unlock(&proto_rw);
	if (errnop != NULL)
		*errnop = my_errno;
	return (rc);
}

int
getipsecprotobyname(const char *proto_name)
{
	int rc = -1;
	ipsec_proto_t *proto;

	_build_internal_algs(NULL, NULL);

	(void) rw_rdlock(&proto_rw);
	proto = findprotobyname(proto_name);
	if (proto != NULL)
		rc = proto->proto_num;
	(void) rw_unlock(&proto_rw);
	return (rc);
}

char *
getipsecprotobynum(int proto_num)
{
	ipsec_proto_t *proto;
	char *rc = NULL;

	_build_internal_algs(NULL, NULL);

	(void) rw_rdlock(&proto_rw);
	proto = findprotobynum(proto_num);
	if (proto != NULL)
		rc = strdup(proto->proto_name);

	(void) rw_unlock(&proto_rw);
	return (rc);
}

void
freeipsecalgent(struct ipsecalgent *ptr)
{
	char **walker;

	if (ptr == NULL)
		return;

	if (ptr->a_names != NULL) {
		for (walker = ptr->a_names; *walker != NULL; walker++)
			free(*walker);
	}

	/*
	 * Remember folks, free(NULL) works.
	 */
	free(ptr->a_names);
	free(ptr->a_mech_name);
	free(ptr->a_block_sizes);
	free(ptr->a_mech_params);
	free(ptr->a_key_sizes);
	free(ptr);
}