]>
git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/auth.c
2 * "$Id: auth.c 5442 2006-04-20 14:00:02Z 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) && defined(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
: "");
1502 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1503 "cupsdIsAuthorized: owner=\"%s\"", owner
);
1506 * If there is no "best" authentication rule for this request, then
1507 * access is allowed from the local system and denied from other
1513 if (!strcmp(con
->http
.hostname
, "localhost") ||
1514 !strcmp(con
->http
.hostname
, ServerName
))
1517 return (HTTP_FORBIDDEN
);
1522 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1523 "cupsdIsAuthorized: level=AUTH_%s, type=AUTH_%s, "
1524 "satisfy=AUTH_SATISFY_%s, num_names=%d",
1525 levels
[best
->level
], types
[best
->type
],
1526 best
->satisfy
? "ANY" : "ALL", best
->num_names
);
1528 if (best
->limit
== AUTH_LIMIT_IPP
)
1529 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: op=%x(%s)",
1530 best
->op
, ippOpString(best
->op
));
1533 * Check host/ip-based accesses...
1537 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET6
)
1540 * Copy IPv6 address...
1543 address
[0] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[0]);
1544 address
[1] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[1]);
1545 address
[2] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[2]);
1546 address
[3] = ntohl(con
->http
.hostaddr
->ipv6
.sin6_addr
.s6_addr32
[3]);
1549 #endif /* AF_INET6 */
1550 if (con
->http
.hostaddr
->addr
.sa_family
== AF_INET
)
1553 * Copy IPv4 address...
1559 address
[3] = ntohl(con
->http
.hostaddr
->ipv4
.sin_addr
.s_addr
);
1562 memset(address
, 0, sizeof(address
));
1564 hostlen
= strlen(con
->http
.hostname
);
1566 if (!strcasecmp(con
->http
.hostname
, "localhost"))
1569 * Access from localhost (127.0.0.1 or ::1) is always allowed...
1577 * Do authorization checks on the domain/address...
1580 switch (best
->order_type
)
1583 auth
= AUTH_DENY
; /* anti-compiler-warning-code */
1586 case AUTH_ALLOW
: /* Order Deny,Allow */
1589 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1590 best
->num_deny
, best
->deny
))
1593 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1594 best
->num_allow
, best
->allow
))
1598 case AUTH_DENY
: /* Order Allow,Deny */
1601 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1602 best
->num_allow
, best
->allow
))
1605 if (cupsdCheckAuth(address
, con
->http
.hostname
, hostlen
,
1606 best
->num_deny
, best
->deny
))
1612 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: auth=AUTH_%s...",
1613 auth
? "DENY" : "ALLOW");
1615 if (auth
== AUTH_DENY
&& best
->satisfy
== AUTH_SATISFY_ALL
)
1616 return (HTTP_FORBIDDEN
);
1620 * See if encryption is required...
1623 if (best
->encryption
>= HTTP_ENCRYPT_REQUIRED
&& !con
->http
.tls
&&
1624 best
->satisfy
== AUTH_SATISFY_ALL
)
1626 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1627 "cupsdIsAuthorized: Need upgrade to TLS...");
1628 return (HTTP_UPGRADE_REQUIRED
);
1630 #endif /* HAVE_SSL */
1633 * Now see what access level is required...
1636 if (best
->level
== AUTH_ANON
|| /* Anonymous access - allow it */
1637 (best
->type
== AUTH_NONE
&& best
->num_names
== 0))
1640 if (!con
->username
[0] && best
->type
== AUTH_NONE
&&
1641 best
->limit
== AUTH_LIMIT_IPP
)
1644 * Check for unauthenticated username...
1647 ipp_attribute_t
*attr
; /* requesting-user-name attribute */
1650 attr
= ippFindAttribute(con
->request
, "requesting-user-name", IPP_TAG_NAME
);
1653 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1654 "cupsdIsAuthorized: requesting-user-name=\"%s\"",
1655 attr
->values
[0].string
.text
);
1656 username
= attr
->values
[0].string
.text
;
1658 else if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1659 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1661 return (HTTP_OK
); /* unless overridden with Satisfy */
1665 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdIsAuthorized: username=\"%s\"",
1668 if (!con
->username
[0])
1670 if (best
->satisfy
== AUTH_SATISFY_ALL
|| auth
== AUTH_DENY
)
1671 return (HTTP_UNAUTHORIZED
); /* Non-anonymous needs user/pass */
1673 return (HTTP_OK
); /* unless overridden with Satisfy */
1676 username
= con
->username
;
1680 * OK, got a username. See if we need normal user access, or group
1681 * access... (root always matches)
1684 if (!strcmp(username
, "root"))
1688 * Get the user info...
1691 pw
= getpwnam(username
);
1694 if (best
->level
== AUTH_USER
)
1697 * If there are no names associated with this location, then
1698 * any valid user is OK...
1701 if (best
->num_names
== 0)
1705 * Otherwise check the user list and return OK if this user is
1709 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1710 "cupsdIsAuthorized: Checking user membership...");
1712 for (i
= 0; i
< best
->num_names
; i
++)
1714 if (!strcasecmp(best
->names
[i
], "@OWNER") && owner
&&
1715 !strcasecmp(username
, owner
))
1717 else if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1719 for (j
= 0; j
< NumSystemGroups
; j
++)
1720 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1723 else if (best
->names
[i
][0] == '@')
1725 if (cupsdCheckGroup(username
, pw
, best
->names
[i
] + 1))
1728 else if (!strcasecmp(username
, best
->names
[i
]))
1732 return (HTTP_UNAUTHORIZED
);
1736 * Check to see if this user is in any of the named groups...
1739 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1740 "cupsdIsAuthorized: Checking group membership...");
1743 * Check to see if this user is in any of the named groups...
1746 for (i
= 0; i
< best
->num_names
; i
++)
1748 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1749 "cupsdIsAuthorized: Checking group \"%s\" membership...",
1752 if (!strcasecmp(best
->names
[i
], "@SYSTEM"))
1754 for (j
= 0; j
< NumSystemGroups
; j
++)
1755 if (cupsdCheckGroup(username
, pw
, SystemGroups
[j
]))
1758 else if (cupsdCheckGroup(username
, pw
, best
->names
[i
]))
1763 * The user isn't part of the specified group, so deny access...
1766 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1767 "cupsdIsAuthorized: User not in group(s)!");
1769 return (HTTP_UNAUTHORIZED
);
1774 * 'add_allow()' - Add an allow mask to the location.
1777 static cupsd_authmask_t
* /* O - New mask record */
1778 add_allow(cupsd_location_t
*loc
) /* I - Location to add to */
1780 cupsd_authmask_t
*temp
; /* New mask record */
1791 * Try to allocate memory for the record...
1794 if (loc
->num_allow
== 0)
1795 temp
= malloc(sizeof(cupsd_authmask_t
));
1797 temp
= realloc(loc
->allow
, sizeof(cupsd_authmask_t
) * (loc
->num_allow
+ 1));
1803 temp
+= loc
->num_allow
;
1807 * Clear the mask record and return...
1810 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1816 * 'add_deny()' - Add a deny mask to the location.
1819 static cupsd_authmask_t
* /* O - New mask record */
1820 add_deny(cupsd_location_t
*loc
) /* I - Location to add to */
1822 cupsd_authmask_t
*temp
; /* New mask record */
1833 * Try to allocate memory for the record...
1836 if (loc
->num_deny
== 0)
1837 temp
= malloc(sizeof(cupsd_authmask_t
));
1839 temp
= realloc(loc
->deny
, sizeof(cupsd_authmask_t
) * (loc
->num_deny
+ 1));
1845 temp
+= loc
->num_deny
;
1849 * Clear the mask record and return...
1852 memset(temp
, 0, sizeof(cupsd_authmask_t
));
1858 * 'compare_locations()' - Compare two locations.
1861 static int /* O - Result of comparison */
1862 compare_locations(cupsd_location_t
*a
, /* I - First location */
1863 cupsd_location_t
*b
) /* I - Second location */
1865 return (strcmp(b
->location
, a
->location
));
1871 * 'cups_crypt()' - Encrypt the password using the DES or MD5 algorithms,
1875 static char * /* O - Encrypted password */
1876 cups_crypt(const char *pw
, /* I - Password string */
1877 const char *salt
) /* I - Salt (key) string */
1879 if (!strncmp(salt
, "$1$", 3))
1882 * Use MD5 passwords without the benefit of PAM; this is for
1883 * Slackware Linux, and the algorithm was taken from the
1884 * old shadow-19990827/lib/md5crypt.c source code... :(
1887 int i
; /* Looping var */
1888 unsigned long n
; /* Output number */
1889 int pwlen
; /* Length of password string */
1890 const char *salt_end
; /* End of "salt" data for MD5 */
1891 char *ptr
; /* Pointer into result string */
1892 _cups_md5_state_t state
; /* Primary MD5 state info */
1893 _cups_md5_state_t state2
; /* Secondary MD5 state info */
1894 unsigned char digest
[16]; /* MD5 digest result */
1895 static char result
[120]; /* Final password string */
1899 * Get the salt data between dollar signs, e.g. $1$saltdata$md5.
1900 * Get a maximum of 8 characters of salt data after $1$...
1903 for (salt_end
= salt
+ 3; *salt_end
&& (salt_end
- salt
) < 11; salt_end
++)
1904 if (*salt_end
== '$')
1908 * Compute the MD5 sum we need...
1913 _cupsMD5Init(&state
);
1914 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1915 _cupsMD5Append(&state
, (unsigned char *)salt
, salt_end
- salt
);
1917 _cupsMD5Init(&state2
);
1918 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
1919 _cupsMD5Append(&state2
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1920 _cupsMD5Append(&state2
, (unsigned char *)pw
, pwlen
);
1921 _cupsMD5Finish(&state2
, digest
);
1923 for (i
= pwlen
; i
> 0; i
-= 16)
1924 _cupsMD5Append(&state
, digest
, i
> 16 ? 16 : i
);
1926 for (i
= pwlen
; i
> 0; i
>>= 1)
1927 _cupsMD5Append(&state
, (unsigned char *)((i
& 1) ? "" : pw
), 1);
1929 _cupsMD5Finish(&state
, digest
);
1931 for (i
= 0; i
< 1000; i
++)
1933 _cupsMD5Init(&state
);
1936 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1938 _cupsMD5Append(&state
, digest
, 16);
1941 _cupsMD5Append(&state
, (unsigned char *)salt
+ 3, salt_end
- salt
- 3);
1944 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1947 _cupsMD5Append(&state
, digest
, 16);
1949 _cupsMD5Append(&state
, (unsigned char *)pw
, pwlen
);
1951 _cupsMD5Finish(&state
, digest
);
1955 * Copy the final sum to the result string and return...
1958 memcpy(result
, salt
, salt_end
- salt
);
1959 ptr
= result
+ (salt_end
- salt
);
1962 for (i
= 0; i
< 5; i
++, ptr
+= 4)
1964 n
= (((digest
[i
] << 8) | digest
[i
+ 6]) << 8);
1967 n
|= digest
[i
+ 12];
1974 to64(ptr
, digest
[11], 2);
1983 * Use the standard crypt() function...
1986 return (crypt(pw
, salt
));
1989 #endif /* !HAVE_LIBPAM */
1993 * 'get_md5_password()' - Get an MD5 password.
1996 static char * /* O - MD5 password string */
1997 get_md5_password(const char *username
, /* I - Username */
1998 const char *group
, /* I - Group */
1999 char passwd
[33]) /* O - MD5 password string */
2001 cups_file_t
*fp
; /* passwd.md5 file */
2002 char filename
[1024], /* passwd.md5 filename */
2003 line
[256], /* Line from file */
2004 tempuser
[33], /* User from file */
2005 tempgroup
[33]; /* Group from file */
2008 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2009 "get_md5_password(username=\"%s\", group=\"%s\", passwd=%p)",
2010 username
, group
? group
: "(null)", passwd
);
2012 snprintf(filename
, sizeof(filename
), "%s/passwd.md5", ServerRoot
);
2013 if ((fp
= cupsFileOpen(filename
, "r")) == NULL
)
2015 if (errno
!= ENOENT
)
2016 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", filename
,
2022 while (cupsFileGets(fp
, line
, sizeof(line
)) != NULL
)
2024 if (sscanf(line
, "%32[^:]:%32[^:]:%32s", tempuser
, tempgroup
, passwd
) != 3)
2026 cupsdLogMessage(CUPSD_LOG_ERROR
, "Bad MD5 password line: %s", line
);
2030 if (!strcmp(username
, tempuser
) &&
2031 (group
== NULL
|| !strcmp(group
, tempgroup
)))
2034 * Found the password entry!
2037 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "Found MD5 user %s, group %s...",
2038 username
, tempgroup
);
2046 * Didn't find a password entry - return NULL!
2056 * 'pam_func()' - PAM conversation function.
2059 static int /* O - Success or failure */
2061 int num_msg
, /* I - Number of messages */
2062 const struct pam_message
**msg
, /* I - Messages */
2063 struct pam_response
**resp
, /* O - Responses */
2065 /* I - Pointer to connection */
2067 int i
; /* Looping var */
2068 struct pam_response
*replies
; /* Replies */
2069 cupsd_authdata_t
*data
; /* Pointer to auth data */
2073 * Allocate memory for the responses...
2076 if ((replies
= malloc(sizeof(struct pam_response
) * num_msg
)) == NULL
)
2077 return (PAM_CONV_ERR
);
2080 * Answer all of the messages...
2083 DEBUG_printf(("pam_func: appdata_ptr = %p\n", appdata_ptr
));
2087 * Apparently some versions of HP-UX 11 have a broken pam_unix security
2088 * module. This is a workaround...
2094 data
= (cupsd_authdata_t
*)appdata_ptr
;
2097 for (i
= 0; i
< num_msg
; i
++)
2099 DEBUG_printf(("pam_func: Message = \"%s\"\n", msg
[i
]->msg
));
2101 switch (msg
[i
]->msg_style
)
2103 case PAM_PROMPT_ECHO_ON
:
2104 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_ON, returning \"%s\"...\n",
2106 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2107 replies
[i
].resp
= strdup(data
->username
);
2110 case PAM_PROMPT_ECHO_OFF
:
2111 DEBUG_printf(("pam_func: PAM_PROMPT_ECHO_OFF, returning \"%s\"...\n",
2113 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2114 replies
[i
].resp
= strdup(data
->password
);
2118 DEBUG_puts("pam_func: PAM_TEXT_INFO...");
2119 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2120 replies
[i
].resp
= NULL
;
2124 DEBUG_puts("pam_func: PAM_ERROR_MSG...");
2125 replies
[i
].resp_retcode
= PAM_SUCCESS
;
2126 replies
[i
].resp
= NULL
;
2130 DEBUG_printf(("pam_func: Unknown PAM message %d...\n",
2131 msg
[i
]->msg_style
));
2133 return (PAM_CONV_ERR
);
2138 * Return the responses back to PAM...
2143 return (PAM_SUCCESS
);
2149 * 'to64()' - Base64-encode an integer value...
2153 to64(char *s
, /* O - Output string */
2154 unsigned long v
, /* I - Value to encode */
2155 int n
) /* I - Number of digits */
2157 const char *itoa64
= "./0123456789"
2158 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2159 "abcdefghijklmnopqrstuvwxyz";
2162 for (; n
> 0; n
--, v
>>= 6)
2163 *s
++ = itoa64
[v
& 0x3f];
2165 #endif /* HAVE_LIBPAM */
2169 * End of "$Id: auth.c 5442 2006-04-20 14:00:02Z mike $".