154 static PyObject *py_sss_useradd(PySssLocalObject *self,
155 PyObject *args,
156 PyObject *kwds)
157 {
158 struct tools_ctx *tctx = NULL;
159 unsigned long uid = 0;
160 unsigned long gid = 0;
161 const char *gecos = NULL;
162 const char *home = NULL;
163 const char *shell = NULL;
164 const char *skel = NULL;
165 char *username = NULL;
166 int ret;
167 const char * const kwlist[] = { "username", "uid", "gid", "gecos",
168 "homedir", "shell", "skel",
169 "create_home", "groups", NULL };
170 PyObject *py_groups = Py_None;
171 PyObject *py_create_home = Py_None;
172 int create_home = 0;
173
174 /* parse arguments */
175 if (!PyArg_ParseTupleAndKeywords(args, kwds,
176 discard_const_p(char, "s|kkssssO!O!"),
when PyArg_ParseTupleAndKeywords() succeeds
taking False path
177 discard_const_p(char *, kwlist),
178 &username,
179 &uid,
180 &gid,
181 &gecos,
182 &home,
183 &shell,
184 &skel,
185 &PyBool_Type,
186 &py_create_home,
187 &PyList_Type,
188 &py_groups)) {
189 goto fail;
190 }
191
192 tctx = init_ctx(self->mem_ctx, self);
193 if (!tctx) {
194 PyErr_NoMemory();
when treating unknown struct tools_ctx * from src/python/pysss.c:193 as non-NULL
taking False path
195 return NULL;
196 }
197
198 if (py_groups != Py_None) {
199 tctx->octx->addgroups = PyList_AsStringList(tctx, py_groups, "groups");
taking True path
200 if (!tctx->octx->addgroups) {
when treating unknown struct ops_ctx * from src/python/pysss.c:200 as non-NULL
201 PyErr_NoMemory();
when treating unknown struct ops_ctx * from src/python/pysss.c:201 as non-NULL
when treating unknown char * * from src/python/pysss.c:201 as non-NULL
taking False path
202 return NULL;
203 }
204 }
205
206 /* user-wise the parameter is only bool - do or don't,
207 * however we must have a third state - undecided, pick default */
208 if (py_create_home == Py_True) {
209 create_home = DO_CREATE_HOME;
taking False path
210 } else if (py_create_home == Py_False) {
211 create_home = DO_NOT_CREATE_HOME;
taking False path
212 }
213
214 tctx->octx->name = username;
215 tctx->octx->uid = uid;
when treating unknown struct ops_ctx * from src/python/pysss.c:215 as non-NULL
216
when treating unknown struct ops_ctx * from src/python/pysss.c:216 as non-NULL
217 /* fill in defaults */
218 ret = useradd_defaults(tctx,
219 self->confdb,
220 tctx->octx, gecos,
221 home, shell,
222 create_home,
223 skel);
224 if (ret != EOK) {
225 PyErr_SetSssError(ret);
when considering value == (int)0 from src/python/pysss.c:219
taking False path
226 goto fail;
227 }
228
229 /* Add the user within a transaction */
230 tctx->error = sysdb_transaction_start(tctx->sysdb);
231 if (tctx->error != EOK) {
232 PyErr_SetSssError(tctx->error);
when considering value == (int)0 from src/python/pysss.c:231
taking False path
233 goto fail;
234 }
235
236 /* useradd */
237 tctx->error = useradd(tctx, tctx->sysdb, tctx->octx);
238 if (tctx->error) {
239 /* cancel transaction */
when considering value == (int)0 from src/python/pysss.c:238
taking False path
240 sysdb_transaction_cancel(tctx->sysdb);
241 PyErr_SetSssError(tctx->error);
242 goto fail;
243 }
244
245 tctx->error = sysdb_transaction_commit(tctx->sysdb);
246 if (tctx->error) {
247 PyErr_SetSssError(tctx->error);
when considering value == (int)0 from src/python/pysss.c:246
taking False path
248 goto fail;
249 }
250
251 /* Create user's home directory and/or mail spool */
252 if (tctx->octx->create_homedir) {
253 /* We need to know the UID and GID of the user, if
when treating unknown struct ops_ctx * from src/python/pysss.c:253 as non-NULL
when taking True path
254 * sysdb did assign it automatically, do a lookup */
255 if (tctx->octx->uid == 0 || tctx->octx->gid == 0) {
256 ret = sysdb_getpwnam_sync(tctx,
when treating unknown struct ops_ctx * from src/python/pysss.c:256 as non-NULL
when considering value == (uid_t)0 from src/python/pysss.c:256
taking True path
257 tctx->sysdb,
258 tctx->octx->name,
259 tctx->local,
when treating unknown struct ops_ctx * from src/python/pysss.c:259 as non-NULL
260 tctx->octx);
261 if (ret != EOK) {
262 PyErr_SetSssError(ret);
when considering range: -0x80000000 <= value <= -1
taking True path
263 goto fail;
264 }
265 }
266
267 ret = create_homedir(tctx,
268 tctx->octx->skeldir,
269 tctx->octx->home,
270 tctx->octx->name,
271 tctx->octx->uid,
272 tctx->octx->gid,
273 tctx->octx->umask);
274 if (ret != EOK) {
275 PyErr_SetSssError(ret);
276 goto fail;
277 }
278
279 /* failure here should not be fatal */
280 create_mail_spool(tctx,
281 tctx->octx->name,
282 tctx->octx->maildir,
283 tctx->octx->uid,
284 tctx->octx->gid);
285 }
286
287 talloc_zfree(tctx);
288 Py_RETURN_NONE;
289
290 fail:
291 talloc_zfree(tctx);
292 return NULL;
293 }
294