~ubuntu-branches/debian/lenny/netatalk/lenny

« back to all changes in this revision

Viewing changes to etc/afpd/uam.c

  • Committer: Bazaar Package Importer
  • Author(s): Ante Karamatic
  • Date: 2005-10-07 13:46:11 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20051007134611-r07qa2g67xwkp2if
Tags: 2.0.3-1ubuntu1
* debian/netatalk.init
  - run cnid_metad if CNID_METAD_RUN=yes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: uam.c,v 1.22.4.1 2003/06/09 14:30:45 srittau Exp $
 
2
 * $Id: uam.c,v 1.24.6.7.2.3 2005/02/01 11:33:48 didg Exp $
3
3
 *
4
4
 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5
5
 * All Rights Reserved.  See COPYRIGHT.
56
56
#include "auth.h"
57
57
#include "uam_auth.h"
58
58
 
 
59
#ifdef AFP3x
 
60
#define utf8_encoding() (afp_version >= 30)
 
61
#else
 
62
#define utf8_encoding() (0)
 
63
#endif
 
64
 
59
65
#ifdef TRU64
60
66
#include <netdb.h>
61
67
#include <sia.h>
64
70
#endif /* TRU64 */
65
71
 
66
72
/* --- server uam functions -- */
 
73
#ifndef NO_LOAD_UAM
 
74
extern  int uam_setup(const char *path);
 
75
#endif
67
76
 
68
77
/* uam_load. uams must have a uam_setup function. */
69
78
struct uam_mod *uam_load(const char *path, const char *name)
72
81
    struct uam_mod *mod;
73
82
    void *module;
74
83
 
 
84
#ifndef NO_LOAD_UAM
75
85
    if ((module = mod_open(path)) == NULL) {
76
86
        LOG(log_error, logtype_afpd, "uam_load(%s): failed to load: %s", name, mod_error());
77
87
        return NULL;
78
88
    }
 
89
#endif
79
90
 
80
91
    if ((mod = (struct uam_mod *) malloc(sizeof(struct uam_mod))) == NULL) {
81
92
        LOG(log_error, logtype_afpd, "uam_load(%s): malloc failed", name);
82
93
        goto uam_load_fail;
83
94
    }
84
95
 
85
 
    strncpy(buf, name, sizeof(buf));
86
 
    buf[sizeof(buf) - 1] = '\0';
 
96
    strlcpy(buf, name, sizeof(buf));
87
97
    if ((p = strchr(buf, '.')))
88
98
        *p = '\0';
 
