Postfix3.3.1
全て データ構造 ファイル 関数 変数 型定義 マクロ定義
user_acl.c
[詳解]
1 /*++
2 /* NAME
3 /* user_acl 3
4 /* SUMMARY
5 /* user name based access control
6 /* SYNOPSIS
7 /* #include <user_acl.h>
8 /*
9 /* const char *check_user_acl_byuid(pname, acl, uid)
10 /* const char *pname;
11 /* const char *acl;
12 /* uid_t uid;
13 /* DESCRIPTION
14 /* check_user_acl_byuid() converts the given uid into a user
15 /* name, and checks the result against a user name matchlist.
16 /* If the uid cannot be resolved to a user name, "unknown"
17 /* is used as the lookup key instead.
18 /* The result is NULL on success, the username upon failure.
19 /* The error result lives in static storage and must be saved
20 /* if it is to be used to across multiple check_user_acl_byuid()
21 /* calls.
22 /*
23 /* Arguments:
24 /* .IP pname
25 /* The parameter name of the acl.
26 /* .IP acl
27 /* Authorized user name list suitable for input to string_list_init(3).
28 /* .IP uid
29 /* The uid to be checked against the access list.
30 /* LICENSE
31 /* .ad
32 /* .fi
33 /* The Secure Mailer license must be distributed with this software.
34 /* AUTHOR(S)
35 /* Wietse Venema
36 /* IBM T.J. Watson Research
37 /* P.O. Box 704
38 /* Yorktown Heights, NY 10598, USA
39 /*
40 /* Victor Duchovni
41 /* Morgan Stanley
42 /*--*/
43 
44 /* System library. */
45 
46 #include <sys_defs.h>
47 #include <string.h>
48 
49 /* Utility library. */
50 
51 #include <vstring.h>
52 #include <dict_static.h>
53 
54 /* Global library. */
55 
56 #include <string_list.h>
57 #include <mypwd.h>
58 
59 /* Application-specific. */
60 
61 #include "user_acl.h"
62 
63 /* check_user_acl_byuid - check user authorization */
64 
65 const char *check_user_acl_byuid(const char *pname, const char *acl, uid_t uid)
66 {
67  struct mypasswd *mypwd;
68  STRING_LIST *list;
69  static VSTRING *who = 0;
70  int matched;
71  const char *name;
72 
73  /*
74  * Optimize for the most common case. This also makes Postfix a little
75  * more robust in the face of local infrastructure failures. Note that we
76  * only need to match the "static:" substring, not the result value.
77  */
78  if (strncmp(acl, DICT_TYPE_STATIC ":", sizeof(DICT_TYPE_STATIC)) == 0)
79  return (0);
80 
81  /*
82  * XXX: Substitute "unknown" for UIDs without username, so that
83  * static:anyone results in "permit" even when the uid is not found in
84  * the password file, and so that a pattern of !unknown can be used to
85  * block non-existent accounts.
86  *
87  * The alternative is to use the UID as a surrogate lookup key for
88  * non-existent accounts. There are several reasons why this is not a
89  * good idea. 1) An ACL with a numerical UID should work regardless of
90  * whether or not an account has a password file entry. Therefore we
91  * would always have search on the numerical UID whenever the username
92  * fails to produce a match. 2) The string-list infrastructure is not
93  * really suitable for mixing numerical and non-numerical user
94  * information, because the numerical match is done in a separate pass
95  * from the non-numerical match. This breaks when the ! operator is used.
96  *
97  * XXX To avoid waiting until the lookup completes (e.g., LDAP or NIS down)
98  * invoke mypwuid_err(), and either change the user_acl() API to
99  * propagate the error to the caller, or treat lookup errors as fatal.
100  */
101  if ((mypwd = mypwuid(uid)) == 0) {
102  name = "unknown";
103  } else {
104  name = mypwd->pw_name;
105  }
106 
107  list = string_list_init(pname, MATCH_FLAG_NONE, acl);
108  if ((matched = string_list_match(list, name)) == 0) {
109  if (!who)
110  who = vstring_alloc(10);
111  vstring_strcpy(who, name);
112  }
113  string_list_free(list);
114  if (mypwd)
115  mypwfree(mypwd);
116 
117  return (matched ? 0 : vstring_str(who));
118 }
#define DICT_TYPE_STATIC
Definition: dict_static.h:22
#define vstring_str(vp)
Definition: vstring.h:71
Definition: mypwd.h:17
#define STRING_LIST
Definition: string_list.h:22
struct mypasswd * mypwuid(uid_t uid)
Definition: mypwd.c:165
char * pw_name
Definition: mypwd.h:19
VSTRING * vstring_strcpy(VSTRING *vp, const char *src)
Definition: vstring.c:431
#define string_list_init(o, f, p)
Definition: string_list.h:24
#define string_list_free
Definition: string_list.h:27
#define string_list_match
Definition: string_list.h:26
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
void mypwfree(struct mypasswd *mypwd)
Definition: mypwd.c:292
const char * check_user_acl_byuid(const char *pname, const char *acl, uid_t uid)
Definition: user_acl.c:65
#define MATCH_FLAG_NONE
Definition: match_list.h:38