~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to server/access.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "common.h"
26
26
#include "access.h"
27
 
#include "levels.h"
28
27
 
29
28
        struct  acl_t   *acl_head = NULL;
30
29
        struct  access_t        *access_head = NULL;
51
50
        return 0;       /* not found */
52
51
}
53
52
 
54
 
/* this is only used for simple stuff: MONITOR and BASE */
55
 
int access_check(const struct sockaddr_in *addr, int level)
 
53
/* return ACCEPT/REJECT based on source address */
 
54
int access_check(const struct sockaddr_in *addr)
56
55
{
57
56
        struct  access_t        *tmp;
58
57
        int     ret;
59
58
 
60
 
        /* check for insanity */
61
 
        if (level > LEVEL_MONITOR) {
62
 
                upslogx(LOG_ERR, "checkaccess called with level=%d", level);
63
 
                return 0;       /* failed */
64
 
        }
65
 
 
66
59
        tmp = access_head;
67
60
 
68
61
        while (tmp != NULL) {
69
62
                ret = acl_check(tmp->aclname, addr);
70
63
 
71
 
                upsdebugx(3, "acl_check: %s: match %d, level %d (%d)", 
72
 
                        tmp->aclname, ret, tmp->level, level);
73
 
 
74
 
                /* if ip matches and access line provides the right level.. */
75
 
 
76
 
                if ((ret == 1) && (tmp->level >= level)) {
77
 
 
78
 
                        if (tmp->action == ACTION_GRANT)
79
 
                                return 1;       /* allowed */
80
 
 
81
 
                        upsdebugx(1, "access denied");
82
 
                        return 0;
83
 
                }       /* if (ip matches) && (level is provided) */
84
 
 
85
 
                /* otherwise ip didn't match or the level was inadequate */
 
64
                upsdebugx(3, "acl_check: %s: match %d", tmp->aclname, ret);
 
65
 
 
66
                if (ret == 1) {
 
67
                        upsdebugx(1, "ACL [%s] matches, action=%d", 
 
68
                                tmp->aclname, tmp->action);
 
69
                        return tmp->action;
 
70
                }
86
71
 
87
72
                tmp = tmp->next;
88
73
        }
89
74
 
90
 
        /* default = deny */
91
 
        return 0;
 
75
        /* fail safe */
 
76
        return ACCESS_REJECT;
92
77
}
93
78
 
94
79
/* add to the master list of ACL names */
109
94
        /* 192.168.1.1/255.255.255.255: valid */
110
95
        /* 192.168.1.1: invalid */
111
96
 
112
 
        if (!mask)      /* no slash = broken acl declaration */
 
97
        /* no slash = broken acl declaration */
 
98
        if (!mask)      
113
99
                return;
114
100
 
115
101
        *mask++ = '\0';
117
103
 
118
104
        tmp = last = acl_head;
119
105
 
120
 
        while (tmp != NULL) {   /* find end */
 
106
        while (tmp != NULL) {
121
107
                last = tmp;
122
108
                tmp = tmp->next;
123
109
        }
124
110
 
125
 
        tmp = xmalloc(sizeof (struct acl_t));
 
111
        tmp = xmalloc(sizeof(struct acl_t));
126
112
        tmp->name = xstrdup(aclname);
127
113
        tmp->addr = ntohl(inet_addr(addr));
128
114
        tmp->next = NULL;
129
115
 
130
 
        if (strstr(mask, ".") == NULL) { /* must be a /nn CIDR type block */
 
116
        /* must be a /nn CIDR type block */
 
117
        if (strstr(mask, ".") == NULL) { 
131
118
                if (atoi(mask) != 32)
132
119
                        tmp->mask = ((unsigned int) ((1 << atoi(mask)) - 1) << 
133
 
                                    (32 - atoi(mask)));
 
120
                                        (32 - atoi(mask)));
134
121
                else
135
122
                        tmp->mask = 0xffffffff; /* avoid overflow from 2^32 */
136
123
        }
137
124
        else
138
 
                tmp->mask = ntohl(inet_addr (mask));
 
125
                tmp->mask = ntohl(inet_addr(mask));
139
126
 
140
127
        if (last == NULL)       /* first */
141
128
                acl_head = tmp;
143
130
                last->next = tmp;
144
131
}
145
132
 
