Whamcloud - gitweb
osd/mdd: new (weaker) locking for ->do_attr_{g,e}set(), see message to colibri@ for...
[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         if (getuid() == 0) {
65                 fprintf(stderr, "root user don't want to use lkinit\n");
66                 return 1;
67         }
68
69         newpag();
70         pag = getpag();
71
72         snprintf(kcmd, CMD_BUFSIZE, "kinit -c /tmp/krb5cc_pag_%lx", pag);
73
74         while ((opt = getopt(argc, argv, "k:")) != -1) {
75                 switch (opt) {
76                 case 'k':
77                         if (check_kopt(optarg)) {
78                                 fprintf(stderr, "Can't specify cache file\n");
79                                 return 1;
80                         }
81
82                         snprintf(kcmd, CMD_BUFSIZE,
83                                  "kinit -c /tmp/krb5cc_pag_%lx %s",
84                                  pag, optarg);
85                         break;
86                 default:
87                         usage(argv[0]);
88                 }
89         }
90
91         if (optind >= argc) {
92                 snprintf(cmd, CMD_BUFSIZE, "/bin/sh");
93         } else {
94                 for (i = optind; i < argc; i++) {
95                         if (i != optind)
96                                 strncat(cmd, " ", CMD_BUFSIZE);
97                         strncat(cmd, argv[i], CMD_BUFSIZE);
98                 }
99         }
100
101         if (system(kcmd)) {
102                 fprintf(stderr, "can't get kerberos TGT\n");
103                 return 1;
104         }
105
106         if (system(cmd))
107                 fprintf(stderr, "execute error\n");
108
109         /* flush in-kernel credential cache */
110         snprintf(cmd, CMD_BUFSIZE, "lfs flushctx");
111         if (system(cmd))
112                 fprintf(stderr, "failed to flush in-kernel credential\n");
113
114         /* flush user-space credential cache */
115         snprintf(kcmd, CMD_BUFSIZE, "kdestroy -c /tmp/krb5cc_pag_%lx", pag);
116         system(kcmd);
117
118         return 0;
119 }