]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/auth.c
2 * "$Id: auth.c 5567 2006-05-22 15:33:11Z mike $"
4 * Authorization routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products, all rights reserved.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
26 * cupsdAddLocation() - Add a location for authorization.
27 * cupsdAddName() - Add a name to a location...
28 * cupsdAllowHost() - Add a host name that is allowed to access the
30 * cupsdAllowIP() - Add an IP address or network that is allowed
31 * to access the location.
32 * cupsdAuthorize() - Validate any authorization credentials.
33 * cupsdCheckAuth() - Check authorization masks.
34 * cupsdCheckGroup() - Check for a user's group membership.
35 * cupsdCopyLocation() - Make a copy of a location...
36 * cupsdDeleteAllLocations() - Free all memory used for location
38 * cupsdDeleteLocation() - Free all memory used by a location.
39 * cupsdDenyHost() - Add a host name that is not allowed to access
41 * cupsdDenyIP() - Add an IP address or network that is not
42 * allowed to access the location.
43 * cupsdFindBest() - Find the location entry that best matches the
45 * cupsdFindLocation() - Find the named location.
46 * cupsdIsAuthorized() - Check to see if the user is authorized...
47 * add_allow() - Add an allow mask to the location.
48 * add_deny() - Add a deny mask to the location.
49 * compare_locations() - Compare two locations.
50 * cups_crypt() - Encrypt the password using the DES or MD5
51 * algorithms, as needed.
52 * get_md5_password() - Get an MD5 password.
53 * pam_func() - PAM conversation function.
54 * to64() - Base64-encode an integer value...
58 * Include necessary headers...
65 #endif /* HAVE_SHADOW_H */
68 #endif /* HAVE_CRYPT_H */
70 # ifdef HAVE_PAM_PAM_APPL_H
71 # include <pam/pam_appl.h>
73 # include <security/pam_appl.h>
74 # endif /* HAVE_PAM_PAM_APPL_H */
75 #endif /* HAVE_LIBPAM */
78 #endif /* HAVE_USERSEC_H */
79 #ifdef HAVE_MEMBERSHIP_H
80 # include <membership.h>
81 #endif /* HAVE_MEMBERSHIP_H */
88 static cupsd_authmask_t
*add_allow(cupsd_location_t
*loc
);
89 static cupsd_authmask_t
*add_deny(cupsd_location_t
*loc
);
90 static int compare_locations(cupsd_location_t
*a
,
93 static char *cups_crypt(const char *pw
, const char *salt
);
94 #endif /* !HAVE_LIBPAM */
95 static char *get_md5_password(const char *username
,
96 const char *group
, char passwd
[33]);
98 static int pam_func(int, const struct pam_message
**,
99 struct pam_response
**, void *);
101 static void to64(char *s
, unsigned long v
, int n
);
102 #endif /* HAVE_LIBPAM */
106 * Local structures...
110 typedef struct cupsd_authdata_s
/**** Authentication data ****/
112 char username
[33], /* Username string */
113 password
[33]; /* Password string */
115 #endif /* HAVE_LIBPAM */
122 #if defined(__hpux) && HAVE_LIBPAM
123 static cupsd_authdata_t
*auth_data
; /* Current client being authenticated */
124 #endif /* __hpux && HAVE_LIBPAM */
128 * 'cupsdAddLocation()' - Add a location for authorization.
131 cupsd_location_t
* /* O - Pointer to new location record */
132 cupsdAddLocation(const char *location
) /* I - Location path */
134 cupsd_location_t
*temp
; /* New location */
138 * Make sure the locations array is created...
142 Locations
= cupsArrayNew((cups_array_func_t
)compare_locations
, NULL
);
148 * Try to allocate memory for the new location.
151 if ((temp
= calloc(1, sizeof(cupsd_location_t
))) == NULL
)
155 * Initialize the record and copy the name over...
158 temp
->location
= strdup(location
);
159 temp
->length
= strlen(temp
->location
);
161 cupsArrayAdd(Locations
, temp
);
163 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddLocation: added location \'%s\'",
167 * Return the new record...
175 * 'cupsdAddName()' - Add a name to a location...
179 cupsdAddName(cupsd_location_t
*loc
, /* I - Location to add to */
180 char *name
) /* I - Name to add */
182 char **temp
; /* Pointer to names array */
185 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddName(loc=%p, name=\"%s\")",
188 if (loc
->num_names
== 0)
189 temp
= malloc(sizeof(char *));
191 temp
= realloc(loc
->names
, (loc
->num_names
+ 1) * sizeof(char *));
195 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to add name to location %s: %s",
196 loc
->location
? loc
->location
: "nil", strerror(errno
));
202 if ((temp
[loc
->num_names
] = strdup(name
)) == NULL
)
204 cupsdLogMessage(CUPSD_LOG_ERROR
,
205 "Unable to duplicate name for location %s: %s",
206 loc
->location
? loc
->location
: "nil", strerror(errno
));
215 * 'cupsdAllowHost()' - Add a host name that is allowed to access the location.
219 cupsdAllowHost(cupsd_location_t
*loc
, /* I - Location to add to */
220 char *name
) /* I - Name of host or domain to add */
222 cupsd_authmask_t
*temp
; /* New host/domain mask */
223 char ifname
[32], /* Interface name */
224 *ifptr
; /* Pointer to end of name */
227 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAllowHost(loc=%p(%s), name=\"%s\")",
228 loc
, loc
->location
? loc
->location
: "nil", name
);
230 if ((temp
= add_allow(loc
)) == NULL
)
233 if (!strcasecmp(name
, "@LOCAL"))
236 * Allow *interface*...
239 temp
->type
= AUTH_INTERFACE
;
240 temp
->mask
.name
.name
= strdup("*");
241 temp
->mask
.name
.length
= 1;
243 else if (!strncasecmp(name
, "@IF(", 4))
246 * Allow *interface*...
249 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
251 ifptr
= ifname
+ strlen(ifname
);
253 if (ifptr
[-1] == ')')
259 temp
->type
= AUTH_INTERFACE
;
260 temp
->mask
.name
.name
= strdup(ifname
);
261 temp
->mask
.name
.length
= ifptr
- ifname
;
269 temp
->type
= AUTH_NAME
;
270 temp
->mask
.name
.name
= strdup(name
);
271 temp
->mask
.name
.length
= strlen(name
);
277 * 'cupsdAllowIP()' - Add an IP address or network that is allowed to access
282 cupsdAllowIP(cupsd_location_t
*loc
, /* I - Location to add to */
283 unsigned address
[4], /* I - IP address to add */
284 unsigned netmask
[4]) /* I - Netmask of address */
286 cupsd_authmask_t
*temp
; /* New host/domain mask */
289 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
290 "cupsdAllowIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
291 loc
, loc
->location
? loc
->location
: "nil",
292 address
[0], address
[1], address
[2], address
[3],
293 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
295 if ((temp
= add_allow(loc
)) == NULL
)
298 temp
->type
= AUTH_IP
;
299 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
300 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
305 * 'cupsdAuthorize()' - Validate any authorization credentials.
309 cupsdAuthorize(cupsd_client_t
*con
) /* I - Client connection */
311 int type
; /* Authentication type */
312 char *authorization
, /* Pointer into Authorization string */
313 *ptr
, /* Pointer into string */
314 username
[65], /* Username string */
315 password
[33]; /* Password string */
316 const char *localuser
; /* Certificate username */
317 char nonce
[HTTP_MAX_VALUE
], /* Nonce value from client */
318 md5
[33], /* MD5 password */
319 basicmd5
[33]; /* MD5 of Basic password */
320 static const char * const states
[] = /* HTTP client states... */
340 * Locate the best matching location so we know what kind of
341 * authentication to expect...
344 con
->best
= cupsdFindBest(con
->uri
, con
->http
.state
);
346 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
347 "cupsdAuthorize: con->uri=\"%s\", con->best=%p(%s)",
348 con
->uri
, con
->best
, con
->best
? con
->best
->location
: "");
350 if (con
->best
&& con
->best
->type
!= AUTH_NONE
)
351 type
= con
->best
->type
;
353 type
= DefaultAuthType
;
356 * Decode the Authorization string...
359 authorization
= con
->http
.fields
[HTTP_FIELD_AUTHORIZATION
];
361 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAuthorize: Authorization=\"%s\"",
367 if (type
== AUTH_NONE
)
370 * No authorization required, return early...
373 cupsdLogMessage(CUPSD_LOG_DEBUG
,
374 "cupsdAuthorize: No authentication required.");
377 else if (!*authorization
)
380 * No authorization data provided, return early...
383 cupsdLogMessage(CUPSD_LOG_DEBUG
,
384 "cupsdAuthorize: No authentication data provided.");
387 else if (!strncmp(authorization
, "Local", 5) &&
388 !strcasecmp(con
->http
.hostname
, "localhost"))
391 * Get Local certificate authentication data...
395 while (isspace(*authorization
& 255))
398 if ((localuser
= cupsdFindCert(authorization
)) != NULL
)
399 strlcpy(username
, localuser
, sizeof(username
));
402 cupsdLogMessage(CUPSD_LOG_ERROR
,
403 "cupsdAuthorize: Local authentication certificate not "
408 else if (!strncmp(authorization
, "Basic", 5) &&
409 (type
== AUTH_BASIC
|| type
== AUTH_BASICDIGEST
))
412 * Get the Basic authentication data...
415 int userlen
; /* Username:password length */
419 while (isspace(*authorization
& 255))
422 userlen
= sizeof(username
);
423 httpDecode64_2(username
, &userlen
, authorization
);
426 * Pull the username and password out...
429 if ((ptr
= strchr(username
, ':')) == NULL
)
431 cupsdLogMessage(CUPSD_LOG_ERROR
,
432 "cupsdAuthorize: Missing Basic password!");
441 * Username must not be empty...
444 cupsdLogMessage(CUPSD_LOG_ERROR
,
445 "cupsdAuthorize: Empty Basic username!");
452 * Password must not be empty...
455 cupsdLogMessage(CUPSD_LOG_ERROR
,
456 "cupsdAuthorize: Empty Basic password!");
460 strlcpy(password
, ptr
, sizeof(password
));
463 * Validate the username and password...
472 * Only use PAM to do authentication. This supports MD5
473 * passwords, among other things...
476 pam_handle_t
*pamh
; /* PAM authentication handle */
477 int pamerr
; /* PAM error code */
478 struct pam_conv pamdata
;/* PAM conversation data */
479 cupsd_authdata_t data
; /* Authentication data */
482 strlcpy(data
.username
, username
, sizeof(data
.username
));
483 strlcpy(data
.password
, password
, sizeof(data
.password
));
486 pamdata
.conv
= (int (*)(int, struct pam_message
**,
487 struct pam_response
**,
490 pamdata
.conv
= pam_func
;
492 pamdata
.appdata_ptr
= &data
;
496 * Workaround for HP-UX bug in pam_unix; see pam_func() below for
503 pamerr
= pam_start("cups", username
, &pamdata
, &pamh
);
504 if (pamerr
!= PAM_SUCCESS
)
506 cupsdLogMessage(CUPSD_LOG_ERROR
,
507 "cupsdAuthorize: pam_start() returned %d (%s)!\n",
508 pamerr
, pam_strerror(pamh
, pamerr
));
513 pamerr
= pam_authenticate(pamh
, PAM_SILENT
);
514 if (pamerr
!= PAM_SUCCESS
)
516 cupsdLogMessage(CUPSD_LOG_ERROR
,
517 "cupsdAuthorize: pam_authenticate() returned %d "
519 pamerr
, pam_strerror(pamh
, pamerr
));
524 pamerr
= pam_acct_mgmt(pamh
, PAM_SILENT
);
525 if (pamerr
!= PAM_SUCCESS
)
527 cupsdLogMessage(CUPSD_LOG_ERROR
,
528 "cupsdAuthorize: pam_acct_mgmt() returned %d "
530 pamerr
, pam_strerror(pamh
, pamerr
));
535 pam_end(pamh
, PAM_SUCCESS
);
537 #elif defined(HAVE_USERSEC_H)
539 * Use AIX authentication interface...
542 char *authmsg
; /* Authentication message */
543 char *loginmsg
; /* Login message */
544 int reenter
; /* ??? */
547 cupsdLogMessage(CUPSD_LOG_DEBUG
,
548 "cupsdAuthorize: AIX authenticate of username \"%s\"",
552 if (authenticate(username
, password
, &reenter
, &authmsg
) != 0)
554 cupsdLogMessage(CUPSD_LOG_DEBUG
,
555 "cupsdAuthorize: Unable to authenticate username "
557 username
, strerror(errno
));
563 * Use normal UNIX password file-based authentication...
566 char *pass
; /* Encrypted password */
567 struct passwd
*pw
; /* User password data */
568 # ifdef HAVE_SHADOW_H
569 struct spwd
*spw
; /* Shadow password data */
570 # endif /* HAVE_SHADOW_H */
573 pw
= getpwnam(username
); /* Get the current password */
574 endpwent(); /* Close the password file */
582 cupsdLogMessage(CUPSD_LOG_ERROR
,
583 "cupsdAuthorize: Unknown username \"%s\"!",
588 # ifdef HAVE_SHADOW_H
589 spw
= getspnam(username
);
592 if (!spw
&& !strcmp(pw
->pw_passwd
, "x"))
595 * Don't allow blank passwords!
598 cupsdLogMessage(CUPSD_LOG_ERROR
,
599 "cupsdAuthorize: Username \"%s\" has no shadow "
600 "password!", username
);
604 if (spw
&& !spw
->sp_pwdp
[0] && !pw
->pw_passwd
[0])
606 if (!pw
->pw_passwd
[0])
607 # endif /* HAVE_SHADOW_H */
610 * Don't allow blank passwords!
613 cupsdLogMessage(CUPSD_LOG_ERROR
,
614 "cupsdAuthorize: Username \"%s\" has no password!",
620 * OK, the password isn't blank, so compare with what came from the
624 pass
= cups_crypt(password
, pw
->pw_passwd
);
626 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
627 "cupsdAuthorize: pw_passwd=\"%s\", crypt=\"%s\"",
628 pw
->pw_passwd
, pass
);
630 if (!pass
|| strcmp(pw
->pw_passwd
, pass
))
632 # ifdef HAVE_SHADOW_H
635 pass
= cups_crypt(password
, spw
->sp_pwdp
);
637 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
638 "cupsdAuthorize: sp_pwdp=\"%s\", crypt=\"%s\"",
641 if (pass
== NULL
|| strcmp(spw
->sp_pwdp
, pass
))
643 cupsdLogMessage(CUPSD_LOG_ERROR
,
644 "cupsdAuthorize: Authentication failed for "
651 # endif /* HAVE_SHADOW_H */
653 cupsdLogMessage(CUPSD_LOG_ERROR
,
654 "cupsdAuthorize: Authentication failed for "
660 #endif /* HAVE_LIBPAM */
664 case AUTH_BASICDIGEST
:
666 * Do Basic authentication with the Digest password file...
669 if (!get_md5_password(username
, NULL
, md5
))
671 cupsdLogMessage(CUPSD_LOG_ERROR
,
672 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
677 httpMD5(username
, "CUPS", password
, basicmd5
);
679 if (strcmp(md5
, basicmd5
))
681 cupsdLogMessage(CUPSD_LOG_ERROR
,
682 "cupsdAuthorize: Authentication failed for \"%s\"!",
689 else if (!strncmp(authorization
, "Digest", 6) && type
== AUTH_DIGEST
)
692 * Get the username, password, and nonce from the Digest attributes...
695 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "username",
696 username
, sizeof(username
)) || !username
[0])
699 * Username must not be empty...
702 cupsdLogMessage(CUPSD_LOG_ERROR
,
703 "cupsdAuthorize: Empty or missing Digest username!");
707 if (!httpGetSubField2(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "response",
708 password
, sizeof(password
)) || !password
[0])
711 * Password must not be empty...
714 cupsdLogMessage(CUPSD_LOG_ERROR
,
715 "cupsdAuthorize: Empty or missing Digest password!");
719 if (!httpGetSubField(&(con
->http
), HTTP_FIELD_AUTHORIZATION
, "nonce",
722 cupsdLogMessage(CUPSD_LOG_ERROR
,
723 "cupsdAuthorize: No nonce value for Digest "
728 if (strcmp(con
->http
.hostname
, nonce
))
730 cupsdLogMessage(CUPSD_LOG_ERROR
,
731 "cupsdAuthorize: Bad nonce value, expected \"%s\", "
732 "got \"%s\"!", con
->http
.hostname
, nonce
);
737 * Validate the username and password...
740 if (!get_md5_password(username
, NULL
, md5
))
742 cupsdLogMessage(CUPSD_LOG_ERROR
,
743 "cupsdAuthorize: Unknown MD5 username \"%s\"!",
748 httpMD5Final(nonce
, states
[con
->http
.state
], con
->uri
, md5
);
750 if (strcmp(md5
, password
))
752 cupsdLogMessage(CUPSD_LOG_ERROR
,
753 "cupsdAuthorize: Authentication failed for \"%s\"!",
760 cupsdLogMessage(CUPSD_LOG_DEBUG
,
761 "cupsdAuthorize: Bad authentication data.");
766 * If we get here, then we were able to validate the username and
767 * password - copy the validated username and password to the client
771 strlcpy(con
->username
, username
, sizeof(con
->username
));
772 strlcpy(con
->password
, password
, sizeof(con
->password
));
774 cupsdLogMessage(CUPSD_LOG_DEBUG
, "cupsdAuthorize: username=\"%s\"",
780 * 'cupsdCheckAuth()' - Check authorization masks.
783 int /* O - 1 if mask matches, 0 otherwise */
785 unsigned ip
[4], /* I - Client address */
786 char *name
, /* I - Client hostname */
787 int name_len
, /* I - Length of hostname */
788 int num_masks
, /* I - Number of masks */
789 cupsd_authmask_t
*masks
) /* I - Masks */
791 int i
; /* Looping var */
792 cupsd_netif_t
*iface
; /* Network interface */
793 unsigned netip4
; /* IPv4 network address */
795 unsigned netip6
[4]; /* IPv6 network address */
796 #endif /* AF_INET6 */
798 while (num_masks
> 0)
802 case AUTH_INTERFACE
:
804 * Check for a match with a network interface...
807 netip4
= htonl(ip
[3]);
810 netip6
[0] = htonl(ip
[0]);
811 netip6
[1] = htonl(ip
[1]);
812 netip6
[2] = htonl(ip
[2]);
813 netip6
[3] = htonl(ip
[3]);
814 #endif /* AF_INET6 */
816 if (!strcmp(masks
->mask
.name
.name
, "*"))
819 * Check against all local interfaces...
824 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
826 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
829 * Only check local interfaces...
832 if (!iface
->is_local
)
835 if (iface
->address
.addr
.sa_family
== AF_INET
)
838 * Check IPv4 address...
841 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
842 (iface
->address
.ipv4
.sin_addr
.s_addr
&
843 iface
->mask
.ipv4
.sin_addr
.s_addr
))
850 * Check IPv6 address...
853 for (i
= 0; i
< 4; i
++)
854 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
855 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
856 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
862 #endif /* AF_INET6 */
868 * Check the named interface...
871 for (iface
= (cupsd_netif_t
*)cupsArrayFirst(NetIFList
);
872 iface
&& !strcmp(masks
->mask
.name
.name
, iface
->name
);
873 iface
= (cupsd_netif_t
*)cupsArrayNext(NetIFList
))
875 if (iface
->address
.addr
.sa_family
== AF_INET
)
878 * Check IPv4 address...
881 if ((netip4
& iface
->mask
.ipv4
.sin_addr
.s_addr
) ==
882 (iface
->address
.ipv4
.sin_addr
.s_addr
&
883 iface
->mask
.ipv4
.sin_addr
.s_addr
))
890 * Check IPv6 address...
893 for (i
= 0; i
< 4; i
++)
894 if ((netip6
[i
] & iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]) !=
895 (iface
->address
.ipv6
.sin6_addr
.s6_addr32
[i
] &
896 iface
->mask
.ipv6
.sin6_addr
.s6_addr32
[i
]))
902 #endif /* AF_INET6 */
909 * Check for exact name match...
912 if (!strcasecmp(name
, masks
->mask
.name
.name
))
916 * Check for domain match...
919 if (name_len
>= masks
->mask
.name
.length
&&
920 masks
->mask
.name
.name
[0] == '.' &&
921 !strcasecmp(name
+ name_len
- masks
->mask
.name
.length
,
922 masks
->mask
.name
.name
))
928 * Check for IP/network address match...
931 for (i
= 0; i
< 4; i
++)
932 if ((ip
[i
] & masks
->mask
.ip
.netmask
[i
]) !=
933 masks
->mask
.ip
.address
[i
])
950 * 'cupsdCheckGroup()' - Check for a user's group membership.
953 int /* O - 1 if user is a member, 0 otherwise */
955 const char *username
, /* I - User name */
956 struct passwd
*user
, /* I - System user info */
957 const char *groupname
) /* I - Group name */
959 int i
; /* Looping var */
960 struct group
*group
; /* System group info */
961 char junk
[33]; /* MD5 password (not used) */
962 #ifdef HAVE_MBR_UID_TO_UUID
963 uuid_t useruuid
, /* UUID for username */
964 groupuuid
; /* UUID for groupname */
965 int is_member
; /* True if user is a member of group */
966 #endif /* HAVE_MBR_UID_TO_UUID */
969 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
970 "cupsdCheckGroup(username=\"%s\", user=%p, groupname=\"%s\")",
971 username
, user
, groupname
);
977 if (!username
|| !groupname
)
981 * Check to see if the user is a member of the named group...
984 group
= getgrnam(groupname
);
990 * Group exists, check it...
993 for (i
= 0; group
->gr_mem
[i
]; i
++)
994 if (!strcasecmp(username
, group
->gr_mem
[i
]))
999 * Group doesn't exist or user not in group list, check the group ID
1000 * against the user's group ID...
1003 if (user
&& group
&& group
->gr_gid
== user
->pw_gid
)
1006 #ifdef HAVE_MBR_UID_TO_UUID
1008 * Check group membership through MacOS X membership API...
1012 if (!mbr_uid_to_uuid(user
->pw_uid
, useruuid
))
1013 if (!mbr_gid_to_uuid(group
->gr_gid
, groupuuid
))
1014 if (!mbr_check_membership(useruuid
, groupuuid
, &is_member
))
1017 #endif /* HAVE_MBR_UID_TO_UUID */
1020 * Username not found, group not found, or user is not part of the
1021 * system group... Check for a user and group in the MD5 password
1025 if (get_md5_password(username
, groupname
, junk
) != NULL
)
1029 * If we get this far, then the user isn't part of the named group...
1037 * 'cupsdCopyLocation()' - Make a copy of a location...
1040 cupsd_location_t
* /* O - New location */
1042 cupsd_location_t
**loc
) /* IO - Original location */
1044 int i
; /* Looping var */
1045 cupsd_location_t
*temp
; /* New location */
1046 char location
[HTTP_MAX_URI
];
1047 /* Location of resource */
1051 * Use a local copy of location because cupsdAddLocation may cause
1052 * this memory to be moved...
1055 strlcpy(location
, (*loc
)->location
, sizeof(location
));
1057 if ((temp
= cupsdAddLocation(location
)) == NULL
)
1061 * Copy the information from the original location to the new one.
1064 temp
->limit
= (*loc
)->limit
;
1065 temp
->order_type
= (*loc
)->order_type
;
1066 temp
->type
= (*loc
)->type
;
1067 temp
->level
= (*loc
)->level
;
1068 temp
->satisfy
= (*loc
)->satisfy
;
1069 temp
->encryption
= (*loc
)->encryption
;
1071 if ((temp
->num_names
= (*loc
)->num_names
) > 0)
1074 * Copy the names array...
1077 if ((temp
->names
= calloc(temp
->num_names
, sizeof(char *))) == NULL
)
1079 cupsdLogMessage(CUPSD_LOG_ERROR
,
1080 "cupsdCopyLocation: Unable to allocate memory for %d names: %s",
1081 temp
->num_names
, strerror(errno
));
1083 cupsdDeleteLocation(temp
);
1087 for (i
= 0; i
< temp
->num_names
; i
++)
1088 if ((temp
->names
[i
] = strdup((*loc
)->names
[i
])) == NULL
)
1090 cupsdLogMessage(CUPSD_LOG_ERROR
,
1091 "cupsdCopyLocation: Unable to copy name \"%s\": %s",
1092 (*loc
)->names
[i
], strerror(errno
));
1094 cupsdDeleteLocation(temp
);
1099 if ((temp
->num_allow
= (*loc
)->num_allow
) > 0)
1102 * Copy allow rules...
1105 if ((temp
->allow
= calloc(temp
->num_allow
, sizeof(cupsd_authmask_t
))) == NULL
)
1107 cupsdLogMessage(CUPSD_LOG_ERROR
,
1108 "cupsdCopyLocation: Unable to allocate memory for %d allow rules: %s",
1109 temp
->num_allow
, strerror(errno
));
1110 cupsdDeleteLocation(temp
);
1114 for (i
= 0; i
< temp
->num_allow
; i
++)
1115 switch (temp
->allow
[i
].type
= (*loc
)->allow
[i
].type
)
1118 temp
->allow
[i
].mask
.name
.length
= (*loc
)->allow
[i
].mask
.name
.length
;
1119 temp
->allow
[i
].mask
.name
.name
= strdup((*loc
)->allow
[i
].mask
.name
.name
);
1121 if (temp
->allow
[i
].mask
.name
.name
== NULL
)
1123 cupsdLogMessage(CUPSD_LOG_ERROR
,
1124 "cupsdCopyLocation: Unable to copy allow name \"%s\": %s",
1125 (*loc
)->allow
[i
].mask
.name
.name
, strerror(errno
));
1126 cupsdDeleteLocation(temp
);
1131 memcpy(&(temp
->allow
[i
].mask
.ip
), &((*loc
)->allow
[i
].mask
.ip
),
1132 sizeof(cupsd_ipmask_t
));
1137 if ((temp
->num_deny
= (*loc
)->num_deny
) > 0)
1140 * Copy deny rules...
1143 if ((temp
->deny
= calloc(temp
->num_deny
, sizeof(cupsd_authmask_t
))) == NULL
)
1145 cupsdLogMessage(CUPSD_LOG_ERROR
,
1146 "cupsdCopyLocation: Unable to allocate memory for %d deny rules: %s",
1147 temp
->num_deny
, strerror(errno
));
1148 cupsdDeleteLocation(temp
);
1152 for (i
= 0; i
< temp
->num_deny
; i
++)
1153 switch (temp
->deny
[i
].type
= (*loc
)->deny
[i
].type
)
1156 temp
->deny
[i
].mask
.name
.length
= (*loc
)->deny
[i
].mask
.name
.length
;
1157 temp
->deny
[i
].mask
.name
.name
= strdup((*loc
)->deny
[i
].mask
.name
.name
);
1159 if (temp
->deny
[i
].mask
.name
.name
== NULL
)
1161 cupsdLogMessage(CUPSD_LOG_ERROR
,
1162 "cupsdCopyLocation: Unable to copy deny name \"%s\": %s",
1163 (*loc
)->deny
[i
].mask
.name
.name
, strerror(errno
));
1164 cupsdDeleteLocation(temp
);
1169 memcpy(&(temp
->deny
[i
].mask
.ip
), &((*loc
)->deny
[i
].mask
.ip
),
1170 sizeof(cupsd_ipmask_t
));
1180 * 'cupsdDeleteAllLocations()' - Free all memory used for location authorization.
1184 cupsdDeleteAllLocations(void)
1186 cupsd_location_t
*loc
; /* Current location */
1190 * Free all of the allow/deny records first...
1193 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1195 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1196 cupsdDeleteLocation(loc
);
1199 * Then free the location array...
1202 cupsArrayDelete(Locations
);
1208 * 'cupsdDeleteLocation()' - Free all memory used by a location.
1212 cupsdDeleteLocation(
1213 cupsd_location_t
*loc
) /* I - Location to delete */
1215 int i
; /* Looping var */
1216 cupsd_authmask_t
*mask
; /* Current mask */
1219 cupsArrayRemove(Locations
, loc
);
1221 for (i
= loc
->num_names
- 1; i
>= 0; i
--)
1222 free(loc
->names
[i
]);
1224 if (loc
->num_names
> 0)
1227 for (i
= loc
->num_allow
, mask
= loc
->allow
; i
> 0; i
--, mask
++)
1228 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1229 free(mask
->mask
.name
.name
);
1231 if (loc
->num_allow
> 0)
1234 for (i
= loc
->num_deny
, mask
= loc
->deny
; i
> 0; i
--, mask
++)
1235 if (mask
->type
== AUTH_NAME
|| mask
->type
== AUTH_INTERFACE
)
1236 free(mask
->mask
.name
.name
);
1238 if (loc
->num_deny
> 0)
1241 free(loc
->location
);
1247 * 'cupsdDenyHost()' - Add a host name that is not allowed to access the
1252 cupsdDenyHost(cupsd_location_t
*loc
, /* I - Location to add to */
1253 char *name
) /* I - Name of host or domain to add */
1255 cupsd_authmask_t
*temp
; /* New host/domain mask */
1256 char ifname
[32], /* Interface name */
1257 *ifptr
; /* Pointer to end of name */
1260 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDenyHost(loc=%p(%s), name=\"%s\")",
1261 loc
, loc
->location
? loc
->location
: "nil", name
);
1263 if ((temp
= add_deny(loc
)) == NULL
)
1266 if (!strcasecmp(name
, "@LOCAL"))
1269 * Deny *interface*...
1272 temp
->type
= AUTH_INTERFACE
;
1273 temp
->mask
.name
.name
= strdup("*");
1274 temp
->mask
.name
.length
= 1;
1276 else if (!strncasecmp(name
, "@IF(", 4))
1279 * Deny *interface*...
1282 strlcpy(ifname
, name
+ 4, sizeof(ifname
));
1284 ifptr
= ifname
+ strlen(ifname
);
1286 if (ifptr
[-1] == ')')
1292 temp
->type
= AUTH_INTERFACE
;
1293 temp
->mask
.name
.name
= strdup(ifname
);
1294 temp
->mask
.name
.length
= ifptr
- ifname
;
1302 temp
->type
= AUTH_NAME
;
1303 temp
->mask
.name
.name
= strdup(name
);
1304 temp
->mask
.name
.length
= strlen(name
);
1310 * 'cupsdDenyIP()' - Add an IP address or network that is not allowed to
1311 * access the location.
1315 cupsdDenyIP(cupsd_location_t
*loc
, /* I - Location to add to */
1316 unsigned address
[4],/* I - IP address to add */
1317 unsigned netmask
[4])/* I - Netmask of address */
1319 cupsd_authmask_t
*temp
; /* New host/domain mask */
1322 cupsdLogMessage(CUPSD_LOG_DEBUG
,
1323 "cupsdDenyIP(loc=%p(%s), address=%x:%x:%x:%x, netmask=%x:%x:%x:%x)",
1324 loc
, loc
->location
? loc
->location
: "nil",
1325 address
[0], address
[1], address
[2], address
[3],
1326 netmask
[0], netmask
[1], netmask
[2], netmask
[3]);
1328 if ((temp
= add_deny(loc
)) == NULL
)
1331 temp
->type
= AUTH_IP
;
1332 memcpy(temp
->mask
.ip
.address
, address
, sizeof(temp
->mask
.ip
.address
));
1333 memcpy(temp
->mask
.ip
.netmask
, netmask
, sizeof(temp
->mask
.ip
.netmask
));
1338 * 'cupsdFindBest()' - Find the location entry that best matches the resource.
1341 cupsd_location_t
* /* O - Location that matches */
1342 cupsdFindBest(const char *path
, /* I - Resource path */
1343 http_state_t state
) /* I - HTTP state/request */
1345 char uri
[HTTP_MAX_URI
],
1346 /* URI in request... */
1347 *uriptr
; /* Pointer into URI */
1348 cupsd_location_t
*loc
, /* Current location */
1349 *best
; /* Best match for location so far */
1350 int bestlen
; /* Length of best match */
1351 int limit
; /* Limit field */
1352 static const int limits
[] = /* Map http_status_t to AUTH_LIMIT_xyz */
1372 * First copy the connection URI to a local string so we have drop
1373 * any .ppd extension from the pathname in /printers or /classes
1377 strlcpy(uri
, path
, sizeof(uri
));
1379 if (!strncmp(uri
, "/printers/", 10) ||
1380 !strncmp(uri
, "/classes/", 9))
1383 * Check if the URI has .ppd on the end...
1386 uriptr
= uri
+ strlen(uri
) - 4; /* len > 4 if we get here... */
1388 if (!strcmp(uriptr
, ".ppd"))
1392 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: uri = \"%s\"...", uri
);
1395 * Loop through the list of locations to find a match...
1398 limit
= limits
[state
];
1402 for (loc
= (cupsd_location_t
*)cupsArrayFirst(Locations
);
1404 loc
= (cupsd_location_t
*)cupsArrayNext(Locations
))
1406 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: Location %s Limit %x",
1407 loc
->location
? loc
->location
: "nil", loc
->limit
);
1409 if (!strncmp(uri
, "/printers/", 10) || !strncmp(uri
, "/classes/", 9))
1412 * Use case-insensitive comparison for queue names...
1415 if (loc
->length
> bestlen
&& loc
->location
&&
1416 !strncasecmp(uri
, loc
->location
, loc
->length
) &&
1417 loc
->location
[0] == '/' &&
1418 (limit
& loc
->limit
) != 0)
1421 bestlen
= loc
->length
;
1427 * Use case-sensitive comparison for other URIs...
1430 if (loc
->length
> bestlen
&& loc
->location
&&
1431 !strncmp(uri
, loc
->location
, loc
->length
) &&
1432 loc
->location
[0] == '/' &&
1433 (limit
& loc
->limit
) != 0)
1436 bestlen
= loc
->length
;
1442 * Return the match, if any...
1445 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdFindBest: best = %s",
1446 best
? best
->location
: "NONE");
1453 * 'cupsdFindLocation()' - Find the named location.
1456 cupsd_location_t
* /* O - Location that matches */
1457 cupsdFindLocation(const char *location
) /* I - Connection */
1459 cupsd_location_t key
; /* Search key */
1462 key
.location
= (char *)location
;
1464 return ((cupsd_location_t
*)cupsArrayFind(Locations
, &key
));
1469 * 'cupsdIsAuthorized()' - Check to see if the user is authorized...
1472 http_status_t
/* O - HTTP_OK if authorized or error code */
1473 cupsdIsAuthorized(cupsd_client_t
*con
, /* I - Connection */
1474 const char *owner
)/* I - Owner of object */
1476 int i
, j
, /* Looping vars */
1477 auth
; /* Authorization status */
1478 unsigned address
[4]; /* Authorization address */
1479 cupsd_location_t
*best
; /* Best match for location so far */
1480 int hostlen
; /* Length of hostname */
1481 const char *username
; /* Username to authorize */
1482 struct passwd
*pw
; /* User password data */
1483 static const char * const levels
[] = /* Auth levels */
1489 static const char * const types
[] = /* Auth types */
1498 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1499 "cupsdIsAuthorized: con->uri=\"%s\", con->best=%p(%s)",
1500 con
->uri
, con
->best
, con
->best
? con
->best
->location
?
1501 con
->best
->location
: "(null)" : "");
1503 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1504 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1507 * If there is no "best" authentication rule for this request, then
1508 * access is allowed from the local system and denied from other
1514 if (!strcmp(con
->http
.hostname
, "localhost") ||
1515 !strcmp(con
->http
.hostname
, ServerName
))
1518 return (HTTP_FORBIDDEN
);
1523 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1524 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1525 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1526 levels
[best
->level
], types
[best
->type
],
1527 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1529 if (best
->limit
== AUTH_LIMIT_IPP
)
1530 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1531 best
->op
, ippOpString(best
->op
));
1534 * Check host/ip-based accesses...
1538 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1541 * Copy IPv6 address...
1544 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1545 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1546 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1547 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1550 #endif /* AF_INET6 */
1551 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1554 * Copy IPv4 address...
1560 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1563 memset(address
, 0, sizeof(address
));
1565 hostlen
= strlen(con
->http
.hostname
);
1567 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1570 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1578 * Do authorization checks on the domain/address...
1581 switch (best
->order_type
)
1584 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1587 case AUTH_ALLOW
: /* Order Deny,Allow */
1590 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1591 best
->num_deny
, best
->deny
))
1594 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1595 best
->num_allow
, best
->allow
))
1599 case AUTH_DENY
: /* Order Allow,Deny */
1602 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1603 best
->num_allow
, best
->allow
))
1606 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1607 best
->num_deny
, best
->deny
))
1613 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1614 auth
? "DENY" : "ALLOW");
1616 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1617 return (HTTP_FORBIDDEN
);
1621 * See if encryption is required...
1624 if (best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1625 strcasecmp(con
->http
.hostname
, "localhost") &&
1626 best
->satisfy
== AUTH_SATISFY_ALL
)
1628 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1629 "cupsdIsAuthorized: Need upgrade to TLS...");
1630 return (HTTP_UPGRADE_REQUIRED
);
1632 #endif /* HAVE_SSL */
1635 * Now see what access level is required...
1638 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1639 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1642 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1643 best
->limit
== AUTH_LIMIT_IPP
)
1646 * Check for unauthenticated username...
1649 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1652 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1655 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1656 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1657 attr
->values
[0].string
.text
);
1658 username
= attr
->values
[0].string
.text
;
1660 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1661 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1663 return (HTTP_OK
); /* unless overridden with Satisfy */
1667 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1670 if (!con
->username
[0])
1672 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1673 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1675 return (HTTP_OK
); /* unless overridden with Satisfy */
1678 username
= con
->username
;
1682 * OK, got a username. See if we need normal user access, or group
1683 * access... (root always matches)
1686 if (!strcmp(username
, "root"))
1690 * Get the user info...
1693 pw
= getpwnam(username
);
1696 if (best
->level
== AUTH_USER
)
1699 * If there are no names associated with this location, then
1700 * any valid user is OK...
1703 if (best
->num_names
== 0)
1707 * Otherwise check the user list and return OK if this user is
1711 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1712 "cupsdIsAuthorized: Checking user membership...");
1714 for (i
= 0; i
< best
->num_names
; i
++)
1716 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1717 !strcasecmp(username
, owner
))
1719 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1721 for (j
= 0; j
< NumSystemGroups
; j
++)
1722 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1725 else if (best
->names
[i
][0] == '@')
1727 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1730 else if (!strcasecmp(username
, best
->names
[i
]))
1734 return (HTTP_UNAUTHORIZED
);
1738 * Check to see if this user is in any of the named groups...
1741 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1742 "cupsdIsAuthorized: Checking group membership...");
1745 * Check to see if this user is in any of the named groups...
1748 for (i
= 0; i
< best
->num_names
; i
++)
1750 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1751 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1754 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1756 for (j
= 0; j
< NumSystemGroups
; j
++)
1757 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1760 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1765 * The user isn't part of the specified group, so deny access...
1768 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1769 "cupsdIsAuthorized: User not in group(s)!");
1771 return (HTTP_UNAUTHORIZED
);
1776 * 'add_allow()' - Add an allow mask to the location.
1779 static cupsd_authmask_t
* /* O - New mask record */
1780 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
1782 cupsd_authmask_t
*temp
; /* New mask record */
1793 * Try to allocate memory for the record...
1796 if (loc
->num_allow
== 0)
1797 temp
= malloc(sizeof(cupsd_authmask_t
));
1799 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
1805 temp
+= loc
->num_allow
;
1809 * Clear the mask record and return...
1812 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1818 * 'add_deny()' - Add a deny mask to the location.
1821 static cupsd_authmask_t
* /* O - New mask record */
1822 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
1824 cupsd_authmask_t
*temp
; /* New mask record */
1835 * Try to allocate memory for the record...
1838 if (loc
->num_deny
== 0)
1839 temp
= malloc(sizeof(cupsd_authmask_t
));
1841 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
1847 temp
+= loc
->num_deny
;
1851 * Clear the mask record and return...
1854 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1860 * 'compare_locations()' - Compare two locations.
1863 static int /* O - Result of comparison */
1864 compare_locations(cupsd_location_t
*a
, /* I - First location */
1865 cupsd_location_t
*b
) /* I - Second location */
1867 return (strcmp(b
->location
, a
->location
));
1873 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1877 static char * /* O - Encrypted password */
1878 cups_crypt(const char *pw
, /* I - Password string */
1879 const char *salt
) /* I - Salt (key) string */
1881 if (!strncmp(salt
, "$1$", 3))
1884 * Use MD5 passwords without the benefit of PAM; this is for
1885 * Slackware Linux, and the algorithm was taken from the
1886 * old shadow-19990827/lib/md5crypt.c source code... :(
1889 int i
; /* Looping var */
1890 unsigned long n
; /* Output number */
1891 int pwlen
; /* Length of password string */
1892 const char *salt_end
; /* End of "salt" data for MD5 */
1893 char *ptr
; /* Pointer into result string */
1894 _cups_md5_state_t state
; /* Primary MD5 state info */
1895 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1896 unsigned char digest
[16]; /* MD5 digest result */
1897 static char result
[120]; /* Final password string */
1901 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
1902 * Get a maximum of 8 characters of salt data after $1$...
1905 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
1906 if (*salt_end
== '$')
1910 * Compute the MD5 sum we need...
1915 _cupsMD5Init(&state
);
1916 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1917 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
1919 _cupsMD5Init(&state2
);
1920 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
1921 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1922 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
1923 _cupsMD5Finish(&state2
, digest
);
1925 for (i
= pwlen
; i
> 0; i
-= 16)
1926 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
1928 for (i
= pwlen
; i
> 0; i
>>= 1)
1929 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
1931 _cupsMD5Finish(&state
, digest
);
1933 for (i
= 0; i
< 1000; i
++)
1935 _cupsMD5Init(&state
);
1938 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1940 _cupsMD5Append(&state
, digest
, 16);
1943 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1946 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1949 _cupsMD5Append(&state
, digest
, 16);
1951 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1953 _cupsMD5Finish(&state
, digest
);
1957 * Copy the final sum to the result string and return...
1960 memcpy(result
, salt
, salt_end
- salt
);
1961 ptr
= result
+ (salt_end
- salt
);
1964 for (i
= 0; i
< 5; i
++, ptr
+= 4)
1966 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
1969 n
|= digest
[i
+ 12];
1976 to64(ptr
, digest
[11], 2);
1985 * Use the standard crypt() function...
1988 return (crypt(pw
, salt
));
1991 #endif /* !HAVE_LIBPAM */
1995 * 'get_md5_password()' - Get an MD5 password.
1998 static char * /* O - MD5 password string */
1999 get_md5_password(const char *username
, /* I - Username */
2000 const char *group
, /* I - Group */
2001 char passwd
[33]) /* O - MD5 password string */
2003 cups_file_t
*fp
; /* passwd.md5 file */
2004 char filename
[1024], /* passwd.md5 filename */
2005 line
[256], /* Line from file */
2006 tempuser
[33], /* User from file */
2007 tempgroup
[33]; /* Group from file */
2010 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2011 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2012 username
, group
? group
: "(null)", passwd
);
2014 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2015 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2017 if (errno
!= ENOENT
)
2018 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2024 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2026 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2028 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2032 if (!strcmp(username
, tempuser
) &&
2033 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2036 * Found the password entry!
2039 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2040 username
, tempgroup
);
2048 * Didn't find a password entry - return NULL!
2058 * 'pam_func()' - PAM conversation function.
2061 static int /* O - Success or failure */
2063 int num_msg
, /* I - Number of messages */
2064 const struct pam_message
**msg
, /* I - Messages */
2065 struct pam_response
**resp
, /* O - Responses */
2067 /* I - Pointer to connection */
2069 int i
; /* Looping var */
2070 struct pam_response
*replies
; /* Replies */
2071 cupsd_authdata_t
*data
; /* Pointer to auth data */
2075 * Allocate memory for the responses...
2078 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2079 return (PAM_CONV_ERR
);
2082 * Answer all of the messages...
2085 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2089 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2090 * module. This is a workaround...
2096 data
= (cupsd_authdata_t
*)appdata_ptr
;
2099 for (i
= 0; i
< num_msg
; i
++)
2101 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2103 switch (msg
[i
]->msg_style
)
2105 case PAM_PROMPT_ECHO_ON
:
2106 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2108 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2109 replies
[i
].resp
= strdup(data
->username
);
2112 case PAM_PROMPT_ECHO_OFF
:
2113 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2115 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2116 replies
[i
].resp
= strdup(data
->password
);
2120 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2121 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2122 replies
[i
].resp
= NULL
;
2126 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2127 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2128 replies
[i
].resp
= NULL
;
2132 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2133 msg
[i
]->msg_style
));
2135 return (PAM_CONV_ERR
);
2140 * Return the responses back to PAM...
2145 return (PAM_SUCCESS
);
2151 * 'to64()' - Base64-encode an integer value...
2155 to64(char *s
, /* O - Output string */
2156 unsigned long v
, /* I - Value to encode */
2157 int n
) /* I - Number of digits */
2159 const char *itoa64
= "./0123456789"
2160 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2161 "abcdefghijklmnopqrstuvwxyz";
2164 for (; n
> 0; n
--, v
>>= 6)
2165 *s
++ = itoa64
[v
& 0x3f];
2167 #endif /* HAVE_LIBPAM */
2171 * End of "$Id: auth.c 5567 2006-05-22 15:33:11Z mike $".