146
 
/* add to the access linked list */
147
 
void access_add(const char *action, const char *level, const char *aclname)
148
 
{
149
 
        struct  access_t        *tmp, *last;
150
 
 
151
 
        /* more sanity checking (null password is OK) */
152
 
        if ((!action) || (!level) || (!aclname))
153
 
                return;
154
 
 
155
 
        tmp = last = access_head;
156
 
 
157
 
        while (tmp != NULL) {   /* find end */
158
 
                last = tmp;
159
 
                tmp = tmp->next;
160
 
        }
161
 
 
162
 
        tmp = xmalloc(sizeof(struct access_t));
163
 
        tmp->action = 0;
164
 
        tmp->level = 0;
165
 
 
166
 
        if (!strcasecmp(action, "grant"))
167
 
                tmp->action = ACTION_GRANT;
168
 
        if (!strcasecmp(action, "deny"))
169
 
                tmp->action = ACTION_DENY;
170
 
 
171
 
        /* we don't do "drop" any more - convert to deny */
172
 
        if (!strcasecmp(action, "drop")) {
173
 
                upslogx(LOG_WARNING, "ACCESS action of 'drop' deprecated; change to 'deny'");
174
 
                tmp->action = ACTION_DENY;
175
 
        }
176
 
 
177
 
        if (!strcasecmp(level, "base"))
178
 
                tmp->level = LEVEL_BASE;
179
 
        if (!strcasecmp(level, "monitor"))
180
 
                tmp->level = LEVEL_MONITOR;
181
 
 
182
 
        /* deprecated in 1.2, removed in 1.3 */
183
 
        if ((!strcasecmp(level, "login")) ||
184
 
           (!strcasecmp(level, "master"))) {
185
 
 
186
 
                upslogx(LOG_WARNING, "LOGIN and MASTER no longer supported "
187
 
                        " in upsd.conf - switch to upsd.users");
188
 
 
189
 
                /* give them something somewhat useful */
190
 
                tmp->level = LEVEL_MONITOR;
191
 
        }
192
 
 
193
 
        /* deprecated in 1.0, removed in 1.1 */
194
 
        if (!strcasecmp(level, "manager")) {
195
 
                upslogx(LOG_WARNING, "ACCESS type manager no longer supported -"
196
 
                        " switch to upsd.users");
197
 
 
198
 
                /* but don't leave them totally out in the cold */
199
 
                tmp->level = LEVEL_MONITOR;
200
 
        }
201
 
 
202
 
        if (!strcasecmp(level, "all"))
203
 
                tmp->level = LEVEL_ALL;
204
 
 
205
 
        tmp->aclname = xstrdup(aclname);
206
 
        tmp->next = NULL;
207
 
 
208
 
        if (last == NULL)       /* first */
209
 
                access_head = tmp;
210
 
        else
211
 
                last->next = tmp;       
212
 
}
213
 
 
214
133
void acl_free(void)
215
134
{
216
135
        struct  acl_t   *ptr, *next;
248
167
 
249
168
        access_head = NULL;
250
169
}       
 
170
 
 
171
static void access_append(int action, const char *aclname)
 
172
{
 
173
        struct  access_t        *tmp, *last;
 
174
 
 
175
        tmp = last = access_head;
 
176
 
 
177
        while (tmp != NULL) {
 
178
                last = tmp;
 
179
                tmp = tmp->next;
 
180
        }
 
181
 
 
182
        tmp = xmalloc(sizeof(struct access_t));
 
183
 
 
184
        tmp->action = action;
 
185
        tmp->aclname = xstrdup(aclname);
 
186
 
 
187
        tmp->next = NULL;
 
188
 
 
189
        if (last)
 
190
                last->next = tmp;
 
191
        else
 
192
                access_head = tmp;
 
193
}               
 
194
 
 
195
void access_add(int type, int numargs, const char **arg)
 
196
{
 
197
        int     i;
 
198
 
 
199
        for (i = 0; i < numargs; i++)
 
200
                access_append(type, arg[i]);
 
201
}