Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow uid/gid to be specified numerically #217

Closed
cgzones opened this issue Aug 22, 2018 · 4 comments · Fixed by #333
Closed

allow uid/gid to be specified numerically #217

cgzones opened this issue Aug 22, 2018 · 4 comments · Fixed by #333
Labels

Comments

@cgzones
Copy link
Member

cgzones commented Aug 22, 2018

From https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=415857:

Logrotate should allow the user to specify the create uid/gid
numerically.  This is useful with network filesystems which have their
own set of uids that don't map to a system username.

Attached patch:

diff -ur logrotate-3.7.1/config.c new/config.c
--- logrotate-3.7.1/config.c	2003-08-07 06:13:14.000000000 -0500
+++ new/config.c	2007-03-22 11:36:47.000000000 -0500
@@ -14,6 +14,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <assert.h>
+#include <limits.h>
 
 #include "basenames.h"
 #include "log.h"
@@ -544,24 +555,38 @@
 		    newlog->createMode = createMode;
 		
 		if (rc > 1) {
-		    pw = getpwnam(createOwner);
-		    if (!pw) {
-			message(MESS_ERROR, "%s:%d unknown user '%s'\n", 
-				configFile, lineNum, createOwner);
-			return 1;
-		    } 
-		    newlog->createUid = pw->pw_uid;
-		    endpwent();
+		    char* endptr;
+		    uid_t myuid;
+		    myuid = strtoul(createOwner, &endptr, 10);
+		    if (createOwner[0] != '\0' && *endptr == '\0' && myuid != ULONG_MAX)
+			newlog->createUid = myuid;
+		    else {
+			pw = getpwnam(createOwner);
+			if (!pw) {
+			    message(MESS_ERROR, "%s:%d unknown user '%s'\n", 
+				    configFile, lineNum, createOwner);
+			    return 1;
+			} 
+			newlog->createUid = pw->pw_uid;
+			endpwent();
+		    }
 		} 
 		if (rc > 2) {
-		    group = getgrnam(createGroup);
-		    if (!group) {
-			message(MESS_ERROR, "%s:%d unknown group '%s'\n", 
-				configFile, lineNum, createGroup);
-			return 1;
-		    } 
-		    newlog->createGid = group->gr_gid;
-		    endgrent();
+		    char* endptr;
+		    gid_t mygid;
+		    mygid = strtoul(createGroup, &endptr, 10);
+		    if (createGroup[0] != '\0' && *endptr == '\0' && mygid != ULONG_MAX)
+			newlog->createGid = mygid;
+		    else {
+			group = getgrnam(createGroup);
+			if (!group) {
+			    message(MESS_ERROR, "%s:%d unknown group '%s'\n", 
+				    configFile, lineNum, createGroup);
+			    return 1;
+			} 
+			newlog->createGid = group->gr_gid;
+			endgrent();
+		    }
 		} 
 
 		newlog->flags |= LOG_FLAG_CREATE;
@kdudka
Copy link
Member

kdudka commented Aug 22, 2018

What is the actual use case for this? Rotating log files over network file systems?

I am afraid this would introduce all the troubles with user names consisting of digits only:

https://www.gnu.org/software/coreutils/manual/html_node/Disambiguating-names-and-IDs.html

@cgzones
Copy link
Member Author

cgzones commented Aug 22, 2018

Ok, thanks.

So I guess it's a wontfix then.

@glensc
Copy link
Collaborator

glensc commented Aug 22, 2018

aside the network mounts, more typical usercase:

  • rotate log files of docker containers in docker host

for backward compatibility, should use pw_nam if such user exist, then use numeric pw_uid

i.e if getpwnam for numeric value fails, lookup numerically via getpwuid

cgzones added a commit to cgzones/logrotate that referenced this issue Jun 18, 2020
Allow uids and gids to be specified numerically, but only as a fallback
to retain backward compatibility.

Closes: logrotate#217
cgzones added a commit to cgzones/logrotate that referenced this issue Jun 18, 2020
Allow uids and gids to be specified numerically, but only as a fallback
to retain backward compatibility.

Drop usage of endpwent()/endgrent(), they are coupled with getpwent()/
getgrent() not getpwnam() and getgrnam().

Closes: logrotate#217
cgzones added a commit to cgzones/logrotate that referenced this issue Jun 19, 2020
Allow uids and gids to be specified numerically, but only as a fallback
to retain backward compatibility.

Drop usage of endpwent()/endgrent(), they are coupled with getpwent()/
getgrent() not getpwnam() and getgrnam().

Closes: logrotate#217
cgzones added a commit to cgzones/logrotate that referenced this issue Jul 1, 2020
Allow uids and gids to be specified numerically, but only as a fallback
to retain backward compatibility.

Drop usage of endpwent()/endgrent(), they are coupled with getpwent()/
getgrent() not getpwnam() and getgrnam().

Closes: logrotate#217
cgzones added a commit that referenced this issue Nov 2, 2020
Allow uids and gids to be specified numerically, but only as a fallback
to retain backward compatibility.

Drop usage of endpwent()/endgrent(), they are coupled with getpwent()/
getgrent() not getpwnam() and getgrnam().

Closes: #217
@saper
Copy link
Contributor

saper commented Nov 12, 2023

I tried to update the manual page to reflect the fact that mode, owner and group are all optional.

What does "create 1000 1000" mean?

This "wontfixed" problem uncovers parsing ambiguity introduced in b196609 and is demonstrated by #561

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants