Whamcloud - gitweb
some env (cnbuild) can't build without explicitly include
[fs/lustre-release.git] / lustre / utils / lkinit.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <linux/unistd.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29
30 #ifndef __NR_newpag
31 #define __NR_newpag             289
32 #define __NR_getpag             290
33 #endif
34
35 _syscall0(int, newpag);
36 _syscall0(int, getpag);
37
38 void usage(const char *exe)
39 {
40         printf("Usage: %s -k \"parameters_to_kinit\" command line\n", exe);
41         exit(1);
42 }
43
44 int check_kopt(const char *opt)
45 {
46         /* FIXME check "-c" here */
47         return 0;
48 }
49
50 #define CMD_BUFSIZE     4096
51
52 int main(int argc, char *argv[])
53 {
54         extern char *optarg;
55         int opt, i;
56         unsigned long pag;
57         char kopt[CMD_BUFSIZE];
58         char kcmd[CMD_BUFSIZE];
59         char cmd[CMD_BUFSIZE];
60
61         kopt[0] = '\0';
62         cmd[0] = '\0';
63
64         newpag();
65         pag = getpag();
66
67         snprintf(kcmd, CMD_BUFSIZE, "kinit -c /tmp/krb5cc_pag_%lx", pag);
68
69         while ((opt = getopt(argc, argv, "k:")) != -1) {
70                 switch (opt) {
71                 case 'k':
72                         if (check_kopt(optarg)) {
73                                 fprintf(stderr, "Can't specify cache file\n");
74                                 return 1;
75                         }
76
77                         snprintf(kcmd, CMD_BUFSIZE,
78                                  "kinit -c /tmp/krb5cc_pag_%lx %s",
79                                  pag, optarg);
80                         break;
81                 default:
82                         usage(argv[0]);
83                 }
84         }
85
86         if (optind >= argc) {
87                 snprintf(cmd, CMD_BUFSIZE, "/bin/sh");
88         } else {
89                 for (i = optind; i < argc; i++) {
90                         if (i != optind)
91                                 strncat(cmd, " ", CMD_BUFSIZE);
92                         strncat(cmd, argv[i], CMD_BUFSIZE);
93                 }
94         }
95
96         if (system(kcmd)) {
97                 fprintf(stderr, "can't get kerberos TGT\n");
98                 return 1;
99         }
100
101         if (system(cmd))
102                 fprintf(stderr, "execute error\n");
103
104         /* flush in-kernel credential cache */
105         snprintf(cmd, CMD_BUFSIZE, "lctl flush_cred");
106         if (system(cmd))
107                 fprintf(stderr, "failed to flush in-kernel credential\n");
108
109         /* flush user-space credential cache */
110         snprintf(kcmd, CMD_BUFSIZE, "kdestroy -c /tmp/krb5cc_pag_%lx", pag);
111         system(kcmd);
112
113         return 0;
114 }