File: cleanup_masquerade.c

package info (click to toggle)
postfix 0.0.19991231pl11-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,044 kB
  • ctags: 4,401
  • sloc: ansic: 33,767; makefile: 5,099; sh: 1,790; awk: 19
file content (173 lines) | stat: -rw-r--r-- 4,470 bytes parent folder | download | duplicates (3)
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
/*++
/* NAME
/*	cleanup_masquerade 3
/* SUMMARY
/*	address masquerading
/* SYNOPSIS
/*	#include <cleanup.h>
/*
/*	void	cleanup_masquerade_external(addr, masq_domains)
/*	VSTRING	*addr;
/*	ARGV	*masq_domains;
/*
/*	void	cleanup_masquerade_internal(addr, masq_domains)
/*	VSTRING	*addr;
/*	ARGV	*masq_domains;
/*
/*	void	cleanup_masquerade_tree(tree, masq_domains)
/*	TOK822	*tree;
/*	ARGV	*masq_domains;
/* DESCRIPTION
/*	This module masquerades addresses, that is, it strips subdomains
/*	below domain names that are listed in the masquerade_domains
/*	configuration parameter, except for user names listed in the
/*	masquerade_exceptions configuration parameter.
/*
/*	cleanup_masquerade_external() rewrites the external (quoted) string
/*	form of an address.
/*
/*	cleanup_masquerade_internal() is a wrapper around the
/*	cleanup_masquerade_external() routine that transforms from
/*	internal (quoted) string form to external form and back.
/*
/*	cleanup_masquerade_tree() is a wrapper around the
/*	cleanup_masquerade_external() routine that transforms from
/*	internal parse tree form to external form and back.
/* DIAGNOSTICS
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <string.h>

#ifdef STRCASECMP_IN_STRINGS_H
#include <strings.h>
#endif

/* Utility library. */

#include <msg.h>
#include <vstring.h>
#include <argv.h>
#include <htable.h>
#include <mymalloc.h>
#include <stringops.h>

/* Global library. */

#include <mail_params.h>
#include <tok822.h>
#include <quote_822_local.h>

/* Application-specific. */

#include "cleanup.h"

#define STR	vstring_str

/* cleanup_masquerade_external - masquerade address external form */

void    cleanup_masquerade_external(VSTRING *addr, ARGV *masq_domains)
{
    char   *domain;
    int     domain_len;
    char  **masqp;
    int     masq_len;
    char   *parent;

    /* Stuff for excluded names. */
    static HTABLE *masq_except_table = 0;
    char   *saved_names;
    char   *name;
    char   *ptr;
    int     excluded;

    /*
     * First time, build a lookup table for excluded names.
     */
    if (*var_masq_exceptions && masq_except_table == 0) {
	masq_except_table = htable_create(5);
	ptr = saved_names = mystrdup(var_masq_exceptions);
	while ((name = mystrtok(&ptr, ", \t\r\n")) != 0)
	    htable_enter(masq_except_table, lowercase(name), (char *) 0);
	myfree(saved_names);
    }

    /*
     * Find the domain part.
     */
    if ((domain = strrchr(STR(addr), '@')) == 0)
	return;
    domain += 1;
    domain_len = strlen(domain);

    /*
     * Don't masquerade excluded names (regardless of domain).
     */
    if (masq_except_table) {
	name = mystrndup(STR(addr), domain - 1 - STR(addr));
	excluded = (htable_locate(masq_except_table, lowercase(name)) != 0);
	myfree(name);
	if (excluded)
	    return;
    }

    /*
     * If any parent domain matches the list of masquerade domains, replace
     * the domain in the address and terminate. If the domain matches a
     * masquerade domain, leave it alone. Order of specification matters.
     */
    for (masqp = masq_domains->argv; *masqp; masqp++) {
	masq_len = strlen(*masqp);
	if (masq_len == domain_len) {
	    if (strcasecmp(*masqp, domain) == 0)
		break;
	} else if (masq_len < domain_len) {
	    parent = domain + domain_len - masq_len;
	    if (parent[-1] == '.' && strcasecmp(*masqp, parent) == 0) {
		if (msg_verbose)
		    msg_info("masquerade: %s -> %s", domain, *masqp);
		vstring_truncate(addr, domain - STR(addr));
		vstring_strcat(addr, *masqp);
		break;
	    }
	}
    }
}

/* cleanup_masquerade_tree - masquerade address node */

void    cleanup_masquerade_tree(TOK822 *tree, ARGV *masq_domains)
{
    VSTRING *temp = vstring_alloc(100);

    tok822_externalize(temp, tree->head, TOK822_STR_DEFL);
    cleanup_masquerade_external(temp, masq_domains);
    tok822_free_tree(tree->head);
    tree->head = tok822_scan(STR(temp), &tree->tail);

    vstring_free(temp);
}

/* cleanup_masquerade_internal - masquerade address internal form */

void    cleanup_masquerade_internal(VSTRING *addr, ARGV *masq_domains)
{
    VSTRING *temp = vstring_alloc(100);

    quote_822_local(temp, STR(addr));
    cleanup_masquerade_external(temp, masq_domains);
    unquote_822_local(addr, STR(temp));

    vstring_free(temp);
}