Upload Tizen:Base source
[framework/base/util-linux-ng.git] / login-utils / newgrp.c
1 /* setgrp.c - by Michael Haardt. Set the gid if possible */
2 /* Added a bit more error recovery/reporting - poe */
3 /* Vesa Roukonen added code for asking password */
4 /* Currently maintained at ftp://ftp.daimi.aau.dk/pub/linux/poe/ */
5
6 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
7  * - added Native Language Support
8  */
9
10 #include <unistd.h>
11 #include <pwd.h>
12 #include <grp.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include "pathnames.h"
18 #include "my_crypt.h"
19 #include "nls.h"
20
21 #ifndef TRUE
22 # define TRUE 1
23 #endif
24
25 #ifndef FALSE
26 # define FALSE 0
27 #endif
28
29 /* try to read password from gshadow */
30 static char *
31 get_gshadow_pwd(char *groupname)
32 {
33         char buf[BUFSIZ];
34         char *pwd = NULL;
35         FILE *f = fopen(_PATH_GSHADOW, "r");
36
37         if (groupname == NULL || *groupname == '\0' || f == NULL)
38                 return NULL;
39
40         while(fgets(buf, sizeof buf, f))
41         {
42                 char *cp = strchr (buf, ':');
43                 if (!cp)
44                         continue;                               /* any junk in gshadow? */
45                 *cp = '\0';
46                 if (strcmp(buf, groupname) == 0)
47                 {
48                         if (cp-buf >= BUFSIZ)
49                                 break;                          /* only group name on line */
50                         pwd = cp+1;
51                         if ((cp = strchr(pwd, ':')) && pwd == cp+1 )
52                                 pwd = NULL;                     /* empty password */
53                         else if (cp)
54                                 *cp = '\0';
55                         break;
56                 }
57         }
58         fclose(f);
59         return pwd ? strdup(pwd) : NULL;
60 }
61
62 static int
63 allow_setgid(struct passwd *pe, struct group *ge) 
64 {
65     char **look;
66     int notfound = 1;
67     char *pwd, *xpwd;
68
69     if (getuid() == 0) return TRUE;     /* root may do anything */
70     if (ge->gr_gid == pe->pw_gid) return TRUE; /* You can switch back to your default group */
71
72     look = ge->gr_mem;
73     while (*look && (notfound = strcmp(*look++,pe->pw_name)));
74
75     if(!notfound) return TRUE;          /* member of group => OK */
76
77     /* Ask for password. Often there is no password in /etc/group, so
78        contrary to login et al. we let an empty password mean the same
79        as * in /etc/passwd */
80
81     /* check /etc/gshadow */
82     if (!(pwd = get_gshadow_pwd(ge->gr_name)))
83         pwd = ge->gr_passwd;
84
85     if(pwd && *pwd && (xpwd = getpass(_("Password: ")))) {
86         if(strcmp(pwd, crypt(xpwd, pwd)) == 0)
87            return TRUE;         /* password accepted */
88      }
89
90     return FALSE;                       /* default to denial */
91 }
92
93 int 
94 main(int argc, char *argv[])
95 {
96     struct passwd *pw_entry;
97     struct group *gr_entry;
98     char *shell;
99
100     setlocale(LC_ALL, "");
101     bindtextdomain(PACKAGE, LOCALEDIR);
102     textdomain(PACKAGE);
103     
104     if (!(pw_entry = getpwuid(getuid()))) {
105         perror(_("newgrp: Who are you?"));
106         exit(1);
107     }
108     
109     shell = (pw_entry->pw_shell[0] ? pw_entry->pw_shell : _PATH_BSHELL);
110     
111     if (argc < 2) {
112         if(setgid(pw_entry->pw_gid) < 0) {
113             perror(_("newgrp: setgid"));
114             exit(1);
115         }
116     } else {
117         errno = 0;
118         if (!(gr_entry = getgrnam(argv[1]))) {
119             if (errno)
120                     perror(_("newgrp: No such group."));                        /* error */
121             else
122                     fprintf(stderr, "%s\n", _("newgrp: No such group."));       /* no group */
123             exit(1);
124         } else {
125             if(allow_setgid(pw_entry, gr_entry)) {
126                 if(setgid(gr_entry->gr_gid) < 0) {
127                     perror(_("newgrp: setgid"));
128                     exit(1);
129                 }
130             } else {
131                 puts(_("newgrp: Permission denied"));
132                 exit(1);
133             }
134         }
135     }
136
137     if(setuid(getuid()) < 0) {
138         perror(_("newgrp: setuid"));
139         exit(1);
140     }
141
142     fflush(stdout); fflush(stderr);
143     execl(shell,shell,(char*)0);
144     perror(_("No shell"));
145     fflush(stderr);
146     exit(1);
147 }