~ubuntu-branches/ubuntu/trusty/libpam-tacplus/trusty

« back to all changes in this revision

Viewing changes to libtac/lib/magic.c

  • Committer: Package Import Robot
  • Author(s): Jeroen Nijhof
  • Date: 2014-01-31 12:32:00 UTC
  • mfrom: (4.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140131123200-slruika7jmfmyfp6
Tags: 1.3.8-1
* New upstream release.
* Fixed pam-configs. Closes: #717716
* Added dh-autoreconf. Closes: #734228

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
#include <sys/types.h>
24
24
#include <sys/time.h>
25
25
#include <unistd.h>
26
 
 
27
 
/* u_int32_t support for sun */
28
 
#ifdef sun
29
 
typedef unsigned int u_int32_t;
30
 
#endif
31
 
 
32
 
#include "magic.h"
33
 
 
34
 
#ifndef __linux__
35
 
extern long mrand48 __P((void));
36
 
extern void srand48 __P((long));
37
 
#else
38
26
#include <sys/stat.h>
39
27
#include <fcntl.h>
40
28
 
41
 
/* on Linux we use /dev/urandom as random numbers source 
42
 
   I find it really cool :) */
43
 
int rfd = -1;   /* /dev/urandom */
44
 
int magic_inited = 0;
45
 
#endif
 
29
#include "magic.h"
 
30
 
 
31
static int rfd = -1;    /* fd for /dev/urandom */
 
32
static int magic_inited = 0;
46
33
 
47
34
/*
48
35
 * magic_init - Initialize the magic number generator.
54
41
void
55
42
magic_init()
56
43
{
 
44
    struct stat statbuf;
57
45
    long seed;
58
46
    struct timeval t;
59
47
 
60
 
#ifdef __linux__
 
48
    if (magic_inited)
 
49
        return;
 
50
 
61
51
    magic_inited = 1;
62
 
    rfd = open("/dev/urandom", O_RDONLY);
63
 
    if(rfd != -1) 
64
 
        return;
65
 
#endif
66
 
    /* if /dev/urandom fails, we try traditional method */
 
52
 
 
53
    /*
 
54
        try using /dev/urandom
 
55
        also check that it's a character device
 
56
        If it doesn't exist, fallback to other method
 
57
    */
 
58
 
 
59
    if (!lstat("/dev/urandom", &statbuf) && S_ISCHR(statbuf.st_mode)) {
 
60
        rfd = open("/dev/urandom", O_RDONLY);
 
61
        if (rfd >= 0)
 
62
            return;
 
63
    } 
 
64
 
67
65
    gettimeofday(&t, NULL);
68
66
    seed = gethostid() ^ t.tv_sec ^ t.tv_usec ^ getpid();
69
 
    srand48(seed);
 
67
    srandom(seed);
70
68
}
71
69
 
72
70
/*
75
73
u_int32_t
76
74
magic()
77
75
{
78
 
#ifdef __linux__
79
 
    u_int32_t ret = 0;
80
 
 
81
 
    if (magic_inited == 0 )
82
 
        magic_init();
83
 
 
84
 
        if(rfd > -1) {
85
 
            read(rfd, &ret, sizeof(ret));
86
 
            return ret;
 
76
    magic_init();
 
77
 
 
78
    if(rfd > -1) {
 
79
        u_int32_t ret;
 
80
 
 
81
        if (read(rfd, &ret, sizeof(ret)) < sizeof(ret)) {
 
82
            /* on read() error fallback to other method */
 
83
            return (u_int32_t)random();
87
84
        }
88
 
        else
89
 
            return (u_int32_t) mrand48();
90
 
#else
91
 
    return (u_int32_t) mrand48();
92
 
#endif
93
 
}
94
 
 
95
 
#ifdef NO_DRAND48
96
 
/*
97
 
 * Substitute procedures for those systems which don't have
98
 
 * drand48 et al.
99
 
 */
100
 
 
101
 
double
102
 
drand48()
103
 
{
104
 
    return (double)random() / (double)0x7fffffffL; /* 2**31-1 */
105
 
}
106
 
 
107
 
long
108
 
mrand48()
109
 
{
110
 
    return random();
111
 
}
112
 
 
113
 
void
114
 
srand48(seedval)
115
 
long seedval;
116
 
{
117
 
    srandom((int)seedval);
118
 
}
119
 
 
120
 
#endif
 
85
        return ret;
 
86
    }
 
87
    return (u_int32_t)random();
 
88
}
 
89