99
 
 
100
#ifndef NO_LOAD_UAM
89
101
    if ((mod->uam_fcn = mod_symbol(module, buf)) == NULL) {
90
102
        LOG(log_error, logtype_afpd, "uam_load(%s): mod_symbol error for symbol %s",
91
103
            name,
106
118
        LOG(log_error, logtype_afpd, "uam_load(%s): uam_setup failed", name);
107
119
        goto uam_load_err;
108
120
    }
 
121
#else
 
122
   uam_setup(name);
 
123
#endif
109
124
 
110
125
    mod->uam_module = module;
111
126
    return mod;
124
139
{
125
140
    if (mod->uam_fcn->uam_cleanup)
126
141
        (*mod->uam_fcn->uam_cleanup)();
 
142
 
 
143
#ifndef NO_LOAD_UAM
127
144
    mod_close(mod->uam_module);
 
145
#endif    
128
146
    free(mod);
129
147
}
130
148
 
131
149
/* -- client-side uam functions -- */
132
 
 
 
150
#ifndef ATACC
133
151
/* set up stuff for this uam. */
134
152
int uam_register(const int type, const char *path, const char *name, ...)
135
153
{
136
154
    va_list ap;
137
155
    struct uam_obj *uam;
 
156
    int ret;
138
157
 
139
158
    if (!name)
140
159
        return -1;
161
180
 
162
181
    va_start(ap, name);
163
182
    switch (type) {
 
183
    case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
 
184
        uam->u.uam_login.login = va_arg(ap, void *);
 
185
        uam->u.uam_login.logincont = va_arg(ap, void *);
 
186
        uam->u.uam_login.logout = va_arg(ap, void *);
 
187
        uam->u.uam_login.login_ext = va_arg(ap, void *);
 
188
        break;
 
189
    
164
190
    case UAM_SERVER_LOGIN: /* expect three arguments */
 
191
        uam->u.uam_login.login_ext = NULL;
165
192
        uam->u.uam_login.login = va_arg(ap, void *);
166
193
        uam->u.uam_login.logincont = va_arg(ap, void *);
167
194
        uam->u.uam_login.logout = va_arg(ap, void *);
176
203
    va_end(ap);
177
204
 
178
205
    /* attach to other uams */
 
206
    ret = auth_register(type, uam);
 
207
    if ( ret) {
 
208
        free(uam->uam_path);
 
209
        free(uam);
 
210
    }
 
211
 
 
212
    return ret;
 
213
}
 
214
#endif
 
215
 
 
216
#ifdef ATACC
 
217
int uam_register_fn(const int type, const char *path, const char *name, void *fn1, void *fn2, 
 
218
                     void *fn3, void *fn4)
 
219
{
 
220
    va_list ap;
 
221
    struct uam_obj *uam;
 
222
 
 
223
    if (!name)
 
224
        return -1;
 
225
 
 
226
    /* see if it already exists. */
 
227
    if ((uam = auth_uamfind(type, name, strlen(name)))) {
 
228
        if (strcmp(uam->uam_path, path)) {
 
229
            /* it exists, but it's not the same module. */
 
230
            LOG(log_error, logtype_afpd, "uam_register: \"%s\" already loaded by %s",
 
231
                name, path);
 
232
            return -1;
 
233
        }
 
234
        uam->uam_count++;
 
235
        return 0;
 
236
    }
 
237
 
 
238
    /* allocate space for uam */
 
239
    if ((uam = calloc(1, sizeof(struct uam_obj))) == NULL)
 
240
        return -1;
 
241
 
 
242
    uam->uam_name = name;
 
243
    uam->uam_path = strdup(path);
 
244
    uam->uam_count++;
 
245
 
 
246
    switch (type) {
 
247
    case UAM_SERVER_LOGIN_EXT: /* expect four arguments */
 
248
        uam->u.uam_login.login_ext = fn4;
 
249
        uam->u.uam_login.login = fn1;
 
250
        uam->u.uam_login.logincont = fn2;
 
251
        uam->u.uam_login.logout = fn3;
 
252
        break;
 
253
    case UAM_SERVER_LOGIN: /* expect three arguments */
 
254
        uam->u.uam_login.login_ext = NULL;
 
255
        uam->u.uam_login.login = fn1;
 
256
        uam->u.uam_login.logincont = fn2;
 
257
        uam->u.uam_login.logout = fn3;
 
258
        break;
 
259
    case UAM_SERVER_CHANGEPW: /* one argument */
 
260
        uam->u.uam_changepw = fn1;
 
261
        break;
 
262
    case UAM_SERVER_PRINTAUTH: /* x arguments */
 
263
    default:
 
264
        break;
 
265
    }
 
266
 
 
267
    /* attach to other uams */
179
268
    if (auth_register(type, uam) < 0) {
180
269
        free(uam->uam_path);
181
270
        free(uam);
184
273
 
185
274
    return 0;
186
275
}
 
276
#endif
187
277
 
188
278
void uam_unregister(const int type, const char *name)
189
279
{
203
293
 
204
294
/* --- helper functions for plugin uams --- */
205
295
 
206
 
struct passwd *uam_getname(char *name, const int len)
 
296
struct passwd *uam_getname(void *private, char *name, const int len)
207
297
{
 
298
    AFPObj *obj = private;
208
299
    struct passwd *pwent;
209
 
    char *user;
210
 
    int i;
 
300
    static char username[256];
 
301
    static char user[256];
 
302
    static char pwname[256];
 
303
    char *p;
 
304
    size_t namelen, gecoslen = 0, pwnamelen = 0;
211
305
 
212
306
    if ((pwent = getpwnam(name)))
213
307
        return pwent;
214
308
 
215
309
#ifndef NO_REAL_USER_NAME
216
 
    for (i = 0; i < len; i++)
217
 
        name[i] = tolower(name[i]);
 
310
 
 
311
    if ( (size_t) -1 == (namelen = convert_string((utf8_encoding())?CH_UTF8_MAC:obj->options.maccharset,
 
312
                                CH_UCS2, name, strlen(name), username, sizeof(username))))
 
313
        return NULL;
218
314
 
219
315
    setpwent();
220
316
    while ((pwent = getpwent())) {
221
 
        if ((user = strchr(pwent->pw_gecos, ',')))
222
 
            *user = '\0';
223
 
        user = pwent->pw_gecos;
 
317
        if ((p = strchr(pwent->pw_gecos, ',')))
 
318
            *p = '\0';
 
319
 
 
320
        if ((size_t)-1 == ( gecoslen = convert_string(obj->options.unixcharset, CH_UCS2, 
 
321
                                pwent->pw_gecos, strlen(pwent->pw_gecos), user, sizeof(username))) )
 
322
                continue;
 
323
        if ((size_t)-1 == ( pwnamelen = convert_string(obj->options.unixcharset, CH_UCS2, 
 
324
                                pwent->pw_name, strlen(pwent->pw_name), pwname, sizeof(username))) )
 
325
                continue;
 
326
 
224
327
 
225
328
        /* check against both the gecos and the name fields. the user
226
329
         * might have just used a different capitalization. */
227
 
        if ((strncasecmp(user, name, len) == 0) ||
228
 
                (strncasecmp(pwent->pw_name, name, len) == 0)) {
229
 
            strncpy(name, pwent->pw_name, len);
230
 
            name[len - 1] = '\0';
 
330
 
 
331
        if ( (namelen == gecoslen && strncasecmp_w((ucs2_t*)user, (ucs2_t*)username, len) == 0) || 
 
332
                ( namelen == pwnamelen && strncasecmp_w ( (ucs2_t*) pwname, (ucs2_t*) username, len) == 0)) {
 
333
            strlcpy(name, pwent->pw_name, len);
231
334
            break;
232
335
        }
233
336
    }
250
353
                LOG(log_info, logtype_afpd, "uam_checkuser: User %s does not have a shell", pwd->pw_name);
251
354
                return -1;
252
355
        }
253
 
#endif
254
356
 
255
357
    while ((p = getusershell())) {
256
358
        if ( strcmp( p, pwd->pw_shell ) == 0 )
258
360
    }
259
361
    endusershell();
260
362
 
261
 
#ifndef DISABLE_SHELLCHECK
262
363
    if (!p) {
263
364
        LOG(log_info, logtype_afpd, "illegal shell %s for %s", pwd->pw_shell, pwd->pw_name);
264
365
        return -1;
268
369
    return 0;
269
370
}
270
371
 
 
372
int uam_random_string (AFPObj *obj, char *buf, int len)
 
373
{
 
374
    u_int32_t result;
 
375
    int ret;
 
376
    int fd;
 
377
 
 
378
    if ( (len <= 0) || (len % sizeof(result)))
 
379
            return -1;
 
380
 
 
381
    /* construct a random number */
 
382
    if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
 
383
        struct timeval tv;
 
384
        struct timezone tz;
 
385
        int i;
 
386
 
 
387
        if (gettimeofday(&tv, &tz) < 0)
 
388
            return -1;
 
389
        srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
 
390
        for (i = 0; i < len; i += sizeof(result)) {
 
391
            result = random();
 
392
            memcpy(buf + i, &result, sizeof(result));
 
393
        }
 
394
    } else {
 
395
        ret = read(fd, buf, len);
 
396
        close(fd);
 
397
        if (ret <= 0)
 
398
            return -1;
 
399
    }
 
400
    return 0;
 
401
}
 
402
 
271
403
/* afp-specific functions */
272
404
int uam_afpserver_option(void *private, const int what, void *option,
273
405
                         int *len)
274
406
{
275
407
AFPObj *obj = private;
276
408
    char **buf = (char **) option; /* most of the options are this */
277
 
    int32_t result;
278
 
    int fd;
 
409
    struct session_info **sinfo = (struct session_info **) option;
279
410
 
280
411
    if (!obj || !option)
281
412
        return -1;
282
413
 
283
414
    switch (what) {
284
415
    case UAM_OPTION_USERNAME:
285
 
        *buf = (void *) obj->username;
 
416
        *buf = obj->username;
286
417
        if (len)
287
418
            *len = sizeof(obj->username) - 1;
288
419
        break;
289
420
 
290
421
    case UAM_OPTION_GUEST:
291
 
        *buf = (void *) obj->options.guest;
 
422
        *buf = obj->options.guest;
292
423
        if (len)
293
424
            *len = strlen(obj->options.guest);
294
425
        break;
299
430
 
300
431
        switch (*len) {
301
432
        case UAM_PASSWD_FILENAME:
302
 
            *buf = (void *) obj->options.passwdfile;
 
433
            *buf = obj->options.passwdfile;
303
434
            *len = strlen(obj->options.passwdfile);
304
435
            break;
305
436
 
327
458
        break;
328
459
 
329
460
    case UAM_OPTION_RANDNUM: /* returns a random number in 4-byte units. */
330
 
        if (!len || (*len < 0) || (*len % sizeof(result)))
 
461
        if (!len)
331
462
            return -1;
332
463
 
333
 
        /* construct a random number */
334
 
        if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
335
 
            struct timeval tv;
336
 
            struct timezone tz;
337
 
            char *randnum = (char *) option;
338
 
            int i;
339
 
 
340
 
            if (gettimeofday(&tv, &tz) < 0)
341
 
                return -1;
342
 
            srandom(tv.tv_sec + (unsigned long) obj + (unsigned long) obj->handle);
343
 
            for (i = 0; i < *len; i += sizeof(result)) {
344
 
                result = random();
345
 
                memcpy(randnum + i, &result, sizeof(result));
346
 
            }
347
 
        } else {
348
 
            result = read(fd, option, *len);
349
 
            close(fd);
350
 
            if (result < 0)
351
 
                return -1;
352
 
        }
 
464
        return uam_random_string(obj, option, *len);
353
465
        break;
354
466
 
355
467
    case UAM_OPTION_HOSTNAME:
356
 
        *buf = (void *) obj->options.hostname;
 
468
        *buf = obj->options.hostname;
357
469
        if (len)
358
470
            *len = strlen(obj->options.hostname);
359
471
        break;
360
472
 
361
473
    case UAM_OPTION_PROTOCOL:
362
 
        *buf = (void *) obj->proto;
 
474
        *((int *) option) = obj->proto;
363
475
        break;
 
476
        
364
477
    case UAM_OPTION_CLIENTNAME:
365
478
        {
366
479
            struct DSI *dsi = obj->handle;
370
483
                                sizeof( struct in_addr ),
371
484
                                dsi->client.sin_family );
372
485
            if( hp )
373
 
                *buf = (void *) hp->h_name;
 
486
                *buf = hp->h_name;
374
487
            else
375
 
                *buf = (void *) inet_ntoa( dsi->client.sin_addr );
 
488
                *buf = inet_ntoa( dsi->client.sin_addr );
376
489
        }
377
490
        break;
378
491
    case UAM_OPTION_COOKIE:
383
496
        *buf = (void *) &obj->uam_cookie;
384
497
        break;
385
498
    case UAM_OPTION_KRB5SERVICE:
386
 
        *buf = obj->options.k5service;
387
 
        if (len)
388
 
            *len = strlen(obj->options.k5service);
 
499
        *buf = obj->options.k5service;
 
500
        if (len)
 
501
            *len = (*buf)?strlen(*buf):0;
 
502
        break;
 
503
    case UAM_OPTION_KRB5REALM:
 
504
        *buf = obj->options.k5realm;
 
505
        if (len)
 
506
            *len = (*buf)?strlen(*buf):0;
 
507
        break;
 
508
    case UAM_OPTION_FQDN:
 
509
        *buf = obj->options.fqdn;
 
510
        if (len)
 
511
            *len = (*buf)?strlen(*buf):0;
 
512
        break;
 
513
    case UAM_OPTION_MACCHARSET:
 
514
        *((int *) option) = obj->options.maccharset;
 
515
        *len = sizeof(obj->options.maccharset);
 
516
        break;
 
517
    case UAM_OPTION_UNIXCHARSET:
 
518
        *((int *) option) = obj->options.unixcharset;
 
519
        *len = sizeof(obj->options.unixcharset);
 
520
        break;
 
521
    case UAM_OPTION_SESSIONINFO:
 
522
        *sinfo = &(obj->sinfo);
389
523
        break;
390
524
    default:
391
525
        return -1;