]>
cvs.zerfleddert.de Git - micropolis/blob - src/tclx/src/tclxid.c
b23d7bb8b571cbe577a86d5e49bee513dc1ab95b
4 * Tcl commands to access getuid, setuid, getgid, setgid and friends.
5 *---------------------------------------------------------------------------
6 * Copyright 1992 Karl Lehenbauer and Mark Diekhans.
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation for any purpose and without fee is hereby granted, provided
10 * that the above copyright notice appear in all copies. Karl Lehenbauer and
11 * Mark Diekhans make no representations about the suitability of this
12 * software for any purpose. It is provided "as is" without express or
14 *-----------------------------------------------------------------------------
15 * $Id: tclXid.c,v 2.0 1992/10/16 04:50:51 markd Rel $
16 *-----------------------------------------------------------------------------
22 * Prototypes of internal functions.
25 UseridToUsernameResult
_ANSI_ARGS_((Tcl_Interp
*interp
,
29 UsernameToUseridResult
_ANSI_ARGS_((Tcl_Interp
*interp
,
33 GroupidToGroupnameResult
_ANSI_ARGS_((Tcl_Interp
*interp
,
37 GroupnameToGroupidResult
_ANSI_ARGS_((Tcl_Interp
*interp
,
42 *-----------------------------------------------------------------------------
45 * Implements the TCL id command:
48 * id convert user <name>
51 * id convert userid <uid>
54 * id convert group <name>
57 * id convert groupid <gid>
62 * id process group set
68 * id effective groupid
71 * Standard TCL results, may return the UNIX system error message.
73 *-----------------------------------------------------------------------------
77 UseridToUsernameResult (interp
, userId
)
81 struct passwd
*pw
= getpwuid (userId
);
85 sprintf (numBuf
, "%d", userId
);
86 Tcl_AppendResult (interp
, "unknown user id: ", numBuf
, (char *) NULL
);
89 strcpy (interp
->result
, pw
->pw_name
);
94 UsernameToUseridResult (interp
, userName
)
98 struct passwd
*pw
= getpwnam (userName
);
100 Tcl_AppendResult (interp
, "unknown user id: ", userName
,
104 sprintf (interp
->result
, "%d", pw
->pw_uid
);
109 GroupidToGroupnameResult (interp
, groupId
)
113 struct group
*grp
= getgrgid (groupId
);
117 sprintf (numBuf
, "%d", groupId
);
118 Tcl_AppendResult (interp
, "unknown group id: ", numBuf
, (char *) NULL
);
121 strcpy (interp
->result
, grp
->gr_name
);
126 GroupnameToGroupidResult (interp
, groupName
)
130 struct group
*grp
= getgrnam (groupName
);
132 Tcl_AppendResult (interp
, "unknown group id: ", groupName
,
136 sprintf (interp
->result
, "%d", grp
->gr_gid
);
141 Tcl_IdCmd (clientData
, interp
, argc
, argv
)
142 ClientData clientData
;
155 * If the first argument is "convert", handle the conversion.
157 if (STREQU (argv
[1], "convert")) {
159 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
160 " convert arg arg", (char *) NULL
);
164 if (STREQU (argv
[2], "user"))
165 return UsernameToUseridResult (interp
, argv
[3]);
167 if (STREQU (argv
[2], "userid")) {
168 if (Tcl_GetInt (interp
, argv
[3], &uid
) != TCL_OK
)
170 return UseridToUsernameResult (interp
, uid
);
173 if (STREQU (argv
[2], "group"))
174 return GroupnameToGroupidResult (interp
, argv
[3]);
176 if (STREQU (argv
[2], "groupid")) {
177 if (Tcl_GetInt (interp
, argv
[3], &gid
) != TCL_OK
) return TCL_ERROR
;
178 return GroupidToGroupnameResult (interp
, gid
);
185 * If the first argument is "effective", return the effective user ID,
186 * name, group ID or name.
188 if (STREQU (argv
[1], "effective")) {
190 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
191 " effective arg", (char *) NULL
);
195 if (STREQU (argv
[2], "user"))
196 return UseridToUsernameResult (interp
, geteuid ());
198 if (STREQU (argv
[2], "userid")) {
199 sprintf (interp
->result
, "%d", geteuid ());
203 if (STREQU (argv
[2], "group"))
204 return GroupidToGroupnameResult (interp
, getegid ());
206 if (STREQU (argv
[2], "groupid")) {
207 sprintf (interp
->result
, "%d", getegid ());
214 * If the first argument is "process", return the process ID, parent's
215 * process ID, process group or set the process group depending on args.
217 if (STREQU (argv
[1], "process")) {
219 sprintf (interp
->result
, "%d", getpid ());
223 if (STREQU (argv
[2], "parent")) {
225 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
226 " process parent", (char *) NULL
);
229 sprintf (interp
->result
, "%d", getppid ());
232 if (STREQU (argv
[2], "group")) {
234 sprintf (interp
->result
, "%d", getpgrp ());
237 if ((argc
!= 4) || !STREQU (argv
[3], "set")) {
238 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
239 " process group [set]", (char *) NULL
);
245 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0],
246 " process [parent|group|group set]", (char *) NULL
);
251 * Handle setting or returning the user ID or group ID (by name or number).
256 if (STREQU (argv
[1], "user")) {
258 return UseridToUsernameResult (interp
, getuid ());
260 pw
= getpwnam (argv
[2]);
262 goto name_doesnt_exist
;
263 if (setuid (pw
->pw_uid
) < 0)
264 goto cannot_set_name
;
269 if (STREQU (argv
[1], "userid")) {
271 sprintf (interp
->result
, "%d", getuid ());
274 if (Tcl_GetInt (interp
, argv
[2], &uid
) != TCL_OK
)
276 if (setuid (uid
) < 0)
277 goto cannot_set_name
;
282 if (STREQU (argv
[1], "group")) {
284 return GroupidToGroupnameResult (interp
, getgid ());
286 grp
= getgrnam (argv
[2]);
288 goto name_doesnt_exist
;
289 if (setgid (grp
->gr_gid
) < 0)
290 goto cannot_set_name
;
295 if (STREQU (argv
[1], "groupid")) {
297 sprintf (interp
->result
, "%d", getgid ());
300 if (Tcl_GetInt (interp
, argv
[2], &gid
) != TCL_OK
)
302 if (setgid (gid
) < 0)
303 goto cannot_set_name
;
307 Tcl_AppendResult (interp
, "bad arg: ", argv
[0],
308 " second arg must be convert, effective, process, ",
309 "user, userid, group or groupid", (char *) NULL
);
314 Tcl_AppendResult (interp
, "bad arg: ", argv
[0], ": ", argv
[1],
315 ": third arg must be user, userid, group or groupid",
319 Tcl_AppendResult (interp
, tclXWrongArgs
, argv
[0], " arg [arg..]",
324 Tcl_AppendResult (interp
, " \"", argv
[2], "\" does not exists",
329 interp
->result
= Tcl_UnixError (interp
);