--- rc_script.c 2003/05/16 12:37:10 1.16
+++ rc_script.c 2003/05/16 18:43:31 1.17
@@ -27,9 +27,9 @@
** rc_script.c: Run-command processor ISO C source file
*/
-#include <stdlib.h>
#include <string.h>
#include <ctype.h> /* For isspace(3) */
+#include <pwd.h> /* For getlogin(2) */
#include "rc.h" /* Public Rc interface */
#include "rc_pcre.h" /* For section parsing */
@@ -110,8 +110,12 @@
char *piBlocend = NULL; /* Misnomer used to control section looping */
char *piStart = NULL;
char *piEnd = NULL;
+ char *piSep = NULL;
+ char *szUser = NULL;
+ struct passwd *pPwd = NULL;
long nPri = 0;
long nUid = 0;
+ int nUserbytes = 0;
int nTmp = 0;
int nOffset = 0;
int nFound = 0;
@@ -190,19 +194,47 @@
if (piSubtemp) { /* Priority pattern found */
for (nTmp = (int)piSubtemp + strlen(RC_DEF_PRG);\
isspace(*(char *)nTmp); nTmp += sizeof (char)); /* Strip */
- nPri = strtol((char *)nTmp, (char **)NULL, 10);
- sectionSetpri(pSec, nPri);
+ nPri = strtol((char *)nTmp, &piSep, 10);
+ if ((char *)nTmp == piSep) /* No priority number follows */
+ sectionSetpri(pSec, RC_DEF_PRI); /* which is an error */
+ else
+ sectionSetpri(pSec, nPri); /* Found a priority value */
}
else /* Fallback to default value */
sectionSetpri(pSec, RC_DEF_PRI);
/* Handle the section userid */
piSubtemp = strnstr(piStart, RC_DEF_UIG, piEnd - piStart);
- if (piSubtemp) { /* Priority pattern found */
+ if (piSubtemp) { /* Userid pattern found */
for (nTmp = (int)piSubtemp + strlen(RC_DEF_UIG);\
isspace(*(char *)nTmp); nTmp += sizeof (char)); /* Strip */
- nUid = strtol((char *)nTmp, (char **)NULL, 10);
- sectionSetuid(pSec, nUid);
+ nUid = strtol((char *)nTmp, &piSep, 10);
+ if ((char *)nTmp == piSep) /* No userid number follows */
+ {
+ nUserbytes = (strcspn(piSep, " \t\n") + sizeof (char)) * sizeof (char);
+ szUser = malloc(nUserbytes);
+ if (!szUser)
+ RC_THROW(RC_ERR_MEM);
+ strncpy(szUser, (const char *)nTmp, nUserbytes);
+ strtok(szUser, " \t\n");
+ pPwd = getpwnam(szUser);
+ if (pPwd) {
+ sectionSetuid(pSec, pPwd->pw_uid); /* Set to given */
+ sectionSetlogin(pSec, szUser); /* uid and login */
+ }
+ else
+ sectionSetuid(pSec, RC_DEF_UID); /* Set to default */
+ free(szUser);
+ }
+ else {
+ pPwd = getpwuid(nUid);
+ if (pPwd) {
+ sectionSetuid(pSec, nUid); /* Found a value */
+ sectionSetlogin(pSec, pPwd->pw_name); /* uid and login */
+ }
+ else
+ sectionSetuid(pSec, RC_DEF_UID); /* Set to default */
+ }
}
else /* Fallback to default value */
sectionSetuid(pSec, RC_DEF_UID);
|