~ubuntu-branches/ubuntu/saucy/nfs-utils/saucy-proposed

« back to all changes in this revision

Viewing changes to utils/nfsidmap/nfsidmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Luk Claes
  • Date: 2011-07-09 16:28:32 UTC
  • mfrom: (1.2.20 upstream)
  • mto: (71.1.1 ubuntu)
  • mto: This revision was merged to the branch mainline in revision 41.
  • Revision ID: james.westby@ubuntu.com-20110709162832-ovaehe77pm3hyy35
Tags: 1:1.2.4-1
* New upstream version
  - Fix host_reliable_addrinfo (Closes: #633155)
  - Allow multiple RPC listeners to share listener port number
  (Closes: #619877)
  - Add --enable-libmount-mount (Closes: #626478)
  - 12-svcgssd-document-n-option.patch applied upstream
  - Refresh 19-exports.man-Fix-comment-syntax.patch
  - 21-anticipate-RLIMIT_FSIZE.patch applied upstream
  - Add nfsidmap binary and manpage
  - Use autoreconf to avoid build failure

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <stdarg.h>
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
#include <string.h>
 
6
 
 
7
#include <pwd.h>
 
8
#include <grp.h>
 
9
#include <keyutils.h>
 
10
#include <nfsidmap.h>
 
11
 
 
12
#include <syslog.h>
 
13
 
 
14
/* gcc nfsidmap.c -o nfsidmap -l nfsidmap -l keyutils */
 
15
 
 
16
#define MAX_ID_LEN   11
 
17
#define IDMAP_NAMESZ 128
 
18
#define USER  1
 
19
#define GROUP 0
 
20
 
 
21
 
 
22
/*
 
23
 * Find either a user or group id based on the name@domain string
 
24
 */
 
25
int id_lookup(char *name_at_domain, key_serial_t key, int type)
 
26
{
 
27
        char id[MAX_ID_LEN];
 
28
        uid_t uid = 0;
 
29
        gid_t gid = 0;
 
30
        int rc;
 
31
 
 
32
        if (type == USER) {
 
33
                rc = nfs4_owner_to_uid(name_at_domain, &uid);
 
34
                sprintf(id, "%u", uid);
 
35
        } else {
 
36
                rc = nfs4_group_owner_to_gid(name_at_domain, &gid);
 
37
                sprintf(id, "%u", gid);
 
38
        }
 
39
 
 
40
        if (rc == 0)
 
41
                rc = keyctl_instantiate(key, id, strlen(id) + 1, 0);
 
42
 
 
43
        return rc;
 
44
}
 
45
 
 
46
/*
 
47
 * Find the name@domain string from either a user or group id
 
48
 */
 
49
int name_lookup(char *id, key_serial_t key, int type)
 
50
{
 
51
        char name[IDMAP_NAMESZ];
 
52
        char domain[NFS4_MAX_DOMAIN_LEN];
 
53
        uid_t uid;
 
54
        gid_t gid;
 
55
        int rc;
 
56
 
 
57
        rc = nfs4_get_default_domain(NULL, domain, NFS4_MAX_DOMAIN_LEN);
 
58
        if (rc != 0) {
 
59
                rc = -1;
 
60
                goto out;
 
61
        }
 
62
 
 
63
        if (type == USER) {
 
64
                uid = atoi(id);
 
65
                rc = nfs4_uid_to_name(uid, domain, name, IDMAP_NAMESZ);
 
66
        } else {
 
67
                gid = atoi(id);
 
68
                rc = nfs4_gid_to_name(gid, domain, name, IDMAP_NAMESZ);
 
69
        }
 
70
 
 
71
        if (rc == 0)
 
72
                rc = keyctl_instantiate(key, &name, strlen(name), 0);
 
73
 
 
74
out:
 
75
        return rc;
 
76
}
 
77
 
 
78
int main(int argc, char **argv)
 
79
{
 
80
        char *arg;
 
81
        char *value;
 
82
        char *type;
 
83
        int rc = 1;
 
84
        int timeout = 600;
 
85
        key_serial_t key;
 
86
 
 
87
        if (argc < 3)
 
88
                return 1;
 
89
 
 
90
        arg = malloc(sizeof(char) * strlen(argv[2]) + 1);
 
91
        strcpy(arg, argv[2]);
 
92
        type = strtok(arg, ":");
 
93
        value = strtok(NULL, ":");
 
94
 
 
95
        if (argc == 4) {
 
96
                timeout = atoi(argv[3]);
 
97
                if (timeout < 0)
 
98
                        timeout = 0;
 
99
        }
 
100
 
 
101
        key = strtol(argv[1], NULL, 10);
 
102
 
 
103
        if (strcmp(type, "uid") == 0)
 
104
                rc = id_lookup(value, key, USER);
 
105
        else if (strcmp(type, "gid") == 0)
 
106
                rc = id_lookup(value, key, GROUP);
 
107
        else if (strcmp(type, "user") == 0)
 
108
                rc = name_lookup(value, key, USER);
 
109
        else if (strcmp(type, "group") == 0)
 
110
                rc = name_lookup(value, key, GROUP);
 
111
 
 
112
        /* Set timeout to 5 (600 seconds) minutes */
 
113
        if (rc == 0)
 
114
                keyctl_set_timeout(key, timeout);
 
115
 
 
116
        free(arg);
 
117
        return rc;
 
118
}