aboutsummaryrefslogtreecommitdiff
path: root/loginutils/addgroup.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2006-04-04 19:19:53 +0000
committerRob Landley <rob@landley.net>2006-04-04 19:19:53 +0000
commit9a2dd51237c065982b918f7e15d9248aa10d89d4 (patch)
treecb2aa1bf6f3ac94c8517994924da1a1a3a607429 /loginutils/addgroup.c
parentab8736064b0220c0b7dd3d4226c3f40976a31c53 (diff)
downloadbusybox-w32-9a2dd51237c065982b918f7e15d9248aa10d89d4.tar.gz
busybox-w32-9a2dd51237c065982b918f7e15d9248aa10d89d4.tar.bz2
busybox-w32-9a2dd51237c065982b918f7e15d9248aa10d89d4.zip
Rewrite/shrink by tito.
Diffstat (limited to 'loginutils/addgroup.c')
-rw-r--r--loginutils/addgroup.c112
1 files changed, 27 insertions, 85 deletions
diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c
index 7b68f394e..f4962ffb9 100644
--- a/loginutils/addgroup.c
+++ b/loginutils/addgroup.c
@@ -5,48 +5,20 @@
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu 5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org> 6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * 9 *
22 */ 10 */
23 11
24#include <errno.h>
25#include <fcntl.h>
26#include <stdarg.h>
27#include <stdio.h> 12#include <stdio.h>
28#include <stdlib.h>
29#include <string.h> 13#include <string.h>
30#include <sys/param.h>
31#include <sys/stat.h>
32#include <sys/types.h> 14#include <sys/types.h>
33#include <unistd.h> 15#include <unistd.h>
34#include "busybox.h"
35#include "pwd_.h"
36#include "grp_.h"
37
38
39/* structs __________________________ */
40
41/* data _____________________________ */
42
43/* defaults : should this be in an external file? */
44static const char default_passwd[] = "x";
45 16
17#include "busybox.h"
46 18
47/* make sure gr_name isn't taken, make sure gid is kosher 19/* make sure gr_name isn't taken, make sure gid is kosher
48 * return 1 on failure */ 20 * return 1 on failure */
49static int group_study(const char *filename, struct group *g) 21static int group_study(struct group *g)
50{ 22{
51 FILE *etc_group; 23 FILE *etc_group;
52 gid_t desired; 24 gid_t desired;
@@ -54,16 +26,16 @@ static int group_study(const char *filename, struct group *g)
54 struct group *grp; 26 struct group *grp;
55 const int max = 65000; 27 const int max = 65000;
56 28
57 etc_group = bb_xfopen(filename, "r"); 29 etc_group = bb_xfopen(bb_path_group_file, "r");
58 30
59 /* make sure gr_name isn't taken, make sure gid is kosher */ 31 /* make sure gr_name isn't taken, make sure gid is kosher */
60 desired = g->gr_gid; 32 desired = g->gr_gid;
61 while ((grp = fgetgrent(etc_group))) { 33 while ((grp = fgetgrent(etc_group))) {
62 if ((strcmp(grp->gr_name, g->gr_name)) == 0) { 34 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
63 bb_error_msg_and_die("%s: group already in use\n", g->gr_name); 35 bb_error_msg_and_die("%s: group already in use", g->gr_name);
64 } 36 }
65 if ((desired) && grp->gr_gid == desired) { 37 if ((desired) && grp->gr_gid == desired) {
66 bb_error_msg_and_die("%d: gid has already been allocated\n", 38 bb_error_msg_and_die("%d: gid already in use",
67 desired); 39 desired);
68 } 40 }
69 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) { 41 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
@@ -83,56 +55,33 @@ static int group_study(const char *filename, struct group *g)
83} 55}
84 56
85/* append a new user to the passwd file */ 57/* append a new user to the passwd file */
86static int addgroup(const char *filename, char *group, gid_t gid, const char *user) 58static int addgroup(char *group, gid_t gid, const char *user)
87{ 59{
88 FILE *etc_group; 60 FILE *file;
89
90#ifdef CONFIG_FEATURE_SHADOWPASSWDS
91 FILE *etc_gshadow;
92#endif
93
94 struct group gr; 61 struct group gr;
95 62
96 /* group:passwd:gid:userlist */
97 static const char entryfmt[] = "%s:%s:%d:%s\n";
98
99 /* make sure gid and group haven't already been allocated */ 63 /* make sure gid and group haven't already been allocated */
100 gr.gr_gid = gid; 64 gr.gr_gid = gid;
101 gr.gr_name = group; 65 gr.gr_name = group;
102 if (group_study(filename, &gr)) 66 if (group_study(&gr))
103 return 1; 67 return 1;
104 68
105 /* add entry to group */ 69 /* add entry to group */
106 etc_group = bb_xfopen(filename, "a"); 70 file = bb_xfopen(bb_path_group_file, "a");
107 71 /* group:passwd:gid:userlist */
108 fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, user); 72 fprintf(file, "%s:%s:%d:%s\n", group, "x", gr.gr_gid, user);
109 fclose(etc_group); 73 fclose(file);
110
111 74
112#ifdef CONFIG_FEATURE_SHADOWPASSWDS 75#if ENABLE_FEATURE_SHADOWPASSWDS
113 /* add entry to gshadow if necessary */ 76 file = bb_xfopen(bb_path_gshadow_file, "a");
114 if (access(bb_path_gshadow_file, F_OK|W_OK) == 0) { 77 fprintf(file, "%s:!::\n", group);
115 etc_gshadow = bb_xfopen(bb_path_gshadow_file, "a"); 78 fclose(file);
116 fprintf(etc_gshadow, "%s:!::\n", group);
117 fclose(etc_gshadow);
118 }
119#endif 79#endif
120 80
121 /* return 1; */ 81 /* return 1; */
122 return 0; 82 return 0;
123} 83}
124 84
125#ifndef CONFIG_ADDUSER
126static inline void if_i_am_not_root(void)
127{
128 if (geteuid()) {
129 bb_error_msg_and_die( "Only root may add a user or group to the system.");
130 }
131}
132#else
133extern void if_i_am_not_root(void);
134#endif
135
136/* 85/*
137 * addgroup will take a login_name as its first parameter. 86 * addgroup will take a login_name as its first parameter.
138 * 87 *
@@ -143,29 +92,22 @@ extern void if_i_am_not_root(void);
143int addgroup_main(int argc, char **argv) 92int addgroup_main(int argc, char **argv)
144{ 93{
145 char *group; 94 char *group;
146 char *user;
147 gid_t gid = 0; 95 gid_t gid = 0;
96
97 /* check for min, max and missing args and exit on error */
98 bb_opt_complementally = "-1:?2:?";
148 99
149 /* get remaining args */ 100 if (bb_getopt_ulflags(argc, argv, "g:", &group)) {
150 if(bb_getopt_ulflags(argc, argv, "g:", &group)) {
151 gid = bb_xgetlarg(group, 10, 0, LONG_MAX); 101 gid = bb_xgetlarg(group, 10, 0, LONG_MAX);
152 } 102 }
103 /* move past the commandline options */
104 argv += optind;
153 105
154 if (optind < argc) { 106 /* need to be root */
155 group = argv[optind]; 107 if(geteuid()) {
156 optind++; 108 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
157 } else {
158 bb_show_usage();
159 } 109 }
160 110
161 if (optind < argc) {
162 user = argv[optind];
163 } else {
164 user = "";
165 }
166
167 if_i_am_not_root();
168
169 /* werk */ 111 /* werk */
170 return addgroup(bb_path_group_file, group, gid, user); 112 return addgroup(argv[0], gid, (argv[1]) ? argv[1] : "");
171} 113}