~ubuntu-branches/ubuntu/oneiric/jabberd2/oneiric-security

« back to all changes in this revision

Viewing changes to router/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolai Spohrer
  • Date: 2008-08-12 16:13:43 UTC
  • mfrom: (1.1.3 upstream) (0.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20080812161343-6trz3r97dtevxd17
Tags: 2.2.1-1ubuntu1
* Merge with Debian unstable (LP: #257130), remaining changes:
  - debian/control:
    + Modify Maintainer field as per spec
    + Depend on libdb4.6-dev instead of libdb4.4-dev
    + Added Conflicts and Replaces: ..., jabber for jabberd2
  - debian/rules: Added libtoolize call (jabberd2 ships with
     an older ltmain.sh version that conflicts with the
     current libtool version)
  - debian/init: create /var/run/jabber directory with correct
     permissions
* Dropped changes:
  - Debian already depends on libpq-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
static sig_atomic_t router_shutdown = 0;
24
24
static sig_atomic_t router_logrotate = 0;
25
25
 
26
 
void router_signal(int signum)
 
26
static void router_signal(int signum)
27
27
{
28
28
    router_shutdown = 1;
29
29
}
30
30
 
31
 
void router_signal_hup(int signum)
 
31
static void router_signal_hup(int signum)
32
32
{
33
33
    router_logrotate = 1;
34
34
}
52
52
 
53
53
    if(fprintf(f, "%d", pid) < 0) {
54
54
        log_write(r->log, LOG_ERR, "couldn't write to %s: %s", pidfile, strerror(errno));
 
55
        fclose(f);
55
56
        return;
56
57
    }
57
58
 
176
177
            if(name == NULL || target == NULL)
177
178
                continue;
178
179
 
179
 
            alias = (alias_t) malloc(sizeof(struct alias_st));
180
 
            memset(alias, 0, sizeof(struct alias_st));
 
180
            alias = (alias_t) calloc(1, sizeof(struct alias_st));
181
181
 
182
182
            alias->name = name;
183
183
            alias->target = target;
190
190
    r->check_keepalive = j_atoi(config_get_one(r->config, "check.keepalive", 0), 0);
191
191
}
192
192
 
193
 
static int _router_sx_sasl_callback(int cb, void *arg, void **res, scod_t sd, void *cbarg) {
 
193
static int _router_sx_sasl_callback(int cb, void *arg, void ** res, sx_t s, void *cbarg) {
194
194
    router_t r = (router_t) cbarg;
195
 
    sx_t s;
196
 
    scod_cb_creds_t creds;
 
195
    sx_sasl_creds_t creds;
 
196
    static char buf[1024];
197
197
    char *pass;
198
198
 
199
199
    switch(cb) {
200
200
        case sx_sasl_cb_GET_REALM:
201
 
            s = (sx_t) arg;
202
 
            strcpy((char *) res, "jabberd-router");
 
201
            strcpy(buf, "jabberd-router");
 
202
            *res = (void *)buf;
 
203
            return sx_sasl_ret_OK;
203
204
            break;
204
205
 
205
206
        case sx_sasl_cb_GET_PASS:
206
 
            creds = (scod_cb_creds_t) arg;
 
207
            creds = (sx_sasl_creds_t) arg;      
207
208
 
208
209
            log_debug(ZONE, "sx sasl callback: get pass (authnid=%s, realm=%s)", creds->authnid, creds->realm);
209
210
 
210
 
            *res = xhash_get(r->users, creds->authnid);
211
 
            if(*res == NULL)
212
 
                return 1;
 
211
            pass = xhash_get(r->users, creds->authnid);
 
212
            if(pass == NULL)
 
213
                return sx_sasl_ret_FAIL;
213
214
 
214
 
            return 0;
 
215
            *res = (void *)pass;
 
216
            return sx_sasl_ret_OK;
 
217
            break;
215
218
 
216
219
        case sx_sasl_cb_CHECK_PASS:
217
 
            creds = (scod_cb_creds_t) arg;
 
220
            creds = (sx_sasl_creds_t) arg;
218
221
 
219
222
            log_debug(ZONE, "sx sasl callback: check pass (authnid=%s, realm=%s)", creds->authnid, creds->realm);
220
223
 
221
224
            pass = xhash_get(r->users, creds->authnid);
222
225
            if(pass == NULL || strcmp(creds->pass, pass) != 0)
223
 
                return 1;
 
226
                return sx_sasl_ret_OK;
224
227
 
225
 
            return 0;
 
228
            return sx_sasl_ret_FAIL;
 
229
            break;
226
230
 
227
231
        case sx_sasl_cb_CHECK_AUTHZID:
228
 
            creds = (scod_cb_creds_t) arg;
229
 
            if(creds->authzid[0] == '\0')
230
 
                strcpy(creds->authzid, creds->authnid);
231
 
 
232
 
            if(strcmp(creds->authnid, creds->authzid) != 0) {
233
 
                log_debug(ZONE, "authnid '%s' doesn't match authzid '%s'", creds->authnid, creds->authzid);
234
 
                return 1;
235
 
            }
236
 
            return 0;
 
232
            creds = (sx_sasl_creds_t) arg;
 
233
 
 
234
            if (strcmp(creds->authnid, creds->authzid) == 0)
 
235
                return sx_sasl_ret_OK;
 
236
            else
 
237
                return sx_sasl_ret_FAIL;
 
238
            break;
 
239
 
 
240
        case sx_sasl_cb_CHECK_MECH:
 
241
 
 
242
            if (strcasecmp((char *)arg,"DIGEST-MD5")==0)
 
243
                return sx_sasl_ret_OK;
 
244
 
 
245
            return sx_sasl_ret_FAIL;
 
246
            break;
237
247
 
238
248
        default:
239
249
            break;
240
250
    }
241
251
 
242
 
    return 0;
 
252
    return sx_sasl_ret_FAIL;
243
253
}
244
254
 
245
255
static void _router_time_checks(router_t r) {
256
266
          xhash_iter_get(r->components, NULL, xhv.val);
257
267
 
258
268
         if(r->check_keepalive > 0 && target->last_activity > 0 && now > target->last_activity + r->check_keepalive && target->s->state >= state_STREAM) {
259
 
               log_debug(ZONE, "sending keepalive for %d", target->fd);
 
269
               log_debug(ZONE, "sending keepalive for %d", target->fd->fd);
260
270
               sx_raw_write(target->s, " ", 1);
261
271
          }
262
272
       } while(xhash_iter_next(r->components));
264
274
}
265
275
 
266
276
 
267
 
int main(int argc, char **argv)
 
277
JABBER_MAIN("jabberd2router", "Jabber 2 Router", "Jabber Open Source Server: Router", NULL)
268
278
{
269
279
    router_t r;
270
280
    char *config_file;
309
319
    jabber_signal(SIGPIPE, SIG_IGN);
310
320
#endif
311
321
 
312
 
    r = (router_t) malloc(sizeof(struct router_st));
313
 
    memset(r, 0, sizeof(struct router_st));
 
322
    r = (router_t) calloc(1, sizeof(struct router_st));
314
323
 
315
324
    /* load our config */
316
325
    r->config = config_new();
368
377
 
369
378
    r->aci = aci_load(r);
370
379
 
 
380
    if(filter_load(r)) exit(1);
 
381
 
371
382
    r->conn_rates = xhash_new(101);
372
383
 
373
 
    r->pc = prep_cache_new();
374
 
 
375
384
    r->components = xhash_new(101);
376
385
    r->routes = xhash_new(101);
377
386
 
383
392
 
384
393
#ifdef HAVE_SSL
385
394
    if(r->local_pemfile != NULL) {
386
 
        r->sx_ssl = sx_env_plugin(r->sx_env, sx_ssl_init, r->local_pemfile, NULL);
 
395
        r->sx_ssl = sx_env_plugin(r->sx_env, sx_ssl_init, NULL, r->local_pemfile, NULL, NULL);
387
396
        if(r->sx_ssl == NULL)
388
397
            log_write(r->log, LOG_ERR, "failed to load SSL pemfile, SSL disabled");
389
398
    }
390
399
#endif
391
400
 
392
401
    /* get sasl online */
393
 
    r->sx_sasl = sx_env_plugin(r->sx_env, sx_sasl_init, _router_sx_sasl_callback, (void *) r, sd_flag_GET_PASS);
 
402
    r->sx_sasl = sx_env_plugin(r->sx_env, sx_sasl_init, "jabberd-router", _router_sx_sasl_callback, (void *) r);
394
403
    if(r->sx_sasl == NULL) {
395
404
        log_write(r->log, LOG_ERR, "failed to initialise SASL context, aborting");
396
405
        exit(1);
399
408
    r->mio = mio_new(r->io_max_fds);
400
409
 
401
410
    r->fd = mio_listen(r->mio, r->local_port, r->local_ip, router_mio_callback, (void *) r);
402
 
    if(r->fd < 0) {
403
 
        log_write(r->log, LOG_ERR, "[%s, port=%d] unable to listen (%s)", r->local_ip, r->local_port, strerror(errno));
 
411
    if(r->fd == NULL) {
 
412
        log_write(r->log, LOG_ERR, "[%s, port=%d] unable to listen (%s)", r->local_ip, r->local_port, MIO_STRERROR(MIO_ERROR));
404
413
        exit(1);
405
414
    }
406
415
 
407
 
    log_write(r->log, LOG_NOTICE, "[%s, port=%d] listening for incoming connections", r->local_ip, r->local_port, strerror(errno));
 
416
    log_write(r->log, LOG_NOTICE, "[%s, port=%d] listening for incoming connections", r->local_ip, r->local_port, MIO_STRERROR(MIO_ERROR));
408
417
 
409
418
    while(!router_shutdown)
410
419
    {
417
426
            r->log = log_new(r->log_type, r->log_ident, r->log_facility);
418
427
            log_write(r->log, LOG_NOTICE, "log started");
419
428
 
 
429
            log_write(r->log, LOG_NOTICE, "reloading filter ...");
 
430
            filter_unload(r);
 
431
            filter_load(r);
 
432
 
420
433
            router_logrotate = 0;
421
434
        }
422
435
 
487
500
    /* unload acls */
488
501
    aci_unload(r->aci);
489
502
 
 
503
    /* unload filter */
 
504
    filter_unload(r);
 
505
 
490
506
    sx_env_free(r->sx_env);
491
507
 
492
 
    prep_cache_free(r->pc);
493
 
 
494
508
    mio_free(r->mio);
495
509
 
496
510
    access_free(r->access);